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;
27 import org.onap.policy.apex.core.infrastructure.messaging.MessageListener;
28 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
29 import org.onap.policy.apex.core.infrastructure.messaging.MessagingService;
30 import org.onap.policy.apex.core.infrastructure.messaging.MessagingServiceFactory;
31 import org.onap.policy.apex.core.infrastructure.messaging.impl.ws.messageblock.MessageBlock;
32 import org.slf4j.ext.XLogger;
33 import org.slf4j.ext.XLoggerFactory;
36 * This class uses a web socket client to send and receive strings over a web socket.
38 * @author Liam Fallon (liam.fallon@ericsson.com)
40 public class WsStringMessageClient implements WsStringMessager {
41 private static final XLogger LOGGER = XLoggerFactory.getXLogger(WsStringMessageClient.class);
43 // Repeated string constants
44 private static final String MESSAGE_PREAMBLE = "web socket event consumer client to \"";
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 String host;
55 private final int port;
56 private String uriString;
59 * Constructor, define the host and port of the server to connect to.
61 * @param host the host of the server
62 * @param port the port of the server
64 public WsStringMessageClient(final String host, final int port) {
73 * org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WSStringMessageSender#start(org.onap.policy.
74 * apex. core.infrastructure.messaging. stringmessaging.WSStringMessageListener)
77 public void start(final WsStringMessageListener newWsStringMessageListener) throws MessagingException {
78 this.wsStringMessageListener = newWsStringMessageListener;
80 uriString = "ws://" + host + ":" + port;
81 String messagePreamble = MESSAGE_PREAMBLE + uriString + "\" ";
82 LOGGER.entry(messagePreamble + "starting . . .");
85 service = factory.createClient(new URI(uriString));
86 service.addMessageListener(new WsStringMessageClientListener());
87 service.startConnection();
88 } catch (final Exception e) {
89 String message = messagePreamble + "start failed";
90 LOGGER.warn(message, e);
91 throw new MessagingException(message, e);
94 LOGGER.exit(messagePreamble + "started");
100 * @see org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WSStringMessageSender#stop()
104 LOGGER.entry(MESSAGE_PREAMBLE + uriString + "\" stopping . . .");
105 service.stopConnection();
106 LOGGER.exit(MESSAGE_PREAMBLE + uriString + "\" stopped");
113 * org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WSStringMessageSender#sendString(java.lang.
117 public void sendString(final String stringMessage) {
118 service.send(stringMessage);
120 if (LOGGER.isDebugEnabled()) {
121 String message = "message sent to server: " + stringMessage;
122 LOGGER.debug(message);
127 * The Class WSStringMessageClientListener.
129 private class WsStringMessageClientListener implements MessageListener<String> {
133 * @see org.onap.policy.apex.core.infrastructure.messaging.MessageListener#onMessage(org.onap.policy.apex.core.
134 * infrastructure.messaging.impl.ws.messageblock. MessageBlock)
138 public void onMessage(final MessageBlock<String> messageBlock) {
139 throw new UnsupportedOperationException("raw messages are not supported on string message clients");
145 * @see org.onap.policy.apex.core.infrastructure.messaging.MessageListener#onMessage(java.lang.String)
149 public void onMessage(final String messageString) {
150 wsStringMessageListener.receiveString(messageString);