package com.aphyr.riemann.client;

import java.net.InetSocketAddress;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.CopyOnWriteArrayList;
import java.net.*;
import java.io.*;
import org.jboss.netty.util.*;
import org.jboss.netty.channel.*;
import org.jboss.netty.channel.group.*;
import org.jboss.netty.handler.codec.oneone.*;
import org.jboss.netty.handler.codec.protobuf.*;
import org.jboss.netty.handler.codec.frame.*;
import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.handler.execution.ExecutionHandler;
import org.jboss.netty.handler.timeout.*;
import org.jboss.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor;
import com.aphyr.riemann.Proto.Msg;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.LinkedBlockingQueue;

public class ReconnectHandler extends SimpleChannelUpstreamHandler {
  final ClientBootstrap bootstrap;
  public final Timer timer;
  public long startTime = -1;
  public final long delay;
  public final TimeUnit unit;

  public ReconnectHandler(ClientBootstrap bootstrap, Timer timer, long delay, TimeUnit unit) {
    this.bootstrap = bootstrap;
    this.timer = timer;
    this.delay = delay;
    this.unit = unit;
  }

  InetSocketAddress getRemoteAddress() {
    return (InetSocketAddress) bootstrap.getOption("remoteAddress");
  }

  @Override
  public void channelDisconnected(ChannelHandlerContext c, ChannelStateEvent e) {
    System.out.println("disconnected.");
    // Go ahead and close. I don't know why Netty doesn't close disconnected
    // TCP sockets, but it seems not to.
    e.getChannel().close();
  }

  @Override
  public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) {
    System.out.println("Channel closed.");
    try {
      timer.newTimeout(new TimerTask() {
        public void run(Timeout timeout) throws Exception {
          System.out.println("Connect");
          bootstrap.connect();
        }
      }, delay, unit);
    } catch (java.lang.IllegalStateException ex) {
      // The timer must have been stopped.
    }
  }

  @Override
  public void channelConnected(ChannelHandlerContext c, ChannelStateEvent e) {
    if (startTime < 0) {
      System.out.println("Connected");
      startTime = System.currentTimeMillis();
    }
  }

  @Override
  public void exceptionCaught(ChannelHandlerContext c, ExceptionEvent e) {
    Throwable cause = e.getCause();
    if (cause instanceof ConnectException) {
      startTime = -1;
    }
    else if (cause instanceof ReadTimeoutException) {
      // The connection was OK but there was no traffic for the last period.
    } else {
      cause.printStackTrace();
    }
    c.getChannel().close();
  }
}

@@ -883,16 +883,44 @@
 ngQueue;
+%0Aimport java.nio.channels.*;
 %0A%0Apublic
@@ -1548,49 +1548,8 @@
 ) %7B%0A
-    System.out.println(%22disconnected.%22);%0A
     
@@ -1789,51 +1789,8 @@
 ) %7B%0A
-    System.out.println(%22Channel closed.%22);%0A
     
@@ -1900,49 +1900,8 @@
 n %7B%0A
-          System.out.println(%22Connect%22);%0A
     
@@ -2185,47 +2185,8 @@
 ) %7B%0A
-      System.out.println(%22Connected%22);%0A
     
@@ -2484,24 +2484,24 @@
 xception) %7B%0A
+
       // The
@@ -2569,52 +2569,8 @@
 od.%0A
-    %7D else %7B%0A      cause.printStackTrace();%0A
     

Remove some debugging printlns