1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2016-2018 Ericsson. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* ============LICENSE_END=========================================================
*/
package org.onap.policy.apex.apps.uservice.test.adapt.websocket;
import org.onap.policy.apex.apps.uservice.test.adapt.events.EventGenerator;
import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageListener;
import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageServer;
import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
/**
* The Class WebSocketEventProducerServer.
*/
public class WebSocketEventProducerServer implements WsStringMessageListener {
private final int port;
private final int eventCount;
private final boolean xmlEvents;
private final long eventInterval;
private long eventsSentCount = 0;
WsStringMessageServer server;
/**
* Instantiates a new web socket event producer server.
*
* @param port the port
* @param eventCount the event count
* @param xmlEvents the xml events
* @param eventInterval the event interval
* @throws MessagingException the messaging exception
*/
public WebSocketEventProducerServer(final int port, final int eventCount, final boolean xmlEvents,
final long eventInterval) throws MessagingException {
this.port = port;
this.eventCount = eventCount;
this.xmlEvents = xmlEvents;
this.eventInterval = eventInterval;
server = new WsStringMessageServer(port);
server.start(this);
System.out.println(WebSocketEventProducerServer.class.getCanonicalName() + ": port " + port + ", event count "
+ eventCount + ", xmlEvents " + xmlEvents);
}
/**
* Send events.
*/
public void sendEvents() {
System.out.println(WebSocketEventProducerServer.class.getCanonicalName() + ": sending events on port " + port
+ ", event count " + eventCount + ", xmlEvents " + xmlEvents);
for (int i = 0; i < eventCount; i++) {
System.out.println(WebSocketEventProducerServer.class.getCanonicalName() + ": waiting " + eventInterval
+ " milliseconds before sending next event");
ThreadUtilities.sleep(eventInterval);
String eventString = null;
if (xmlEvents) {
eventString = EventGenerator.xmlEvent();
} else {
eventString = EventGenerator.jsonEvent();
}
server.sendString(eventString);
eventsSentCount++;
System.out.println(WebSocketEventProducerServer.class.getCanonicalName() + ": port " + port
+ ", sent event " + eventString);
}
System.out.println(WebSocketEventProducerServer.class.getCanonicalName() + ": event sending completed");
}
/**
* Gets the events sent count.
*
* @return the events sent count
*/
public long getEventsSentCount() {
return eventsSentCount;
}
/**
* Shutdown.
*/
public void shutdown() {
server.stop();
System.out.println(WebSocketEventProducerServer.class.getCanonicalName() + ": stopped");
}
/*
* (non-Javadoc)
*
* @see
* org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WSStringMessageListener#
* receiveString(java.lang.String)
*/
@Override
public void receiveString(final String eventString) {
System.out.println(WebSocketEventProducerServer.class.getCanonicalName() + ": port " + port
+ ", received event " + eventString);
}
/**
* The main method.
*
* @param args the arguments
* @throws MessagingException the messaging exception
*/
public static void main(final String[] args) throws MessagingException {
if (args.length != 4) {
System.err.println("usage WebSocketEventProducerServer port #events XML|JSON eventInterval");
return;
}
int port = 0;
try {
port = Integer.parseInt(args[0]);
} catch (final Exception e) {
System.err.println("usage WebSocketEventProducerServer port #events XML|JSON eventInterval");
e.printStackTrace();
return;
}
int eventCount = 0;
try {
eventCount = Integer.parseInt(args[1]);
} catch (final Exception e) {
System.err.println("usage WebSocketEventProducerServer port #events XML|JSON eventInterval");
e.printStackTrace();
return;
}
long eventInterval = 0;
try {
eventInterval = Long.parseLong(args[3]);
} catch (final Exception e) {
System.err.println("usage WebSocketEventProducerServer port #events XML|JSON eventInterval");
e.printStackTrace();
return;
}
boolean xmlEvents = false;
if (args[2].equalsIgnoreCase("XML")) {
xmlEvents = true;
} else if (!args[2].equalsIgnoreCase("JSON")) {
System.err.println("usage WebSocketEventProducerServer port #events XML|JSON startDelay eventInterval");
return;
}
final WebSocketEventProducerServer server =
new WebSocketEventProducerServer(port, eventCount, xmlEvents, eventInterval);
server.sendEvents();
server.shutdown();
}
}
|