List of usage examples for io.netty.handler.timeout ReadTimeoutHandler ReadTimeoutHandler
public ReadTimeoutHandler(int timeoutSeconds)
From source file:nearenough.examples.NettyClient.java
License:Open Source License
public static void main(String[] args) throws InterruptedException { InetSocketAddress addr = new InetSocketAddress(INT08H_SERVER_HOST, INT08H_SERVER_PORT); System.out.printf("Sending request to %s\n", addr); // Below is Netty boilerplate for setting-up an event loop and registering a handler NioEventLoopGroup nioEventLoopGroup = new NioEventLoopGroup(); Bootstrap bootstrap = new Bootstrap().group(nioEventLoopGroup).remoteAddress(addr) .channel(NioDatagramChannel.class).handler(new ChannelInitializer<NioDatagramChannel>() { @Override//from ww w . j av a2 s. c o m protected void initChannel(NioDatagramChannel ch) { ch.pipeline().addLast(new ReadTimeoutHandler(5)).addLast(new RequestHandler(addr)); } }); ChannelFuture connectFuture = bootstrap.connect(); connectFuture.addListener(future -> { if (!future.isSuccess()) { System.out.println("Connect fail:"); System.out.println(future.cause().getMessage()); } }); connectFuture.channel().closeFuture().sync(); nioEventLoopGroup.shutdownGracefully(); }
From source file:net.floodlightcontroller.core.internal.OFChannelInitializer.java
License:Apache License
@Override protected void initChannel(Channel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); OFChannelHandler handler = new OFChannelHandler(switchManager, connectionListener, pipeline, debugCounters, timer, ofBitmaps, defaultFactory); if (keyStore != null && keyStorePassword != null) { try {/* w w w . j a v a2 s . c o m*/ /* Set up factories and stores. */ TrustManagerFactory tmFactory = TrustManagerFactory .getInstance(TrustManagerFactory.getDefaultAlgorithm()); KeyStore tmpKS = null; tmFactory.init(tmpKS); /* Use keystore/pass defined in properties file. */ KeyStore ks = KeyStore.getInstance("JKS"); ks.load(new FileInputStream(keyStore), keyStorePassword.toCharArray()); KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); kmf.init(ks, keyStorePassword.toCharArray()); KeyManager[] km = kmf.getKeyManagers(); TrustManager[] tm = tmFactory.getTrustManagers(); /* Set up SSL prereqs for Netty. */ SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(km, tm, null); SSLEngine sslEngine = sslContext.createSSLEngine(); /* We are the server and we will create secure sessions. */ sslEngine.setUseClientMode(false); sslEngine.setEnableSessionCreation(true); /* These are redundant (default), but for clarity... */ sslEngine.setEnabledProtocols(sslEngine.getSupportedProtocols()); sslEngine.setEnabledCipherSuites(sslEngine.getSupportedCipherSuites()); /* First, decrypt w/handler+engine; then, proceed with rest of handlers. */ pipeline.addLast(PipelineHandler.SSL_TLS_ENCODER_DECODER, new SslHandler(sslEngine)); log.info("SSL OpenFlow socket initialized and handler ready for switch."); } catch (Exception e) { /* There are lots of possible exceptions to catch, so this should get them all. */ log.error("Exception initializing SSL OpenFlow socket: {}", e.getMessage()); throw e; /* If we wanted secure but didn't get it, we should bail. */ } } pipeline.addLast(PipelineHandler.OF_MESSAGE_DECODER, new OFMessageDecoder()); pipeline.addLast(PipelineHandler.OF_MESSAGE_ENCODER, new OFMessageEncoder()); pipeline.addLast(PipelineHandler.MAIN_IDLE, new IdleStateHandler(PipelineIdleReadTimeout.MAIN, PipelineIdleWriteTimeout.MAIN, 0)); pipeline.addLast(PipelineHandler.READ_TIMEOUT, new ReadTimeoutHandler(30)); pipeline.addLast(PipelineHandler.CHANNEL_HANDSHAKE_TIMEOUT, new HandshakeTimeoutHandler(handler, timer, PipelineHandshakeTimeout.CHANNEL)); pipeline.addLast(PipelineHandler.CHANNEL_HANDLER, handler); }
From source file:net.ieldor.network.ChannelChildHandler.java
License:Open Source License
@Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new ReadTimeoutHandler(5), new ByteBufEncoder(), new HandshakeDecoder(), new ServerChannelAdapterHandler(mainContext)); }
From source file:net.petercashel.nettyCore.client.clientCore.java
License:Apache License
/** * Initializes a Client Connection/* www. j a v a 2 s . c om*/ * * @param addr * - String address to connect to * @param port * - int Port number to connect to * @throws Exception */ public static void initializeConnection(final String addr, final int port) throws Exception { _host = addr; _port = port; PacketRegistry.setupRegistry(); PacketRegistry.Side = side; if (UseSSL) SSLContextProvider.SetupSSL(); group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast("readTimeoutHandler", new ReadTimeoutHandler(300)); if (UseSSL && !SSLContextProvider.selfSigned) p.addLast("ssl", getClientSSLHandler(addr, port)); if (UseSSL && SSLContextProvider.selfSigned) p.addLast("ssl", SSLContextProvider.getSelfClient().newHandler(ch.alloc(), addr, port)); p.addLast("InboundOutboundClientHandler", new ClientConnectionHander()); } }); // Make the connection attempt. ChannelFuture f = b.connect(addr, port).sync(); f.awaitUninterruptibly(2000, TimeUnit.MILLISECONDS); if (!f.isSuccess()) throw new RuntimeException("Failed to connect"); // if a wait option was selected and the connect did not fail, // the Date can now be sent. System.out.println("Client Core Connected!"); connection = f.channel(); connClosed = false; // Initiate the Ping->Pong->PingPong Packet test. PacketRegistry.pack(new PingPacket()) .sendPacket(connection.pipeline().context("InboundOutboundClientHandler")); // Wait until the connection is closed. f.channel().closeFuture().sync(); } finally { group.shutdownGracefully(); System.out.println("Connection Closed"); connClosed = true; } }
From source file:net.petercashel.nettyCore.server.serverCore.java
License:Apache License
/** * Initializes the Server listerning socket * * @param port//www . ja va 2 s .c om * - Int port to bind to * @throws Exception */ public static void initializeServer(int port) throws Exception { clientConnectionMap = new HashMap<SocketAddress, ChannelUserHolder>(); AuthTmpUserMap = new HashMap<String, String>(); PacketRegistry.setupRegistry(); PacketRegistry.Side = side; if (UseSSL) SSLContextProvider.SetupSSL(); bossGroup = new NioEventLoopGroup(); // (1) workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); // (2) b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) // (3) .childHandler(new ChannelInitializer<SocketChannel>() { // (4) @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast("readTimeoutHandler", new ReadTimeoutHandler(300)); if (UseSSL && !SSLContextProvider.selfSigned) p.addLast("ssl", getSSLHandler()); if (UseSSL && SSLContextProvider.selfSigned) p.addLast("ssl", SSLContextProvider.getSelfServer().newHandler(ch.alloc())); p.addLast("InboundOutboundServerHandler", new ServerConnectionHandler()); } }).option(ChannelOption.SO_BACKLOG, 128) // (5) .childOption(ChannelOption.TCP_NODELAY, true); // (6) // Bind and start to accept incoming connections. ChannelFuture f = b.bind("0.0.0.0", port).sync(); // (7) System.out.println("Server Core Initalised!"); // Wait until the server socket is closed. // In this example, this does not happen, but you can do that to // gracefully // shut down your server. f.channel().closeFuture().sync(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:net.petercashel.nettyCore.serverUDS.serverCoreUDS.java
License:Apache License
public static void initializeServer(File socket) throws Exception { clientConnectionMap = new HashMap<Channel, Channel>(); PacketRegistry.setupRegistry();//from w w w. j a v a2s . c o m PacketRegistry.Side = side; alive = true; bossGroup = new EpollEventLoopGroup(); // (1) workerGroup = new EpollEventLoopGroup(); try { ServerBootstrap b = new BootstrapFactory<ServerBootstrap>() { @Override public ServerBootstrap newInstance() { return new ServerBootstrap().group(bossGroup, workerGroup) .channel(EpollServerDomainSocketChannel.class) .childHandler(new ChannelInitializer<EpollDomainSocketChannel>() { @Override public void initChannel(EpollDomainSocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast("readTimeoutHandler", new ReadTimeoutHandler(300)); p.addLast("InboundOutboundServerHandler", new ServerUDSConnectionHandler()); } }); } }.newInstance(); // Bind and start to accept incoming connections. ChannelFuture f = b.bind(newSocketAddress(socket)).sync(); // (7) System.out.println("Server UDS Initalised!"); // Wait until the server socket is closed. // In this example, this does not happen, but you can do that to // gracefully // shut down your server. c = f.channel(); f.channel().closeFuture().sync(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:nettyprivateprotocolclientdemo.NettyClient.java
public void connect(int port, String host) throws InterruptedException { try {//from w ww.j av a 2 s . c o m Bootstrap bootstrap = new Bootstrap(); bootstrap.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new NettyMessageDecoder(1024 * 1024, 4, 4)); ch.pipeline().addLast("MessageEncoder", new NettyMessageEncoder()); ch.pipeline().addLast("readTimeoutHandler", new ReadTimeoutHandler(50)); ch.pipeline().addLast("LoginAuthHandler", new LoginAuthReqHandler()); ch.pipeline().addLast("HeartBeatHandler", new HeartBeatReqHandler()); } //initChannel() }); ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port), new InetSocketAddress(NettyConstant.LOCAL_IP, NettyConstant.LOCAL_PORT)).sync(); future.channel().closeFuture().sync(); } finally { executor.execute(new Runnable() { @Override public void run() { try { TimeUnit.SECONDS.sleep(5); try { connect(NettyConstant.PORT, NettyConstant.REMOTEIP); } catch (Exception e) { e.printStackTrace(); } } catch (InterruptedException ie) { ie.printStackTrace(); } } //run() }); } //try-finally }
From source file:nettyprivateprotocolserverdemo.NettyServer.java
public void bind() throws InterruptedException { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); ServerBootstrap serverBootstrap = new ServerBootstrap(); serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override/*w w w .j a v a 2s.c om*/ protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new NettyMessageDecoder(1024 * 1024, 4, 4)); ch.pipeline().addLast(new NettyMessageEncoder()); ch.pipeline().addLast("readTimeoutHandler", new ReadTimeoutHandler(50)); ch.pipeline().addLast(new LoginAuthRespHandler()); ch.pipeline().addLast(new HeartBeatRespHandler()); } //initChannel() }); serverBootstrap.bind(NettyConstant.REMOTEIP, NettyConstant.PORT).sync(); System.out.println("Netty server start OK : " + NettyConstant.REMOTEIP + " : " + NettyConstant.PORT); }
From source file:nss.delta.agentmanager.dummycon.OFChannelInitializer.java
License:Apache License
@Override protected void initChannel(Channel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); OFChannelHandler newhandler = new OFChannelHandler(pipeline, timer, ofBitmaps, defaultFactory, testHandShake);/*from w w w. j a v a2 s.c om*/ if (this.handler == null) this.handler = newhandler; // for Multiple Switches // handlerlist.add(handler); if (keyStore != null && keyStorePassword != null) { try { /* Set up factories and stores. */ TrustManagerFactory tmFactory = TrustManagerFactory .getInstance(TrustManagerFactory.getDefaultAlgorithm()); KeyStore tmpKS = null; tmFactory.init(tmpKS); /* Use keystore/pass defined in properties file. */ KeyStore ks = KeyStore.getInstance("JKS"); ks.load(new FileInputStream(keyStore), keyStorePassword.toCharArray()); KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); kmf.init(ks, keyStorePassword.toCharArray()); KeyManager[] km = kmf.getKeyManagers(); TrustManager[] tm = tmFactory.getTrustManagers(); /* Set up SSL prereqs for Netty. */ SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(km, tm, null); SSLEngine sslEngine = sslContext.createSSLEngine(); /* We are the server and we will create secure sessions. */ sslEngine.setUseClientMode(false); sslEngine.setEnableSessionCreation(true); /* These are redundant (default), but for clarity... */ sslEngine.setEnabledProtocols(sslEngine.getSupportedProtocols()); sslEngine.setEnabledCipherSuites(sslEngine.getSupportedCipherSuites()); /* * First, decrypt w/handler+engine; then, proceed with rest of * handlers. */ pipeline.addLast(PipelineHandler.SSL_TLS_ENCODER_DECODER, new SslHandler(sslEngine)); log.info("SSL OpenFlow socket initialized and handler ready for switch."); } catch (Exception e) { /* * There are lots of possible exceptions to * catch, so this should get them all. */ log.error("Exception initializing SSL OpenFlow socket: {}", e.getMessage()); throw e; /* * If we wanted secure but didn't get it, we should * bail. */ } } pipeline.addLast(PipelineHandler.OF_MESSAGE_DECODER, new OFMessageDecoder()); pipeline.addLast(PipelineHandler.OF_MESSAGE_ENCODER, new OFMessageEncoder()); pipeline.addLast(PipelineHandler.MAIN_IDLE, new IdleStateHandler(PipelineIdleReadTimeout.MAIN, PipelineIdleWriteTimeout.MAIN, 0)); pipeline.addLast(PipelineHandler.READ_TIMEOUT, new ReadTimeoutHandler(30)); pipeline.addLast(PipelineHandler.CHANNEL_HANDSHAKE_TIMEOUT, new HandshakeTimeoutHandler(newhandler, timer, PipelineHandshakeTimeout.CHANNEL)); pipeline.addLast(PipelineHandler.CHANNEL_HANDLER, newhandler); }
From source file:org.atmosphere.nettosphere.extra.FlashPolicyServerChannelInitializer.java
License:Apache License
@Override protected void initChannel(Channel ch) throws Exception { // Create a default pipeline implementation. ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast("timeout", new ReadTimeoutHandler(30)); pipeline.addLast("decoder", new FlashPolicyServerDecoder()); pipeline.addLast("handler", new FlashPolicyServerHandler()); }