b244feab0c4f3894f34e83cb91213216192bae2d
[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.InetAddress;
26 import java.net.InetSocketAddress;
27
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;
36
37 /**
38  * This class runs a web socket server for sending and receiving of strings over a web socket.
39  *
40  * @author Liam Fallon (liam.fallon@ericsson.com)
41  */
42 public class WsStringMessageServer implements WsStringMessager {
43     private static final XLogger LOGGER = XLoggerFactory.getXLogger(WsStringMessageServer.class);
44
45     // Message service factory and the message service itself
46     private final MessagingServiceFactory<String> factory = new MessagingServiceFactory<>();
47     private MessagingService<String> service = null;
48
49     // The listener to use for reception of strings
50     private WsStringMessageListener wsStringMessageListener;
51
52     // Address of the server
53     private final int port;
54
55     /**
56      * Constructor, define the port of the server.
57      *
58      * @param port the port of the server
59      */
60     public WsStringMessageServer(final int port) {
61         this.port = port;
62     }
63
64     /**
65      * {@inheritDoc}.
66      */
67     @Override
68     public void start(final WsStringMessageListener newWsStringMessageListener) throws MessagingException {
69         this.wsStringMessageListener = newWsStringMessageListener;
70
71         LOGGER.entry("web socket event consumer server starting . . .");
72
73         try {
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());
78
79             service = factory.createServer(new InetSocketAddress(port));
80             service.addMessageListener(new WsStringMessageServerListener());
81
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);
86         }
87
88         LOGGER.exit("web socket string message server started");
89     }
90
91     /**
92      * {@inheritDoc}.
93      */
94     @Override
95     public void stop() {
96         LOGGER.entry("web socket string message server stopping . . .");
97         service.stopConnection();
98         LOGGER.exit("web socket string message server stopped");
99     }
100
101     /**
102      * {@inheritDoc}.
103      */
104     @Override
105     public void sendString(final String stringMessage) {
106         service.send(stringMessage);
107         if (LOGGER.isDebugEnabled()) {
108             LOGGER.debug("server sent message: {}", stringMessage);
109         }
110     }
111
112     /**
113      * The listener for strings coming into the server.
114      */
115     private class WsStringMessageServerListener implements MessageListener<String> {
116
117         /**
118          * {@inheritDoc}.
119          */
120         @Subscribe
121         @Override
122         public void onMessage(final MessageBlock<String> messageBlock) {
123             throw new UnsupportedOperationException("raw messages are not supported on string message clients");
124         }
125
126         /**
127          * {@inheritDoc}.
128          */
129         @Subscribe
130         @Override
131         public void onMessage(final String messageString) {
132             wsStringMessageListener.receiveString(messageString);
133         }
134     }
135 }