8b6d0c6a8c2b65305385c3ec4974d7f93764e03a
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019-2020 Nordix Foundation.
5  *  Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.core.infrastructure.messaging.stringmessaging;
24
25 import com.google.common.eventbus.Subscribe;
26 import java.net.InetAddress;
27 import java.net.InetSocketAddress;
28 import java.net.UnknownHostException;
29 import org.onap.policy.apex.core.infrastructure.messaging.MessageListener;
30 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
31 import org.onap.policy.apex.core.infrastructure.messaging.MessagingService;
32 import org.onap.policy.apex.core.infrastructure.messaging.MessagingServiceFactory;
33 import org.onap.policy.apex.core.infrastructure.messaging.impl.ws.messageblock.MessageBlock;
34 import org.onap.policy.apex.core.infrastructure.messaging.util.MessagingUtils;
35 import org.slf4j.ext.XLogger;
36 import org.slf4j.ext.XLoggerFactory;
37
38 /**
39  * This class runs a web socket server for sending and receiving of strings over a web socket.
40  *
41  * @author Liam Fallon (liam.fallon@ericsson.com)
42  */
43 public class WsStringMessageServer implements WsStringMessager {
44     private static final XLogger LOGGER = XLoggerFactory.getXLogger(WsStringMessageServer.class);
45
46     // Message service factory and the message service itself
47     private final MessagingServiceFactory<String> factory = new MessagingServiceFactory<>();
48     private MessagingService<String> service = null;
49
50     // The listener to use for reception of strings
51     private WsStringMessageListener wsStringMessageListener;
52
53     // Address of the server
54     private final int port;
55
56     /**
57      * Constructor, define the port of the server.
58      *
59      * @param port the port of the server
60      */
61     public WsStringMessageServer(final int port) {
62         this.port = port;
63     }
64
65     /**
66      * {@inheritDoc}.
67      */
68     @Override
69     public void start(final WsStringMessageListener newWsStringMessageListener) throws MessagingException {
70
71         LOGGER.entry("web socket event consumer server starting . . .");
72         if (LOGGER.isDebugEnabled()) {
73             var lanaddress = "unknown";
74             try {
75                 lanaddress = MessagingUtils.getLocalHostLanAddress().getHostAddress();
76             } catch (final UnknownHostException ignore) {
77                 LOGGER.debug("Failed to find name of local address name", ignore);
78             }
79             LOGGER.debug("web socket string message server LAN address=" + lanaddress);
80             var hostaddress = "unknown";
81             try {
82                 hostaddress = InetAddress.getLocalHost().getHostAddress();
83             } catch (final UnknownHostException ignore) {
84                 LOGGER.debug("Failed to find name of local address", ignore);
85             }
86             LOGGER.debug("web socket string message server host address=" + hostaddress);
87         }
88
89         this.wsStringMessageListener = newWsStringMessageListener;
90
91         try {
92             service = factory.createServer(new InetSocketAddress(port));
93             service.addMessageListener(new WsStringMessageServerListener());
94             service.startConnection();
95         } catch (final Exception e) {
96             LOGGER.warn("web socket string message server start failed", e);
97             throw new MessagingException("web socket string message start failed", e);
98         }
99
100         LOGGER.exit("web socket string message server started");
101     }
102
103     /**
104      * {@inheritDoc}.
105      */
106     @Override
107     public void stop() {
108         LOGGER.entry("web socket string message server stopping . . .");
109         service.stopConnection();
110         LOGGER.exit("web socket string message server stopped");
111     }
112
113     /**
114      * {@inheritDoc}.
115      */
116     @Override
117     public void sendString(final String stringMessage) {
118         service.send(stringMessage);
119         if (LOGGER.isDebugEnabled()) {
120             LOGGER.debug("server sent message: {}", stringMessage);
121         }
122     }
123
124     /**
125      * The listener for strings coming into the server.
126      */
127     private class WsStringMessageServerListener implements MessageListener<String> {
128
129         /**
130          * {@inheritDoc}.
131          */
132         @Subscribe
133         @Override
134         public void onMessage(final MessageBlock<String> messageBlock) {
135             throw new UnsupportedOperationException("raw messages are not supported on string message clients");
136         }
137
138         /**
139          * {@inheritDoc}.
140          */
141         @Subscribe
142         @Override
143         public void onMessage(final String messageString) {
144             wsStringMessageListener.receiveString(messageString);
145         }
146     }
147
148     /**
149      * {@inheritDoc}.
150      */
151     @Override
152     public boolean isStarted() {
153         return service.isStarted();
154     }
155 }