Author: bdonlan
Date: 2005-05-23 21:22:26 -0400 (Mon, 23 May 2005)
New Revision: 722

Modified:
   trunk/clients/Javer2/src/org/haverdev/haver/server/UserConnection.java
Log:
Implement BYE; stop using IOException emulation

Modified: trunk/clients/Javer2/src/org/haverdev/haver/server/UserConnection.java
===================================================================
--- trunk/clients/Javer2/src/org/haverdev/haver/server/UserConnection.java      
2005-05-24 01:09:11 UTC (rev 721)
+++ trunk/clients/Javer2/src/org/haverdev/haver/server/UserConnection.java      
2005-05-24 01:22:26 UTC (rev 722)
@@ -149,9 +149,7 @@
                     String[] ping = {"PING", lastPing};
                     sendLine(ping);
                 } else {
-                    // XXX: mention why
-                    // XXX: fix reason in QUIT
-                    ioExcept(new IOException("Ping timeout"));
+                    cutConnection("ping", "Ping timeout: 30 secs");
                 }
             }
         };
@@ -183,16 +181,24 @@
         }
     }
     
-    protected synchronized void ioExcept(IOException e) {
-        e.printStackTrace();
+    protected synchronized void cutConnection(String reason, String detail) {
         try { writer.close(); } catch (Throwable t) { }
+        try {
+            Thread.sleep(1000); // Hopefully flush the writer
+                                // XXX: blocking close/flush
+        } catch (InterruptedException e) {}
         try { sock.close(); } catch (Throwable t) { }
         writer = null;
+        pingTimer.cancel();
         if (this.e != null)
-            this.e.quit("disconnected", e.getMessage());
-        pingTimer.cancel();
+            this.e.quit(reason, detail);
     }
     
+    protected synchronized void ioExcept(IOException e) {
+        e.printStackTrace();
+        cutConnection("discon", e.getMessage());
+    }
+    
     public synchronized void sendLine(String[] args) {
         if (writer == null) return;
         String line = encodeLine(args);
@@ -305,8 +311,6 @@
         }
     }
     
-
-    
     public interface UserCommandContext {
         public void processCommand(String[] cmd) throws PropagatedException;
     }
@@ -425,5 +429,12 @@
             lastPong = args[1];
         }
         
+        public void handle_BYE(String[] args) {
+            String detail = args[1];
+            String[] sayonara = { "BYE", "active", detail };
+            sendLine(sayonara);
+            cutConnection("active", detail);
+        }
+        
     }
 }


Reply via email to