List of usage examples for io.netty.channel ChannelHandlerContext channel
Channel channel();
From source file:com.antsdb.saltedfish.server.mysql.MysqlServerHandler.java
License:Open Source License
private void run(ChannelHandlerContext ctx, Object msg) throws Exception { if (msg instanceof AuthPacket) { auth(ctx, (AuthPacket) msg);//from ww w .j a v a 2s. com return; } else if (msg instanceof ShutdownPacket) { ctx.channel().close(); ctx.channel().parent().close(); System.exit(0); } if (this.session == null) { throw new OrcaException("session is closed"); } if (this.session.isClosed()) { throw new OrcaException("session is closed"); } if (msg instanceof PingPacket) { ping(ctx); } else if (msg instanceof QueryPacket) { query(ctx, (QueryPacket) msg); } else if (msg instanceof InitPacket) { init(ctx, (InitPacket) msg); } else if (msg instanceof StmtPreparePacket) { stmtPrepare(ctx, (StmtPreparePacket) msg); } else if (msg instanceof LongDataPacket) { stmtPrepareLongData(ctx, (LongDataPacket) msg); } else if (msg instanceof StmtExecutePacket) { stmtExecute(ctx, (StmtExecutePacket) msg); } else if (msg instanceof StmtClosePacket) { stmtClose(ctx, (StmtClosePacket) msg); } else if (msg instanceof ClosePacket) { close(ctx); } else if (msg instanceof FieldListPacket) { fieldList(ctx, (FieldListPacket) msg); } else if (msg instanceof SetOptionPacket) { setOption(ctx, (SetOptionPacket) msg); } else { unknown(ctx); throw new CodingError("unknown message type: " + msg.getClass().toString()); } }
From source file:com.athena.dolly.websocket.client.test.WebSocketClientHandler.java
License:Apache License
@Override public void channelActive(ChannelHandlerContext ctx) throws Exception { handshaker.handshake(ctx.channel()); }
From source file:com.athena.dolly.websocket.client.test.WebSocketClientHandler.java
License:Apache License
@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 . ja va2 s. c o m*/ return; } if (msg instanceof FullHttpResponse) { FullHttpResponse response = (FullHttpResponse) msg; throw new Exception("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:com.athena.dolly.websocket.server.test.WebSocketServerHandler.java
License:Apache License
private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception { // Handle a bad request. if (!req.getDecoderResult().isSuccess()) { sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST)); return;//from w ww . j a v a 2 s . co m } // 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 = WebSocketServerIndexPage.getContent(getWebSocketLocation(req)); FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, OK, content); res.headers().set(CONTENT_TYPE, "text/html; charset=UTF-8"); 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, false); handshaker = wsFactory.newHandshaker(req); if (handshaker == null) { WebSocketServerHandshakerFactory.sendUnsupportedWebSocketVersionResponse(ctx.channel()); } else { handshaker.handshake(ctx.channel(), req); } }
From source file:com.athena.dolly.websocket.server.test.WebSocketServerHandler.java
License:Apache License
private void handleWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) { // Check for closing frame if (frame instanceof CloseWebSocketFrame) { handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame.retain()); try {//from w ww . java 2 s. c o m fos.flush(); fos.close(); fos = null; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return; } if (frame instanceof PingWebSocketFrame) { ctx.channel().write(new PongWebSocketFrame(frame.content().retain())); return; } if (frame instanceof TextWebSocketFrame) { // Send the uppercase string back. String fileName = ((TextWebSocketFrame) frame).text(); logger.debug(String.format("Received file name is [%s]", fileName)); destFile = new File(fileName + ".received"); try { fos = new FileOutputStream(destFile); } catch (FileNotFoundException e) { e.printStackTrace(); } ctx.channel().write(new TextWebSocketFrame(fileName.toUpperCase())); } if (frame instanceof BinaryWebSocketFrame) { byte[] buffer = null; ByteBuf rawMessage = ((BinaryWebSocketFrame) frame).content(); //logger.debug(">>>> BinaryWebSocketFrame Found, " + rawMessage); // check if this ByteBuf is DIRECT (no backing byte[]) if (rawMessage.hasArray() == false) { int size = rawMessage.readableBytes(); buffer = new byte[size]; rawMessage.readBytes(buffer); try { fos.write(buffer); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { buffer = rawMessage.array(); } logger.debug(">>>> Read Byte Array: " + buffer.length); return; } }
From source file:com.athena.dolly.websocket.server.test.WebSocketServerHandler.java
License:Apache License
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);// w w w.j a va2 s . c o m buf.release(); setContentLength(res, res.content().readableBytes()); } // Send the response and close the connection if necessary. ChannelFuture f = ctx.channel().writeAndFlush(res); if (!isKeepAlive(req) || res.getStatus().code() != 200) { f.addListener(ChannelFutureListener.CLOSE); } }
From source file:com.athena.dolly.websocket.server.test.WebSocketSslServerHandler.java
License:Apache License
private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception { // Handle a bad request. if (!req.getDecoderResult().isSuccess()) { sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST)); return;/* w w w . j a va 2s.co m*/ } // 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 = WebSocketServerIndexPage.getContent(getWebSocketLocation(req)); FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, OK, content); res.headers().set(CONTENT_TYPE, "text/html; charset=UTF-8"); 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, false); handshaker = wsFactory.newHandshaker(req); if (handshaker == null) { WebSocketServerHandshakerFactory.sendUnsupportedWebSocketVersionResponse(ctx.channel()); } else { handshaker.handshake(ctx.channel(), req); } }
From source file:com.athena.dolly.websocket.server.test.WebSocketSslServerHandler.java
License:Apache License
private void handleWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) { // Check for closing frame if (frame instanceof CloseWebSocketFrame) { handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame.retain()); return;//from w w w .jav a2s . co 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(); if (logger.isLoggable(Level.FINE)) { logger.fine(String.format("%s received %s", ctx.channel(), request)); } ctx.channel().write(new TextWebSocketFrame(request.toUpperCase())); }
From source file:com.athena.dolly.websocket.server.test.WebSocketSslServerHandler.java
License:Apache License
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 w ww . j ava2 s . c o m*/ buf.release(); setContentLength(res, res.content().readableBytes()); } // Send the response and close the connection if necessary. ChannelFuture f = ctx.channel().write(res); if (!isKeepAlive(req) || res.getStatus().code() != 200) { f.addListener(ChannelFutureListener.CLOSE); } }
From source file:com.athena.meerkat.agent.netty.MeerkatClientHandler.java
License:Apache License
@Override public void channelActive(ChannelHandlerContext ctx) throws Exception { LOGGER.debug("channelActive() has invoked. RemoteAddress=[{}]", ctx.channel().remoteAddress()); connected = true;/*from w w w. j av a 2s .c o m*/ String ipAddr = ctx.channel().remoteAddress().toString(); ipAddr = ipAddr.substring(1, ipAddr.indexOf(":")); // register a new channel if (ChannelManagement.registerChannel(ipAddr, ctx.channel()) == 1) { // ? ? ?? ?? ? System . ctx.writeAndFlush(getAgentInitialInfo()); } else { while (true) { if (this.machineId == null) { Thread.sleep(100); } else { break; } } AgentInitialInfoMessage message = new AgentInitialInfoMessage(); message.setMachineId(this.machineId); ctx.writeAndFlush(new MeerkatDatagram<AgentInitialInfoMessage>(message)); } }