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