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;
26 import org.onap.policy.apex.core.infrastructure.messaging.MessageListener;
27 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
28 import org.onap.policy.apex.core.infrastructure.messaging.MessagingService;
29 import org.onap.policy.apex.core.infrastructure.messaging.MessagingServiceFactory;
30 import org.onap.policy.apex.core.infrastructure.messaging.impl.ws.messageblock.MessageBlock;
31 import org.slf4j.ext.XLogger;
32 import org.slf4j.ext.XLoggerFactory;
35 * This class uses a web socket client to send and receive strings over a web socket.
37 * @author Liam Fallon (liam.fallon@ericsson.com)
39 public class WsStringMessageClient implements WsStringMessager {
40 private static final XLogger LOGGER = XLoggerFactory.getXLogger(WsStringMessageClient.class);
42 // Repeated string constants
43 private static final String MESSAGE_PREAMBLE = "web socket event consumer client to \"";
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 String host;
54 private final int port;
55 private String uriString;
58 * Constructor, define the host and port of the server to connect to.
60 * @param host the host of the server
61 * @param port the port of the server
63 public WsStringMessageClient(final String host, final int port) {
72 public void start(final WsStringMessageListener newWsStringMessageListener) throws MessagingException {
73 this.wsStringMessageListener = newWsStringMessageListener;
75 uriString = "ws://" + host + ":" + port;
76 String messagePreamble = MESSAGE_PREAMBLE + uriString + "\" ";
77 LOGGER.entry(messagePreamble + "starting . . .");
80 service = factory.createClient(new URI(uriString));
81 service.addMessageListener(new WsStringMessageClientListener());
82 service.startConnection();
83 } catch (final Exception e) {
84 String message = messagePreamble + "start failed";
85 LOGGER.warn(message, e);
86 throw new MessagingException(message, e);
89 LOGGER.exit(messagePreamble + "started");
97 LOGGER.entry(MESSAGE_PREAMBLE + uriString + "\" stopping . . .");
98 service.stopConnection();
99 LOGGER.exit(MESSAGE_PREAMBLE + uriString + "\" stopped");
106 public void sendString(final String stringMessage) {
107 service.send(stringMessage);
109 if (LOGGER.isDebugEnabled()) {
110 String message = "message sent to server: " + stringMessage;
111 LOGGER.debug(message);
116 * The Class WSStringMessageClientListener.
118 private class WsStringMessageClientListener implements MessageListener<String> {
124 public void onMessage(final MessageBlock<String> messageBlock) {
125 throw new UnsupportedOperationException("raw messages are not supported on string message clients");
133 public void onMessage(final String messageString) {
134 wsStringMessageListener.receiveString(messageString);
142 public boolean isStarted() {
143 return service.isStarted();