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;
30 * The Class WebSocketEventProducerClient.
32 public class WebSocketEventProducerClient implements WsStringMessageListener {
33 private final String host;
34 private final int port;
35 private final int eventCount;
36 private final boolean xmlEvents;
37 private final long eventInterval;
38 private long eventsSentCount = 0;
40 WsStringMessageClient client;
43 * Instantiates a new web socket event producer client.
45 * @param host the host
46 * @param port the port
47 * @param eventCount the event count
48 * @param xmlEvents the xml events
49 * @param eventInterval the event interval
50 * @throws MessagingException the messaging exception
52 public WebSocketEventProducerClient(final String host, final int port, final int eventCount,
53 final boolean xmlEvents, final long eventInterval) throws MessagingException {
56 this.eventCount = eventCount;
57 this.xmlEvents = xmlEvents;
58 this.eventInterval = eventInterval;
60 client = new WsStringMessageClient(host, port);
63 System.out.println(WebSocketEventProducerClient.class.getCanonicalName() + ": host " + host + ", port " + port
64 + ", event count " + eventCount + ", xmlEvents " + xmlEvents);
70 public void sendEvents() {
71 System.out.println(WebSocketEventProducerClient.class.getCanonicalName() + ": sending events on host " + host
72 + ", port " + port + ", event count " + eventCount + ", xmlEvents " + xmlEvents);
74 for (int i = 0; i < eventCount; i++) {
75 System.out.println(WebSocketEventProducerClient.class.getCanonicalName() + ": waiting " + eventInterval
76 + " milliseconds before sending next event");
77 ThreadUtilities.sleep(eventInterval);
79 String eventString = null;
81 eventString = EventGenerator.xmlEvent();
83 eventString = EventGenerator.jsonEvent();
85 client.sendString(eventString);
87 System.out.println(WebSocketEventProducerClient.class.getCanonicalName() + ": host " + host + ", port "
88 + port + ", sent event " + eventString);
90 System.out.println(WebSocketEventProducerClient.class.getCanonicalName() + ": completed");
94 * Gets the events sent count.
96 * @return the events sent count
98 public long getEventsSentCount() {
99 return eventsSentCount;
105 public void shutdown() {
107 System.out.println(WebSocketEventProducerClient.class.getCanonicalName() + ": stopped");
114 * org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WSStringMessageListener#
115 * receiveString(java.lang.String)
118 public void receiveString(final String eventString) {
119 System.out.println(WebSocketEventProducerServer.class.getCanonicalName() + ": host " + host + ", port " + port
120 + ", received event " + 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 System.err.println("usage WebSocketEventProducerClient host port #events XML|JSON eventInterval");
137 port = Integer.parseInt(args[1]);
138 } catch (final Exception e) {
139 System.err.println("usage WebSocketEventProducerClient host port #events XML|JSON eventInterval");
146 eventCount = Integer.parseInt(args[2]);
147 } catch (final Exception e) {
148 System.err.println("usage WebSocketEventProducerClient host port #events XML|JSON eventInterval");
153 long eventInterval = 0;
155 eventInterval = Long.parseLong(args[4]);
156 } catch (final Exception e) {
157 System.err.println("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 System.err.println("usage WebSocketEventProducerClient host port #events XML|JSON eventInterval");
170 final WebSocketEventProducerClient client =
171 new WebSocketEventProducerClient(args[0], port, eventCount, xmlEvents, eventInterval);