List of usage examples for io.netty.buffer ByteBuf nioBuffer
public abstract ByteBuffer nioBuffer();
From source file:com.heliosapm.shorthand.caster.broadcast.BroadcastListenerRouter.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, DatagramPacket msg, List<Object> out) throws Exception { ByteBuf data = msg.content(); byte msgType = data.readByte(); BroadcastType bt = BroadcastType.ORD2ENUM.get((int) msgType); log("Processing Broadcast [%s]", bt.name()); BroadcastExecutable exec = bt.unmarshallPacket(data.nioBuffer(), msg.sender()); taskThreadPool.execute(exec);// w w w. jav a2s .c o m }
From source file:com.hiido.eagle.hes.transfer.FileTransferServer.java
License:Apache License
protected void storeFile(ChannelHandlerContext ctx, Object msg, FileStoreInfo fileStoreInfo) throws Exception { ByteBuf in = (ByteBuf) msg; fileStoreInfo.incrCurrFileSize(in.readableBytes()); fileStoreInfo.getFileChannel().write(in.nioBuffer()); }
From source file:com.ibm.crail.datanode.netty.NettyDataNode.java
License:Apache License
@Override public void run() throws Exception { int entries = (int) (NettyConstants.DATANODE_NETTY_STORAGE_LIMIT / NettyConstants.DATANODE_NETTY_ALLOCATION_SIZE); map = new ConcurrentHashMap<Integer, ByteBuf>(entries); datanode = null;/*from www.jav a2 s .com*/ /* we start with stag 1 and increment it constantly */ stag = 1; LOG.info("Booting with " + entries + " nums of " + NettyConstants.DATANODE_NETTY_ALLOCATION_SIZE + " byte buffers"); /* this manages the netty datanode which processes the client requests */ datanode = new NettyServer(getAddress(), this); datanode.start(); /* now the Namenode Processor communication part */ long allocated = 0; double perc; LOG.info("Allocation started for the target of : " + NettyConstants.DATANODE_NETTY_STORAGE_LIMIT); while (allocated < NettyConstants.DATANODE_NETTY_STORAGE_LIMIT) { /* allocate a new buffer */ ByteBuf buf = directBuffer((int) NettyConstants.DATANODE_NETTY_ALLOCATION_SIZE, (int) NettyConstants.DATANODE_NETTY_ALLOCATION_SIZE); /* retain this buffer */ buf.retain(); Long address = ((DirectBuffer) buf.nioBuffer()).address(); /* update entries */ map.put(this.stag, buf); this.setBlock(address, (int) NettyConstants.DATANODE_NETTY_ALLOCATION_SIZE, this.stag); LOG.info("MAP entry : " + Long.toHexString(address) + " length : " + (int) NettyConstants.DATANODE_NETTY_ALLOCATION_SIZE + " stag : " + this.stag + " refCount: " + buf.refCnt()); /* update counters */ allocated += NettyConstants.DATANODE_NETTY_ALLOCATION_SIZE; perc = allocated * 100 / NettyConstants.DATANODE_NETTY_STORAGE_LIMIT; this.stag++; LOG.info("Allocation done : " + perc + "% , allocated " + allocated + " / " + NettyConstants.DATANODE_NETTY_STORAGE_LIMIT); } while (true) { DataNodeStatistics statistics = this.getDataNode(); LOG.info("Datanode statistics, freeBlocks " + statistics.getFreeBlockCount()); Thread.sleep(2000); } /* now we wait for the other thread */ //datanode.join(); }
From source file:com.ibm.crail.datanode.netty.server.IncomingRequestHandler.java
License:Apache License
private void handleRead(ChannelHandlerContext ctx, RdmaMsgRx incomingRead, ByteBuf srcBuf) { /* for a read, we need to write the buffer */ int offset = (int) (incomingRead.address() - ((DirectBuffer) srcBuf.nioBuffer()).address()); final RdmaMsgTx readResponse = incomingRead.makeTxMsg(0, MessageTypes.READ_RESP); readResponse.referenceTxPayload(srcBuf, offset, readResponse.opLength()); //FIXME:/*from w w w. j a v a 2s . co m*/ assert ctx.channel() == this.channel; synchronized (this.channel) { /* push the header */ this.channel.write(readResponse.getHeaderPayload()); /* then data */ ctx.channel().writeAndFlush(readResponse.getDataPayload().retain()) .addListener(new GenericFutureListener<Future<? super Void>>() { public void operationComplete(Future<? super Void> future) throws Exception { readResponse.releaseHeaderPayload(); readResponse.releaseTxPayload(); } }); } }
From source file:com.ibm.crail.datanode.netty.server.IncomingRequestHandler.java
License:Apache License
private void handleWrite(ChannelHandlerContext ctx, RdmaMsgRx incomingWrite, ByteBuf targetBuf) { /* we make the right offset */ int offset = (int) (incomingWrite.address() - ((DirectBuffer) targetBuf.nioBuffer()).address()); incomingWrite.copyAndReleaseRxPayload(targetBuf.duplicate(), offset); final RdmaMsgTx writeResponse = incomingWrite.makeTxMsg(0, MessageTypes.WRITE_RESP); //FIXME://from ww w . ja v a2 s . c o m assert ctx.channel() == this.channel; synchronized (this.channel) { /* write the header only, there is no payload */ this.channel.writeAndFlush(writeResponse.getHeaderPayload()) .addListener(new GenericFutureListener<Future<? super Void>>() { public void operationComplete(Future<? super Void> future) throws Exception { writeResponse.releaseHeaderPayload(); } }); } }
From source file:com.kazzla.asterisk.netty.MessageDecoder.java
License:Apache License
public void decode(ChannelHandlerContext ctx, ByteBuf b, List<Object> out) throws CodecException { ByteBuffer buffer = b.nioBuffer(); Option<Message> msg = codec.decode(buffer); if (msg instanceof Some) { b.skipBytes(buffer.position());//from w w w .java 2 s.c om out.add(msg); } }
From source file:com.king.platform.net.http.netty.response.HttpClientResponseHandler.java
License:Apache License
public void handleResponse(ChannelHandlerContext ctx, Object msg) throws Exception { HttpRequestContext httpRequestContext = ctx.channel().attr(HttpRequestContext.HTTP_REQUEST_ATTRIBUTE_KEY) .get();//from ww w. j av a 2s. c om if (httpRequestContext == null) { logger.trace("httpRequestContext is null, msg was {}", msg); return; } NettyHttpClientResponse nettyHttpClientResponse = httpRequestContext.getNettyHttpClientResponse(); RequestEventBus requestEventBus = nettyHttpClientResponse.getRequestEventBus(); ResponseBodyConsumer responseBodyConsumer = nettyHttpClientResponse.getResponseBodyConsumer(); try { if (msg instanceof HttpResponse) { requestEventBus.triggerEvent(Event.TOUCH); logger.trace("read HttpResponse"); HttpResponse response = (HttpResponse) msg; HttpResponseStatus httpResponseStatus = response.getStatus(); HttpHeaders httpHeaders = response.headers(); nettyHttpClientResponse.setHttpResponseStatus(httpResponseStatus); nettyHttpClientResponse.setHttpHeaders(httpHeaders); requestEventBus.triggerEvent(Event.onReceivedStatus, httpResponseStatus); requestEventBus.triggerEvent(Event.onReceivedHeaders, httpHeaders); httpRequestContext.getTimeRecorder().readResponseHttpHeaders(); if (httpRequestContext.isFollowRedirects() && httpRedirector.isRedirectResponse(httpResponseStatus)) { httpRedirector.redirectRequest(httpRequestContext, httpHeaders); return; } if (response.getStatus().code() == 100) { requestEventBus.triggerEvent(Event.WRITE_BODY, ctx); return; } String contentLength = httpHeaders.get(HttpHeaders.Names.CONTENT_LENGTH); String contentType = httpHeaders.get(HttpHeaders.Names.CONTENT_TYPE); String charset = StringUtil.substringAfter(contentType, '='); if (charset == null) { charset = StandardCharsets.ISO_8859_1.name(); } contentType = StringUtil.substringBefore(contentType, ';'); if (contentLength != null) { long length = Long.parseLong(contentLength); responseBodyConsumer.onBodyStart(contentType, charset, length); } else { responseBodyConsumer.onBodyStart(contentType, charset, 0); } httpRequestContext.getTimeRecorder().responseBodyStart(); } else if (msg instanceof HttpContent) { logger.trace("read HttpContent"); requestEventBus.triggerEvent(Event.TOUCH); HttpResponseStatus httpResponseStatus = nettyHttpClientResponse.getHttpResponseStatus(); HttpHeaders httpHeaders = nettyHttpClientResponse.getHttpHeaders(); if (httpResponseStatus == null || (httpRequestContext.isFollowRedirects() && httpRedirector.isRedirectResponse(httpResponseStatus))) { return; } if (msg == LastHttpContent.EMPTY_LAST_CONTENT && nettyHttpClientResponse.getHttpResponseStatus().code() == 100) { logger.trace("read EMPTY_LAST_CONTENT with status code 100"); return; } HttpContent chunk = (HttpContent) msg; ByteBuf content = chunk.content(); content.resetReaderIndex(); int readableBytes = content.readableBytes(); if (readableBytes > 0) { ByteBuffer byteBuffer = content.nioBuffer(); responseBodyConsumer.onReceivedContentPart(byteBuffer); requestEventBus.triggerEvent(Event.onReceivedContentPart, readableBytes, content); } content.release(); requestEventBus.triggerEvent(Event.TOUCH); if (chunk instanceof LastHttpContent) { responseBodyConsumer.onCompletedBody(); requestEventBus.triggerEvent(Event.onReceivedCompleted, httpResponseStatus, httpHeaders); httpRequestContext.getTimeRecorder().responseBodyCompleted(); com.king.platform.net.http.HttpResponse httpResponse = new com.king.platform.net.http.HttpResponse( httpResponseStatus.code(), responseBodyConsumer); for (Map.Entry<String, String> entry : httpHeaders.entries()) { httpResponse.addHeader(entry.getKey(), entry.getValue()); } requestEventBus.triggerEvent(Event.onHttpResponseDone, httpResponse); requestEventBus.triggerEvent(Event.COMPLETED, httpRequestContext); } } } catch (Throwable e) { requestEventBus.triggerEvent(Event.ERROR, httpRequestContext, e); } }
From source file:com.lambdaworks.redis.codec.StringCodecTest.java
License:Apache License
@Test public void encodeAndDecodeUtf8Buf() throws Exception { StringCodec codec = new StringCodec(LettuceCharsets.UTF8); ByteBuf buffer = Unpooled.buffer(1234); codec.encodeKey(teststring, buffer); assertThat(codec.decodeKey(buffer.nioBuffer())).isEqualTo(teststring); }
From source file:com.lambdaworks.redis.codec.StringCodecTest.java
License:Apache License
@Test public void encodeAndDecodeAsciiBuf() throws Exception { StringCodec codec = new StringCodec(LettuceCharsets.ASCII); ByteBuf buffer = Unpooled.buffer(1234); codec.encode(teststringPlain, buffer); assertThat(codec.decodeKey(buffer.nioBuffer())).isEqualTo(teststringPlain); }
From source file:com.lambdaworks.redis.codec.StringCodecTest.java
License:Apache License
@Test public void encodeAndDecodeIso88591Buf() throws Exception { StringCodec codec = new StringCodec(StandardCharsets.ISO_8859_1); ByteBuf buffer = Unpooled.buffer(1234); codec.encode(teststringPlain, buffer); assertThat(codec.decodeKey(buffer.nioBuffer())).isEqualTo(teststringPlain); }