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