Changes for checkstyle 8.32
[policy/apex-pdp.git] / core / core-infrastructure / src / main / java / org / onap / policy / apex / core / infrastructure / messaging / impl / ws / client / InternalMessageBusClient.java
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.impl.ws.client;
22
23 import java.net.URI;
24 import java.nio.ByteBuffer;
25 import org.onap.policy.apex.core.infrastructure.messaging.MessageListener;
26 import org.onap.policy.apex.core.infrastructure.messaging.impl.ws.RawMessageHandler;
27 import org.onap.policy.apex.core.infrastructure.messaging.impl.ws.messageblock.MessageBlockHandler;
28 import org.onap.policy.apex.core.infrastructure.messaging.impl.ws.messageblock.RawMessageBlock;
29 import org.onap.policy.apex.core.infrastructure.threading.ApplicationThreadFactory;
30 import org.slf4j.ext.XLogger;
31 import org.slf4j.ext.XLoggerFactory;
32
33 /**
34  * The Class InternalMessageBusClient handles the client side of a web socket and handles the callback mechanism used to
35  * receive messages on the web socket.
36  *
37  * @author Sajeevan Achuthan (sajeevan.achuthan@ericsson.com)
38  * @param <M> the generic type of message being handled
39  */
40 abstract class InternalMessageBusClient<M> extends WebSocketClientImpl {
41     private static final int THREAD_FACTORY_STACK_SIZE = 256;
42
43     // The logger for this class
44     private static final XLogger LOGGER = XLoggerFactory.getXLogger(InternalMessageBusClient.class);
45
46     // Name of the event bus.
47     private static final String RAW_EVENT_BUS = "Raw-Event-Bus";
48
49     // This instance handles the raw data received from the web socket
50     private final RawMessageHandler<M> rawMessageHandler = new RawMessageHandler<>();
51
52     // The message block handler to which to pass messages coming in on this client
53     private MessageBlockHandler<M> messageBlockHandler = null;
54
55     // The raw message handler uses a thread to process incoming events off a queue, this class owns and controls that
56     // thread. These fields hold the thread and
57     // the thread factory for creating threads.
58     private ApplicationThreadFactory threadFactory =
59             new ApplicationThreadFactory("ws-client-thread", THREAD_FACTORY_STACK_SIZE);
60     private Thread forwarderThread = null;
61
62     /**
63      * Construct the class and start the forwarding thread for received messages.
64      *
65      * @param serverUri the server URI to connect to
66      */
67     InternalMessageBusClient(final URI serverUri) {
68         // Call the super class to create the web socket
69         super(serverUri);
70         LOGGER.entry(serverUri.toString());
71
72         // Create the data handler for forwarding messages
73         messageBlockHandler = new MessageBlockHandler<>(RAW_EVENT_BUS);
74         messageBlockHandler.registerMessageHandler(rawMessageHandler);
75
76         // Create the thread that manages the queue in the data handler
77         forwarderThread = threadFactory.newThread(rawMessageHandler);
78         forwarderThread.start();
79
80         LOGGER.exit();
81     }
82
83     /**
84      * Callback for binary messages received from the remote host.
85      *
86      * @param rawMessage the received raw message
87      * @see org.java_websocket.client.WebSocketClient#onMessage(java.nio.ByteBuffer)
88      */
89     @Override
90     public void onMessage(final ByteBuffer rawMessage) {
91         // Post the message to the data handler for forwarding to its listeners
92         messageBlockHandler.post(new RawMessageBlock(rawMessage, null));
93     }
94
95     /**
96      * Callback for binary messages received from the remote host.
97      *
98      * @param stringMessage the string message
99      * @see org.java_websocket.client.WebSocketClient#onMessage(java.lang.String)
100      */
101     @Override
102     public final void onMessage(final String stringMessage) {
103         messageBlockHandler.post(stringMessage);
104     }
105
106     /**
107      * Register a subscriber class to the raw message handler.
108      *
109      * @param listener a simple class, that listens for the events from Event
110      */
111     public void addMessageListener(final MessageListener<M> listener) {
112         rawMessageHandler.registerDataForwarder(listener);
113     }
114
115     /**
116      * Removes the message listener.
117      *
118      * @param listener the listener
119      */
120     public void removeMessageListener(final MessageListener<M> listener) {
121         rawMessageHandler.unRegisterDataForwarder(listener);
122     }
123
124     /**
125      * Stop the thread handling message forwarding.
126      */
127     protected void stopListener() {
128         rawMessageHandler.shutdown();
129     }
130 }