Author: bdonlan
Date: 2005-05-23 20:36:56 -0400 (Mon, 23 May 2005)
New Revision: 720

Modified:
   trunk/clients/Javer2/src/org/haverdev/haver/server/UserConnection.java
Log:
Some hacky PING/PONG POKE/OUCH stuff. Need to improve PING/PONG later.


Modified: trunk/clients/Javer2/src/org/haverdev/haver/server/UserConnection.java
===================================================================
--- trunk/clients/Javer2/src/org/haverdev/haver/server/UserConnection.java      
2005-05-24 00:18:01 UTC (rev 719)
+++ trunk/clients/Javer2/src/org/haverdev/haver/server/UserConnection.java      
2005-05-24 00:36:56 UTC (rev 720)
@@ -18,6 +18,10 @@
  */
 public class UserConnection {
     
+    static Timer timer = new Timer(true);
+    
+    TimerTask pingTimer = null;
+    
     Socket sock;
     NonblockingOutputStream nws;
     PrintWriter writer;
@@ -26,6 +30,9 @@
     UserCommandContext context = new InitContext();
     UserEntity e = null;
     
+    String lastPing = "";
+    String lastPong = "";
+    
     /**
      * Reverses Haver escaping.
      * @param val String to unescape
@@ -130,6 +137,25 @@
         });
         readThread.setName("New incoming connection...");
         readThread.start();
+        
+        pingTimer = new TimerTask() {
+            public void run() {
+                if (!(context instanceof NormalContext)){
+                    // XXX
+                    ioExcept(new IOException("Took too long to handshake"));
+                }
+                if (lastPing.equals(lastPong)) {
+                    lastPing = "" + System.currentTimeMillis();
+                    String[] ping = {"PING", lastPing};
+                    sendLine(ping);
+                } else {
+                    // XXX: mention why
+                    // XXX: fix reason in QUIT
+                    ioExcept(new IOException("Ping timeout"));
+                }
+            }
+        };
+        timer.schedule(pingTimer, 120 * 1000, 30 * 1000);
     }
     
     public static final String[] greeting = {"HAVER", 
"org.haverdev.haver.server/0.1"};
@@ -389,5 +415,14 @@
             chan_e.distributePublicMessage(e, margs);
         }
         
+        public void handle_POKE(String[] args) {
+            args[0] = "OUCH";
+            sendLine(args);
+        }
+        
+        public void handle_PONG(String[] args) {
+            lastPong = args[1];
+        }
+        
     }
 }


Reply via email to