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;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * The Class WebSocketEventProducerServer.
34 public class WebSocketEventProducerServer implements WsStringMessageListener {
35 private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketEventProducerServer.class);
37 private final int port;
38 private final int eventCount;
39 private final boolean xmlEvents;
40 private final long eventInterval;
41 private long eventsSentCount = 0;
43 WsStringMessageServer server;
46 * Instantiates a new web socket event producer server.
48 * @param port the port
49 * @param eventCount the event count
50 * @param xmlEvents the xml events
51 * @param eventInterval the event interval
52 * @throws MessagingException the messaging exception
54 public WebSocketEventProducerServer(final int port, final int eventCount, final boolean xmlEvents,
55 final long eventInterval) throws MessagingException {
57 this.eventCount = eventCount;
58 this.xmlEvents = xmlEvents;
59 this.eventInterval = eventInterval;
61 server = new WsStringMessageServer(port);
64 LOGGER.debug("{}: port {}, event count {}, xmlEvents {}", WebSocketEventProducerServer.class.getCanonicalName(),
65 port, eventCount, xmlEvents);
71 public void sendEvents() {
72 LOGGER.debug("{}: sending events on port {}, event count {}, xmlEvents {}",
73 WebSocketEventProducerServer.class.getCanonicalName(), port, eventCount, xmlEvents);
75 for (int i = 0; i < eventCount; i++) {
76 LOGGER.debug("{}: waiting {} milliseconds before sending next event",
77 WebSocketEventProducerServer.class.getCanonicalName(), eventInterval);
78 ThreadUtilities.sleep(eventInterval);
80 String eventString = null;
82 eventString = EventGenerator.xmlEvent();
84 eventString = EventGenerator.jsonEvent();
86 server.sendString(eventString);
88 LOGGER.debug("{}: port {}, sent event {}", WebSocketEventProducerServer.class.getCanonicalName(), port,
92 LOGGER.debug("{}: event sending completed", WebSocketEventProducerServer.class.getCanonicalName());
96 * Gets the events sent count.
98 * @return the events sent count
100 public long getEventsSentCount() {
101 return eventsSentCount;
107 public void shutdown() {
109 LOGGER.debug("{}: stopped", WebSocketEventProducerServer.class.getCanonicalName());
116 public void receiveString(final String eventString) {
117 LOGGER.debug("{}: port {}, received event {}", WebSocketEventProducerServer.class.getCanonicalName(), port,
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 LOGGER.error("usage WebSocketEventProducerServer port #events XML|JSON eventInterval");
135 port = Integer.parseInt(args[0]);
136 } catch (final Exception e) {
137 LOGGER.error("usage WebSocketEventProducerServer port #events XML|JSON eventInterval");
144 eventCount = Integer.parseInt(args[1]);
145 } catch (final Exception e) {
146 LOGGER.error("usage WebSocketEventProducerServer port #events XML|JSON eventInterval");
151 long eventInterval = 0;
153 eventInterval = Long.parseLong(args[3]);
154 } catch (final Exception e) {
155 LOGGER.error("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 LOGGER.error("usage WebSocketEventProducerServer port #events XML|JSON startDelay eventInterval");
168 final WebSocketEventProducerServer server = new WebSocketEventProducerServer(port, eventCount, xmlEvents,