b75d99ff085af1b7378eeeb97a5568012b0dc933
[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 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
27 import org.onap.policy.apex.testsuites.integration.uservice.adapt.events.EventGenerator;
28
29 /**
30  * The Class WebSocketEventProducerServer.
31  */
32 public class WebSocketEventProducerServer implements WsStringMessageListener {
33     private final int port;
34     private final int eventCount;
35     private final boolean xmlEvents;
36     private final long eventInterval;
37     private long eventsSentCount = 0;
38
39     WsStringMessageServer server;
40
41     /**
42      * Instantiates a new web socket event producer server.
43      *
44      * @param port the port
45      * @param eventCount the event count
46      * @param xmlEvents the xml events
47      * @param eventInterval the event interval
48      * @throws MessagingException the messaging exception
49      */
50     public WebSocketEventProducerServer(final int port, final int eventCount, final boolean xmlEvents,
51             final long eventInterval) throws MessagingException {
52         this.port = port;
53         this.eventCount = eventCount;
54         this.xmlEvents = xmlEvents;
55         this.eventInterval = eventInterval;
56
57         server = new WsStringMessageServer(port);
58         server.start(this);
59
60         System.out.println(WebSocketEventProducerServer.class.getCanonicalName() + ": port " + port + ", event count "
61                 + eventCount + ", xmlEvents " + xmlEvents);
62     }
63
64     /**
65      * Send events.
66      */
67     public void sendEvents() {
68         System.out.println(WebSocketEventProducerServer.class.getCanonicalName() + ": sending events on port " + port
69                 + ", event count " + eventCount + ", xmlEvents " + xmlEvents);
70
71         for (int i = 0; i < eventCount; i++) {
72             System.out.println(WebSocketEventProducerServer.class.getCanonicalName() + ": waiting " + eventInterval
73                     + " milliseconds before sending next event");
74             ThreadUtilities.sleep(eventInterval);
75
76             String eventString = null;
77             if (xmlEvents) {
78                 eventString = EventGenerator.xmlEvent();
79             } else {
80                 eventString = EventGenerator.jsonEvent();
81             }
82             server.sendString(eventString);
83             eventsSentCount++;
84             System.out.println(WebSocketEventProducerServer.class.getCanonicalName() + ": port " + port
85                     + ", sent event " + eventString);
86         }
87
88         System.out.println(WebSocketEventProducerServer.class.getCanonicalName() + ": event sending completed");
89     }
90
91     /**
92      * Gets the events sent count.
93      *
94      * @return the events sent count
95      */
96     public long getEventsSentCount() {
97         return eventsSentCount;
98     }
99
100     /**
101      * Shutdown.
102      */
103     public void shutdown() {
104         server.stop();
105         System.out.println(WebSocketEventProducerServer.class.getCanonicalName() + ": stopped");
106     }
107
108     /**
109      * {@inheritDoc}.
110      */
111     @Override
112     public void receiveString(final String eventString) {
113         System.out.println(WebSocketEventProducerServer.class.getCanonicalName() + ": port " + port
114                 + ", received event " + eventString);
115     }
116
117     /**
118      * The main method.
119      *
120      * @param args the arguments
121      * @throws MessagingException the messaging exception
122      */
123     public static void main(final String[] args) throws MessagingException {
124         if (args.length != 4) {
125             System.err.println("usage WebSocketEventProducerServer port #events XML|JSON eventInterval");
126             return;
127         }
128
129         int port = 0;
130         try {
131             port = Integer.parseInt(args[0]);
132         } catch (final Exception e) {
133             System.err.println("usage WebSocketEventProducerServer port #events XML|JSON eventInterval");
134             e.printStackTrace();
135             return;
136         }
137
138         int eventCount = 0;
139         try {
140             eventCount = Integer.parseInt(args[1]);
141         } catch (final Exception e) {
142             System.err.println("usage WebSocketEventProducerServer port #events XML|JSON eventInterval");
143             e.printStackTrace();
144             return;
145         }
146
147         long eventInterval = 0;
148         try {
149             eventInterval = Long.parseLong(args[3]);
150         } catch (final Exception e) {
151             System.err.println("usage WebSocketEventProducerServer port #events XML|JSON eventInterval");
152             e.printStackTrace();
153             return;
154         }
155
156         boolean xmlEvents = false;
157         if (args[2].equalsIgnoreCase("XML")) {
158             xmlEvents = true;
159         } else if (!args[2].equalsIgnoreCase("JSON")) {
160             System.err.println("usage WebSocketEventProducerServer port #events XML|JSON startDelay eventInterval");
161             return;
162         }
163
164         final WebSocketEventProducerServer server =
165                 new WebSocketEventProducerServer(port, eventCount, xmlEvents, eventInterval);
166
167         server.sendEvents();
168         server.shutdown();
169     }
170 }