2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.core.infrastructure.messaging.impl.ws.server;
24 import java.io.IOException;
25 import java.net.InetSocketAddress;
26 import java.util.Collection;
27 import java.util.concurrent.atomic.AtomicBoolean;
28 import org.java_websocket.WebSocket;
29 import org.onap.policy.apex.core.infrastructure.messaging.MessageHolder;
30 import org.onap.policy.apex.core.infrastructure.messaging.util.MessagingUtils;
31 import org.slf4j.ext.XLogger;
32 import org.slf4j.ext.XLoggerFactory;
35 * A messaging server implementation using web socket.
37 * @author Sajeevan Achuthan (sajeevan.achuthan@ericsson.com)
38 * @param <M> the generic type of message being passed
40 public class MessageServerImpl<M> extends InternalMessageBusServer<M> {
41 // The logger for this class
42 private static final XLogger LOGGER = XLoggerFactory.getXLogger(MessageServerImpl.class);
44 // The Web Socket protocol for URIs and URLs
45 private static final String PROTOCOL = "ws://";
48 private final String connectionUri;
50 // Indicates if the web socket server is started or not
51 private final AtomicBoolean isStarted = new AtomicBoolean(false);
54 * Instantiates a new web socket messaging server for Apex.
56 * @param address the address of the server machine on which to start the server
58 public MessageServerImpl(final InetSocketAddress address) {
59 // Call the super class to create the web socket and set up received message forwarding
61 LOGGER.entry(address);
63 // Compose the Web Socket URI
64 connectionUri = PROTOCOL + address.getHostString() + ":" + address.getPort();
65 LOGGER.debug("Server connection URI: {}", connectionUri);
74 public void startConnection() {
75 // Start reception of connections on the web socket
83 public void stopConnection() {
84 // Stop message listening using our super class
87 // Stop the web socket server
89 // Close all connections on this web socket server
90 for (final WebSocket connection : getConnections()) {
91 connection.closeConnection(0, "");
94 } catch (final IOException ioe) {
96 } catch (final InterruptedException e) {
97 // restore the interrupt status
98 Thread.currentThread().interrupt();
99 // This can happen in normal operation so ignore
101 isStarted.set(false);
105 * Return the current connection URI.
107 * @return connection URI
109 public String getConnectionUrl() {
110 if (connectionUri == null) {
111 throw new IllegalStateException("URI not set - The server is not started");
113 return connectionUri;
120 public void send(final MessageHolder<M> message) {
121 // Send the incoming message to all clients connected to this web socket
122 final Collection<WebSocket> connections = getConnections();
123 for (final WebSocket webSocket : connections) {
124 webSocket.send(MessagingUtils.serializeObject(message));
132 public void send(final String messageString) {
133 final Collection<WebSocket> connections = getConnections();
134 for (final WebSocket webSocket : connections) {
135 webSocket.send(messageString);
143 public boolean isStarted() {
144 return isStarted.get();
148 public void onStart() {
150 LOGGER.debug("started deployment server on URI: {}", connectionUri);