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.apps.uservice.test.adapt.websocket;
23 import org.onap.policy.apex.apps.uservice.test.adapt.events.EventGenerator;
24 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
25 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageListener;
26 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageServer;
27 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
29 public class WebSocketEventProducerServer implements WsStringMessageListener {
30 private final int port;
31 private final int eventCount;
32 private final boolean xmlEvents;
33 private final long eventInterval;
34 private long eventsSentCount = 0;
36 WsStringMessageServer server;
38 public WebSocketEventProducerServer(final int port, final int eventCount, final boolean xmlEvents,
39 final long eventInterval) throws MessagingException {
41 this.eventCount = eventCount;
42 this.xmlEvents = xmlEvents;
43 this.eventInterval = eventInterval;
45 server = new WsStringMessageServer(port);
48 System.out.println(WebSocketEventProducerServer.class.getCanonicalName() + ": port " + port + ", event count "
49 + eventCount + ", xmlEvents " + xmlEvents);
52 public void sendEvents() {
53 System.out.println(WebSocketEventProducerServer.class.getCanonicalName() + ": sending events on port " + port
54 + ", event count " + eventCount + ", xmlEvents " + xmlEvents);
56 for (int i = 0; i < eventCount; i++) {
57 System.out.println(WebSocketEventProducerServer.class.getCanonicalName() + ": waiting " + eventInterval
58 + " milliseconds before sending next event");
59 ThreadUtilities.sleep(eventInterval);
61 String eventString = null;
63 eventString = EventGenerator.xmlEvent();
65 eventString = EventGenerator.jsonEvent();
67 server.sendString(eventString);
69 System.out.println(WebSocketEventProducerServer.class.getCanonicalName() + ": port " + port
70 + ", sent event " + eventString);
73 System.out.println(WebSocketEventProducerServer.class.getCanonicalName() + ": event sending completed");
76 public long getEventsSentCount() {
77 return eventsSentCount;
80 public void shutdown() {
82 System.out.println(WebSocketEventProducerServer.class.getCanonicalName() + ": stopped");
89 * org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WSStringMessageListener#
90 * receiveString(java.lang.String)
93 public void receiveString(final String eventString) {
94 System.out.println(WebSocketEventProducerServer.class.getCanonicalName() + ": port " + port
95 + ", received event " + eventString);
98 public static void main(final String[] args) throws MessagingException {
99 if (args.length != 4) {
100 System.err.println("usage WebSocketEventProducerServer port #events XML|JSON eventInterval");
106 port = Integer.parseInt(args[0]);
107 } catch (final Exception e) {
108 System.err.println("usage WebSocketEventProducerServer port #events XML|JSON eventInterval");
115 eventCount = Integer.parseInt(args[1]);
116 } catch (final Exception e) {
117 System.err.println("usage WebSocketEventProducerServer port #events XML|JSON eventInterval");
122 long eventInterval = 0;
124 eventInterval = Long.parseLong(args[3]);
125 } catch (final Exception e) {
126 System.err.println("usage WebSocketEventProducerServer port #events XML|JSON eventInterval");
131 boolean xmlEvents = false;
132 if (args[2].equalsIgnoreCase("XML")) {
134 } else if (!args[2].equalsIgnoreCase("JSON")) {
135 System.err.println("usage WebSocketEventProducerServer port #events XML|JSON startDelay eventInterval");
139 final WebSocketEventProducerServer server =
140 new WebSocketEventProducerServer(port, eventCount, xmlEvents, eventInterval);