2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 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;
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.slf4j.ext.XLogger;
34 import org.slf4j.ext.XLoggerFactory;
37 * This class uses a web socket client to send and receive strings over a web socket.
39 * @author Liam Fallon (liam.fallon@ericsson.com)
41 public class WsStringMessageClient implements WsStringMessager {
42 private static final XLogger LOGGER = XLoggerFactory.getXLogger(WsStringMessageClient.class);
44 // Repeated string constants
45 private static final String MESSAGE_PREAMBLE = "web socket event consumer client to \"";
47 // Message service factory and the message service itself
48 private final MessagingServiceFactory<String> factory = new MessagingServiceFactory<>();
49 private MessagingService<String> service = null;
51 // The listener to use for reception of strings
52 private WsStringMessageListener wsStringMessageListener;
54 // Address of the server
55 private final String host;
56 private final int port;
57 private String uriString;
60 * Constructor, define the host and port of the server to connect to.
62 * @param host the host of the server
63 * @param port the port of the server
65 public WsStringMessageClient(final String host, final int port) {
74 public void start(final WsStringMessageListener newWsStringMessageListener) throws MessagingException {
75 this.wsStringMessageListener = newWsStringMessageListener;
77 uriString = "ws://" + host + ":" + port;
78 String messagePreamble = MESSAGE_PREAMBLE + uriString + "\" ";
79 LOGGER.entry(messagePreamble + "starting . . .");
82 service = factory.createClient(new URI(uriString));
83 service.addMessageListener(new WsStringMessageClientListener());
84 service.startConnection();
85 } catch (final Exception e) {
86 String message = messagePreamble + "start failed";
87 LOGGER.warn(message, e);
88 throw new MessagingException(message, e);
91 LOGGER.exit(messagePreamble + "started");
99 LOGGER.entry(MESSAGE_PREAMBLE + uriString + "\" stopping . . .");
100 service.stopConnection();
101 LOGGER.exit(MESSAGE_PREAMBLE + uriString + "\" stopped");
108 public void sendString(final String stringMessage) {
109 service.send(stringMessage);
111 if (LOGGER.isDebugEnabled()) {
112 String message = "message sent to server: " + stringMessage;
113 LOGGER.debug(message);
118 * The Class WSStringMessageClientListener.
120 private class WsStringMessageClientListener implements MessageListener<String> {
126 public void onMessage(final MessageBlock<String> messageBlock) {
127 throw new UnsupportedOperationException("raw messages are not supported on string message clients");
135 public void onMessage(final String messageString) {
136 wsStringMessageListener.receiveString(messageString);
144 public boolean isStarted() {
145 return service.isStarted();