package com.google.bitcoin.core;

import static org.jboss.netty.channel.Channels.fireChannelConnected;

import org.jboss.netty.channel.*;

public class FakeChannelSink extends AbstractChannelSink {
    
    public void eventSunk(ChannelPipeline pipeline, ChannelEvent e)
            throws Exception {
        if (e instanceof ChannelStateEvent) {
            ChannelStateEvent event = (ChannelStateEvent) e;

            FakeChannel channel =
                  (FakeChannel) event.getChannel();
            ChannelFuture future = event.getFuture();
            ChannelState state = event.getState();
            Object value = event.getValue();
            switch (state) {
            case OPEN:
                if (Boolean.FALSE.equals(value)) {
                    // Close
                }
                break;
            case BOUND:
                if (value != null) {
                    // Bind
                } else {
                    // Close
                }
                break;
            case CONNECTED:
                if (value != null) {
                    future.setSuccess();
                    fireChannelConnected(channel, channel.getRemoteAddress());
                } else {
                    // Close
                }
                break;
            case INTEREST_OPS:
                // Unsupported - discard silently.
                future.setSuccess();
                break;
            }
            boolean offered = channel.events.offer(event);
            assert offered;
        } else if (e instanceof MessageEvent) {
            MessageEvent event = (MessageEvent) e;
            FakeChannel channel = (FakeChannel) event.getChannel();
            boolean offered = channel.events.offer(event);
            assert offered;
        }
    }
}

@@ -27,16 +27,51 @@
 .core;%0A%0A
+import org.jboss.netty.channel.*;%0A%0A
 import s
@@ -136,43 +136,8 @@
 d;%0A%0A
-import org.jboss.netty.channel.*;%0A%0A
 publ
@@ -263,28 +263,16 @@
 Event e)
-%0A           
  throws 
@@ -424,34 +424,16 @@
 hannel =
-%0A                 
  (FakeCh
@@ -451,32 +451,120 @@
 t.getChannel();%0A
+            boolean offered = channel.events.offer(event);%0A            assert offered;%0A%0A
             Chan
@@ -1494,95 +1494,8 @@
   %7D%0A
-            boolean offered = channel.events.offer(event);%0A            assert offered;%0A
     

Fix FakeChannelSink so events are put into the queue in the right order (BEFORE running event handlers, not after).