Example usage for io.netty.buffer Unpooled copiedBuffer

List of usage examples for io.netty.buffer Unpooled copiedBuffer

Introduction

In this page you can find the example usage for io.netty.buffer Unpooled copiedBuffer.

Prototype

private static ByteBuf copiedBuffer(CharBuffer buffer, Charset charset) 

Source Link

Usage

From source file:com.vela.iot.active.netty.coap.CoAPClient.java

License:Apache License

public static void main(String[] args) throws Exception {
    int bossCount = 1;
    if (args.length > 3) {
        bossCount = Integer.parseInt(args[3]);
    }//from   ww  w. j  a  v a2 s .  c o  m
    int bufSize = 655350;
    if (args.length > 4) {
        bufSize = Integer.parseInt(args[4]);
    }
    EventLoopGroup group = new NioEventLoopGroup(bossCount);
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioDatagramChannel.class).option(ChannelOption.SO_RCVBUF, bufSize)
                .option(ChannelOption.SO_SNDBUF, bufSize).handler(new CoAPClientHandler());
        Channel ch = b.bind(0).sync().channel();
        String ip = "192.168.2.185";
        if (args.length > 0) {
            ip = args[0];
        }
        if (args.length > 1) {
            REQ_NUM = Integer.parseInt(args[1]);
        }
        int concNum = 10;
        if (args.length > 2) {
            concNum = Integer.parseInt(args[2]);
        }
        InetSocketAddress add = new InetSocketAddress(ip, PORT);

        long start = System.nanoTime();
        ch.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer("start", CharsetUtil.UTF_8), add));
        for (int i = 0; i < REQ_NUM; i++) {
            ch.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer(
                    "auth/gw/active?devSn=wZud4fM6SUuvvvBoFyGNYw&devKey=8I8LLGb7QaOZw6wgYInDrQ&devInfo=" + i,
                    CharsetUtil.UTF_8), add));
            if (i % concNum == 0)
                Thread.sleep(0, 1);
        }
        ch.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer("end", CharsetUtil.UTF_8), add));
        // LOGGER.info(":{},:{}", REQ_NUM, System.nanoTime() -
        // start);
        System.out.printf(":%d,:%d", REQ_NUM, System.nanoTime() - start);
        if (!ch.closeFuture().await(5000)) {
            System.err.println("QOTM request timed out.");
        }
    } finally {
        group.shutdownGracefully();
    }
}

From source file:com.whizzosoftware.hobson.api.plugin.channel.ChannelIdleDetectionConfig.java

License:Open Source License

/**
 * Constructor./*ww  w  .  j a  va 2 s .c  om*/
 *
 * @param maxIdleTime the maximum number of seconds a channel can be idle before a heartbeat is sent
 * @param heartbeatFrame the heartbeat frame to send when a channel is considered idle
 */
public ChannelIdleDetectionConfig(int maxIdleTime, String heartbeatFrame) {
    this.maxIdleTime = maxIdleTime;
    this.heartbeatFrame = Unpooled.unreleasableBuffer(Unpooled.copiedBuffer(heartbeatFrame, CharsetUtil.UTF_8));
}

From source file:com.whizzosoftware.hobson.dsc.api.codec.DSCFrameDecoder.java

License:Open Source License

public DSCFrameDecoder() {
    super(MAX_FRAME_LENGTH, Unpooled.copiedBuffer("\n", CharsetUtil.UTF_8));
}

From source file:com.whizzosoftware.hobson.radiora.api.codec.RadioRaFrameDecoder.java

License:Open Source License

public RadioRaFrameDecoder() {
    super(MAX_FRAME_LENGTH, Unpooled.copiedBuffer(FRAME_DELIMETER, CharsetUtil.UTF_8));
}

From source file:com.whizzosoftware.hobson.radiora.api.codec.RadioRaFrameDecoderTest.java

License:Open Source License

@Test
public void testZoneMapDecode() throws Exception {
    // test valid zone map
    ByteBuf readData = Unpooled.copiedBuffer("ZMP,000000000000000000000XXXXXXXXXXX\r", Charset.forName("UTF8"));
    RadioRaFrameDecoder decoder = new RadioRaFrameDecoder();
    Object cmd = decoder.decode(null, readData);
    assertTrue(cmd instanceof ZoneMap);
    assertEquals("000000000000000000000XXXXXXXXXXX", ((ZoneMap) cmd).getZoneMap());
    assertNull(((ZoneMap) cmd).getSystem());

    // test no comma after zone map
    readData = Unpooled.copiedBuffer("ZMP\r", Charset.forName("UTF8"));
    decoder = new RadioRaFrameDecoder();
    try {/*from   www.ja va2  s .  com*/
        decoder.decode(null, readData);
        fail("Should have thrown exception");
    } catch (Exception e) {
        assertTrue(e instanceof CorruptedFrameException);
    }

    // test invalid length zone map
    readData = Unpooled.copiedBuffer("ZMP,0000000000000\r", Charset.forName("UTF8"));
    decoder = new RadioRaFrameDecoder();
    try {
        decoder.decode(null, readData);
        fail("Should have thrown exception");
    } catch (Exception e) {
        assertTrue(e instanceof CorruptedFrameException);
    }
}

From source file:com.whizzosoftware.hobson.radiora.api.codec.RadioRaFrameDecoderTest.java

License:Open Source License

