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