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
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.stringmessaging;
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;
38 * This class runs a web socket server for sending and receiving of strings over a web socket.
40 * @author Liam Fallon (liam.fallon@ericsson.com)
42 public class WsStringMessageServer implements WsStringMessager {
43 private static final XLogger LOGGER = XLoggerFactory.getXLogger(WsStringMessageServer.class);
45 // Message service factory and the message service itself
46 private final MessagingServiceFactory<String> factory = new MessagingServiceFactory<>();
47 private MessagingService<String> service = null;
49 // The listener to use for reception of strings
50 private WsStringMessageListener wsStringMessageListener;
52 // Address of the server
53 private final int port;
56 * Constructor, define the port of the server.
58 * @param port the port of the server
60 public WsStringMessageServer(final int port) {
68 public void start(final WsStringMessageListener newWsStringMessageListener) throws MessagingException {
70 LOGGER.entry("web socket event consumer server starting . . .");
71 if (LOGGER.isDebugEnabled()) {
72 String lanaddress = "unknown";
74 lanaddress = MessagingUtils.getLocalHostLanAddress().getHostAddress();
75 } catch (final UnknownHostException ignore) {
76 LOGGER.debug("Failed to find name of local address name", ignore);
78 LOGGER.debug("web socket string message server LAN address=" + lanaddress);
79 String hostaddress = "unknown";
81 hostaddress = InetAddress.getLocalHost().getHostAddress();
82 } catch (final UnknownHostException ignore) {
83 LOGGER.debug("Failed to find name of local address", ignore);
85 LOGGER.debug("web socket string message server host address=" + hostaddress);
88 this.wsStringMessageListener = newWsStringMessageListener;
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);
99 LOGGER.exit("web socket string message server started");
107 LOGGER.entry("web socket string message server stopping . . .");
108 service.stopConnection();
109 LOGGER.exit("web socket string message server stopped");
116 public void sendString(final String stringMessage) {
117 service.send(stringMessage);
118 if (LOGGER.isDebugEnabled()) {
119 LOGGER.debug("server sent message: {}", stringMessage);
124 * The listener for strings coming into the server.
126 private class WsStringMessageServerListener implements MessageListener<String> {
133 public void onMessage(final MessageBlock<String> messageBlock) {
134 throw new UnsupportedOperationException("raw messages are not supported on string message clients");
142 public void onMessage(final String messageString) {
143 wsStringMessageListener.receiveString(messageString);
151 public boolean isStarted() {
152 return service.isStarted();