List of usage examples for io.netty.channel ChannelHandlerContext channel
Channel channel();
From source file:c5db.discovery.InboundHandler.java
License:Apache License
@Override protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception { SocketAddress remoteAddress = ctx.channel().remoteAddress(); SocketAddress localAddress = ctx.channel().localAddress(); System.out.println("Got message from " + remoteAddress + " to me at: " + localAddress); System.out.println(" It was SOOO big: " + msg.readableBytes()); }
From source file:ca.lambtoncollege.hauntedhouse.server.ServerHandler.java
License:Apache License
@Override public void channelActive(final ChannelHandlerContext ctx) { try {// w w w .jav a 2 s . c o m String str = String.format("%d,Welcome to %s Strategic Master service!", PropertyMgr.CONNECTION_ESTABLISHED, InetAddress.getLocalHost().getHostName()); ctx.writeAndFlush(str + "\n"); /* ctx.writeAndFlush( "Your session is protected by " + ctx.pipeline().get(SslHandler.class).engine().getSession().getCipherSuite() + " cipher suite.\n"); */ channels.add(ctx.channel()); Channel clientB = getClient(ctx.channel()); if (clientB == null) { //str=String.format("%d,,%s", PropertyMgr.WAITING_FOR_ANOTHER_PLAYER,InetAddress.getLocalHost().getHostName()); System.out.println("Can't get ClientB , WAITING_FOR_ANOTHER_PLAYER!"); } else { str = String.format("%d,%d,%s", PropertyMgr.JOIN_A_GAME, PropertyMgr.TEAMB, InetAddress.getLocalHost().getHostName()); clientB.writeAndFlush(str + "\n"); System.out.println("get ClientB , JOIN_A_GAME!"); str = String.format("%d,%d,%s", PropertyMgr.JOIN_A_GAME, PropertyMgr.TEAMA, InetAddress.getLocalHost().getHostName()); ctx.writeAndFlush(str + "\n"); } } catch (UnknownHostException ex) { Logger.getLogger(ServerHandler.class.getName()).log(Level.SEVERE, null, ex); } /* // Once session is secured, send a greeting and register the channel to the global channel // list so the channel received the messages from others. ctx.pipeline().get(SslHandler.class).handshakeFuture().addListener( new GenericFutureListener<Future<Channel>>() { @Override public void operationComplete(Future<Channel> future) throws Exception { String str=String.format("%d,Welcome to %s Strategic Master service!", PropertyMgr.CONNECTION_ESTABLISHED,InetAddress.getLocalHost().getHostName()); ctx.writeAndFlush(str+"\n"); ctx.writeAndFlush( "Your session is protected by " + ctx.pipeline().get(SslHandler.class).engine().getSession().getCipherSuite() + " cipher suite.\n"); channels.add(ctx.channel()); Channel clientB = getClient(ctx.channel()); if (clientB == null) { //str=String.format("%d,,%s", PropertyMgr.WAITING_FOR_ANOTHER_PLAYER,InetAddress.getLocalHost().getHostName()); System.out.println("Can't get ClientB , WAITING_FOR_ANOTHER_PLAYER!"); } else { str=String.format("%d,%d,%s", PropertyMgr.JOIN_A_GAME,PropertyMgr.TEAMB,InetAddress.getLocalHost().getHostName()); clientB.writeAndFlush(str+"\n"); System.out.println("get ClientB , JOIN_A_GAME!"); str=String.format("%d,%d,%s", PropertyMgr.JOIN_A_GAME,PropertyMgr.TEAMA,InetAddress.getLocalHost().getHostName()); ctx.writeAndFlush(str+"\n"); } } }); */ }
From source file:ca.lambtoncollege.hauntedhouse.server.ServerHandler.java
License:Apache License
public void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception { // Send the received message to all channels but the current one. // for (Channel c: channels) { // if (c != ctx.channel()) { // c.writeAndFlush("[" + ctx.channel().remoteAddress() + "] " + msg + '\n'); // } else { // c.writeAndFlush("[you] " + msg + '\n'); // } // }//from w w w. j ava2 s .c o m String str = ""; Channel clientA = ctx.channel(); Channel clientB = getClient(clientA); String[] array = PropertyMgr.split(msg); if (clientB == null) { if (array[0].equals(Integer.toString(PropertyMgr.BYE))) { GlobalMap.resetWaiting(); ctx.close(); } } else { if (array[0].equals(Integer.toString(PropertyMgr.DURING_THE_GAME))) { str = String.format("%d,%s,%s,%s,%s", PropertyMgr.DURING_THE_GAME, array[1], array[2], array[3], clientA.remoteAddress()); clientB.writeAndFlush(str + "\n"); } else if (array[0].equals(Integer.toString(PropertyMgr.BYE))) { str = String.format("%d,,%s", PropertyMgr.ANOTHER_PLAYER_LOST_THE_CONNECTION, clientA.remoteAddress()); map.remove(Integer.toString(clientA.hashCode())); map.remove(Integer.toString(clientB.hashCode())); clientB.writeAndFlush(str + "\n"); ctx.close(); } } }
From source file:ca.lambtoncollege.netty.chat.SecureChatServerHandler.java
License:Apache License
@Override public void channelActive(final ChannelHandlerContext ctx) { // Once session is secured, send a greeting and register the channel to the global channel // list so the channel received the messages from others. ctx.pipeline().get(SslHandler.class).handshakeFuture() .addListener(new GenericFutureListener<Future<Channel>>() { @Override/*from w ww . j a va 2 s . c o m*/ public void operationComplete(Future<Channel> future) throws Exception { ctx.writeAndFlush("Welcome to " + InetAddress.getLocalHost().getHostName() + " secure chat service!\n"); ctx.writeAndFlush("Your session is protected by " + ctx.pipeline().get(SslHandler.class).engine().getSession().getCipherSuite() + " cipher suite.\n"); channels.add(ctx.channel()); } }); }
From source file:ca.lambtoncollege.netty.chat.SecureChatServerHandler.java
License:Apache License
@Override protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception { // System.out.println("Server channelRead0==========="+msg+ctx.channel().toString()+" HASHCODE:"+ctx.channel().hashCode()); // String clientA = Integer.toString(ctx.channel().hashCode()); Channel clientB = getClient(ctx.channel()); if (clientB != null) { messageReceived(ctx, msg, clientB); } else {//from www. j ava2 s .c o m channelActive(ctx); } //A:7889046619952953509 }
From source file:ca.lambtoncollege.netty.webSocket.ClientHandlerWebSocket.java
@Override public void channelActive(ChannelHandlerContext ctx) { handshaker.handshake(ctx.channel()); }
From source file:ca.lambtoncollege.netty.webSocket.ClientHandlerWebSocket.java
@Override public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { Channel ch = ctx.channel(); if (!handshaker.isHandshakeComplete()) { handshaker.finishHandshake(ch, (FullHttpResponse) msg); System.out.println("WebSocket Client connected!"); handshakeFuture.setSuccess();//from w ww . j av a2 s . co m return; } if (msg instanceof FullHttpResponse) { FullHttpResponse response = (FullHttpResponse) msg; throw new IllegalStateException("Unexpected FullHttpResponse (getStatus=" + response.getStatus() + ", content=" + response.content().toString(CharsetUtil.UTF_8) + ')'); } WebSocketFrame frame = (WebSocketFrame) msg; if (frame instanceof TextWebSocketFrame) { TextWebSocketFrame textFrame = (TextWebSocketFrame) frame; System.out.println("WebSocket Client received message: " + textFrame.text()); } else if (frame instanceof PongWebSocketFrame) { System.out.println("WebSocket Client received pong"); } else if (frame instanceof CloseWebSocketFrame) { System.out.println("WebSocket Client received closing"); ch.close(); } }
From source file:ca.lambtoncollege.netty.webSocket.ServerHandlerWebSocket.java
private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) { // Handle a bad request. if (!req.getDecoderResult().isSuccess()) { sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST)); return;// w w w .j a v a 2s . c om } // Allow only GET methods. if (req.getMethod() != GET) { sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN)); return; } // Send the demo page and favicon.ico if ("/".equals(req.getUri())) { ByteBuf content = ServerIndexPageWebSocket.getContent(getWebSocketLocation(req)); FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, OK, content); res.headers().set(CONTENT_TYPE, "text/html; charset=UTF-8"); HttpHeaders.setContentLength(res, content.readableBytes()); sendHttpResponse(ctx, req, res); return; } if ("/favicon.ico".equals(req.getUri())) { FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, NOT_FOUND); sendHttpResponse(ctx, req, res); return; } // Handshake WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(getWebSocketLocation(req), null, true); handshaker = wsFactory.newHandshaker(req); if (handshaker == null) { WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel()); } else { handshaker.handshake(ctx.channel(), req); } }
From source file:ca.lambtoncollege.netty.webSocket.ServerHandlerWebSocket.java
private void handleWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) { // Check for closing frame if (frame instanceof CloseWebSocketFrame) { handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame.retain()); return;/*from ww w . ja v a 2 s . c o m*/ } if (frame instanceof PingWebSocketFrame) { ctx.channel().write(new PongWebSocketFrame(frame.content().retain())); return; } if (!(frame instanceof TextWebSocketFrame)) { throw new UnsupportedOperationException( String.format("%s frame types not supported", frame.getClass().getName())); } // Send the uppercase string back. String request = ((TextWebSocketFrame) frame).text(); System.err.printf("%s received %s%n", ctx.channel(), request); ctx.channel().write(new TextWebSocketFrame(request.toUpperCase())); }
From source file:ca.lambtoncollege.netty.webSocket.ServerHandlerWebSocket.java
private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) { // Generate an error page if response getStatus code is not OK (200). if (res.getStatus().code() != 200) { ByteBuf buf = Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8); res.content().writeBytes(buf);//from ww w . java 2 s .c o m buf.release(); HttpHeaders.setContentLength(res, res.content().readableBytes()); } // Send the response and close the connection if necessary. ChannelFuture f = ctx.channel().writeAndFlush(res); if (!HttpHeaders.isKeepAlive(req) || res.getStatus().code() != 200) { f.addListener(ChannelFutureListener.CLOSE); } }