List of usage examples for io.netty.channel ChannelHandlerContext alloc
ByteBufAllocator alloc();
From source file:com.couchbase.client.core.endpoint.view.ViewHandler.java
License:Apache License
@Override protected CouchbaseResponse decodeResponse(final ChannelHandlerContext ctx, final HttpObject msg) throws Exception { ViewRequest request = currentRequest(); CouchbaseResponse response = null;/*from w ww . j a v a2s . c o m*/ if (msg instanceof HttpResponse) { responseHeader = (HttpResponse) msg; if (responseContent != null) { responseContent.clear(); } else { responseContent = ctx.alloc().buffer(); } } if (request instanceof KeepAliveRequest) { response = new KeepAliveResponse(ResponseStatusConverter.fromHttp(responseHeader.getStatus().code()), request); responseContent.clear(); responseContent.discardReadBytes(); } else if (msg instanceof HttpContent) { responseContent.writeBytes(((HttpContent) msg).content()); if (currentRequest() instanceof ViewQueryRequest) { if (viewRowObservable == null) { response = handleViewQueryResponse(); } parseQueryResponse(msg instanceof LastHttpContent); } } if (msg instanceof LastHttpContent) { if (request instanceof GetDesignDocumentRequest) { response = handleGetDesignDocumentResponse((GetDesignDocumentRequest) request); finishedDecoding(); } else if (request instanceof UpsertDesignDocumentRequest) { response = handleUpsertDesignDocumentResponse((UpsertDesignDocumentRequest) request); finishedDecoding(); } else if (request instanceof RemoveDesignDocumentRequest) { response = handleRemoveDesignDocumentResponse((RemoveDesignDocumentRequest) request); finishedDecoding(); } else if (request instanceof KeepAliveRequest) { finishedDecoding(); } } return response; }
From source file:com.dempe.chat.common.mqtt.codec.ConnectEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext chc, ConnectMessage message, ByteBuf out) { ByteBuf staticHeaderBuff = chc.alloc().buffer(12); ByteBuf buff = chc.alloc().buffer(); ByteBuf variableHeaderBuff = chc.alloc().buffer(12); try {//from ww w . j a v a2s. c o m staticHeaderBuff.writeBytes(Utils.encodeString("MQIsdp")); //version staticHeaderBuff.writeByte(0x03); //connection flags and Strings byte connectionFlags = 0; if (message.isCleanSession()) { connectionFlags |= 0x02; } if (message.isWillFlag()) { connectionFlags |= 0x04; } connectionFlags |= ((message.getWillQos() & 0x03) << 3); if (message.isWillRetain()) { connectionFlags |= 0x020; } if (message.isPasswordFlag()) { connectionFlags |= 0x040; } if (message.isUserFlag()) { connectionFlags |= 0x080; } staticHeaderBuff.writeByte(connectionFlags); //Keep alive timer staticHeaderBuff.writeShort(message.getKeepAlive()); //Variable part if (message.getClientID() != null) { variableHeaderBuff.writeBytes(Utils.encodeString(message.getClientID())); if (message.isWillFlag()) { variableHeaderBuff.writeBytes(Utils.encodeString(message.getWillTopic())); variableHeaderBuff.writeBytes(Utils.encodeFixedLengthContent(message.getWillMessage())); } if (message.isUserFlag() && message.getUsername() != null) { variableHeaderBuff.writeBytes(Utils.encodeString(message.getUsername())); if (message.isPasswordFlag() && message.getPassword() != null) { variableHeaderBuff.writeBytes(Utils.encodeFixedLengthContent(message.getPassword())); } } } int variableHeaderSize = variableHeaderBuff.readableBytes(); buff.writeByte(AbstractMessage.CONNECT << 4); buff.writeBytes(Utils.encodeRemainingLength(12 + variableHeaderSize)); buff.writeBytes(staticHeaderBuff).writeBytes(variableHeaderBuff); out.writeBytes(buff); } finally { staticHeaderBuff.release(); buff.release(); variableHeaderBuff.release(); } }
From source file:com.dempe.chat.common.mqtt.codec.PubAckEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext chc, PubAckMessage msg, ByteBuf out) { ByteBuf buff = chc.alloc().buffer(4); try {//w ww . jav a 2 s.co m buff.writeByte(AbstractMessage.PUBACK << 4); buff.writeBytes(Utils.encodeRemainingLength(2)); buff.writeShort(msg.getMessageID()); out.writeBytes(buff); } finally { buff.release(); } }
From source file:com.dempe.chat.common.mqtt.codec.PublishEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext ctx, PublishMessage message, ByteBuf out) { if (message.getQos() == AbstractMessage.QOSType.RESERVED) { throw new IllegalArgumentException("Found a message with RESERVED Qos"); }/*from w w w.jav a2 s . c o m*/ if (message.getTopicName() == null || message.getTopicName().isEmpty()) { throw new IllegalArgumentException("Found a message with empty or null topic name"); } ByteBuf variableHeaderBuff = ctx.alloc().buffer(2); ByteBuf buff = null; try { variableHeaderBuff.writeBytes(Utils.encodeString(message.getTopicName())); if (message.getQos() == AbstractMessage.QOSType.LEAST_ONE || message.getQos() == AbstractMessage.QOSType.EXACTLY_ONCE) { if (message.getMessageID() == null) { throw new IllegalArgumentException("Found a message with QOS 1 or 2 and not MessageID setted"); } variableHeaderBuff.writeShort(message.getMessageID()); } variableHeaderBuff.writeBytes(message.getPayload()); int variableHeaderSize = variableHeaderBuff.readableBytes(); byte flags = Utils.encodeFlags(message); buff = ctx.alloc().buffer(2 + variableHeaderSize); buff.writeByte(AbstractMessage.PUBLISH << 4 | flags); buff.writeBytes(Utils.encodeRemainingLength(variableHeaderSize)); buff.writeBytes(variableHeaderBuff); out.writeBytes(buff); } catch (Exception e) { LOGGER.error(e.getMessage(), e); } finally { variableHeaderBuff.release(); if (buff != null) { buff.release(); } } }
From source file:com.dempe.chat.common.mqtt.codec.SubAckEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext chc, SubAckMessage message, ByteBuf out) { if (message.types().isEmpty()) { throw new IllegalArgumentException("Found a suback message with empty topics"); }/*from w ww. ja va 2s . c o m*/ int variableHeaderSize = 2 + message.types().size(); ByteBuf buff = chc.alloc().buffer(6 + variableHeaderSize); try { buff.writeByte(AbstractMessage.SUBACK << 4); buff.writeBytes(Utils.encodeRemainingLength(variableHeaderSize)); buff.writeShort(message.getMessageID()); for (AbstractMessage.QOSType c : message.types()) { buff.writeByte(c.byteValue()); } out.writeBytes(buff); } finally { buff.release(); } }
From source file:com.dempe.chat.common.mqtt.codec.SubscribeEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext chc, SubscribeMessage message, ByteBuf out) { if (message.subscriptions().isEmpty()) { throw new IllegalArgumentException("Found a subscribe message with empty topics"); }/*from w ww. j a va2s . c o m*/ if (message.getQos() != AbstractMessage.QOSType.LEAST_ONE) { throw new IllegalArgumentException("Expected a message with QOS 1, found " + message.getQos()); } ByteBuf variableHeaderBuff = chc.alloc().buffer(4); ByteBuf buff = null; try { variableHeaderBuff.writeShort(message.getMessageID()); for (SubscribeMessage.Couple c : message.subscriptions()) { variableHeaderBuff.writeBytes(Utils.encodeString(c.topicFilter)); variableHeaderBuff.writeByte(c.qos); } int variableHeaderSize = variableHeaderBuff.readableBytes(); byte flags = Utils.encodeFlags(message); buff = chc.alloc().buffer(2 + variableHeaderSize); buff.writeByte(AbstractMessage.SUBSCRIBE << 4 | flags); buff.writeBytes(Utils.encodeRemainingLength(variableHeaderSize)); buff.writeBytes(variableHeaderBuff); out.writeBytes(buff); } finally { variableHeaderBuff.release(); buff.release(); } }
From source file:com.dempe.chat.common.mqtt.codec.UnsubscribeEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext chc, UnsubscribeMessage message, ByteBuf out) { if (message.topicFilters().isEmpty()) { throw new IllegalArgumentException("Found an unsubscribe message with empty topics"); }/*w w w .j a v a 2 s . co m*/ if (message.getQos() != AbstractMessage.QOSType.LEAST_ONE) { throw new IllegalArgumentException("Expected a message with QOS 1, found " + message.getQos()); } ByteBuf variableHeaderBuff = chc.alloc().buffer(4); ByteBuf buff = null; try { variableHeaderBuff.writeShort(message.getMessageID()); for (String topic : message.topicFilters()) { variableHeaderBuff.writeBytes(Utils.encodeString(topic)); } int variableHeaderSize = variableHeaderBuff.readableBytes(); byte flags = Utils.encodeFlags(message); buff = chc.alloc().buffer(2 + variableHeaderSize); buff.writeByte(AbstractMessage.UNSUBSCRIBE << 4 | flags); buff.writeBytes(Utils.encodeRemainingLength(variableHeaderSize)); buff.writeBytes(variableHeaderBuff); out.writeBytes(buff); } finally { variableHeaderBuff.release(); buff.release(); } }
From source file:com.dempe.chat.common.netty.PublishEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext ctx, PublishMessage message, ByteBuf out) { if (message.getQos() == AbstractMessage.QOSType.RESERVED) { throw new IllegalArgumentException("Found a message with RESERVED Qos"); }//w w w.j a v a 2 s .c o m if (message.getTopicName() == null || message.getTopicName().isEmpty()) { throw new IllegalArgumentException("Found a message with empty or null topic name"); } ByteBuf variableHeaderBuff = ctx.alloc().buffer(2); ByteBuf buff = null; try { variableHeaderBuff.writeBytes(Utils.encodeString(message.getTopicName())); if (message.getQos() == AbstractMessage.QOSType.LEAST_ONE || message.getQos() == AbstractMessage.QOSType.EXACTLY_ONCE) { if (message.getMessageID() == null) { throw new IllegalArgumentException("Found a message with QOS 1 or 2 and not MessageID setted"); } variableHeaderBuff.writeShort(message.getMessageID()); } variableHeaderBuff.writeBytes(message.getPayload()); int variableHeaderSize = variableHeaderBuff.readableBytes(); byte flags = Utils.encodeFlags(message); buff = ctx.alloc().buffer(2 + variableHeaderSize); buff.writeByte(AbstractMessage.PUBLISH << 4 | flags); buff.writeBytes(Utils.encodeRemainingLength(variableHeaderSize)); buff.writeBytes(variableHeaderBuff); out.writeBytes(buff); } finally { variableHeaderBuff.release(); if (buff != null) { buff.release(); } } }
From source file:com.dingwang.netty.encoder.TimeEncoder.java
License:Open Source License
@Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { System.out.println("2222222222222"); System.out.println(msg.toString()); msg = new UnixTime(); UnixTime m = (UnixTime) msg;/*from w w w . j av a 2s .com*/ ByteBuf encoded = ctx.alloc().buffer(4); encoded.writeInt(m.getValue()); ctx.write(encoded, promise); }
From source file:com.ebay.jetstream.messaging.transport.netty.compression.MessageCompressionHandler.java
License:MIT License
/** * Invoked when {@link Channel#write(Object)} is called. *///from w ww.ja v a 2 s. co m public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { try { Attribute<Boolean> attr = ctx.channel().attr(EventProducer.m_eckey); Boolean enableCompression = attr.get(); if ((enableCompression != null) && (enableCompression == true)) { ByteBuf chbuf = (ByteBuf) msg; int msglen = chbuf.readableBytes(); ExtendedChannelPromise epromise = (ExtendedChannelPromise) promise; epromise.setRawBytes(msglen); byte[] compressed = Snappy.rawCompress(chbuf.readBytes(msglen).array(), msglen); epromise.setCompressedBytes(compressed.length + 4); chbuf.release(); // need to release the original buffer - do I need to check if this this a ref counted buffer ByteBuf sendbuf = ctx.alloc().buffer(); sendbuf.writeInt(compressed.length); sendbuf.writeBytes(compressed); ctx.write(sendbuf, promise); m_totalMessagesCompressed.increment(); } else { ctx.write(msg, promise); } } catch (Throwable t) { m_totalMessagesDropped.increment(); LOGGER.debug("Failed to compress message - " + t.getLocalizedMessage()); } }