List of usage examples for io.netty.util Attribute get
T get();
From source file:com.mastfrog.scamper.Associations.java
License:Open Source License
private int getForKey(AttributeKey<AtomicRoundRobin> key, Channel channel) { Attribute<AtomicRoundRobin> attr = channel.attr(key); AtomicRoundRobin r = attr.get(); if (r == null && channel instanceof NioSctpChannel) { synchronized (this) { NioSctpChannel ch = (NioSctpChannel) channel; Address address = new Address((InetSocketAddress) ch.remoteAddress()); Asso asso = new Asso(address, ch); associations.put(address, asso); attr = channel.attr(key);/*from ww w . j av a 2 s . c o m*/ r = attr.get(); } } return r == null ? 0 : r.get(); }
From source file:com.mnt.base.stream.netty.NConnectionHandler.java
License:Open Source License
@Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { Attribute<Connection> connAttr = ctx.channel().attr(NSTREAM_CONNECTION_KEY); Connection conn = connAttr.get(); if (conn != null) { conn.close();// w ww. j a va2 s . co m } super.channelInactive(ctx); }
From source file:com.mnt.base.stream.netty.NStreamDecoder.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext chx, ByteBuf buf, List<Object> out) throws Exception { Attribute<NStreamLightweightParser> parserAttr = chx.channel().attr(NStreamDecoder.NSTREAM_PARSER_KEY); NStreamLightweightParser parser = parserAttr.get(); if (parser == null) { parser = new NStreamLightweightParser(); parserAttr.set(parser);// w w w . j av a2 s.com } parser.read(buf); if (parser.areTherePackets()) { out.addAll(parser.getPacketBytes()); parser.invalidateBufferAndClear(); } }
From source file:com.mnt.base.stream.netty.NStreamEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext ctx, StreamPacket packet, ByteBuf out) throws Exception { Attribute<NStreamLightweightParser> parserAttr = ctx.channel().attr(NStreamDecoder.NSTREAM_PARSER_KEY); NStreamLightweightParser parser = parserAttr.get(); if (parser == null) { parser = new NStreamLightweightParser(); parserAttr.set(parser);/*from w ww . j a va 2s . c o m*/ } ByteArrays source = packet.getSource(); out.writeByte(StreamPacketDef.BYTE_STREAM_PACKET_START); out.writeBytes(BytesUtil.intToBytes(source.getTotalLength())); for (ByteArray ba : source.getByteArrays()) { out.writeBytes(ba.getBytes(), ba.getPosition(), ba.getLength()); } out.writeBytes(BytesUtil.genSign(source)); out.writeByte(StreamPacketDef.BYTE_STREAM_PACKET_END); }
From source file:com.pubkit.platform.messaging.protocol.mqtt.proto.parser.ConnectDecoder.java
License:Open Source License
@Override public AbstractMessage decode(AttributeMap ctx, ByteBuf in) throws UnsupportedEncodingException { in.resetReaderIndex();/*from w w w . j av a 2s . c om*/ //Common decoding part ConnectMessage message = new ConnectMessage(); if (!decodeCommonHeader(message, 0x00, in)) { in.resetReaderIndex(); return null; } int remainingLength = message.getRemainingLength(); int start = in.readerIndex(); int protocolNameLen = in.readUnsignedShort(); byte[] encProtoName; String protoName; Attribute<Integer> versionAttr = ctx.attr(MQTTDecoder.PROTOCOL_VERSION); switch (protocolNameLen) { case 6: //MQTT version 3.1 "MQIsdp" //ProtocolName 8 bytes or 6 bytes if (in.readableBytes() < 10) { in.resetReaderIndex(); return null; } encProtoName = new byte[6]; in.readBytes(encProtoName); protoName = new String(encProtoName, "UTF-8"); if (!"MQIsdp".equals(protoName)) { in.resetReaderIndex(); throw new CorruptedFrameException("Invalid protoName: " + protoName); } message.setProtocolName(protoName); versionAttr.set((int) VERSION_3_1); break; case 4: //MQTT version 3.1.1 "MQTT" //ProtocolName 6 bytes if (in.readableBytes() < 8) { in.resetReaderIndex(); return null; } encProtoName = new byte[4]; in.readBytes(encProtoName); protoName = new String(encProtoName, "UTF-8"); if (!"MQTT".equals(protoName)) { in.resetReaderIndex(); throw new CorruptedFrameException("Invalid protoName: " + protoName); } message.setProtocolName(protoName); versionAttr.set((int) VERSION_3_1_1); break; default: //protocol broken throw new CorruptedFrameException("Invalid protoName size: " + protocolNameLen); } //ProtocolVersion 1 byte (value 0x03 for 3.1, 0x04 for 3.1.1) message.setProcotolVersion(in.readByte()); if (message.getProcotolVersion() == VERSION_3_1_1) { //if 3.1.1, check the flags (dup, retain and qos == 0) if (message.isDupFlag() || message.isRetainFlag() || message.getQos() != AbstractMessage.QOSType.MOST_ONE) { throw new CorruptedFrameException("Received a CONNECT with fixed header flags != 0"); } //check if this is another connect from the same client on the same session Attribute<Boolean> connectAttr = ctx.attr(ConnectDecoder.CONNECT_STATUS); Boolean alreadyConnected = connectAttr.get(); if (alreadyConnected == null) { //never set connectAttr.set(true); } else if (alreadyConnected) { throw new CorruptedFrameException("Received a second CONNECT on the same network connection"); } } //PKMPConnection flag byte connFlags = in.readByte(); if (message.getProcotolVersion() == VERSION_3_1_1) { if ((connFlags & 0x01) != 0) { //bit(0) of connection flags is != 0 throw new CorruptedFrameException("Received a CONNECT with connectionFlags[0(bit)] != 0"); } } boolean cleanSession = ((connFlags & 0x02) >> 1) == 1; boolean willFlag = ((connFlags & 0x04) >> 2) == 1; byte willQos = (byte) ((connFlags & 0x18) >> 3); if (willQos > 2) { in.resetReaderIndex(); throw new CorruptedFrameException("Expected will QoS in range 0..2 but found: " + willQos); } boolean willRetain = ((connFlags & 0x20) >> 5) == 1; boolean passwordFlag = ((connFlags & 0x40) >> 6) == 1; boolean userFlag = ((connFlags & 0x80) >> 7) == 1; //a password is true iff user is true. if (!userFlag && passwordFlag) { in.resetReaderIndex(); throw new CorruptedFrameException( "Expected password flag to true if the user flag is true but was: " + passwordFlag); } message.setCleanSession(cleanSession); message.setWillFlag(willFlag); message.setWillQos(willQos); message.setWillRetain(willRetain); message.setPasswordFlag(passwordFlag); message.setUserFlag(userFlag); //Keep Alive timer 2 bytes //int keepAlive = Utils.readWord(in); int keepAlive = in.readUnsignedShort(); message.setKeepAlive(keepAlive); if ((remainingLength == 12 && message.getProcotolVersion() == VERSION_3_1) || (remainingLength == 10 && message.getProcotolVersion() == VERSION_3_1_1)) { return message; } //Decode the ClientID String clientID = Utils.decodeString(in); if (clientID == null) { in.resetReaderIndex(); return null; } message.setClientID(clientID); //Decode willTopic if (willFlag) { String willTopic = Utils.decodeString(in); if (willTopic == null) { in.resetReaderIndex(); return null; } message.setWillTopic(willTopic); } //Decode willMessage if (willFlag) { String willMessage = Utils.decodeString(in); if (willMessage == null) { in.resetReaderIndex(); return null; } message.setWillMessage(willMessage); } //Compatibility check with v3.0, remaining length has precedence over //the user and password flags int readed = in.readerIndex() - start; if (readed == remainingLength) { return message; } //Decode username if (userFlag) { String userName = Utils.decodeString(in); if (userName == null) { in.resetReaderIndex(); return null; } message.setUsername(userName); } readed = in.readerIndex() - start; if (readed == remainingLength) { return message; } //Decode password if (passwordFlag) { String password = Utils.decodeString(in); if (password == null) { in.resetReaderIndex(); return null; } message.setPassword(password); } return message; }
From source file:com.qq.servlet.demo.netty.telnet.client.ClientHandler.java
License:Apache License
@Override protected void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception { Channel ch = ctx.channel();//from www. j ava 2s. c om AttributeKey<String> key = AttributeKey.valueOf("ASYNC_CONTEXT"); Attribute<String> attr = ch.attr(key); System.out.println("----------->" + attr.get() + "\t" + msg); ChannelFuture future = ch.close(); future.await(100); }
From source file:com.qq.servlet.demo.netty.telnet.TelnetClientHandler.java
License:Apache License
@Override protected void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception { Channel ch = ctx.channel();/*from ww w. ja v a2s .c om*/ AttributeKey<AsyncContext> key = AttributeKey.valueOf("ASYNC_CONTEXT"); Attribute<AsyncContext> attr = ch.attr(key); AsyncContext context = attr.get(); if (context != null) { HttpServletRequest request = (HttpServletRequest) context.getRequest(); HttpServletResponse response = (HttpServletResponse) context.getResponse(); Object object = request.getAttribute("beginTime"); long bt = Long.parseLong(object.toString()); PrintWriter writer = response.getWriter(); writer.println("invoke api result: \t"); long time = System.currentTimeMillis() - bt; // writer.println("???"+time); writer.flush(); //servlet?http? System.err.println(context + "\t" + request.getAttribute("REQUEST_ID")); context.complete(); } else { System.out.println(context + "----------->" + msg); } ChannelFuture future = ch.close(); future.await(100); }
From source file:com.qq.servlet.demo.thrift.netty.client.NettyThriftClientHandler.java
License:Apache License
@Override protected void messageReceived(ChannelHandlerContext ctx, TBase msg) throws Exception { Channel ch = ctx.channel();/*from ww w . j a v a 2 s .com*/ AttributeKey<String> key = AttributeKey.valueOf("ASYNC_CONTEXT"); Attribute<String> attr = ch.attr(key); System.out.println("----------->" + attr.get() + "\t" + msg); ChannelFuture future = ch.close(); future.await(100); }
From source file:com.tc.websocket.server.ContextWrapper.java
License:Apache License
/** * Gets the resource descriptor.//from w w w . j a va 2 s . c o m * * @return the resource descriptor */ public String getResourceDescriptor() { Attribute<Object> attr = this.ctx.channel().attr(AttributeKey.valueOf(RESOURCE_DESC)); return (String) attr.get(); }
From source file:com.tc.websocket.server.DominoWebSocketServer.java
License:Apache License
@Override public void onError(ContextWrapper conn, Exception ex) { LOG.log(Level.FINE, "***DominoWebSocketServer.onError***"); if (conn != null) { Attribute<Object> att = conn.attr(AttributeKey.newInstance("resourceDescriptor")); LOG.log(Level.SEVERE, null, att.get().toString()); }// w w w .j a v a 2 s.c o m LOG.log(Level.SEVERE, null, ex); this.notifyEventObservers(Const.ON_ERROR, ex); }