09fa62d59c1ff27a5bf49caa5a6e123853f7e418
[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 public class StringTestServer {
30     private WSStringMessageServer server;
31
32     public StringTestServer(final int port, long timeToLive) throws MessagingException {
33         System.out.println("StringTestServer starting on port " + port + " for " + timeToLive + " seconds . . .");
34         server = new WSStringMessageServer(port);
35         assertNotNull(server);
36         server.start(new WSStringServerMessageListener());
37
38         System.out.println("StringTestServer started on port " + port + " for " + timeToLive + " seconds");
39
40         for (; timeToLive > 0; timeToLive--) {
41             ThreadUtilities.sleep(1000);
42         }
43
44         server.stop();
45         System.out.println("StringTestServer completed");
46     }
47
48     private class WSStringServerMessageListener implements WSStringMessageListener {
49         @Override
50         public void receiveString(final String stringMessage) {
51             System.out.println("Server received string \"" + stringMessage + "\"");
52             server.sendString("Server echoing back the message: \"" + stringMessage + "\"");
53         }
54     }
55
56     public static void main(final String[] args) throws MessagingException {
57         if (args.length != 2) {
58             System.err.println("Usage: StringTestServer port timeToLive");
59             return;
60         }
61
62         int port = 0;
63         try {
64             port = Integer.parseInt(args[0]);
65         } catch (final Exception e) {
66             System.err.println("Usage: StringTestServer port timeToLive");
67             e.printStackTrace();
68             return;
69         }
70
71         long timeToLive = 0;
72         try {
73             timeToLive = Long.parseLong(args[1]);
74         } catch (final Exception e) {
75             System.err.println("Usage: StringTestServer port timeToLive");
76             e.printStackTrace();
77             return;
78         }
79
80         new StringTestServer(port, timeToLive);
81
82     }
83 }