Author: bdonlan
Date: 2005-01-13 15:06:15 -0500 (Thu, 13 Jan 2005)
New Revision: 595

Modified:
   trunk/misc/javer/
   trunk/misc/javer/src/javer/HaverClient.java
Log:
 [EMAIL PROTECTED]:  bdonlan | 2005-01-13 15:03:36 -0500
 Add lagcheck implementation to javer library



Property changes on: trunk/misc/javer
___________________________________________________________________
Name: svk:merge
   - 1f59643a-e6e5-0310-bc24-f7d4c744f460:/haver/local/trunk/misc/javer:16413
   + 1f59643a-e6e5-0310-bc24-f7d4c744f460:/haver/local/trunk/misc/javer:16414

Modified: trunk/misc/javer/src/javer/HaverClient.java
===================================================================
--- trunk/misc/javer/src/javer/HaverClient.java 2005-01-13 20:06:02 UTC (rev 
594)
+++ trunk/misc/javer/src/javer/HaverClient.java 2005-01-13 20:06:15 UTC (rev 
595)
@@ -23,6 +23,7 @@
     protected java.util.Map fails = new java.util.HashMap();
     protected String uid = null;
     protected Integer lock = new Integer(0);
+    Pinger pingDaemon = null;
     
     protected void send(String s) throws java.io.IOException {
         synchronized (lock) {
@@ -73,6 +74,7 @@
             ));
         send("HAVER\t" + versionString);
         start();
+        pingDaemon = new Pinger(0, 0);
     }
     
     public String getUID() {
@@ -96,6 +98,8 @@
                 synchronized (lock) {
                     if (reader == null)
                         return;
+                    if (pingDaemon != null)
+                        pingDaemon.serverResponse();
                     dispatchCommand(s);
                 }
             } catch (java.io.IOException e) {
@@ -230,6 +234,13 @@
                 send(args);
             }
         });
+        serverevents.put("OUCH", new ServerHandler() {
+            public void trigger(String[] args) throws java.io.IOException {
+                long pingtime = Integer.decode(args[1]).intValue();
+                long lag = System.currentTimeMillis() - pingtime;
+                eventLagMeasured(lag);
+            }
+        });
     }
 
     protected void dispatchCommand(String s) throws java.io.IOException {
@@ -426,4 +437,208 @@
     protected boolean eventIOException(IOException e) {
         return false;
     }
+
+    protected class Pinger extends Thread {
+
+        /**
+         * Holds value of property pingInterval.
+         */
+        private int pingInterval;
+
+        /**
+         * Holds value of property deadInterval.
+         */
+        private int deadInterval;
+
+        /**
+         * Holds value of property lastPing.
+         */
+        private long lastPing;
+        
+        private boolean halting = false;
+        
+        private long lastPong;
+        
+        public void run() {
+            while (true) {
+                synchronized(this) {
+                    this.interrupted(); /* we don't care if we're interrupted
+                                         * but we need the flag cleared
+                                         */
+                    if (halting)
+                        return;
+                    long nextPing = Long.MAX_VALUE;
+                    long nextDead = Long.MAX_VALUE;
+                    long nextEvt;
+                    long now = System.currentTimeMillis();
+                    long delta;
+                    if (pingInterval > 0)
+                        nextPing = lastPing + pingInterval;
+                    else {
+                        halting = true;
+                        return;
+                    }                        
+                    if (deadInterval > 0)
+                        nextDead = lastPong + deadInterval;
+                    if (nextPing < nextDead)
+                        nextEvt = nextPing;
+                    else
+                        nextEvt = nextDead;
+                    if (now < nextPing) {
+                        try {
+                            send("POKE\t" + now);
+                        } catch (IOException e) {
+                            eventIOException(e);
+                        }
+                        nextPing = now + pingInterval;
+                        continue;
+                    }
+                    if (now < nextDead) {
+                        try {
+                            eventServerDead();
+                        } catch (IOException e) {
+                            eventIOException(e);
+                        }
+                        lastPong = Long.MAX_VALUE;
+                        continue;
+                    }
+                    delta = now - nextEvt;
+                    try {
+                        this.sleep(delta);
+                    } catch(InterruptedException e) {}
+                }
+            }
+        }
+
+        /**
+         * Getter for property pingInterval.
+         * @return Value of property pingInterval.
+         */
+        public int getPingInterval() {
+
+            return this.pingInterval;
+        }
+
+        /**
+         * Setter for property pingInterval.
+         * @param pingInterval New value of property pingInterval.
+         */
+        public void setPingInterval(int pingInterval) {
+            if (pingInterval < 0)
+                throw new IllegalArgumentException("pingInterval must not be 
negative");
+            if (deadInterval < pingInterval)
+                throw new IllegalArgumentException("pingInterval should be 
greater than deadInterval");
+            synchronized (this) {
+                this.pingInterval = pingInterval;
+                if (pingInterval != 0) {
+                    this.start();
+                    this.interrupt();
+                } else {
+                    this.halt();
+                }
+            }
+        }
+
+        /**
+         * Getter for property deadInterval.
+         * @return Value of property deadInterval.
+         */
+        public int getDeadInterval() {
+
+            return this.deadInterval;
+        }
+
+        /**
+         * Setter for property deadInterval.
+         * @param deadInterval New value of property deadInterval.
+         */
+        public void setDeadInterval(int deadInterval) {
+            if (deadInterval < 0)
+                throw new IllegalArgumentException("deadInterval must not be 
negative");
+            if (deadInterval < pingInterval)
+                throw new IllegalArgumentException("pingInterval should be 
greater than deadInterval");
+            synchronized (this) {
+                this.deadInterval = deadInterval;
+                if (pingInterval != 0) {
+                    this.start();
+                    this.interrupt();
+                } else {
+                    this.halt();
+                }
+            }
+        }
+
+        /**
+         * Getter for property lastPing.
+         * @return Value of property lastPing.
+         */
+        public long getLastPing() {
+
+            return this.lastPing;
+        }
+
+        public Pinger(int pingInterval, int deadInterval) {
+            lastPing = System.currentTimeMillis();
+            setPingInterval(pingInterval);
+            setDeadInterval(deadInterval);
+        }
+
+        public void halt() {
+            synchronized (this) {
+                halting = true;
+                if (this == Thread.currentThread())
+                    return;
+                if (!this.isAlive())
+                    return;
+                this.interrupt();
+                try {
+                    this.join();
+                } catch (InterruptedException e) {
+                    /* XXX */
+                }
+            }
+        }
+       
+        public void serverResponse() {
+            synchronized (this) {
+                lastPong = System.currentTimeMillis();
+            }
+        }
+
+        public void start() {
+            if (this == Thread.currentThread())
+                return;
+            synchronized (this) {
+                if (this.isAlive()) {
+                    if (halting) {
+                        try {
+                            this.join();
+                        } catch (InterruptedException e) {
+                            /* XXX */
+                        }
+                    }
+                    else return;
+                }
+                super.start();
+            }
+        }
+    }
+
+    protected void eventServerDead() throws IOException {
+        /* STUB */
+    }
+    
+    protected void eventLagMeasured(long lag) throws IOException {
+        /* STUB */
+    }
+    
+    public void setPingTimers(int pingInterval, int deadInterval) {
+        pingDaemon.setDeadInterval(0); /* XXX: prevent exception when new
+                                        * deadInterval > old pingInterval
+                                        * needs to be cleaner
+                                        */
+        pingDaemon.setPingInterval(pingInterval);
+        pingDaemon.setDeadInterval(deadInterval);
+    }
+
 }


Reply via email to