6512e7171579546b8cb43e943cef73b5369a91fc
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. 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 distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  */
18 package org.onap.ccsdk.features.sdnr.wt.websocketmanager2.websocket;
19
20 import java.net.URI;
21 import java.net.URISyntaxException;
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import org.java_websocket.client.WebSocketClient;
26 import org.java_websocket.handshake.ServerHandshake;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public class SyncWebSocketClient extends WebSocketClient {
31
32     public interface WebsocketEventHandler {
33         void onMessageReceived(String message);
34
35         void onOpen(ServerHandshake arg0);
36
37         void onClose(int arg0, String arg1, boolean arg2);
38
39         void onError(Exception e);
40     }
41
42     private static final Logger LOG = LoggerFactory.getLogger(SyncWebSocketClient.class.getName());
43     private String messageToSend;
44     private final List<WebsocketEventHandler> handlers;
45
46     public SyncWebSocketClient(URI serverUri) {
47         super(serverUri);
48         this.handlers = new ArrayList<WebsocketEventHandler>();
49     }
50
51     public SyncWebSocketClient(String uri) throws URISyntaxException {
52         this(new URI(uri));
53     }
54
55     public void addEventHandler(WebsocketEventHandler h) {
56         this.handlers.add(h);
57     }
58
59     public void removeEventHandler(WebsocketEventHandler h) {
60         this.handlers.remove(h);
61     }
62
63     @Override
64     public void onClose(int arg0, String arg1, boolean arg2) {
65         LOG.debug("socket closed: {} {} {}", arg0, arg1, arg2);
66         for (WebsocketEventHandler h : this.handlers) {
67             h.onClose(arg0, arg1, arg2);
68         }
69     }
70
71     @Override
72     public void onError(Exception arg0) {
73         LOG.warn("error on socket: {}", arg0.getMessage());
74         for (WebsocketEventHandler h : this.handlers) {
75             h.onError(arg0);
76         }
77     }
78
79     @Override
80     public void onMessage(String arg0) {
81         LOG.debug("received message: {}", arg0);
82         for (WebsocketEventHandler h : this.handlers) {
83             h.onMessageReceived(arg0);
84         }
85     }
86
87     @Override
88     public void onOpen(ServerHandshake arg0) {
89         LOG.debug("socket opened");
90         if (this.messageToSend != null) {
91             LOG.debug("try to send: " + this.messageToSend);
92             this.send(this.messageToSend);
93             this.messageToSend = null;
94         }
95         for (WebsocketEventHandler h : this.handlers) {
96             h.onOpen(arg0);
97         }
98     }
99
100     public void openAndSendAsync(String message) {
101         this.messageToSend = message;
102         this.connect();
103     }
104
105     public void openAndSendAndCloseSync(String message) {
106         try {
107             this.connectBlocking();
108         } catch (InterruptedException e) {
109             LOG.warn("problem connecting:" + e.getMessage());
110             Thread.currentThread().interrupt();
111         }
112         this.send(message);
113         try {
114             this.closeBlocking();
115         } catch (InterruptedException e) {
116             LOG.warn("problem disconnecting:" + e.getMessage());
117             Thread.currentThread().interrupt();
118         }
119     }
120
121 }