2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.core.infrastructure.messaging.impl.ws.server;
23 import java.io.IOException;
24 import java.net.InetSocketAddress;
25 import java.util.Collection;
27 import org.java_websocket.WebSocket;
28 import org.onap.policy.apex.core.infrastructure.messaging.MessageHolder;
29 import org.onap.policy.apex.core.infrastructure.messaging.util.MessagingUtils;
30 import org.slf4j.ext.XLogger;
31 import org.slf4j.ext.XLoggerFactory;
34 * A messaging server implementation using web socket.
36 * @author Sajeevan Achuthan (sajeevan.achuthan@ericsson.com)
37 * @param <M> the generic type of message being passed
39 public class MessageServerImpl<M> extends InternalMessageBusServer<M> {
40 // The logger for this class
41 private static final XLogger LOGGER = XLoggerFactory.getXLogger(MessageServerImpl.class);
43 // The Web Socket protocol for URIs and URLs
44 private static final String PROTOCOL = "ws://";
47 private final String connectionUri;
49 // Indicates if the web socket server is started or not
50 private boolean isStarted = false;
53 * Instantiates a new web socket messaging server for Apex.
55 * @param address the address of the server machine on which to start the server
57 public MessageServerImpl(final InetSocketAddress address) {
58 // Call the super class to create the web socket and set up received message forwarding
60 LOGGER.entry(address);
62 // Compose the Web Socket URI
63 connectionUri = PROTOCOL + address.getHostString() + ":" + address.getPort();
64 LOGGER.debug("Server connection URI: {}", connectionUri);
72 * @see org.java_websocket.server.WebSocketServer#start()
75 public void startConnection() {
76 // Start reception of connections on the web socket
84 * @see org.java_websocket.server.WebSocketServer#stop()
87 public void stopConnection() {
88 // Stop message listening using our super class
91 // Stop the web socket server
93 // Close all connections on this web socket server
94 for (final WebSocket connection : getConnections()) {
95 connection.closeConnection(0, "");
98 } catch (final IOException ioe) {
100 } catch (final InterruptedException e) {
101 // restore the interrupt status
102 Thread.currentThread().interrupt();
103 // This can happen in normal operation so ignore
109 * Return the current connection URI.
111 * @return connection URI
113 public String getConnectionUrl() {
114 if (connectionUri == null) {
115 throw new IllegalStateException("URI not set - The server is not started");
117 return connectionUri;
123 * @see org.onap.policy.apex.core.infrastructure.messaging.MessagingService#send(org.onap.policy.apex .core.
124 * infrastructure. messaging.MessageHolder)
127 public void send(final MessageHolder<M> message) {
128 // Send the incoming message to all clients connected to this web socket
129 final Collection<WebSocket> connections = getConnections();
130 for (final WebSocket webSocket : connections) {
131 webSocket.send(MessagingUtils.serializeObject(message));
138 * @see org.onap.policy.apex.core.infrastructure.messaging.MessagingService#send(java.lang.String)
141 public void send(final String messageString) {
142 final Collection<WebSocket> connections = getConnections();
143 for (final WebSocket webSocket : connections) {
144 webSocket.send(messageString);
151 * @see org.onap.policy.apex.core.infrastructure.messaging.MessagingService#isStarted()
154 public boolean isStarted() {
159 public void onStart() {
160 LOGGER.debug("started deployment server on URI: {}", connectionUri);