Java tutorial
/* * Copyright 2012 The Netty Project * * The Netty Project licenses this file to you under the Apache License, * version 2.0 (the "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. */ package com.siondream.superjumper.net; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.channel.group.ChannelGroup; import io.netty.channel.group.DefaultChannelGroup; import io.netty.handler.timeout.IdleState; import io.netty.handler.timeout.IdleStateEvent; import io.netty.util.concurrent.GlobalEventExecutor; import java.net.UnknownHostException; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; /** * Handles a server-side channel. */ public class SecureChatServerHandler extends SimpleChannelInboundHandler<Object> { public static final ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE); @Override public void channelActive(final ChannelHandlerContext ctx) throws UnknownHostException { System.out.println(""); channels.add(ctx.channel()); } ScheduledFuture<?> schedule(ChannelHandlerContext ctx, Runnable task, long delay, TimeUnit unit) { return ctx.executor().schedule(task, delay, unit); } @Override public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { //System.out.println("ctx = [" + ctx + "], msg = [" + msg + "]"); ServerNetOptLoop.addClientOpt((ClientNetOptLoop.NetOpt) msg); } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { super.channelInactive(ctx); System.out.println("channelInactive = [" + ctx + "]"); } @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (IdleStateEvent.class.isAssignableFrom(evt.getClass())) { IdleStateEvent event = (IdleStateEvent) evt; if (event.state() == IdleState.READER_IDLE) System.out.println("read idle"); else if (event.state() == IdleState.WRITER_IDLE) System.out.println("write idle"); else if (event.state() == IdleState.ALL_IDLE) System.out.println("all idle"); } else if (Integer.class.isAssignableFrom(evt.getClass())) { Integer event = (Integer) evt; } } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { cause.printStackTrace(); ctx.close(); } }