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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.core.infrastructure.messaging.stringmessaging;
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;
39 * This class runs a web socket server for sending and receiving of strings over a web socket.
41 * @author Liam Fallon (liam.fallon@ericsson.com)
43 public class WsStringMessageServer implements WsStringMessager {
44 private static final XLogger LOGGER = XLoggerFactory.getXLogger(WsStringMessageServer.class);
46 // Message service factory and the message service itself
47 private final MessagingServiceFactory<String> factory = new MessagingServiceFactory<>();
48 private MessagingService<String> service = null;
50 // The listener to use for reception of strings
51 private WsStringMessageListener wsStringMessageListener;
53 // Address of the server
54 private final int port;
57 * Constructor, define the port of the server.
59 * @param port the port of the server
61 public WsStringMessageServer(final int port) {
69 public void start(final WsStringMessageListener newWsStringMessageListener) throws MessagingException {
71 LOGGER.entry("web socket event consumer server starting . . .");
72 if (LOGGER.isDebugEnabled()) {
73 var lanaddress = "unknown";
75 lanaddress = MessagingUtils.getLocalHostLanAddress().getHostAddress();
76 } catch (final UnknownHostException ignore) {
77 LOGGER.debug("Failed to find name of local address name", ignore);
79 LOGGER.debug("web socket string message server LAN address=" + lanaddress);
80 var hostaddress = "unknown";
82 hostaddress = InetAddress.getLocalHost().getHostAddress();
83 } catch (final UnknownHostException ignore) {
84 LOGGER.debug("Failed to find name of local address", ignore);
86 LOGGER.debug("web socket string message server host address=" + hostaddress);
89 this.wsStringMessageListener = newWsStringMessageListener;
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);
100 LOGGER.exit("web socket string message server started");
108 LOGGER.entry("web socket string message server stopping . . .");
109 service.stopConnection();
110 LOGGER.exit("web socket string message server stopped");
117 public void sendString(final String stringMessage) {
118 service.send(stringMessage);
119 if (LOGGER.isDebugEnabled()) {
120 LOGGER.debug("server sent message: {}", stringMessage);
125 * The listener for strings coming into the server.
127 private class WsStringMessageServerListener implements MessageListener<String> {
134 public void onMessage(final MessageBlock<String> messageBlock) {
135 throw new UnsupportedOperationException("raw messages are not supported on string message clients");
143 public void onMessage(final String messageString) {
144 wsStringMessageListener.receiveString(messageString);
152 public boolean isStarted() {
153 return service.isStarted();