package gvs.access;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import gvs.Configuration;

/**
 * 
 * @author muriele
 *
 */
@Singleton
public class Watchdog implements Runnable{
  private int stopwatch;
  private boolean isWatching;

  private final ConnectionMonitor monitor;

  private static final int WATCH_TIME = 1000;
  private static final int INTERVAL = 100;
  private static final Logger logger = LoggerFactory.getLogger(Watchdog.class);

  @Inject
  public Watchdog(ConnectionMonitor monitor) {
    this.monitor = monitor;
  }

  @Override
  public void run() {
    stopwatch = WATCH_TIME;
    isWatching = true;
    while (isWatching && stopwatch > 0) {
      try {
        stopwatch -= INTERVAL;
        Thread.sleep(INTERVAL);
        logger.info("Watchdog is watching");
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
    if (isWatching) {
      logger.info("Watchdog forcefully releases service");
      monitor.releaseService(Configuration.getWatchdog());
    }
    logger.info("Watchdog goes to sleep");
  }

  public void feed() {
    logger.info("Watchdog is beeing fed.");
    stopwatch = WATCH_TIME;
  }

  public void stopWatching() {
    isWatching = false;
  }
  
  public boolean isWatching() {
    return isWatching;
  }
}

@@ -180,27 +180,485 @@
 %0A * 
-%0A * @author muriele
+The Watchdog is responsible for severing stale client connections. Whenever a%0A * client connection is established, the watchdog is started in a new thread.%0A * During the connection the watchdog is being ''fed'' by the creater thread. If%0A * the connection goes stale, i.e. if no new data is received for a certain time%0A * interval, the watchdog goes hungry and forcefully severs the client%0A * connection, so the service is accessible for another client.%0A * %0A * @author mtrentini
 %0A *%0A
@@ -713,16 +713,17 @@
 Runnable
+ 
 %7B%0A  priv
@@ -1076,16 +1076,104 @@
 r;%0A  %7D%0A%0A
+  /**%0A   * Forcefully sever client connections, if the watchdog is not being fed.%0A   */%0A
   @Overr
@@ -1685,24 +1685,72 @@
 eep%22);%0A  %7D%0A%0A
+  /**%0A   * Set the watchdog's timer back.%0A   */%0A
   public voi
@@ -1837,16 +1837,95 @@
 E;%0A  %7D%0A%0A
+  /**%0A   * Informs the watchdog, that its service is no longer required.%0A   */%0A
   public
@@ -1975,18 +1975,78 @@
 se;%0A  %7D%0A
+%0A
   
+/**%0A   * %0A   * @return wheter the watchdog is active.%0A   */
 %0A  publi

add javadoc to watchdog class