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