d5ef40b5b6ed595ded7f08b1bf4b24caaf1bb0b2
[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      * (non-Javadoc)
71      *
72      * @see org.java_websocket.server.WebSocketServer#start()
73      */
74     @Override
75     public void startConnection() {
76         // Start reception of connections on the web socket
77         start();
78         isStarted = true;
79     }
80
81     /*
82      * (non-Javadoc)
83      *
84      * @see org.java_websocket.server.WebSocketServer#stop()
85      */
86     @Override
87     public void stopConnection() {
88         // Stop message listening using our super class
89         stopListener();
90
91         // Stop the web socket server
92         try {
93             // Close all connections on this web socket server
94             for (final WebSocket connection : getConnections()) {
95                 connection.closeConnection(0, "");
96             }
97             stop();
98         } catch (final IOException ioe) {
99             LOGGER.catching(ioe);
100         } catch (final InterruptedException e) {
101             // restore the interrupt status
102             Thread.currentThread().interrupt();
103             // This can happen in normal operation so ignore
104         }
105         isStarted = false;
106     }
107
108     /**
109      * Return the current connection URI.
110      *
111      * @return connection URI
112      */
113     public String getConnectionUrl() {
114         if (connectionUri == null) {
115             throw new IllegalStateException("URI not set - The server is not started");
116         }
117         return connectionUri;
118     }
119
120     /*
121      * (non-Javadoc)
122      *
123      * @see org.onap.policy.apex.core.infrastructure.messaging.MessagingService#send(org.onap.policy.apex .core.
124      * infrastructure. messaging.MessageHolder)
125      */
126     @Override
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));
132         }
133     }
134
135     /*
136      * (non-Javadoc)
137      *
138      * @see org.onap.policy.apex.core.infrastructure.messaging.MessagingService#send(java.lang.String)
139      */
140     @Override
141     public void send(final String messageString) {
142         final Collection<WebSocket> connections = getConnections();
143         for (final WebSocket webSocket : connections) {
144             webSocket.send(messageString);
145         }
146     }
147
148     /*
149      * (non-Javadoc)
150      *
151      * @see org.onap.policy.apex.core.infrastructure.messaging.MessagingService#isStarted()
152      */
153     @Override
154     public boolean isStarted() {
155         return isStarted;
156     }
157
158     @Override
159     public void onStart() {
160         LOGGER.debug("started deployment server on URI: {}", connectionUri);
161     }
162 }