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.WsStringMessageClient;
25 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageListener;
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 WebSocketEventProducerClient.
34 public class WebSocketEventProducerClient implements WsStringMessageListener {
35 private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketEventProducerClient.class);
37 private final String host;
38 private final int port;
39 private final int eventCount;
40 private final boolean xmlEvents;
41 private final long eventInterval;
42 private long eventsSentCount = 0;
44 WsStringMessageClient client;
47 * Instantiates a new web socket event producer client.
49 * @param host the host
50 * @param port the port
51 * @param eventCount the event count
52 * @param xmlEvents the xml events
53 * @param eventInterval the event interval
54 * @throws MessagingException the messaging exception
56 public WebSocketEventProducerClient(final String host, final int port, final int eventCount,
57 final boolean xmlEvents, final long eventInterval) throws MessagingException {
60 this.eventCount = eventCount;
61 this.xmlEvents = xmlEvents;
62 this.eventInterval = eventInterval;
64 client = new WsStringMessageClient(host, port);
67 LOGGER.debug("{}: host {}, port {}, event count {}, xmlEvents {}",
68 WebSocketEventProducerClient.class.getCanonicalName(), host, port, eventCount, xmlEvents);
74 public void sendEvents() {
75 LOGGER.debug("{}: sending events on host {}, port {}, event count {}, xmlEvents {}",
76 WebSocketEventProducerClient.class.getCanonicalName(), host, port, eventCount, xmlEvents);
78 for (int i = 0; i < eventCount; i++) {
79 LOGGER.debug("{}: waiting {} milliseconds before sending next event",
80 WebSocketEventProducerClient.class.getCanonicalName(), eventInterval);
81 ThreadUtilities.sleep(eventInterval);
83 String eventString = null;
85 eventString = EventGenerator.xmlEvent();
87 eventString = EventGenerator.jsonEvent();
89 client.sendString(eventString);
91 LOGGER.debug("{}: host {}, port {}, sent event {}", WebSocketEventProducerClient.class.getCanonicalName(),
92 host, port, eventString);
94 LOGGER.debug("{}: completed", WebSocketEventProducerClient.class.getCanonicalName());
98 * Gets the events sent count.
100 * @return the events sent count
102 public long getEventsSentCount() {
103 return eventsSentCount;
109 public void shutdown() {
111 LOGGER.debug("{}: stopped", WebSocketEventProducerClient.class.getCanonicalName());
118 public void receiveString(final String eventString) {
119 LOGGER.debug("{}: host {}, port {}, received event {}", WebSocketEventProducerServer.class.getCanonicalName(),
120 host, port, eventString);
126 * @param args the arguments
127 * @throws MessagingException the messaging exception
129 public static void main(final String[] args) throws MessagingException {
130 if (args.length != 5) {
131 LOGGER.error("usage WebSocketEventProducerClient host port #events XML|JSON eventInterval");
137 port = Integer.parseInt(args[1]);
138 } catch (final Exception e) {
139 LOGGER.error("usage WebSocketEventProducerClient host port #events XML|JSON eventInterval");
146 eventCount = Integer.parseInt(args[2]);
147 } catch (final Exception e) {
148 LOGGER.error("usage WebSocketEventProducerClient host port #events XML|JSON eventInterval");
153 long eventInterval = 0;
155 eventInterval = Long.parseLong(args[4]);
156 } catch (final Exception e) {
157 LOGGER.error("usage WebSocketEventProducerClient host port #events XML|JSON eventInterval");
162 boolean xmlEvents = false;
163 if (args[3].equalsIgnoreCase("XML")) {
165 } else if (!args[3].equalsIgnoreCase("JSON")) {
166 LOGGER.error("usage WebSocketEventProducerClient host port #events XML|JSON eventInterval");
170 final WebSocketEventProducerClient client = new WebSocketEventProducerClient(args[0], port, eventCount,
171 xmlEvents, eventInterval);