aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/torui/coflsky/websocket/WSClientWrapper.java
blob: 2cdfda16e36c3d8f549ff41371c2070af1602d53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package de.torui.coflsky.websocket;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

import com.neovisionaries.ws.client.WebSocketException;

import de.torui.coflsky.CoflSky;
import de.torui.coflsky.core.Command;
import de.torui.coflsky.core.StringCommand;


public class WSClientWrapper {
    public WSClient socket;
    public Thread thread;
    public boolean isRunning;
    public String uri = "";
    
    public WSClientWrapper(String uri) {
    	this.uri = uri;
    }
    
    public synchronized void start() {
    	if(!isRunning) {
    		try {
    			
				socket = new WSClient(new URI(uri));
			} catch (URISyntaxException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    		/*thread = new Thread(socket);
    		thread.start();
    		isRunning=true;*/
    		isRunning = true;
    		try {
				socket.start();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (WebSocketException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	}
    }
    
    public synchronized void stop() {
    	if(isRunning) {
    	/*	try {
				//socket.closeBlocking();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}*/
    		socket.stop();
    		isRunning = false;
    		socket = null;
    	}
    }
    
    public synchronized void SendMessage(Command cmd){
    	this.socket.SendCommand(cmd);
    }

	public void SendMessage(StringCommand sc) {
		this.socket.SendCommand(sc);
	}
	
	public String GetStatus() {
		return "" + isRunning + " " +  
	    (this.socket!=null ? this.socket.currentState.toString() : "NOT_INITIALIZED");
	}
}