UX extensions
[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
23 import org.java_websocket.client.WebSocketClient;
24 import org.java_websocket.handshake.ServerHandshake;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public class SyncWebSocketClient extends WebSocketClient {
29
30         private static final Logger LOG = LoggerFactory.getLogger(SyncWebSocketClient.class.getName());
31         private String messageToSend;
32
33         public SyncWebSocketClient(URI serverUri) {
34                 super(serverUri);
35         }
36
37         public SyncWebSocketClient(String uri) throws URISyntaxException {
38                 this(new URI(uri));
39         }
40
41         @Override
42         public void onClose(int arg0, String arg1, boolean arg2) {
43                 LOG.debug("socket closed: {} {} {}", arg0, arg1, arg2);
44
45         }
46
47         @Override
48         public void onError(Exception arg0) {
49                 LOG.warn("error on socket: {}", arg0.getMessage());
50
51         }
52
53         @Override
54         public void onMessage(String arg0) {
55                 LOG.debug("received message: {}", arg0);
56
57         }
58
59         @Override
60         public void onOpen(ServerHandshake arg0) {
61                 LOG.debug("socket opened");
62                 if (this.messageToSend != null) {
63                         LOG.debug("try to send: " + this.messageToSend);
64                         this.send(this.messageToSend);
65                         this.messageToSend = null;
66                 }
67
68         }
69
70         public void openAndSendAsync(String message) {
71                 this.messageToSend = message;
72                 this.connect();
73         }
74
75         public void openAndSendAndCloseSync(String message) {
76                 try {
77                         this.connectBlocking();
78                 } catch (InterruptedException e) {
79                         LOG.warn("problem connecting:" + e.getMessage());
80                         Thread.currentThread().interrupt();
81                 }
82                 this.send(message);
83                 try {
84                         this.closeBlocking();
85                 } catch (InterruptedException e) {
86                         LOG.warn("problem disconnecting:" + e.getMessage());
87             Thread.currentThread().interrupt();
88                 }
89         }
90
91 }