98bffdbc606a9a3e227569bb626810e6a5669bf3
[policy/apex-pdp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.core.infrastructure.messaging.stringmessaging;
22
23 import com.google.common.eventbus.Subscribe;
24
25 import java.net.URI;
26
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;
34
35 /**
36  * This class uses a web socket client to send and receive strings over a web socket.
37  *
38  * @author Liam Fallon (liam.fallon@ericsson.com)
39  */
40 public class WsStringMessageClient implements WsStringMessager {
41     private static final XLogger LOGGER = XLoggerFactory.getXLogger(WsStringMessageClient.class);
42
43     // Repeated string constants
44     private static final String MESSAGE_PREAMBLE = "web socket event consumer client to \"";
45
46     // Message service factory and the message service itself
47     private final MessagingServiceFactory<String> factory = new MessagingServiceFactory<>();
48     private MessagingService<String> service = null;
49
50     // The listener to use for reception of strings
51     private WsStringMessageListener wsStringMessageListener;
52
53     // Address of the server
54     private final String host;
55     private final int port;
56     private String uriString;
57
58     /**
59      * Constructor, define the host and port of the server to connect to.
60      *
61      * @param host the host of the server
62      * @param port the port of the server
63      */
64     public WsStringMessageClient(final String host, final int port) {
65         this.host = host;
66         this.port = port;
67     }
68
69     /**
70      * {@inheritDoc}.
71      */
72     @Override
73     public void start(final WsStringMessageListener newWsStringMessageListener) throws MessagingException {
74         this.wsStringMessageListener = newWsStringMessageListener;
75
76         uriString = "ws://" + host + ":" + port;
77         String messagePreamble = MESSAGE_PREAMBLE + uriString + "\" ";
78         LOGGER.entry(messagePreamble + "starting . . .");
79
80         try {
81             service = factory.createClient(new URI(uriString));
82             service.addMessageListener(new WsStringMessageClientListener());
83             service.startConnection();
84         } catch (final Exception e) {
85             String message = messagePreamble + "start failed";
86             LOGGER.warn(message, e);
87             throw new MessagingException(message, e);
88         }
89
90         LOGGER.exit(messagePreamble + "started");
91     }
92
93     /**
94      * {@inheritDoc}.
95      */
96     @Override
97     public void stop() {
98         LOGGER.entry(MESSAGE_PREAMBLE + uriString + "\" stopping . . .");
99         service.stopConnection();
100         LOGGER.exit(MESSAGE_PREAMBLE + uriString + "\" stopped");
101     }
102
103     /**
104      * {@inheritDoc}.
105      */
106     @Override
107     public void sendString(final String stringMessage) {
108         service.send(stringMessage);
109
110         if (LOGGER.isDebugEnabled()) {
111             String message = "message sent to server: " + stringMessage;
112             LOGGER.debug(message);
113         }
114     }
115
116     /**
117      * The Class WSStringMessageClientListener.
118      */
119     private class WsStringMessageClientListener implements MessageListener<String> {
120         /**
121          * {@inheritDoc}.
122          */
123         @Subscribe
124         @Override
125         public void onMessage(final MessageBlock<String> messageBlock) {
126             throw new UnsupportedOperationException("raw messages are not supported on string message clients");
127         }
128
129         /**
130          * {@inheritDoc}.
131          */
132         @Subscribe
133         @Override
134         public void onMessage(final String messageString) {
135             wsStringMessageListener.receiveString(messageString);
136         }
137     }
138 }