2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.testsuites.integration.uservice.adapt.websocket;
24 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
25 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageClient;
26 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageListener;
27 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
28 import org.onap.policy.apex.testsuites.integration.uservice.adapt.events.EventGenerator;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * The Class WebSocketEventProducerClient.
35 public class WebSocketEventProducerClient implements WsStringMessageListener {
36 private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketEventProducerClient.class);
38 private final String host;
39 private final int port;
40 private final int eventCount;
41 private final boolean xmlEvents;
42 private final long eventInterval;
43 private long eventsSentCount = 0;
45 WsStringMessageClient client;
48 * Instantiates a new web socket event producer client.
50 * @param host the host
51 * @param port the port
52 * @param eventCount the event count
53 * @param xmlEvents the xml events
54 * @param eventInterval the event interval
55 * @throws MessagingException the messaging exception
57 public WebSocketEventProducerClient(final String host, final int port, final int eventCount,
58 final boolean xmlEvents, final long eventInterval) throws MessagingException {
61 this.eventCount = eventCount;
62 this.xmlEvents = xmlEvents;
63 this.eventInterval = eventInterval;
65 client = new WsStringMessageClient(host, port);
68 LOGGER.debug("{}: host {}, port {}, event count {}, xmlEvents {}", WebSocketEventProducerClient.class.getName(),
69 host, port, eventCount, xmlEvents);
75 public void sendEvents() {
76 LOGGER.debug("{}: sending events on host {}, port {}, event count {}, xmlEvents {}",
77 WebSocketEventProducerClient.class.getName(), host, port, eventCount, xmlEvents);
79 for (int i = 0; i < eventCount; i++) {
80 LOGGER.debug("{}: waiting {} milliseconds before sending next event",
81 WebSocketEventProducerClient.class.getName(), eventInterval);
82 ThreadUtilities.sleep(eventInterval);
84 String eventString = null;
86 eventString = EventGenerator.xmlEvent();
88 eventString = EventGenerator.jsonEvent();
90 client.sendString(eventString);
92 LOGGER.debug("{}: host {}, port {}, sent event {}", WebSocketEventProducerClient.class.getName(), host,
95 LOGGER.debug("{}: completed", WebSocketEventProducerClient.class.getName());
99 * Gets the events sent count.
101 * @return the events sent count
103 public long getEventsSentCount() {
104 return eventsSentCount;
110 public void shutdown() {
112 LOGGER.debug("{}: stopped", WebSocketEventProducerClient.class.getName());
119 public void receiveString(final String eventString) {
120 LOGGER.debug("{}: host {}, port {}, received event {}", WebSocketEventProducerServer.class.getName(), host,
127 * @param args the arguments
128 * @throws MessagingException the messaging exception
130 public static void main(final String[] args) throws MessagingException {
131 if (args.length != 5) {
132 LOGGER.error("usage WebSocketEventProducerClient host port #events XML|JSON eventInterval");
138 port = Integer.parseInt(args[1]);
139 } catch (final Exception e) {
140 LOGGER.error("usage WebSocketEventProducerClient host port #events XML|JSON eventInterval");
147 eventCount = Integer.parseInt(args[2]);
148 } catch (final Exception e) {
149 LOGGER.error("usage WebSocketEventProducerClient host port #events XML|JSON eventInterval");
154 long eventInterval = 0;
156 eventInterval = Long.parseLong(args[4]);
157 } catch (final Exception e) {
158 LOGGER.error("usage WebSocketEventProducerClient host port #events XML|JSON eventInterval");
163 boolean xmlEvents = false;
164 if (args[3].equalsIgnoreCase("XML")) {
166 } else if (!args[3].equalsIgnoreCase("JSON")) {
167 LOGGER.error("usage WebSocketEventProducerClient host port #events XML|JSON eventInterval");
171 final WebSocketEventProducerClient client = new WebSocketEventProducerClient(args[0], port, eventCount,
172 xmlEvents, eventInterval);