2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 Nordix Foundation.
5 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.testsuites.integration.uservice.adapt.websocket;
26 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
27 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageListener;
28 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageServer;
29 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
30 import org.onap.policy.apex.testsuites.integration.uservice.adapt.events.EventGenerator;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * The Class WebSocketEventProducerServer.
37 public class WebSocketEventProducerServer implements WsStringMessageListener {
38 private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketEventProducerServer.class);
40 private final int port;
41 private final int eventCount;
42 private final boolean xmlEvents;
43 private final long eventInterval;
45 private long eventsSentCount = 0;
47 WsStringMessageServer server;
50 * Instantiates a new web socket event producer server.
52 * @param port the port
53 * @param eventCount the event count
54 * @param xmlEvents the xml events
55 * @param eventInterval the event interval
56 * @throws MessagingException the messaging exception
58 public WebSocketEventProducerServer(final int port, final int eventCount, final boolean xmlEvents,
59 final long eventInterval) throws MessagingException {
61 this.eventCount = eventCount;
62 this.xmlEvents = xmlEvents;
63 this.eventInterval = eventInterval;
65 server = new WsStringMessageServer(port);
68 LOGGER.debug("{}: port {}, event count {}, xmlEvents {}", WebSocketEventProducerServer.class.getName(), port,
69 eventCount, xmlEvents);
75 public void sendEvents() {
76 LOGGER.debug("{}: sending events on port {}, event count {}, xmlEvents {}",
77 WebSocketEventProducerServer.class.getName(), port, eventCount, xmlEvents);
79 for (int i = 0; i < eventCount; i++) {
80 LOGGER.debug("{}: waiting {} milliseconds before sending next event",
81 WebSocketEventProducerServer.class.getName(), eventInterval);
82 ThreadUtilities.sleep(eventInterval);
84 String eventString = null;
86 eventString = EventGenerator.xmlEvent();
88 eventString = EventGenerator.jsonEvent();
90 server.sendString(eventString);
92 LOGGER.debug("{}: port {}, sent event {}", WebSocketEventProducerServer.class.getName(), port, eventString);
95 LOGGER.debug("{}: event sending completed", WebSocketEventProducerServer.class.getName());
101 public void shutdown() {
103 LOGGER.debug("{}: stopped", WebSocketEventProducerServer.class.getName());
110 public void receiveString(final String eventString) {
111 LOGGER.debug("{}: port {}, received event {}", WebSocketEventProducerServer.class.getName(), port, eventString);
117 * @param args the arguments
118 * @throws MessagingException the messaging exception
120 public static void main(final String[] args) throws MessagingException {
121 if (args.length != 4) {
122 LOGGER.error("usage WebSocketEventProducerServer port #events XML|JSON eventInterval");
128 port = Integer.parseInt(args[0]);
129 } catch (final Exception e) {
130 LOGGER.error("usage WebSocketEventProducerServer port #events XML|JSON eventInterval");
137 eventCount = Integer.parseInt(args[1]);
138 } catch (final Exception e) {
139 LOGGER.error("usage WebSocketEventProducerServer port #events XML|JSON eventInterval");
144 long eventInterval = 0;
146 eventInterval = Long.parseLong(args[3]);
147 } catch (final Exception e) {
148 LOGGER.error("usage WebSocketEventProducerServer port #events XML|JSON eventInterval");
153 boolean xmlEvents = false;
154 if (args[2].equalsIgnoreCase("XML")) {
156 } else if (!args[2].equalsIgnoreCase("JSON")) {
157 LOGGER.error("usage WebSocketEventProducerServer port #events XML|JSON startDelay eventInterval");
161 final WebSocketEventProducerServer server = new WebSocketEventProducerServer(port, eventCount, xmlEvents,