package com.continuuity.gateway.router.handlers;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Handles requests to and from a discoverable endpoint.
 */
public class OutboundHandler extends SimpleChannelUpstreamHandler {
  private static final Logger LOG = LoggerFactory.getLogger(OutboundHandler.class);

  private final Channel inboundChannel;

  public OutboundHandler(Channel inboundChannel) {
    this.inboundChannel = inboundChannel;
  }

  @Override
  public void messageReceived(ChannelHandlerContext ctx, final MessageEvent e) throws Exception {
    ChannelBuffer msg = (ChannelBuffer) e.getMessage();
    inboundChannel.write(msg);
  }

  @Override
  public void channelInterestChanged(ChannelHandlerContext ctx, final ChannelStateEvent e) throws Exception {
    inboundChannel.getPipeline().execute(new Runnable() {
      @Override
      public void run() {
        // If outboundChannel is not saturated anymore, continue accepting
        // the incoming traffic from the inboundChannel.
        if (e.getChannel().isWritable()) {
          LOG.trace("Setting inboundChannel readable.");
          inboundChannel.setReadable(true);
        } else {
          // If outboundChannel is saturated, do not read inboundChannel
          LOG.trace("Setting inboundChannel non-readable.");
          inboundChannel.setReadable(false);
        }
      }
    });
  }

  @Override
  public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
    HttpRequestHandler.closeOnFlush(inboundChannel);
  }

  @Override
  public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
    LOG.error("Got exception", e);
    HttpRequestHandler.closeOnFlush(e.getChannel());
  }
}

@@ -994,32 +994,78 @@
 rite(msg);%0A  %7D%0A%0A
+  //TODO: Implement channel interest change%0A//
   @Override%0A  pu
@@ -1052,32 +1052,34 @@
 e%0A//  @Override%0A
+//
   public void ch
@@ -1168,24 +1168,26 @@
 Exception %7B%0A
+//
     inboundC
@@ -1228,24 +1228,26 @@
 unnable() %7B%0A
+//
       @Overr
@@ -1250,16 +1250,18 @@
 verride%0A
+//
       pu
@@ -1274,24 +1274,26 @@
 oid run() %7B%0A
+//
         // I
@@ -1355,16 +1355,18 @@
 cepting%0A
+//
         
@@ -1414,16 +1414,18 @@
 hannel.%0A
+//
         
@@ -1455,24 +1455,26 @@
 itable()) %7B%0A
+//
           LO
@@ -1510,32 +1510,34 @@
 el readable.%22);%0A
+//
           inboun
@@ -1560,24 +1560,26 @@
 able(true);%0A
+//
         %7D el
@@ -1583,16 +1583,18 @@
  else %7B%0A
+//
         
@@ -1658,16 +1658,18 @@
 Channel%0A
+//
         
@@ -1721,16 +1721,18 @@
 ble.%22);%0A
+//
         
@@ -1764,24 +1764,26 @@
 ble(false);%0A
+//
         %7D%0A  
@@ -1780,24 +1780,26 @@
       %7D%0A
+//
       %7D%0A
     %7D);%0A
@@ -1794,16 +1794,18 @@
   %7D%0A
+//
     %7D);%0A
   %7D%0A
@@ -1800,16 +1800,18 @@
     %7D);%0A
+//
   %7D%0A%0A  @

Disable interest change in OutboundHandler