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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.core.infrastructure.messaging.stringmessaging;
23 import com.google.common.eventbus.Subscribe;
25 import java.net.InetAddress;
26 import java.net.InetSocketAddress;
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 {
69 this.wsStringMessageListener = newWsStringMessageListener;
71 LOGGER.entry("web socket event consumer server starting . . .");
74 final InetAddress addrLan = MessagingUtils.getLocalHostLanAddress();
75 LOGGER.debug("web socket string message server LAN address=" + addrLan.getHostAddress());
76 final InetAddress addr = InetAddress.getLocalHost();
77 LOGGER.debug("web socket string message server host address=" + addr.getHostAddress());
79 service = factory.createServer(new InetSocketAddress(port));
80 service.addMessageListener(new WsStringMessageServerListener());
82 service.startConnection();
83 } catch (final Exception e) {
84 LOGGER.warn("web socket string message server start failed", e);
85 throw new MessagingException("web socket string message start failed", e);
88 LOGGER.exit("web socket string message server started");
96 LOGGER.entry("web socket string message server stopping . . .");
97 service.stopConnection();
98 LOGGER.exit("web socket string message server stopped");
105 public void sendString(final String stringMessage) {
106 service.send(stringMessage);
107 if (LOGGER.isDebugEnabled()) {
108 LOGGER.debug("server sent message: {}", stringMessage);
113 * The listener for strings coming into the server.
115 private class WsStringMessageServerListener implements MessageListener<String> {
122 public void onMessage(final MessageBlock<String> messageBlock) {
123 throw new UnsupportedOperationException("raw messages are not supported on string message clients");
131 public void onMessage(final String messageString) {
132 wsStringMessageListener.receiveString(messageString);