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.apps.uservice.test.adapt.websocket;
23 import org.onap.policy.apex.apps.uservice.test.adapt.events.EventGenerator;
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;
29 public class WebSocketEventProducerClient implements WSStringMessageListener {
30 private final String host;
31 private final int port;
32 private final int eventCount;
33 private final boolean xmlEvents;
34 private final long eventInterval;
35 private long eventsSentCount = 0;
37 WSStringMessageClient client;
39 public WebSocketEventProducerClient(final String host, final int port, final int eventCount,
40 final boolean xmlEvents, final long eventInterval) throws MessagingException {
43 this.eventCount = eventCount;
44 this.xmlEvents = xmlEvents;
45 this.eventInterval = eventInterval;
47 client = new WSStringMessageClient(host, port);
50 System.out.println(WebSocketEventProducerClient.class.getCanonicalName() + ": host " + host + ", port " + port
51 + ", event count " + eventCount + ", xmlEvents " + xmlEvents);
54 public void sendEvents() {
55 System.out.println(WebSocketEventProducerClient.class.getCanonicalName() + ": sending events on host " + host
56 + ", port " + port + ", event count " + eventCount + ", xmlEvents " + xmlEvents);
58 for (int i = 0; i < eventCount; i++) {
59 System.out.println(WebSocketEventProducerClient.class.getCanonicalName() + ": waiting " + eventInterval
60 + " milliseconds before sending next event");
61 ThreadUtilities.sleep(eventInterval);
63 String eventString = null;
65 eventString = EventGenerator.xmlEvent();
67 eventString = EventGenerator.jsonEvent();
69 client.sendString(eventString);
71 System.out.println(WebSocketEventProducerClient.class.getCanonicalName() + ": host " + host + ", port "
72 + port + ", sent event " + eventString);
74 System.out.println(WebSocketEventProducerClient.class.getCanonicalName() + ": completed");
77 public long getEventsSentCount() {
78 return eventsSentCount;
81 public void shutdown() {
83 System.out.println(WebSocketEventProducerClient.class.getCanonicalName() + ": stopped");
90 * org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WSStringMessageListener#
91 * receiveString(java.lang.String)
94 public void receiveString(final String eventString) {
95 System.out.println(WebSocketEventProducerServer.class.getCanonicalName() + ": host " + host + ", port " + port
96 + ", received event " + eventString);
99 public static void main(final String[] args) throws MessagingException {
100 if (args.length != 5) {
101 System.err.println("usage WebSocketEventProducerClient host port #events XML|JSON eventInterval");
107 port = Integer.parseInt(args[1]);
108 } catch (final Exception e) {
109 System.err.println("usage WebSocketEventProducerClient host port #events XML|JSON eventInterval");
116 eventCount = Integer.parseInt(args[2]);
117 } catch (final Exception e) {
118 System.err.println("usage WebSocketEventProducerClient host port #events XML|JSON eventInterval");
123 long eventInterval = 0;
125 eventInterval = Long.parseLong(args[4]);
126 } catch (final Exception e) {
127 System.err.println("usage WebSocketEventProducerClient host port #events XML|JSON eventInterval");
132 boolean xmlEvents = false;
133 if (args[3].equalsIgnoreCase("XML")) {
135 } else if (!args[3].equalsIgnoreCase("JSON")) {
136 System.err.println("usage WebSocketEventProducerClient host port #events XML|JSON eventInterval");
140 final WebSocketEventProducerClient client =
141 new WebSocketEventProducerClient(args[0], port, eventCount, xmlEvents, eventInterval);