84a3373628e02b02cac25f2708c4a1653e62da7a
[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.testsuites.integration.uservice.adapt.websocket;
22
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
29 /**
30  * The Class WebSocketEventProducerClient.
31  */
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;
39
40     WsStringMessageClient client;
41
42     /**
43      * Instantiates a new web socket event producer client.
44      *
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
51      */
52     public WebSocketEventProducerClient(final String host, final int port, final int eventCount,
53             final boolean xmlEvents, final long eventInterval) throws MessagingException {
54         this.host = host;
55         this.port = port;
56         this.eventCount = eventCount;
57         this.xmlEvents = xmlEvents;
58         this.eventInterval = eventInterval;
59
60         client = new WsStringMessageClient(host, port);
61         client.start(this);
62
63         System.out.println(WebSocketEventProducerClient.class.getCanonicalName() + ": host " + host + ", port " + port
64                 + ", event count " + eventCount + ", xmlEvents " + xmlEvents);
65     }
66
67     /**
68      * Send events.
69      */
70     public void sendEvents() {
71         System.out.println(WebSocketEventProducerClient.class.getCanonicalName() + ": sending events on host " + host
72                 + ", port " + port + ", event count " + eventCount + ", xmlEvents " + xmlEvents);
73
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);
78
79             String eventString = null;
80             if (xmlEvents) {
81                 eventString = EventGenerator.xmlEvent();
82             } else {
83                 eventString = EventGenerator.jsonEvent();
84             }
85             client.sendString(eventString);
86             eventsSentCount++;
87             System.out.println(WebSocketEventProducerClient.class.getCanonicalName() + ":  host " + host + ", port "
88                     + port + ", sent event " + eventString);
89         }
90         System.out.println(WebSocketEventProducerClient.class.getCanonicalName() + ": completed");
91     }
92
93     /**
94      * Gets the events sent count.
95      *
96      * @return the events sent count
97      */
98     public long getEventsSentCount() {
99         return eventsSentCount;
100     }
101
102     /**
103      * Shutdown.
104      */
105     public void shutdown() {
106         client.stop();
107         System.out.println(WebSocketEventProducerClient.class.getCanonicalName() + ": stopped");
108     }
109
110     /*
111      * (non-Javadoc)
112      * 
113      * @see
114      * org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WSStringMessageListener#
115      * receiveString(java.lang.String)
116      */
117     @Override
118     public void receiveString(final String eventString) {
119         System.out.println(WebSocketEventProducerServer.class.getCanonicalName() + ":  host " + host + ", port " + port
120                 + ", received event " + eventString);
121     }
122
123     /**
124      * The main method.
125      *
126      * @param args the arguments
127      * @throws MessagingException the messaging exception
128      */
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");
132             return;
133         }
134
135         int port = 0;
136         try {
137             port = Integer.parseInt(args[1]);
138         } catch (final Exception e) {
139             System.err.println("usage WebSocketEventProducerClient host port #events XML|JSON eventInterval");
140             e.printStackTrace();
141             return;
142         }
143
144         int eventCount = 0;
145         try {
146             eventCount = Integer.parseInt(args[2]);
147         } catch (final Exception e) {
148             System.err.println("usage WebSocketEventProducerClient host port #events XML|JSON eventInterval");
149             e.printStackTrace();
150             return;
151         }
152
153         long eventInterval = 0;
154         try {
155             eventInterval = Long.parseLong(args[4]);
156         } catch (final Exception e) {
157             System.err.println("usage WebSocketEventProducerClient host port #events XML|JSON eventInterval");
158             e.printStackTrace();
159             return;
160         }
161
162         boolean xmlEvents = false;
163         if (args[3].equalsIgnoreCase("XML")) {
164             xmlEvents = true;
165         } else if (!args[3].equalsIgnoreCase("JSON")) {
166             System.err.println("usage WebSocketEventProducerClient host port #events XML|JSON eventInterval");
167             return;
168         }
169
170         final WebSocketEventProducerClient client =
171                 new WebSocketEventProducerClient(args[0], port, eventCount, xmlEvents, eventInterval);
172
173         client.sendEvents();
174         client.shutdown();
175     }
176 }