/
AgentWebSocketClient.java
118 lines (99 loc) · 3.6 KB
/
AgentWebSocketClient.java
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
package vlove.virt.agent;
import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.UUID;
import jline.console.ConsoleReader;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import vlove.model.json.AgentConnectionMessage;
import vlove.model.json.AgentDisconnectionMessage;
import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.websocket.WebSocket;
import com.ning.http.client.websocket.WebSocketUpgradeHandler;
public class AgentWebSocketClient implements AgentSocketCallback {
protected static final Logger log = LoggerFactory.getLogger(AgentWebSocketClient.class);
private final AsyncHttpClient c = new AsyncHttpClient();
private final ObjectMapper om = new ObjectMapper();
private WebSocket websocket;
private final ConsoleReader reader;
private boolean wantsToClose = false;
private String uuid = null;
public AgentWebSocketClient(ConsoleReader reader) {
this.reader = reader;
}
public void connect() {
try {
c.prepareGet("ws://localhost:8080/vlove/s/agent").execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new AgentWebSocketListener(this, reader)).build()).get();
} catch (Exception ex) {
try {
reader.println("Could not establish WebSocketConnection. " + ex.getMessage());
reader.flush();
} catch (IOException e) {
// ignore this
}
}
}
public void disconnect() {
wantsToClose = true;
if (c != null && !c.isClosed()) {
if (websocket != null && websocket.isOpen()) {
try {
websocket.sendTextMessage(om.writeValueAsString(new AgentDisconnectionMessage(System.currentTimeMillis(), uuid)));
} catch (Exception ex) {
log.error("Could not write disconnection message to server.", ex);
}
}
c.close();
}
}
@Override
public void onClose() {
if (!wantsToClose) {
try {
reader.println("Connection to server closed - reconnecting.");
} catch (IOException ie) {
// ignore this
}
connect();
}
}
@Override
public void onError() {
// FIXME - we really need to track the error and see if we should reconnect
try {
reader.println("Error in server communication - reconnecting.");
} catch (IOException ie) {
// ignore this
}
connect();
}
@Override
public void onOpen(WebSocket websocket) {
this.websocket = websocket;
if (uuid == null) {
try {
// We haven't connected before, so let's let the server know
reader.println("Connecting to remote server to let them know this agent has fired up.");
uuid = UUID.randomUUID().toString();
List<String> ipAddresses = new ArrayList<>();
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
final NetworkInterface iface = networkInterfaces.nextElement();
Enumeration<InetAddress> inetAddresses = iface.getInetAddresses();
while (inetAddresses.hasMoreElements()) {
final InetAddress inetAddress = inetAddresses.nextElement();
ipAddresses.add(inetAddress.getHostAddress());
}
}
websocket.sendTextMessage(om.writeValueAsString(new AgentConnectionMessage(ipAddresses, System.currentTimeMillis(), uuid)));
} catch (Exception ex) {
log.error("Could not notify server of our connection, shutting down.");
}
}
}
}