@Test
public void testLocalZoneChangeDecode() throws Exception {
    // test valid ON
    ByteBuf readData = Unpooled.copiedBuffer("LZC,01,ON \r", Charset.forName("UTF8"));
    RadioRaFrameDecoder decoder = new RadioRaFrameDecoder();
    Object cmd = decoder.decode(null, readData);
    assertTrue(cmd instanceof LocalZoneChange);
    assertEquals(1, ((LocalZoneChange) cmd).getZoneNumber());
    assertEquals(LocalZoneChange.State.ON, ((LocalZoneChange) cmd).getState());

    // test valid OFF
    readData = Unpooled.copiedBuffer("LZC,10,OFF\r", Charset.forName("UTF8"));
    cmd = decoder.decode(null, readData);
    assertTrue(cmd instanceof LocalZoneChange);
    assertEquals(10, ((LocalZoneChange) cmd).getZoneNumber());
    assertEquals(LocalZoneChange.State.OFF, ((LocalZoneChange) cmd).getState());

    // test valid CHG
    readData = Unpooled.copiedBuffer("LZC,21,CHG\r", Charset.forName("UTF8"));
    cmd = decoder.decode(null, readData);
    assertTrue(cmd instanceof LocalZoneChange);
    assertEquals(21, ((LocalZoneChange) cmd).getZoneNumber());
    assertEquals(LocalZoneChange.State.CHG, ((LocalZoneChange) cmd).getState());

    // test invalid frame (no comma)
    readData = Unpooled.copiedBuffer("LZC\r", Charset.forName("UTF8"));
    try {//from   ww  w.j av  a  2  s. c o m
        decoder.decode(null, readData);
        fail("Should have thrown exception");
    } catch (Exception e) {
        assertTrue(e instanceof CorruptedFrameException);
    }

    // test invalid frame (no state)
    readData = Unpooled.copiedBuffer("LZC,1\r", Charset.forName("UTF8"));
    try {
        decoder.decode(null, readData);
        fail("Should have thrown exception");
    } catch (Exception e) {
        assertTrue(e instanceof CorruptedFrameException);
    }

    // test invalid frame (invalid state)
    readData = Unpooled.copiedBuffer("LZC,1,O\r", Charset.forName("UTF8"));
    try {
        decoder.decode(null, readData);
        fail("Should have thrown exception");
    } catch (Exception e) {
        assertTrue(e instanceof CorruptedFrameException);
    }
}

From source file:com.whizzosoftware.hobson.radiora.api.codec.RadioRaFrameDecoderTest.java

License:Open Source License

@Test
public void testLEDMapDecode() throws Exception {
    RadioRaFrameDecoder decoder = new RadioRaFrameDecoder();

    // test valid frame
    ByteBuf readData = Unpooled.copiedBuffer("LMP,000000000000000\r", Charset.forName("UTF8"));
    Object cmd = decoder.decode(null, readData);
    assertTrue(cmd instanceof LEDMap);
    assertEquals("000000000000000", ((LEDMap) cmd).getStates());

    // test invalid frame (no comma)
    readData = Unpooled.copiedBuffer("LMP\r", Charset.forName("UTF8"));
    try {/*from   w  w w  .  ja  va  2s.co m*/
        decoder.decode(null, readData);
        fail("Should have thrown exception");
    } catch (Exception e) {
        assertTrue(e instanceof CorruptedFrameException);
    }

    // test invalid frame (map size)
    readData = Unpooled.copiedBuffer("LMP,000\r", Charset.forName("UTF8"));
    try {
        decoder.decode(null, readData);
        fail("Should have thrown exception");
    } catch (Exception e) {
        assertTrue(e instanceof CorruptedFrameException);
    }
}

From source file:com.whizzosoftware.hobson.radiora.api.codec.RadioRaFrameDecoderTest.java

License:Open Source License

@Test
public void testExclamationPoint() throws Exception {
    ByteBuf readData = Unpooled.copiedBuffer("!\r", CharsetUtil.UTF_8);
    RadioRaFrameDecoder decoder = new RadioRaFrameDecoder();
    Object cmd = decoder.decode(null, readData);
    assertNull(cmd);/*from  w w w .  j  a v a 2  s . c  o m*/
}

From source file:com.whizzosoftware.hobson.radiora.api.codec.RadioRaFrameDecoderTest.java

License:Open Source License

@Test
public void testUnknownCommand() throws Exception {
    RadioRaFrameDecoder decoder = new RadioRaFrameDecoder();

    // typical frame structure
    ByteBuf readData = Unpooled.copiedBuffer("BOO,000000000000000000000XXXXXXXXXXX\r", Charset.forName("UTF8"));
    try {//from  ww w. ja v  a2  s.c  o  m
        decoder.decode(null, readData);
        fail("Should have thrown exception");
    } catch (Exception e) {
        assertTrue(e instanceof DecoderException);
    }

    // other frame structure
    readData = Unpooled.copiedBuffer("BO\r", Charset.forName("UTF8"));
    try {
        decoder.decode(null, readData);
        fail("Should have thrown exception");
    } catch (Exception e) {
        assertTrue(e instanceof CorruptedFrameException);
    }
}

From source file:com.wx3.galacdecks.networking.HttpHandler.java

License:Open Source License

private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {
    if (res.getStatus().code() != 200) {
        ByteBuf buf = Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8);
        res.content().writeBytes(buf);/*from  w  w  w . j a  v  a 2 s.  c  o  m*/
        buf.release();
        HttpHeaders.setContentLength(res, res.content().readableBytes());
    }

    // Send the response and close the connection if necessary.
    ChannelFuture f = ctx.channel().writeAndFlush(res);
    if (!HttpHeaders.isKeepAlive(req) || res.getStatus().code() != 200) {
        f.addListener(ChannelFutureListener.CLOSE);
    }
}