c0d66b2d892ea61e9e2935de55c16f3b60ae5818
[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.core.infrastructure.messaging;
22
23 import static org.junit.Assert.assertNotNull;
24
25 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageListener;
26 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageServer;
27 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
28
29 /**
30  * The Class StringTestServer.
31  */
32 public class StringTestServer {
33     private WsStringMessageServer server;
34
35     /**
36      * Create a string test server.
37      *
38      * @param port port to use
39      * @param timeToLive time to live
40      * @throws MessagingException exceptions on messages
41      */
42     public StringTestServer(final int port, long timeToLive) throws MessagingException {
43         System.out.println("StringTestServer starting on port " + port + " for " + timeToLive + " seconds . . .");
44         server = new WsStringMessageServer(port);
45         assertNotNull(server);
46         server.start(new WsStringServerMessageListener());
47
48         System.out.println("StringTestServer started on port " + port + " for " + timeToLive + " seconds");
49
50         for (; timeToLive > 0; timeToLive--) {
51             ThreadUtilities.sleep(1000);
52         }
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          * (non-Javadoc)
70          * 
71          * @see
72          * org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageListener#receiveString(java
73          * .lang.String)
74          */
75         @Override
76         public void receiveString(final String stringMessage) {
77             System.out.println("Server received string \"" + stringMessage + "\"");
78             server.sendString("Server echoing back the message: \"" + stringMessage + "\"");
79         }
80     }
81
82     /**
83      * The main method.
84      *
85      * @param args the arguments
86      * @throws MessagingException the messaging exception
87      */
88     public static void main(final String[] args) throws MessagingException {
89         if (args.length != 2) {
90             System.err.println("Usage: StringTestServer port timeToLive");
91             return;
92         }
93
94         int port = 0;
95         try {
96             port = Integer.parseInt(args[0]);
97         } catch (final Exception e) {
98             System.err.println("Usage: StringTestServer port timeToLive");
99             e.printStackTrace();
100             return;
101         }
102
103         long timeToLive = 0;
104         try {
105             timeToLive = Long.parseLong(args[1]);
106         } catch (final Exception e) {
107             System.err.println("Usage: StringTestServer port timeToLive");
108             e.printStackTrace();
109             return;
110         }
111
112         new StringTestServer(port, timeToLive);
113
114     }
115 }