Replace depreciated MDSAL interfaces
[ccsdk/features.git] / sdnr / wt / websocketmanager2 / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / websocketmanager2 / websocket / SyncWebSocketClient.java
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                 void onOpen(ServerHandshake arg0);
35                 void onClose(int arg0, String arg1, boolean arg2);
36                 void onError(Exception e);
37         }
38         
39         private static final Logger LOG = LoggerFactory.getLogger(SyncWebSocketClient.class.getName());
40         private String messageToSend;
41         private final List<WebsocketEventHandler> handlers;
42         public SyncWebSocketClient(URI serverUri) {
43                 super(serverUri);
44                 this.handlers = new ArrayList<WebsocketEventHandler>();
45         }
46
47         public SyncWebSocketClient(String uri) throws URISyntaxException {
48                 this(new URI(uri));
49         }
50         public void addEventHandler(WebsocketEventHandler h) {
51                 this.handlers.add(h);
52         }
53         public void removeEventHandler(WebsocketEventHandler h) {
54                 this.handlers.remove(h);
55         }
56         
57         @Override
58         public void onClose(int arg0, String arg1, boolean arg2) {
59                 LOG.debug("socket closed: {} {} {}", arg0, arg1, arg2);
60                 for(WebsocketEventHandler h:this.handlers) {
61                         h.onClose(arg0,arg1,arg2);
62                 }
63         }
64
65         @Override
66         public void onError(Exception arg0) {
67                 LOG.warn("error on socket: {}", arg0.getMessage());
68                 for(WebsocketEventHandler h:this.handlers) {
69                         h.onError(arg0);
70                 }
71         }
72
73         @Override
74         public void onMessage(String arg0) {
75                 LOG.debug("received message: {}", arg0);
76                 for(WebsocketEventHandler h:this.handlers) {
77                         h.onMessageReceived(arg0);
78                 }
79         }
80
81         @Override
82         public void onOpen(ServerHandshake arg0) {
83                 LOG.debug("socket opened");
84                 if (this.messageToSend != null) {
85                         LOG.debug("try to send: " + this.messageToSend);
86                         this.send(this.messageToSend);
87                         this.messageToSend = null;
88                 }
89                 for(WebsocketEventHandler h:this.handlers) {
90                         h.onOpen(arg0);
91                 }
92         }
93
94         public void openAndSendAsync(String message) {
95                 this.messageToSend = message;
96                 this.connect();
97         }
98
99         public void openAndSendAndCloseSync(String message) {
100                 try {
101                         this.connectBlocking();
102                 } catch (InterruptedException e) {
103                         LOG.warn("problem connecting:" + e.getMessage());
104                         Thread.currentThread().interrupt();
105                 }
106                 this.send(message);
107                 try {
108                         this.closeBlocking();
109                 } catch (InterruptedException e) {
110                         LOG.warn("problem disconnecting:" + e.getMessage());
111             Thread.currentThread().interrupt();
112                 }
113         }
114
115 }