dc186a1e8bc47021e543acc6a62d2b1555f193a4
[policy/apex-pdp.git] /
1 /*-
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
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
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.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.apps.uservice.test.adapt.websocket;
22
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;
28
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;
36
37     WSStringMessageClient client;
38
39     public WebSocketEventProducerClient(final String host, final int port, final int eventCount,
40             final boolean xmlEvents, final long eventInterval) throws MessagingException {
41         this.host = host;
42         this.port = port;
43         this.eventCount = eventCount;
44         this.xmlEvents = xmlEvents;
45         this.eventInterval = eventInterval;
46
47         client = new WSStringMessageClient(host, port);
48         client.start(this);
49
50         System.out.println(WebSocketEventProducerClient.class.getCanonicalName() + ": host " + host + ", port " + port
51                 + ", event count " + eventCount + ", xmlEvents " + xmlEvents);
52     }
53
54     public void sendEvents() {
55         System.out.println(WebSocketEventProducerClient.class.getCanonicalName() + ": sending events on host " + host
56                 + ", port " + port + ", event count " + eventCount + ", xmlEvents " + xmlEvents);
57
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);
62
63             String eventString = null;
64             if (xmlEvents) {
65                 eventString = EventGenerator.xmlEvent();
66             } else {
67                 eventString = EventGenerator.jsonEvent();
68             }
69             client.sendString(eventString);
70             eventsSentCount++;
71             System.out.println(WebSocketEventProducerClient.class.getCanonicalName() + ":  host " + host + ", port "
72                     + port + ", sent event " + eventString);
73         }
74         System.out.println(WebSocketEventProducerClient.class.getCanonicalName() + ": completed");
75     }
76
77     public long getEventsSentCount() {
78         return eventsSentCount;
79     }
80
81     public void shutdown() {
82         client.stop();
83         System.out.println(WebSocketEventProducerClient.class.getCanonicalName() + ": stopped");
84     }
85
86     /*
87      * (non-Javadoc)
88      * 
89      * @see
90      * org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WSStringMessageListener#
91      * receiveString(java.lang.String)
92      */
93     @Override
94     public void receiveString(final String eventString) {
95         System.out.println(WebSocketEventProducerServer.class.getCanonicalName() + ":  host " + host + ", port " + port
96                 + ", received event " + eventString);
97     }
98
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");
102             return;
103         }
104
105         int port = 0;
106         try {
107             port = Integer.parseInt(args[1]);
108         } catch (final Exception e) {
109             System.err.println("usage WebSocketEventProducerClient host port #events XML|JSON eventInterval");
110             e.printStackTrace();
111             return;
112         }
113
114         int eventCount = 0;
115         try {
116             eventCount = Integer.parseInt(args[2]);
117         } catch (final Exception e) {
118             System.err.println("usage WebSocketEventProducerClient host port #events XML|JSON eventInterval");
119             e.printStackTrace();
120             return;
121         }
122
123         long eventInterval = 0;
124         try {
125             eventInterval = Long.parseLong(args[4]);
126         } catch (final Exception e) {
127             System.err.println("usage WebSocketEventProducerClient host port #events XML|JSON eventInterval");
128             e.printStackTrace();
129             return;
130         }
131
132         boolean xmlEvents = false;
133         if (args[3].equalsIgnoreCase("XML")) {
134             xmlEvents = true;
135         } else if (!args[3].equalsIgnoreCase("JSON")) {
136             System.err.println("usage WebSocketEventProducerClient host port #events XML|JSON eventInterval");
137             return;
138         }
139
140         final WebSocketEventProducerClient client =
141                 new WebSocketEventProducerClient(args[0], port, eventCount, xmlEvents, eventInterval);
142
143         client.sendEvents();
144         client.shutdown();
145     }
146 }