0615837af82b2a0bfa1bf33d3fecbc294229d71a
[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.impl.ws.client;
23
24 import java.net.URI;
25 import org.java_websocket.WebSocket;
26 import org.onap.policy.apex.core.infrastructure.messaging.MessageHolder;
27 import org.onap.policy.apex.core.infrastructure.messaging.MessagingService;
28 import org.onap.policy.apex.core.infrastructure.messaging.util.MessagingUtils;
29 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
30
31 /**
32  * The Class MessagingClient is the class that wraps web socket handling, message sending, and
33  * message reception on the client side of a web socket in Apex.
34  *
35  * @author Sajeevan Achuthan (sajeevan.achuthan@ericsson.com)
36  * @param <M> the generic type
37  */
38 public class MessagingClient<M> extends InternalMessageBusClient<M> implements MessagingService<M> {
39     // The length of time to wait for a connection to a web socket server before aborting
40     private static final int CONNECTION_TIMEOUT_TIME_MS = 3000;
41
42     // The length of time to wait before checking if a connection to a web socket server has worked
43     // or not
44     private static final int CONNECTION_TRY_INTERVAL_MS = 100;
45
46     /**
47      * Constructor of this class, uses its {@link InternalMessageBusClient} superclass to set up the
48      * web socket and handle incoming message forwarding.
49      *
50      * @param serverUri The URI of the service
51      */
52     public MessagingClient(final URI serverUri) {
53         // Call the super class to create the web socket and set up received message forwarding
54         super(serverUri);
55     }
56
57     /**
58      * {@inheritDoc}.
59      */
60     @Override
61     public void stopConnection() {
62         // Stop message reception in the super class
63         super.stopListener();
64
65         // Close the web socket
66         final WebSocket connection = super.getConnection();
67         if (connection != null && connection.isOpen()) {
68             connection.closeConnection(0, "");
69         }
70         this.close();
71     }
72
73     /**
74      * {@inheritDoc}.
75      */
76     @Override
77     public void startConnection() {
78         // Open the web socket
79         final WebSocket connection = super.getConnection();
80
81         if (connection == null) {
82             throw new IllegalStateException("Could not connect to the server");
83         }
84         if (!connection.isOpen()) {
85             connect();
86         }
87
88         if (!waitforConnection(connection)) {
89             throw new IllegalStateException("Could not connect to the server");
90         }
91     }
92
93     /**
94      * This method waits for the timeout value for the client to connect to the web socket server.
95      *
96      * @param connection the connection to wait on
97      * @return true, if successful
98      */
99     private boolean waitforConnection(final WebSocket connection) {
100         // The total time we have before timeout
101         int timeoutMsCounter = CONNECTION_TIMEOUT_TIME_MS;
102
103         // Check the connection state
104         do {
105             switch (connection.getReadyState()) {
106                 case NOT_YET_CONNECTED:
107                 case CLOSING:
108                     // Not connected yet so wait for the try interval
109                     ThreadUtilities.sleep(CONNECTION_TRY_INTERVAL_MS);
110                     timeoutMsCounter -= CONNECTION_TRY_INTERVAL_MS;
111                     break;
112                 case OPEN:
113                     // Connection is open, happy days
114                     return true;
115                 case CLOSED:
116                     // Connection is closed, bah
117                     return false;
118                 default:
119                     break;
120             }
121         } while (timeoutMsCounter > 0);
122         // While the timeout value has not expired
123
124         // We have timed out
125         return false;
126     }
127
128     /**
129      * {@inheritDoc}.
130      */
131     @Override
132     public void send(final MessageHolder<M> commands) {
133         // Get the connection and send the message
134         final WebSocket connection = super.getConnection();
135         connection.send(MessagingUtils.serializeObject(commands));
136     }
137
138     /**
139      * {@inheritDoc}.
140      */
141     @Override
142     public void send(final String messageString) {
143         final WebSocket connection = super.getConnection();
144         connection.send(messageString);
145     }
146
147     /**
148      * {@inheritDoc}.
149      */
150     @Override
151     public boolean isStarted() {
152         return getConnection().isOpen();
153     }
154 }