3844b92544b8398a37d94061a8308f82fcc02cc8
[policy/apex-pdp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.core.infrastructure.messaging.impl.ws.server;
22
23 import java.io.IOException;
24 import java.net.InetSocketAddress;
25 import java.util.Collection;
26
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;
32
33 /**
34  * A messaging server implementation using web socket.
35  *
36  * @author Sajeevan Achuthan (sajeevan.achuthan@ericsson.com)
37  * @param <M> the generic type of message being passed
38  */
39 public class MessageServerImpl<M> extends InternalMessageBusServer<M> {
40     // The logger for this class
41     private static final XLogger LOGGER = XLoggerFactory.getXLogger(MessageServerImpl.class);
42
43     // The Web Socket protocol for URIs and URLs
44     private static final String PROTOCOL = "ws://";
45
46     // URI of this server
47     private final String connectionUri;
48
49     // Indicates if the web socket server is started or not
50     private boolean isStarted = false;
51
52     /**
53      * Instantiates a new web socket messaging server for Apex.
54      *
55      * @param address the address of the server machine on which to start the server
56      */
57     public MessageServerImpl(final InetSocketAddress address) {
58         // Call the super class to create the web socket and set up received message forwarding
59         super(address);
60         LOGGER.entry(address);
61
62         // Compose the Web Socket URI
63         connectionUri = PROTOCOL + address.getHostString() + ":" + address.getPort();
64         LOGGER.debug("Server connection URI: {}", connectionUri);
65
66         LOGGER.exit();
67     }
68
69     /**
70      * {@inheritDoc}.
71      */
72     @Override
73     public void startConnection() {
74         // Start reception of connections on the web socket
75         start();
76         isStarted = true;
77     }
78
79     /**
80      * {@inheritDoc}.
81      */
82     @Override
83     public void stopConnection() {
84         // Stop message listening using our super class
85         stopListener();
86
87         // Stop the web socket server
88         try {
89             // Close all connections on this web socket server
90             for (final WebSocket connection : getConnections()) {
91                 connection.closeConnection(0, "");
92             }
93             stop();
94         } catch (final IOException ioe) {
95             LOGGER.catching(ioe);
96         } catch (final InterruptedException e) {
97             // restore the interrupt status
98             Thread.currentThread().interrupt();
99             // This can happen in normal operation so ignore
100         }
101         isStarted = false;
102     }
103
104     /**
105      * Return the current connection URI.
106      *
107      * @return connection URI
108      */
109     public String getConnectionUrl() {
110         if (connectionUri == null) {
111             throw new IllegalStateException("URI not set - The server is not started");
112         }
113         return connectionUri;
114     }
115
116     /**
117      * {@inheritDoc}.
118      */
119     @Override
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));
125         }
126     }
127
128     /**
129      * {@inheritDoc}.
130      */
131     @Override
132     public void send(final String messageString) {
133         final Collection<WebSocket> connections = getConnections();
134         for (final WebSocket webSocket : connections) {
135             webSocket.send(messageString);
136         }
137     }
138
139     /**
140      * {@inheritDoc}.
141      */
142     @Override
143     public boolean isStarted() {
144         return isStarted;
145     }
146
147     @Override
148     public void onStart() {
149         LOGGER.debug("started deployment server on URI: {}", connectionUri);
150     }
151 }