7fe2d4306b46748c9e3f78f0cb5ec519adb90ff1
[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.WsStringMessageClient;
26 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageListener;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * The Class WebSocketEventSubscriberClient.
32  */
33 public class WebSocketEventSubscriberClient implements WsStringMessageListener {
34     private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketEventSubscriberClient.class);
35
36     private final int port;
37     private long eventsReceivedCount = 0;
38
39     private final WsStringMessageClient client;
40
41     /**
42      * Instantiates a new web socket event subscriber client.
43      *
44      * @param host the host
45      * @param port the port
46      * @throws MessagingException the messaging exception
47      */
48     public WebSocketEventSubscriberClient(final String host, final int port) throws MessagingException {
49         this.port = port;
50
51         client = new WsStringMessageClient(host, port);
52         client.start(this);
53     }
54
55     /**
56      * {@inheritDoc}.
57      */
58     @Override
59     public void receiveString(final String eventString) {
60         LOGGER.debug("{}: port {}, received event {}", WebSocketEventSubscriberClient.class.getName(), 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         client.stop();
79         LOGGER.debug("{}: stopped", WebSocketEventSubscriberServer.class.getName());
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 != 2) {
90             LOGGER.error("usage WebSocketEventSubscriberClient host 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 WebSocketEventSubscriberClient(args[0], port);
104     }
105 }