Add seed code for sdnr app based on ONF Centennial
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / apps / websocketmanager / impl / src / main / java / org / opendaylight / mwtn / impl / websocket / SyncWebSocketClient.java
1 package org.opendaylight.mwtn.impl.websocket;
2
3 import java.net.URI;
4 import java.net.URISyntaxException;
5
6 import org.java_websocket.client.WebSocketClient;
7 import org.java_websocket.handshake.ServerHandshake;
8 import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory;
10
11 public class SyncWebSocketClient extends WebSocketClient {
12
13         private static final Logger LOG = LoggerFactory.getLogger(SyncWebSocketClient.class.getName());
14         private String messageToSend;
15
16         public SyncWebSocketClient(URI serverUri) {
17                 super(serverUri);
18                 // TODO Auto-generated constructor stub
19         }
20
21         public SyncWebSocketClient(String uri) throws URISyntaxException {
22                 this(new URI(uri));
23         }
24
25         @Override
26         public void onClose(int arg0, String arg1, boolean arg2) {
27                 // TODO Auto-generated method stub
28
29         }
30
31         @Override
32         public void onError(Exception arg0) {
33                 // TODO Auto-generated method stub
34
35         }
36
37         @Override
38         public void onMessage(String arg0) {
39                 // TODO Auto-generated method stub
40
41         }
42
43         @Override
44         public void onOpen(ServerHandshake arg0) {
45                 LOG.debug("ws opened");
46                 if(this.messageToSend!=null)
47                 {
48                         LOG.debug("try to send: "+this.messageToSend);
49                         this.send(this.messageToSend);
50                         this.messageToSend=null;
51                 }
52
53         }
54
55         public void openAndSendAsync(String message)
56         {
57                 this.messageToSend=message;
58                 this.connect();
59         }
60         public void openAndSendAndCloseSync(String message)
61         {
62                 try {
63                         this.connectBlocking();
64                 } catch (InterruptedException e) {
65                         LOG.warn("problem connecting:"+e.getMessage());
66                 }
67                 this.send(message);
68                 try {
69                         this.closeBlocking();
70                 } catch (InterruptedException e) {
71                         LOG.warn("problem disconnecting:"+e.getMessage());
72
73                 }
74         }
75
76
77 }