Java Code Examples for org.jboss.netty.channel.ChannelStateEvent.getState()

The following are Jave code examples for showing how to use getState() of the org.jboss.netty.channel.ChannelStateEvent class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: bigstreams   File: IpFilterHandler.java   Source Code and License Vote up 4 votes
@Override
public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e)
		throws Exception {
	if (e instanceof ChannelStateEvent) {
		ChannelStateEvent evt = (ChannelStateEvent) e;
		switch (evt.getState()) {
		case OPEN:
		case BOUND:
			// Special case: OPEND and BOUND events are before CONNECTED,
			// but CLOSED and UNBOUND events are after DISCONNECTED: should
			// those events be blocked too?
			if (isBlocked(ctx) && !continues(ctx, evt)) {
				// don't pass to next level since channel was blocked early
				return;
			} else {
				ctx.sendUpstream(e);
				return;
			}
		case CONNECTED:
			if (evt.getValue() != null) {
				// CONNECTED
				InetSocketAddress inetSocketAddress = (InetSocketAddress) e
						.getChannel().getRemoteAddress();
				if (!accept(ctx, e, inetSocketAddress)) {
					ctx.setAttachment(Boolean.TRUE);
					
					final ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
					
					buffer.writeBytes("/".getBytes());
					buffer.writeBytes("ok".getBytes());
					
					ChannelFuture future = e.getChannel().write(buffer);
					future.addListener(ChannelFutureListener.CLOSE);
					LOG.info("Ignoring " + inetSocketAddress.getAddress().getHostAddress());
					
					if (isBlocked(ctx) && !continues(ctx, evt)) {
						// don't pass to next level since channel was
						// blocked early
						return;
					}
				}
				// This channel is not blocked
				ctx.setAttachment(null);
			} else {
				// DISCONNECTED
				if (isBlocked(ctx) && !continues(ctx, evt)) {
					// don't pass to next level since channel was blocked
					// early
					return;
				}
			}
			break;
		}
	}
	if (isBlocked(ctx) && !continues(ctx, e)) {
		// don't pass to next level since channel was blocked early
		return;
	}
	// Whatever it is, if not blocked, goes to the next level
	ctx.sendUpstream(e);
}
 
Example 2
Project: fastcatsearch3   File: CheckPointHandler.java   Source Code and License Vote up 4 votes
@Override
public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent e)
           throws Exception {

	
	logger.debug("handleDownstream e={}", e);
	
	
       if (e instanceof MessageEvent) {
       	logger.debug("MessageEvent >> writeRequested");
           writeRequested(ctx, (MessageEvent) e);
       } else if (e instanceof ChannelStateEvent) {
           ChannelStateEvent evt = (ChannelStateEvent) e;
           switch (evt.getState()) {
           case OPEN:
               if (!Boolean.TRUE.equals(evt.getValue())) {
               	logger.debug("ChannelStateEvent >> closeRequested");
                   closeRequested(ctx, evt);
               }
               break;
           case BOUND:
               if (evt.getValue() != null) {
               	logger.debug("ChannelStateEvent >> bindRequested");
                   bindRequested(ctx, evt);
               } else {
               	logger.debug("ChannelStateEvent >> unbindRequested");
                   unbindRequested(ctx, evt);
               }
               break;
           case CONNECTED:
               if (evt.getValue() != null) {
               	logger.debug("ChannelStateEvent >> connectRequested");
                   connectRequested(ctx, evt);
               } else {
               	logger.debug("ChannelStateEvent >> disconnectRequested");
                   disconnectRequested(ctx, evt);
               }
               break;
           case INTEREST_OPS:
           	logger.debug("ChannelStateEvent >> setInterestOpsRequested");
               setInterestOpsRequested(ctx, evt);
               break;
           default:
           	logger.debug("ChannelStateEvent >> sendDownstream");
               ctx.sendDownstream(e);
           }
       } else {
       	logger.debug("------ >> sendDownstream");
           ctx.sendDownstream(e);
       }
   }