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 <MESSAGE> the generic type of message being passed
39 public class MessageServerImpl<MESSAGE> extends InternalMessageBusServer<MESSAGE> {
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 * This method returns the current connection URI , if the server started otherwise it throws
110 * {@link IllegalStateException}.
112 * @return connection URI
114 public String getConnectionURI() {
115 if (connectionURI == null) {
116 throw new IllegalStateException("URI not set - The server is not started");
118 return connectionURI;
125 * org.onap.policy.apex.core.infrastructure.messaging.MessagingService#send(org.onap.policy.apex
126 * .core. infrastructure. messaging.MessageHolder)
129 public void send(final MessageHolder<MESSAGE> message) {
130 // Send the incoming message to all clients connected to this web socket
131 final Collection<WebSocket> connections = getConnections();
132 for (final WebSocket webSocket : connections) {
133 webSocket.send(MessagingUtils.serializeObject(message));
141 * org.onap.policy.apex.core.infrastructure.messaging.MessagingService#send(java.lang.String)
144 public void send(final String messageString) {
145 final Collection<WebSocket> connections = getConnections();
146 for (final WebSocket webSocket : connections) {
147 webSocket.send(messageString);
154 * @see org.onap.policy.apex.core.infrastructure.messaging.MessagingService#isStarted()
157 public boolean isStarted() {
162 public void onStart() {
163 LOGGER.debug("started deployment server on URI: {}", connectionURI);