46973290aa41f98636aab23fb97d4bc3b1960a9c
[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      * {@inheritDoc}.
112      */
113     @Override
114     public void receiveString(final String eventString) {
115         System.out.println(WebSocketEventProducerServer.class.getCanonicalName() + ":  host " + host + ", port " + port
116                 + ", received event " + eventString);
117     }
118
119     /**
120      * The main method.
121      *
122      * @param args the arguments
123      * @throws MessagingException the messaging exception
124      */
125     public static void main(final String[] args) throws MessagingException {
126         if (args.length != 5) {
127             System.err.println("usage WebSocketEventProducerClient host port #events XML|JSON eventInterval");
128             return;
129         }
130
131         int port = 0;
132         try {
133             port = Integer.parseInt(args[1]);
134         } catch (final Exception e) {
135             System.err.println("usage WebSocketEventProducerClient host port #events XML|JSON eventInterval");
136             e.printStackTrace();
137             return;
138         }
139
140         int eventCount = 0;
141         try {
142             eventCount = Integer.parseInt(args[2]);
143         } catch (final Exception e) {
144             System.err.println("usage WebSocketEventProducerClient host port #events XML|JSON eventInterval");
145             e.printStackTrace();
146             return;
147         }
148
149         long eventInterval = 0;
150         try {
151             eventInterval = Long.parseLong(args[4]);
152         } catch (final Exception e) {
153             System.err.println("usage WebSocketEventProducerClient host port #events XML|JSON eventInterval");
154             e.printStackTrace();
155             return;
156         }
157
158         boolean xmlEvents = false;
159         if (args[3].equalsIgnoreCase("XML")) {
160             xmlEvents = true;
161         } else if (!args[3].equalsIgnoreCase("JSON")) {
162             System.err.println("usage WebSocketEventProducerClient host port #events XML|JSON eventInterval");
163             return;
164         }
165
166         final WebSocketEventProducerClient client =
167                 new WebSocketEventProducerClient(args[0], port, eventCount, xmlEvents, eventInterval);
168
169         client.sendEvents();
170         client.shutdown();
171     }
172 }