List of usage examples for io.netty.channel ChannelHandlerContext alloc
ByteBufAllocator alloc();
From source file:com.couchbase.client.core.endpoint.dcp.DCPConnectionHandler.java
License:Apache License
/** * Creates a DCP Open Connection Request. * * @param ctx the channel handler context. * @return a converted {@link BinaryMemcacheRequest}. */// ww w . jav a 2 s .co m private BinaryMemcacheRequest handleOpenConnectionRequest(final ChannelHandlerContext ctx) { ByteBuf extras = ctx.alloc().buffer(8); extras.writeInt(0) // sequence number .writeInt(ConnectionType.CONSUMER.flags()); byte[] key = connectionName.getBytes(CharsetUtil.UTF_8); byte extrasLength = (byte) extras.readableBytes(); short keyLength = (short) key.length; BinaryMemcacheRequest request = new DefaultBinaryMemcacheRequest(key, extras); request.setOpcode(DCPHandler.OP_OPEN_CONNECTION); request.setKeyLength(keyLength); request.setExtrasLength(extrasLength); request.setTotalBodyLength(keyLength + extrasLength); return request; }
From source file:com.couchbase.client.core.endpoint.dcp.DCPConnectionHandler.java
License:Apache License
private FullBinaryMemcacheRequest controlRequest(ChannelHandlerContext ctx, ControlParameter parameter, String value) {/*from ww w .j av a 2 s .com*/ byte[] key = parameter.value().getBytes(CharsetUtil.UTF_8); short keyLength = (short) key.length; byte[] val = value.getBytes(CharsetUtil.UTF_8); ByteBuf body = ctx.alloc().buffer(val.length); body.writeBytes(val); FullBinaryMemcacheRequest request = new DefaultFullBinaryMemcacheRequest(key, Unpooled.EMPTY_BUFFER, body); request.setOpcode(DCPHandler.OP_CONTROL); request.setKeyLength(keyLength); request.setTotalBodyLength(keyLength + body.readableBytes()); return request; }
From source file:com.couchbase.client.core.endpoint.dcp.DCPHandler.java
License:Apache License
/** * Creates a DCP Stream Request./* w w w . j a v a2 s . c o m*/ * * See the [Protocol Description](https://github.com/couchbaselabs/dcp-documentation/blob/master/documentation/ * commands/stream-request.md) for more details. * * @param ctx the channel handler context. * @param msg the stream request message. * @return a converted {@link BinaryMemcacheRequest}. */ private BinaryMemcacheRequest handleStreamRequestRequest(final ChannelHandlerContext ctx, final StreamRequestRequest msg) { ByteBuf extras = ctx.alloc().buffer(48); extras.writeInt(0) // flags .writeInt(0) // reserved .writeLong(msg.startSequenceNumber()) // start sequence number .writeLong(msg.endSequenceNumber()) // end sequence number .writeLong(msg.vbucketUUID()) // vbucket UUID .writeLong(msg.snapshotStartSequenceNumber()) // snapshot start sequence number .writeLong(msg.snapshotEndSequenceNumber()); // snapshot end sequence number byte extrasLength = (byte) extras.readableBytes(); BinaryMemcacheRequest request = new DefaultBinaryMemcacheRequest(extras); request.setOpcode(OP_STREAM_REQUEST); request.setExtrasLength(extrasLength); request.setTotalBodyLength(extrasLength); return request; }
From source file:com.couchbase.client.core.endpoint.kv.KeyValueAuthHandler.java
License:Apache License
/** * Handles an incoming SASL list mechanisms response and dispatches the next SASL AUTH step. * * @param ctx the handler context./*from w w w . j av a 2 s.c o m*/ * @param msg the incoming message to investigate. * @throws Exception if something goes wrong during negotiation. */ private void handleListMechsResponse(ChannelHandlerContext ctx, FullBinaryMemcacheResponse msg) throws Exception { String remote = ctx.channel().remoteAddress().toString(); String[] supportedMechanisms = msg.content().toString(CharsetUtil.UTF_8).split(" "); if (supportedMechanisms.length == 0) { throw new AuthenticationException("Received empty SASL mechanisms list from server: " + remote); } saslClient = Sasl.createSaslClient(supportedMechanisms, null, "couchbase", remote, null, this); selectedMechanism = saslClient.getMechanismName(); int mechanismLength = selectedMechanism.length(); byte[] bytePayload = saslClient.hasInitialResponse() ? saslClient.evaluateChallenge(new byte[] {}) : null; ByteBuf payload = bytePayload != null ? ctx.alloc().buffer().writeBytes(bytePayload) : Unpooled.EMPTY_BUFFER; FullBinaryMemcacheRequest initialRequest = new DefaultFullBinaryMemcacheRequest( selectedMechanism.getBytes(CharsetUtil.UTF_8), Unpooled.EMPTY_BUFFER, payload); initialRequest.setOpcode(SASL_AUTH_OPCODE).setKeyLength((short) mechanismLength) .setTotalBodyLength(mechanismLength + payload.readableBytes()); ChannelFuture future = ctx.writeAndFlush(initialRequest); future.addListener(new GenericFutureListener<Future<Void>>() { @Override public void operationComplete(Future<Void> future) throws Exception { if (!future.isSuccess()) { LOGGER.warn("Error during SASL Auth negotiation phase.", future); } } }); }
From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandler.java
License:Apache License
/** * Encodes a {@link GetRequest} into its lower level representation. * * Depending on the flags set on the {@link GetRequest}, the appropriate opcode gets chosen. Currently, a regular * get, as well as "get and touch" and "get and lock" are supported. Latter variants have server-side side-effects * but do not differ in response behavior. * * @param ctx the {@link ChannelHandlerContext} to use for allocation and others. * @param msg the incoming message./* w w w . ja v a 2 s . co m*/ * @return a ready {@link BinaryMemcacheRequest}. */ private static BinaryMemcacheRequest handleGetRequest(final ChannelHandlerContext ctx, final GetRequest msg) { byte opcode; ByteBuf extras; if (msg.lock()) { opcode = OP_GET_AND_LOCK; extras = ctx.alloc().buffer().writeInt(msg.expiry()); } else if (msg.touch()) { opcode = OP_GET_AND_TOUCH; extras = ctx.alloc().buffer().writeInt(msg.expiry()); } else { opcode = OP_GET; extras = Unpooled.EMPTY_BUFFER; } byte[] key = msg.keyBytes(); short keyLength = (short) key.length; byte extrasLength = (byte) extras.readableBytes(); BinaryMemcacheRequest request = new DefaultBinaryMemcacheRequest(key); request.setOpcode(opcode).setKeyLength(keyLength).setExtras(extras).setExtrasLength(extrasLength) .setTotalBodyLength(keyLength + extrasLength); return request; }
From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandler.java
License:Apache License
/** * Encodes a {@link BinaryStoreRequest} into its lower level representation. * * There are three types of store operations that need to be considered: insert, upsert and replace, which * directly translate to the add, set and replace binary memcached opcodes. By convention, only the replace * command supports setting a CAS value, even if the others theoretically would do as well (but do not provide * benefit in such cases)./* ww w.j a v a 2 s. com*/ * * Currently, the content is loaded and sent down in one batch, streaming for requests is not supported. * * @return a ready {@link BinaryMemcacheRequest}. */ private static BinaryMemcacheRequest handleStoreRequest(final ChannelHandlerContext ctx, final BinaryStoreRequest msg) { ByteBuf extras = ctx.alloc().buffer(8); extras.writeInt(msg.flags()); extras.writeInt(msg.expiration()); byte[] key = msg.keyBytes(); short keyLength = (short) key.length; byte extrasLength = (byte) extras.readableBytes(); FullBinaryMemcacheRequest request = new DefaultFullBinaryMemcacheRequest(key, extras, msg.content()); if (msg instanceof InsertRequest) { request.setOpcode(OP_INSERT); } else if (msg instanceof UpsertRequest) { request.setOpcode(OP_UPSERT); } else if (msg instanceof ReplaceRequest) { request.setOpcode(OP_REPLACE); request.setCAS(((ReplaceRequest) msg).cas()); } else { throw new IllegalArgumentException("Unknown incoming BinaryStoreRequest type " + msg.getClass()); } request.setKeyLength(keyLength); request.setTotalBodyLength(keyLength + msg.content().readableBytes() + extrasLength); request.setExtrasLength(extrasLength); return request; }
From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandler.java
License:Apache License
/** * Encodes a {@link CounterRequest} into its lower level representation. * * Depending on if the {@link CounterRequest#delta} is positive or negative, either the incr or decr memcached * commands are utilized. The value is converted to its absolute variant to conform with the protocol. * * @return a ready {@link BinaryMemcacheRequest}. *//* w ww . j a v a2 s. c o m*/ private static BinaryMemcacheRequest handleCounterRequest(final ChannelHandlerContext ctx, final CounterRequest msg) { ByteBuf extras = ctx.alloc().buffer(); extras.writeLong(Math.abs(msg.delta())); extras.writeLong(msg.initial()); extras.writeInt(msg.expiry()); byte[] key = msg.keyBytes(); short keyLength = (short) key.length; byte extrasLength = (byte) extras.readableBytes(); BinaryMemcacheRequest request = new DefaultBinaryMemcacheRequest(key, extras); request.setOpcode(msg.delta() < 0 ? OP_COUNTER_DECR : OP_COUNTER_INCR); request.setKeyLength(keyLength); request.setTotalBodyLength(keyLength + extrasLength); request.setExtrasLength(extrasLength); return request; }
From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandler.java
License:Apache License
/** * Encodes a {@link TouchRequest} into its lower level representation. * * @return a ready {@link BinaryMemcacheRequest}. */// w ww . j a v a 2 s.c om private static BinaryMemcacheRequest handleTouchRequest(final ChannelHandlerContext ctx, final TouchRequest msg) { ByteBuf extras = ctx.alloc().buffer(); extras.writeInt(msg.expiry()); byte[] key = msg.keyBytes(); short keyLength = (short) key.length; byte extrasLength = (byte) extras.readableBytes(); BinaryMemcacheRequest request = new DefaultBinaryMemcacheRequest(key); request.setExtras(extras); request.setOpcode(OP_TOUCH); request.setKeyLength(keyLength); request.setTotalBodyLength(keyLength + extrasLength); request.setExtrasLength(extrasLength); return request; }
From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandler.java
License:Apache License
/** * Encodes a {@link ObserveRequest} into its lower level representation. * * @return a ready {@link BinaryMemcacheRequest}. */// ww w.j a v a2 s .c o m private static BinaryMemcacheRequest handleObserveRequest(final ChannelHandlerContext ctx, final ObserveRequest msg) { String key = msg.key(); short keyLength = (short) msg.keyBytes().length; ByteBuf content = ctx.alloc().buffer(); content.writeShort(msg.partition()); content.writeShort(keyLength); content.writeBytes(key.getBytes(CHARSET)); BinaryMemcacheRequest request = new DefaultFullBinaryMemcacheRequest(EMPTY_BYTES, Unpooled.EMPTY_BUFFER, content); request.setOpcode(OP_OBSERVE); request.setTotalBodyLength(content.readableBytes()); return request; }
From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandler.java
License:Apache License
private static BinaryMemcacheRequest handleObserveSeqnoRequest(final ChannelHandlerContext ctx, final ObserveSeqnoRequest msg) { ByteBuf content = ctx.alloc().buffer(); content.writeLong(msg.vbucketUUID()); BinaryMemcacheRequest request = new DefaultFullBinaryMemcacheRequest(EMPTY_BYTES, Unpooled.EMPTY_BUFFER, content);//from w w w . j a v a 2 s.co m request.setOpcode(OP_OBSERVE_SEQ); request.setTotalBodyLength(content.readableBytes()); return request; }