9cf99feb3f996372e7cf6b141d06fb879bd83522
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.core.infrastructure.messaging;
23
24 import static org.junit.Assert.assertNotNull;
25
26 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageListener;
27 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageServer;
28 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
29
30 /**
31  * The Class StringTestServer.
32  */
33 public class StringTestServer {
34     private WsStringMessageServer server;
35
36     /**
37      * Create a string test server.
38      *
39      * @param port port to use
40      * @param timeToLive time to live
41      * @throws MessagingException exceptions on messages
42      */
43     public StringTestServer(final int port, long timeToLive) throws MessagingException {
44         System.out.println("StringTestServer starting on port " + port + " for " + timeToLive + " seconds . . .");
45         server = new WsStringMessageServer(port);
46         assertNotNull(server);
47         server.start(new WsStringServerMessageListener());
48
49         System.out.println("StringTestServer started on port " + port + " for " + timeToLive + " seconds");
50
51         // convert to milliSeconds
52         ThreadUtilities.sleep(1000 * timeToLive);
53
54         server.stop();
55         System.out.println("StringTestServer completed");
56     }
57
58     /**
59      * The listener interface for receiving WSStringServerMessage events. The class that is interested in processing a
60      * WSStringServerMessage event implements this interface, and the object created with that class is registered with
61      * a component using the component's <code>addWSStringServerMessageListener</code> method. When the
62      * WSStringServerMessage event occurs, that object's appropriate method is invoked.
63      *
64      * @see WSStringServerMessageEvent
65      */
66     private class WsStringServerMessageListener implements WsStringMessageListener {
67
68         /**
69          * {@inheritDoc}.
70          */
71         @Override
72         public void receiveString(final String stringMessage) {
73             System.out.println("Server received string \"" + stringMessage + "\"");
74             server.sendString("Server echoing back the message: \"" + stringMessage + "\"");
75         }
76     }
77
78     /**
79      * The main method.
80      *
81      * @param args the arguments
82      * @throws MessagingException the messaging exception
83      */
84     public static void main(final String[] args) throws MessagingException {
85         if (args.length != 2) {
86             System.err.println("Usage: StringTestServer port timeToLive");
87             return;
88         }
89
90         int port = 0;
91         try {
92             port = Integer.parseInt(args[0]);
93         } catch (final Exception e) {
94             System.err.println("Usage: StringTestServer port timeToLive");
95             e.printStackTrace();
96             return;
97         }
98
99         long timeToLive = 0;
100         try {
101             timeToLive = Long.parseLong(args[1]);
102         } catch (final Exception e) {
103             System.err.println("Usage: StringTestServer port timeToLive");
104             e.printStackTrace();
105             return;
106         }
107
108         new StringTestServer(port, timeToLive);
109
110     }
111 }