48d221791f3f4c75e64dcc1181b51185c5405ecd
[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.testsuites.integration.uservice.adapt.websocket;
22
23 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
24 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageListener;
25 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageServer;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * The Class WebSocketEventSubscriberServer.
31  */
32 public class WebSocketEventSubscriberServer implements WsStringMessageListener {
33     private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketEventSubscriberServer.class);
34
35     private final int port;
36     private long eventsReceivedCount = 0;
37
38     private final WsStringMessageServer server;
39
40     /**
41      * Instantiates a new web socket event subscriber server.
42      *
43      * @param port the port
44      * @throws MessagingException the messaging exception
45      */
46     public WebSocketEventSubscriberServer(final int port) throws MessagingException {
47         this.port = port;
48
49         server = new WsStringMessageServer(port);
50         server.start(this);
51
52         LOGGER.debug("{}: port {}, waiting for events", WebSocketEventSubscriberServer.class.getCanonicalName(), port);
53     }
54
55     /**
56      * {@inheritDoc}.
57      */
58     @Override
59     public void receiveString(final String eventString) {
60         LOGGER.debug("{}: port {}, received event {}", WebSocketEventSubscriberServer.class.getCanonicalName(), port,
61                         eventString);
62         eventsReceivedCount++;
63     }
64
65     /**
66      * Gets the events received count.
67      *
68      * @return the events received count
69      */
70     public long getEventsReceivedCount() {
71         return eventsReceivedCount;
72     }
73
74     /**
75      * Shutdown.
76      */
77     public void shutdown() {
78         server.stop();
79         LOGGER.debug("{} : stopped", WebSocketEventSubscriberServer.class.getCanonicalName());
80     }
81
82     /**
83      * The main method.
84      *
85      * @param args the arguments
86      * @throws MessagingException the messaging exception
87      */
88     public static void main(final String[] args) throws MessagingException {
89         if (args.length != 1) {
90             LOGGER.error("usage WebSocketEventSubscriberClient port");
91             return;
92         }
93
94         int port = 0;
95         try {
96             port = Integer.parseInt(args[0]);
97         } catch (final Exception e) {
98             LOGGER.error("usage WebSocketEventSubscriberClient port");
99             e.printStackTrace();
100             return;
101         }
102
103         new WebSocketEventSubscriberServer(port);
104     }
105 }