23d32e252339836e5860387903dbf77ff834b2c2
[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  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.testsuites.integration.uservice.adapt.websocket;
24
25 import lombok.Getter;
26 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
27 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageClient;
28 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageListener;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * The Class WebSocketEventSubscriberClient.
34  */
35 public class WebSocketEventSubscriberClient implements WsStringMessageListener {
36     private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketEventSubscriberClient.class);
37
38     private final int port;
39     @Getter
40     private long eventsReceivedCount = 0;
41
42     private final WsStringMessageClient client;
43
44     /**
45      * Instantiates a new web socket event subscriber client.
46      *
47      * @param host the host
48      * @param port the port
49      * @throws MessagingException the messaging exception
50      */
51     public WebSocketEventSubscriberClient(final String host, final int port) throws MessagingException {
52         this.port = port;
53
54         client = new WsStringMessageClient(host, port);
55         client.start(this);
56     }
57
58     /**
59      * {@inheritDoc}.
60      */
61     @Override
62     public void receiveString(final String eventString) {
63         LOGGER.debug("{}: port {}, received event {}", WebSocketEventSubscriberClient.class.getName(), port,
64                         eventString);
65         eventsReceivedCount++;
66     }
67
68     /**
69      * Shutdown.
70      */
71     public void shutdown() {
72         client.stop();
73         LOGGER.debug("{}: stopped", WebSocketEventSubscriberServer.class.getName());
74     }
75
76     /**
77      * The main method.
78      *
79      * @param args the arguments
80      * @throws MessagingException the messaging exception
81      */
82     public static void main(final String[] args) throws MessagingException {
83         if (args.length != 2) {
84             LOGGER.error("usage WebSocketEventSubscriberClient host port");
85             return;
86         }
87
88         int port = 0;
89         try {
90             port = Integer.parseInt(args[0]);
91         } catch (final Exception e) {
92             LOGGER.error("usage WebSocketEventSubscriberClient port");
93             e.printStackTrace();
94             return;
95         }
96
97         new WebSocketEventSubscriberClient(args[0], port);
98     }
99 }