aboutsummaryrefslogtreecommitdiffstats
path: root/core/core-infrastructure/src/main/java/org/onap/policy/apex/core/infrastructure/messaging/impl/ws/client/MessagingClient.java
blob: 36ad3b163231eecabddcc2413c23dcf53a65c9ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.apex.core.infrastructure.messaging.impl.ws.client;

import java.net.URI;

import org.java_websocket.WebSocket;
import org.onap.policy.apex.core.infrastructure.messaging.MessageHolder;
import org.onap.policy.apex.core.infrastructure.messaging.MessagingService;
import org.onap.policy.apex.core.infrastructure.messaging.util.MessagingUtils;
import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;

/**
 * The Class MessagingClient is the class that wraps web socket handling, message sending, and
 * message reception on the client side of a web socket in Apex.
 *
 * @author Sajeevan Achuthan (sajeevan.achuthan@ericsson.com)
 * @param <MESSAGE> the generic type
 */
public class MessagingClient<MESSAGE> extends InternalMessageBusClient<MESSAGE> implements MessagingService<MESSAGE> {
    // The length of time to wait for a connection to a web socket server before aborting
    private static final int CONNECTION_TIMEOUT_TIME_MS = 3000;

    // The length of time to wait before checking if a connection to a web socket server has worked
    // or not
    private static final int CONNECTION_TRY_INTERVAL_MS = 100;

    /**
     * Constructor of this class, uses its {@link InternalMessageBusClient} superclass to set up the
     * web socket and handle incoming message forwarding.
     *
     * @param serverUri The URI of the service
     */
    public MessagingClient(final URI serverUri) {
        // Call the super class to create the web socket and set up received message forwarding
        super(serverUri);
    }

    /*
     * (non-Javadoc)
     *
     * @see org.onap.policy.apex.core.infrastructure.messaging.MessagingService#stopConnection()
     */
    @Override
    public void stopConnection() {
        // Stop message reception in the super class
        super.stopListener();

        // Close the web socket
        final WebSocket connection = super.getConnection();
        if (connection != null && connection.isOpen()) {
            connection.closeConnection(0, "");
        }
        this.close();
    }

    /*
     * (non-Javadoc)
     *
     * @see org.onap.policy.apex.core.infrastructure.messaging.MessagingService#startConnection()
     */
    @Override
    public void startConnection() {
        // Open the web socket
        final WebSocket connection = super.getConnection();

        if (connection == null) {
            throw new IllegalStateException("Could not connect to the server");
        }
        if (!connection.isOpen()) {
            connect();
        }

        if (!waitforConnection(connection)) {
            throw new IllegalStateException("Could not connect to the server");
        }
    }

    /**
     * This method waits for the timeout value for the client to connect to the web socket server.
     *
     * @param connection the connection to wait on
     * @return true, if successful
     */
    private boolean waitforConnection(final WebSocket connection) {
        // The total time we have before timeout
        int timeoutMSCounter = CONNECTION_TIMEOUT_TIME_MS;

        // Check the connection state
        do {
            switch (connection.getReadyState()) {
                case NOT_YET_CONNECTED:
                case CONNECTING:
                case CLOSING:
                    // Not connected yet so wait for the try interval
                    ThreadUtilities.sleep(CONNECTION_TRY_INTERVAL_MS);
                    timeoutMSCounter -= CONNECTION_TRY_INTERVAL_MS;
                    break;
                case OPEN:
                    // Connection is open, happy days
                    return true;
                case CLOSED:
                    // Connection is closed, bah
                    return false;
                default:
                    break;
            }
        }
        // While the timeout value has not expired
        while (timeoutMSCounter > 0);

        // We have timed out
        return false;
    }

    /*
     * (non-Javadoc)
     *
     * @see
     * org.onap.policy.apex.core.infrastructure.messaging.MessagingService#send(org.onap.policy.apex
     * .core. infrastructure. messaging.MessageHolder)
     */
    @Override
    public void send(final MessageHolder<MESSAGE> commands) {
        // Get the connection and send the message
        final WebSocket connection = super.getConnection();
        connection.send(MessagingUtils.serializeObject(commands));
    }

    /*
     * (non-Javadoc)
     *
     * @see
     * org.onap.policy.apex.core.infrastructure.messaging.MessagingService#send(java.lang.String)
     */
    @Override
    public void send(final String messageString) {
        final WebSocket connection = super.getConnection();
        connection.send(messageString);
    }

    /*
     * (non-Javadoc)
     *
     * @see org.onap.policy.apex.core.infrastructure.messaging.MessagingService#isStarted()
     */
    @Override
    public boolean isStarted() {
        return getConnection().isOpen();
    }
}