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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.testsuites.integration.uservice.adapt.websocket;
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;
30 * The Class WebSocketEventProducerServer.
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;
39 WsStringMessageServer server;
42 * Instantiates a new web socket event producer server.
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
50 public WebSocketEventProducerServer(final int port, final int eventCount, final boolean xmlEvents,
51 final long eventInterval) throws MessagingException {
53 this.eventCount = eventCount;
54 this.xmlEvents = xmlEvents;
55 this.eventInterval = eventInterval;
57 server = new WsStringMessageServer(port);
60 System.out.println(WebSocketEventProducerServer.class.getCanonicalName() + ": port " + port + ", event count "
61 + eventCount + ", xmlEvents " + xmlEvents);
67 public void sendEvents() {
68 System.out.println(WebSocketEventProducerServer.class.getCanonicalName() + ": sending events on port " + port
69 + ", event count " + eventCount + ", xmlEvents " + xmlEvents);
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);
76 String eventString = null;
78 eventString = EventGenerator.xmlEvent();
80 eventString = EventGenerator.jsonEvent();
82 server.sendString(eventString);
84 System.out.println(WebSocketEventProducerServer.class.getCanonicalName() + ": port " + port
85 + ", sent event " + eventString);
88 System.out.println(WebSocketEventProducerServer.class.getCanonicalName() + ": event sending completed");
92 * Gets the events sent count.
94 * @return the events sent count
96 public long getEventsSentCount() {
97 return eventsSentCount;
103 public void shutdown() {
105 System.out.println(WebSocketEventProducerServer.class.getCanonicalName() + ": stopped");
112 * org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WSStringMessageListener#
113 * receiveString(java.lang.String)
116 public void receiveString(final String eventString) {
117 System.out.println(WebSocketEventProducerServer.class.getCanonicalName() + ": port " + port
118 + ", received event " + eventString);
124 * @param args the arguments
125 * @throws MessagingException the messaging exception
127 public static void main(final String[] args) throws MessagingException {
128 if (args.length != 4) {
129 System.err.println("usage WebSocketEventProducerServer port #events XML|JSON eventInterval");
135 port = Integer.parseInt(args[0]);
136 } catch (final Exception e) {
137 System.err.println("usage WebSocketEventProducerServer port #events XML|JSON eventInterval");
144 eventCount = Integer.parseInt(args[1]);
145 } catch (final Exception e) {
146 System.err.println("usage WebSocketEventProducerServer port #events XML|JSON eventInterval");
151 long eventInterval = 0;
153 eventInterval = Long.parseLong(args[3]);
154 } catch (final Exception e) {
155 System.err.println("usage WebSocketEventProducerServer port #events XML|JSON eventInterval");
160 boolean xmlEvents = false;
161 if (args[2].equalsIgnoreCase("XML")) {
163 } else if (!args[2].equalsIgnoreCase("JSON")) {
164 System.err.println("usage WebSocketEventProducerServer port #events XML|JSON startDelay eventInterval");
168 final WebSocketEventProducerServer server =
169 new WebSocketEventProducerServer(port, eventCount, xmlEvents, eventInterval);