30590c0c0c4afa5e9a9e90c54ffe3cc184b71815
[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 // TODO: Auto-generated Javadoc
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         for (; timeToLive > 0; timeToLive--) {
52             ThreadUtilities.sleep(1000);
53         }
54
55         server.stop();
56         System.out.println("StringTestServer completed");
57     }
58
59     /**
60      * The listener interface for receiving WSStringServerMessage events. The class that is interested in processing a
61      * WSStringServerMessage event implements this interface, and the object created with that class is registered with
62      * a component using the component's <code>addWSStringServerMessageListener</code> method. When the
63      * WSStringServerMessage event occurs, that object's appropriate method is invoked.
64      *
65      * @see WSStringServerMessageEvent
66      */
67     private class WsStringServerMessageListener implements WsStringMessageListener {
68
69         /*
70          * (non-Javadoc)
71          * 
72          * @see
73          * org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageListener#receiveString(java
74          * .lang.String)
75          */
76         @Override
77         public void receiveString(final String stringMessage) {
78             System.out.println("Server received string \"" + stringMessage + "\"");
79             server.sendString("Server echoing back the message: \"" + stringMessage + "\"");
80         }
81     }
82
83     /**
84      * The main method.
85      *
86      * @param args the arguments
87      * @throws MessagingException the messaging exception
88      */
89     public static void main(final String[] args) throws MessagingException {
90         if (args.length != 2) {
91             System.err.println("Usage: StringTestServer port timeToLive");
92             return;
93         }
94
95         int port = 0;
96         try {
97             port = Integer.parseInt(args[0]);
98         } catch (final Exception e) {
99             System.err.println("Usage: StringTestServer port timeToLive");
100             e.printStackTrace();
101             return;
102         }
103
104         long timeToLive = 0;
105         try {
106             timeToLive = Long.parseLong(args[1]);
107         } catch (final Exception e) {
108             System.err.println("Usage: StringTestServer port timeToLive");
109             e.printStackTrace();
110             return;
111         }
112
113         new StringTestServer(port, timeToLive);
114
115     }
116 }