List of usage examples for io.netty.util Attribute set
void set(T value);
From source file:com.mnt.base.stream.netty.NConnectionHandler.java
License:Open Source License
@Override public void handlerAdded(ChannelHandlerContext ctx) throws Exception { super.handlerAdded(ctx); Connection connection = createConnection(ctx, UUID.randomUUID().toString()); // Create a new NIOConnection for the new session Attribute<Connection> connAttr = ctx.channel().attr(NSTREAM_CONNECTION_KEY); connAttr.set(connection); Attribute<RequestHandler> handlerAttr = ctx.channel().attr(NSTREAM_HANDLER_KEY); handlerAttr.set(createRequestHandler(connection)); }
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 ww .ja v a 2s .co m*/ 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 w w. ja v a 2 s .c om 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 ww w. j av a2 s .c o m*/ //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.tc.websocket.server.ContextWrapper.java
License:Apache License
/** * Sets the resource descriptor.// w w w . java 2s . co m * * @param resourceDescriptor the new resource descriptor */ public void setResourceDescriptor(String resourceDescriptor) { Attribute<Object> attr = this.ctx.channel().attr(AttributeKey.valueOf(RESOURCE_DESC)); attr.set(resourceDescriptor); }
From source file:freddo.dtalk2.broker.netty.NettyChannel.java
License:Apache License
public void setAttribute(AttributeKey<Object> key, Object value) { Attribute<Object> attr = mContext.attr(key); attr.set(value); }
From source file:io.gatling.http.client.pool.ChannelPool.java
License:Apache License
private void incrementStreamCount(Channel channel) { Attribute<Integer> streamCountAttr = channel.attr(CHANNEL_POOL_STREAM_COUNT_ATTRIBUTE_KEY); streamCountAttr.set(streamCountAttr.get() + 1); }
From source file:io.gatling.http.client.pool.ChannelPool.java
License:Apache License
public void offer(Channel channel) { ChannelPoolKey key = channel.attr(CHANNEL_POOL_KEY_ATTRIBUTE_KEY).get(); assertNotNull(key, "Channel doesn't have a key"); if (isHttp1(channel)) { remoteChannels(key).offer(channel); touch(channel);//from w w w .ja va 2s .c o m } else { Attribute<Integer> streamCountAttr = channel.attr(CHANNEL_POOL_STREAM_COUNT_ATTRIBUTE_KEY); Integer currentStreamCount = streamCountAttr.get(); if (currentStreamCount == null) { remoteChannels(key).offer(channel); streamCountAttr.set(1); } else { streamCountAttr.set(currentStreamCount - 1); if (currentStreamCount == 1) { // so new value is 0 touch(channel); } } } }
From source file:io.moquette.parser.netty.ConnectDecoderTest.java
License:Open Source License
@Test(expected = CorruptedFrameException.class) public void testDoubleConnectInTheSameSession() throws Exception { //setup the connection session as already connected Attribute<Boolean> connectAttr = this.attrMap.attr(ConnectDecoder.CONNECT_STATUS); connectAttr.set(true); m_buff = Unpooled.buffer(12);/*from w w w. j ava2s.c o m*/ initBaseHeader311(m_buff); List<Object> results = new ArrayList<Object>(); //Excercise m_msgdec.decode(this.attrMap, m_buff, results); }
From source file:io.moquette.server.netty.metrics.BytesMetricsHandler.java
License:Open Source License
@Override public void channelActive(ChannelHandlerContext ctx) throws Exception { Attribute<BytesMetrics> attr = ctx.attr(ATTR_KEY_METRICS); attr.set(new BytesMetrics()); super.channelActive(ctx); }