Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

* Swapped out the 1.8.0-SNAPSHOT of the async client for 1.7.5 - all …

…seems good

* The client now reconnects when dropped by the server, and also send JSON messages when connecting/disconnecting - this is done so that the server can track clients that connect/disconnect.
  • Loading branch information...
commit 2acab5cdb7f3ce45e475fbf581d0cb6d4e1cc644 1 parent 6684918
@mlaccetti mlaccetti authored
View
2  vlove-agent/pom.xml
@@ -34,7 +34,7 @@
<dependency>
<groupId>com.ning</groupId>
<artifactId>async-http-client</artifactId>
- <version>1.8.0-SNAPSHOT</version>
+ <version>1.7.5</version>
</dependency>
<!-- libvirt -->
View
11 vlove-agent/src/main/java/vlove/virt/agent/AgentSocketCallback.java
@@ -0,0 +1,11 @@
+package vlove.virt.agent;
+
+import com.ning.http.client.websocket.WebSocket;
+
+public interface AgentSocketCallback {
+ public void onOpen(WebSocket websocket);
+
+ public void onClose();
+
+ public void onError();
+}
View
131 vlove-agent/src/main/java/vlove/virt/agent/AgentWebSocketClient.java
@@ -1,21 +1,37 @@
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.WebSocketTextListener;
import com.ning.http.client.websocket.WebSocketUpgradeHandler;
-public class AgentWebSocketClient {
+public class AgentWebSocketClient implements AgentSocketCallback {
protected static final Logger log = LoggerFactory.getLogger(AgentWebSocketClient.class);
+
private final AsyncHttpClient c = new AsyncHttpClient();
- protected final ConsoleReader reader;
+ 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;
@@ -23,65 +39,80 @@ public AgentWebSocketClient(ConsoleReader reader) {
public void connect() {
try {
- c.prepareGet("ws://localhost:80/vlove/s/agent").execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketTextListener() {
- @Override
- public void onOpen(WebSocket websocket) {
- try {
- reader.println("WebSocket connection established.");
- reader.flush();
- } catch (IOException e) {
- // empty
- }
- }
-
- @Override
- public void onClose(WebSocket websocket) {
- try {
- reader.println("WebSocket connection terminated.");
- reader.flush();
- } catch (IOException e) {
- // empty
- }
- }
-
- @Override
- public void onError(Throwable t) {
- try {
- reader.println("Error occured in WebSocket communication. " + t.getMessage());
- reader.flush();
- } catch (IOException e) {
- // empty
- }
- }
-
- @Override
- public void onMessage(String message) {
- try {
- reader.println("Message from server: " + message);
- reader.flush();
- } catch (IOException e) {
- // empty
- }
- }
-
- @Override
- public void onFragment(String fragment, boolean last) {
- // empty
- }
- }).build()).get();
+ 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) {
- // empty
+ // 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.");
+ }
+ }
+ }
}
View
67 vlove-agent/src/main/java/vlove/virt/agent/AgentWebSocketListener.java
@@ -0,0 +1,67 @@
+package vlove.virt.agent;
+
+import java.io.IOException;
+
+import jline.console.ConsoleReader;
+
+import com.ning.http.client.websocket.WebSocket;
+import com.ning.http.client.websocket.WebSocketTextListener;
+
+public class AgentWebSocketListener implements WebSocketTextListener {
+ private final AgentSocketCallback callback;
+ private final ConsoleReader reader;
+
+ public AgentWebSocketListener(AgentSocketCallback callback, ConsoleReader reader) {
+ this.callback = callback;
+ this.reader = reader;
+ }
+
+ @Override
+ public void onOpen(WebSocket websocket) {
+ try {
+ reader.println("WebSocket connection established.");
+ reader.flush();
+ callback.onOpen(websocket);
+ } catch (IOException e) {
+ // empty
+ }
+ }
+
+ @Override
+ public void onClose(WebSocket websocket) {
+ try {
+ reader.println("WebSocket connection terminated.");
+ reader.flush();
+ callback.onClose();
+ } catch (IOException e) {
+ // empty
+ }
+ }
+
+ @Override
+ public void onError(Throwable t) {
+ try {
+ reader.println("Error occured in WebSocket communication. "
+ + t.getMessage());
+ reader.flush();
+ callback.onError();
+ } catch (IOException e) {
+ // empty
+ }
+ }
+
+ @Override
+ public void onMessage(String message) {
+ try {
+ reader.println("Message from server: " + message);
+ reader.flush();
+ } catch (IOException e) {
+ // empty
+ }
+ }
+
+ @Override
+ public void onFragment(String fragment, boolean last) {
+ // empty
+ }
+}
View
27 vlove-common/src/main/java/vlove/model/json/AgentBaseMessage.java
@@ -0,0 +1,27 @@
+package vlove.model.json;
+
+import java.io.Serializable;
+
+public abstract class AgentBaseMessage implements Serializable {
+ private final Long timestamp;
+ private final String clientId;
+ private final String messageType;
+
+ public AgentBaseMessage(Long timestamp, String clientId) {
+ this.timestamp = timestamp;
+ this.clientId = clientId;
+ messageType = this.getClass().getName();
+ }
+
+ public Long getTimestamp() {
+ return timestamp;
+ }
+
+ public String getClientId() {
+ return clientId;
+ }
+
+ public String getMessageType() {
+ return messageType;
+ }
+}
View
24 vlove-common/src/main/java/vlove/model/json/AgentConnectionMessage.java
@@ -0,0 +1,24 @@
+package vlove.model.json;
+
+import java.util.List;
+
+import org.codehaus.jackson.map.annotate.JsonRootName;
+
+@JsonRootName("connect")
+public class AgentConnectionMessage extends AgentBaseMessage {
+ private List<String> ipAddresses;
+
+ public AgentConnectionMessage(List<String> ipAddresses, Long timestamp,
+ String clientId) {
+ super(timestamp, clientId);
+ this.ipAddresses = ipAddresses;
+ }
+
+ public List<String> getIpAddresses() {
+ return ipAddresses;
+ }
+
+ public void setIpAddresses(List<String> ipAddresses) {
+ this.ipAddresses = ipAddresses;
+ }
+}
View
10 vlove-common/src/main/java/vlove/model/json/AgentDisconnectionMessage.java
@@ -0,0 +1,10 @@
+package vlove.model.json;
+
+import org.codehaus.jackson.map.annotate.JsonRootName;
+
+@JsonRootName("disconnect")
+public class AgentDisconnectionMessage extends AgentBaseMessage {
+ public AgentDisconnectionMessage(Long timestamp, String clientId) {
+ super(timestamp, clientId);
+ }
+}
View
7 vlove-common/src/main/java/vlove/model/json/ConnectionStatusType.java
@@ -0,0 +1,7 @@
+package vlove.model.json;
+
+public enum ConnectionStatusType {
+ CONNECT,
+ DISCONNECT,
+ RECONNECT
+}
View
1  vlove-server/src/main/java/vlove/dao/impl/GenericDaoImpl.java
@@ -34,7 +34,6 @@
@Repository
@Transactional
-@SuppressWarnings("unchecked")
public class GenericDaoImpl implements GenericDao {
@PersistenceContext
private EntityManager em;
View
6 vlove-server/src/main/java/vlove/web/websocket/VloveWebSocketHandler.java
@@ -38,12 +38,14 @@ public void onMessage(Message<String> message) {
@Override
public void onTextMessage(WebSocket webSocket, String message) {
- AtmosphereResource r = webSocket.resource();
+ log.debug("Received a message from an agent: {}", message);
+
+ /*AtmosphereResource r = webSocket.resource();
Broadcaster b = lookupBroadcaster(r.getRequest().getPathInfo());
if (message != null && message.indexOf("message") != -1) {
b.broadcast(message.substring("message=".length()));
- }
+ }*/
}
@Override
View
10 vlove-server/src/main/resources/rebel.xml
@@ -2,15 +2,19 @@
<application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.zeroturnaround.com" xsi:schemaLocation="http://www.zeroturnaround.com http://www.zeroturnaround.com/alderaan/rebel-2_0.xsd">
<classpath>
- <dir name="C:/Users/Michael/workspace/vlove/vlove-server/target/classes">
+ <dir name="/home/michael/workspace/vlove/vlove-server/target/classes">
</dir>
- <dir name="C:/Users/Michael/workspace/vlove/vlove-server/target/test-classes">
+ <dir name="/home/michael/workspace/vlove/vlove-server/target/test-classes">
</dir>
</classpath>
<web>
<link target="/">
- <dir name="C:/Users/Michael/workspace/vlove/vlove-server/src/main/webapp">
+ <dir name="/home/michael/workspace/vlove/vlove-server/target/m2e-wtp/web-resources">
+ </dir>
+ </link>
+ <link target="/">
+ <dir name="/home/michael/workspace/vlove/vlove-server/src/main/webapp">
</dir>
</link>
</web>
View
2  vlove-server/src/main/resources/vlove.properties
@@ -1,4 +1,4 @@
# DS
-datasource.jdbcUrl=jdbc:h2:/User/michael/tmp/vlove
+datasource.jdbcUrl=jdbc:h2:/home/michael/workspace/vlove/db
datasource.user=sa
datasource.password=
Please sign in to comment.
Something went wrong with that request. Please try again.