aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/torui/coflsky/websocket/WSClientWrapper.java
blob: 30e5bc893531cf0355e365b69a3d5648cf1debc1 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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;
import de.torui.coflsky.minecraft_integration.PlayerDataProvider;
import net.minecraft.client.Minecraft;
import net.minecraft.util.ChatComponentText;
import de.torui.coflsky.minecraft_integration.CoflSessionManager;


public class WSClientWrapper {
    public WSClient socket;
   // public Thread thread;
    public boolean isRunning;
    
    private String[] uris;

    
    public WSClientWrapper(String[] uris) {
    	this.uris = uris;
    }
    
    public void restartWebsocketConnection() {
    	socket.socket.clearListeners();
    	socket.stop();
    	
    	System.out.println("Sleeping...");
    	Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText("Lost connection to Coflnet, trying to reestablish the connection in 2 Seconds..."));
    	
    	try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    	socket = new WSClient(socket.uri);
    	isRunning = false;    	
    	start();
    }
    
    
    public boolean startConnection() {
    	
    	if(isRunning)
    		return false;
    	
    	for(String s : uris) {
    		
    		System.out.println("Trying connection with uri=" + s);
    		
    		if(initializeNewSocket(s)) {
    			return true;
    		}
    	}
    	
    	throw new Error("Could not connect to any websocket remote!");
    }
    
    
    
    private boolean initializeNewSocket(String uriPrefix) {
    	
    	
    	String uri = uriPrefix;
    	uri += "?version=" + CoflSky.VERSION;
    	
    	String username = PlayerDataProvider.getUsername();
    	uri += "&player=" + username;
    	
    	//Generate a CoflSession
    	
    	try {
			CoflSessionManager.UpdateCoflSessions();
			String coflSessionID = CoflSessionManager.GetCoflSession(username).SessionUUID;
			
			uri += "&SId=" + coflSessionID;	
	    	
			socket = new WSClient(URI.create(uri));
			
			boolean successfull = start();
			if(successfull) {
				socket.shouldRun = true;
			}
			return successfull;
    	} catch(IOException e) {
    		e.printStackTrace();
    	}			

		return false;   	
    	
    }
    
    private synchronized boolean start() {
    	if(!isRunning) {
    		try {
    			
				socket.start();
				isRunning = true;

				return true;
			} 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();
			}
    		return false;
    	}
		return false;
    }
    
    public synchronized void stop() {
    	if(isRunning) {
    		socket.shouldRun = false;
    		socket.stop();
    		isRunning = false;
    		socket = null;
    	}
    }
    
    public synchronized void SendMessage(Command cmd){
    	if(this.isRunning) {
    		this.socket.SendCommand(cmd);
    	} else {
    		Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText("tried sending a callback to coflnet but failed. the connection must be closed."));
    	}
    	
    }

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