List of usage examples for io.netty.buffer Unpooled buffer
public static ByteBuf buffer(int initialCapacity)
From source file:com.xxx.util.io.client.netty.EchoClientHandler.java
License:Apache License
/** * ?/*from www . j a v a 2s . c om*/ */ public void startHeartBeatThread(final ChannelHandlerContext ctx) { final Timer heartBeatTimer = new Timer(); TimerTask heartBeatTask = new TimerTask() { @Override public void run() { if (!ctx.channel().isWritable()) { heartBeatTimer.cancel(); logger.info("Socket???"); return; } logger.info("Socket??"); if (headSize == EchoClient.HEAD_SIZE_8) { ByteBuf firstMessage = Unpooled.buffer(EchoClient.SIZE); firstMessage.writeBytes("00000000".getBytes()); ctx.writeAndFlush(firstMessage); } else if (headSize == EchoClient.HEAD_SIZE_6) { ByteBuf firstMessage = Unpooled.buffer(EchoClient.SIZE); firstMessage.writeBytes("000000".getBytes()); ctx.writeAndFlush(firstMessage); } } }; heartBeatTimer.schedule(heartBeatTask, 5000, 5000); }
From source file:com.yahoo.pulsar.broker.service.ServerCnxTest.java
License:Apache License
@Test(timeOut = 30000) public void testSendCommand() throws Exception { resetChannel();/*w w w. j a v a2s.c o m*/ setChannelConnected(); ByteBuf clientCommand = Commands.newProducer(successTopicName, 1 /* producer id */, 1 /* request id */, "prod-name"); channel.writeInbound(clientCommand); assertTrue(getResponse() instanceof CommandProducerSuccess); // test SEND success MessageMetadata messageMetadata = MessageMetadata.newBuilder().setPublishTime(System.currentTimeMillis()) .setProducerName("prod-name").setSequenceId(0).build(); ByteBuf data = Unpooled.buffer(1024); clientCommand = Commands.newSend(1, 0, 1, ChecksumType.None, messageMetadata, data); channel.writeInbound(Unpooled.copiedBuffer(clientCommand)); clientCommand.release(); assertTrue(getResponse() instanceof CommandSendReceipt); channel.finish(); }
From source file:com.yahoo.pulsar.common.compression.CommandsTest.java
License:Apache License
@Test public void testChecksumSendCommand() throws Exception { // test checksum in send command String producerName = "prod-name"; int sequenceId = 0; ByteBuf data = Unpooled.buffer(1024); MessageMetadata messageMetadata = MessageMetadata.newBuilder().setPublishTime(System.currentTimeMillis()) .setProducerName(producerName).setSequenceId(sequenceId).build(); int expectedChecksum = computeChecksum(messageMetadata, data); ByteBuf clientCommand = Commands.newSend(1, 0, 1, ChecksumType.Crc32c, messageMetadata, data); clientCommand.retain();/*w w w .j a v a 2s . co m*/ ByteBuffer inputBytes = clientCommand.nioBuffer(); ByteBuf receivedBuf = Unpooled.wrappedBuffer(inputBytes); receivedBuf.skipBytes(4); //skip [total-size] int cmdSize = (int) receivedBuf.readUnsignedInt(); receivedBuf.readerIndex(8 + cmdSize); int startMessagePos = receivedBuf.readerIndex(); /*** 1. verify checksum and metadataParsing ***/ boolean hasChecksum = Commands.hasChecksum(receivedBuf); int checksum = Commands.readChecksum(receivedBuf).intValue(); // verify checksum is present assertTrue(hasChecksum); // verify checksum value assertEquals(expectedChecksum, checksum); MessageMetadata metadata = Commands.parseMessageMetadata(receivedBuf); // verify metadata parsing assertEquals(metadata.getProducerName(), producerName); /** 2. parseMessageMetadata should skip checksum if present **/ receivedBuf.readerIndex(startMessagePos); metadata = Commands.parseMessageMetadata(receivedBuf); // verify metadata parsing assertEquals(metadata.getProducerName(), producerName); }
From source file:com.yahoo.pulsar.utils.StatsOutputStreamTest.java
License:Apache License
@BeforeMethod public void reset() { buf = Unpooled.buffer(4096); stream = new StatsOutputStream(buf); }
From source file:com.zaradai.distributor.messaging.netty.handler.MessageDecoderTest.java
License:Apache License
ByteBuf createValidBuffer() {
return Unpooled.buffer(1024);
}
From source file:com.zaradai.distributor.messaging.netty.InetSocketAddressSerializerTest.java
License:Apache License
@Test public void shouldSerializeAndDeserialize() throws Exception { InetSocketAddress test = new InetSocketAddress(TEST_PORT); ByteBuf buffer = Unpooled.buffer(64); // serialize/* w w w .ja va2 s . c o m*/ InetSocketAddressSerializer.serialize(test, buffer); // now deserialize InetSocketAddress res = InetSocketAddressSerializer.deserialize(buffer); assertThat(res, is(test)); }
From source file:com.zhaopeng.timeserver.netty.stick.correct.TimeClientHandler.java
License:Apache License
public void channelActive(ChannelHandlerContext ctx) { ByteBuf message = null;/*from w w w.ja v a2s .c o m*/ for (int i = 0; i < 100; i++) { message = Unpooled.buffer(req.length); message.writeBytes(req); ctx.writeAndFlush(message); } }
From source file:core.communication.threadpool.io.CallServerHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) { ByteBuf reqBuf = (ByteBuf) msg;/* w w w .j a v a2 s . c om*/ ByteBufWrapper reqBufWrapper = new ByteBufWrapper(reqBuf); if (reqBufWrapper.readLong() == ServiceConstants.VERSION) { //?->request ServerStream server = stream.createServer(); MessageRequest request = server.analyzeRequestBytes(reqBufWrapper); //?->response Handler serverHandler = HandlerFactory.createServerHandler(); MessageResponse response = serverHandler.handlerMessage(request); ByteBuf respBuf = Unpooled.buffer(CallClient.SIZE); ByteBufWrapper respBufWrapper = new ByteBufWrapper(respBuf); server.buildResponseBytes(response, respBufWrapper); ctx.writeAndFlush(respBufWrapper.getBuffer()); } }
From source file:de.gandev.modjn.entity.func.ModbusError.java
License:Apache License
@Override public ByteBuf encode() { ByteBuf buf = Unpooled.buffer(calculateLength()); buf.writeByte(getFunctionCode()); buf.writeByte(exceptionCode); return buf; }
From source file:de.gandev.modjn.entity.func.response.ReadCoilsResponse.java
License:Apache License
@Override public ByteBuf encode() { ByteBuf buf = Unpooled.buffer(calculateLength()); buf.writeByte(getFunctionCode());//from w ww .ja v a 2 s . co m buf.writeByte(byteCount); buf.writeBytes(coilStatus.toByteArray()); return buf; }