Example usage for io.netty.buffer PooledByteBufAllocator DEFAULT

List of usage examples for io.netty.buffer PooledByteBufAllocator DEFAULT

Introduction

In this page you can find the example usage for io.netty.buffer PooledByteBufAllocator DEFAULT.

Prototype

PooledByteBufAllocator DEFAULT

To view the source code for io.netty.buffer PooledByteBufAllocator DEFAULT.

Click Source Link

Usage

From source file:org.opendaylight.protocol.bmp.mock.BmpMockDispatcher.java

License:Open Source License

private ServerBootstrap createServerInstance() {
    final ServerBootstrap serverBootstrap = new ServerBootstrap();
    serverBootstrap.childHandler(new ChannelInitializer<Channel>() {
        @Override//from w ww.  jav a2s  . c  o  m
        protected void initChannel(final Channel ch) throws Exception {
            ch.pipeline().addLast(BmpMockDispatcher.this.sessionFactory.getSession(ch, null));
            ch.pipeline().addLast(BmpMockDispatcher.this.hf.getEncoders());
        }
    });

    serverBootstrap.option(ChannelOption.SO_BACKLOG, MAX_CONNECTIONS_COUNT);
    serverBootstrap.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    serverBootstrap.channel(NioServerSocketChannel.class);
    serverBootstrap.group(bossGroup, workerGroup);
    return serverBootstrap;
}

From source file:org.opendaylight.protocol.framework.AbstractDispatcher.java

License:Open Source License

/**
 * Creates server. Each server needs factories to pass their instances to client sessions.
 *
 * @param address address to which the server should be bound
 * @param channelClass The {@link Class} which is used to create {@link Channel} instances from.
 * @param initializer instance of PipelineInitializer used to initialize the channel pipeline
 *
 * @return ChannelFuture representing the binding process
 *//*  w  w w  . jav a 2 s.c o  m*/
protected <CH extends Channel> ChannelFuture createServer(final SocketAddress address,
        final Class<? extends ServerChannel> channelClass,
        final ChannelPipelineInitializer<CH, S> initializer) {
    final ServerBootstrap b = new ServerBootstrap();
    b.childHandler(new ChannelInitializer<CH>() {

        @Override
        protected void initChannel(final CH ch) {
            initializer.initializeChannel(ch, new DefaultPromise<S>(executor));
        }
    });

    b.option(ChannelOption.SO_BACKLOG, 128);
    if (LocalServerChannel.class.equals(channelClass) == false) {
        // makes no sense for LocalServer and produces warning
        b.childOption(ChannelOption.SO_KEEPALIVE, true);
        b.childOption(ChannelOption.TCP_NODELAY, true);
    }
    b.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    customizeBootstrap(b);

    if (b.group() == null) {
        b.group(bossGroup, workerGroup);
    }
    try {
        b.channel(channelClass);
    } catch (IllegalStateException e) {
        // FIXME: if this is ok, document why
        LOG.trace("Not overriding channelFactory on bootstrap {}", b, e);
    }

    // Bind and start to accept incoming connections.
    final ChannelFuture f = b.bind(address);
    LOG.debug("Initiated server {} at {}.", f, address);
    return f;
}

From source file:org.opendaylight.protocol.pcep.impl.PCEPDispatcherImpl.java

License:Open Source License

protected ServerBootstrap createServerBootstrap(final ChannelPipelineInitializer initializer) {
    final ServerBootstrap b = new ServerBootstrap();
    b.childHandler(new ChannelInitializer<SocketChannel>() {
        @Override/*from   w  w w .  java2  s. c  o  m*/
        protected void initChannel(final SocketChannel ch) {
            initializer.initializeChannel(ch, new DefaultPromise(PCEPDispatcherImpl.this.executor));
        }
    });
    b.option(ChannelOption.SO_BACKLOG, SOCKET_BACKLOG_SIZE);

    b.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);

    if (Epoll.isAvailable()) {
        b.channel(EpollServerSocketChannel.class);
        b.childOption(EpollChannelOption.EPOLL_MODE, EpollMode.LEVEL_TRIGGERED);
    } else {
        b.channel(NioServerSocketChannel.class);
    }
    if (this.keys.isPresent()) {
        if (Epoll.isAvailable()) {
            b.option(EpollChannelOption.TCP_MD5SIG, this.keys.get());
        } else {
            throw new UnsupportedOperationException(Epoll.unavailabilityCause().getCause());
        }
    }

    // Make sure we are doing round-robin processing
    b.childOption(ChannelOption.MAX_MESSAGES_PER_READ, 1);

    if (b.group() == null) {
        b.group(this.bossGroup, this.workerGroup);
    }

    return b;
}

From source file:org.opendaylight.sxp.core.messaging.MessageFactory.java

License:Open Source License

/**
 * Generate Message header according message type and create ByteBuff,
 * that includes generated header and provided payload
 *
 * @param messageType Type of message header to be generated
 * @param payload     Data to be included into Message
 * @return ByteBuf representation of message
 */// w  w w  .j  a  va 2  s. co m
protected static ByteBuf getMessage(MessageType messageType, byte[] payload) {
    byte[] header = getMessageHeader(messageType, payload.length);
    int messageLength = header.length + payload.length;
    ByteBuf message = PooledByteBufAllocator.DEFAULT.buffer(messageLength, messageLength);
    message.writeBytes(header);
    message.writeBytes(payload);
    return message;
}

From source file:org.opendaylight.sxp.core.messaging.MessageFactoryTest.java

License:Open Source License

@Test
public void testParseOpen() throws Exception {
    byte[] msg = new byte[] { 0, 0, 0, 32, 0, 0, 0, 1, 0, 0, 0, 4, 0, 0, 0, 2, 80, 6, 6, 3, 0, 2, 0, 1, 0, 80,
            7, 4, 0, 120, 0, -106 };//from www .  j  ava  2s .co m
    ByteBuf message = PooledByteBufAllocator.DEFAULT.buffer(msg.length);
    message.writeBytes(msg);
    Notification notification = MessageFactory.parse(Version.Version4, message);
    message.release();
    assertTrue(notification instanceof OpenMessage);
    assertEquals(MessageType.Open, ((OpenMessage) notification).getType());
    msg = new byte[] { 0, 0, 0, 32, 0, 0, 0, 1, 0, 0, 0, 3, 0, 0, 0, 2, 80, 6, 6, 3, 0, 2, 0, 1, 0, 80, 7, 4, 0,
            120, 0, -106 };
    message = PooledByteBufAllocator.DEFAULT.buffer(msg.length);
    message.writeBytes(msg);
    notification = MessageFactory.parse(Version.Version4, message);
    message.release();
    assertTrue(notification instanceof OpenMessageLegacy);
    assertEquals(MessageType.Open, ((OpenMessageLegacy) notification).getType());

}

From source file:org.opendaylight.sxp.core.messaging.MessageFactoryTest.java

License:Open Source License

@Test
public void testParseResp() throws Exception {
    byte[] msg = new byte[] { 0, 0, 0, 32, 0, 0, 0, 2, 0, 0, 0, 4, 0, 0, 0, 2, 80, 6, 6, 3, 0, 2, 0, 1, 0, 80,
            7, 4, 0, 120, 0, -106 };// w w  w.  jav a 2  s .c  o m
    ByteBuf message = PooledByteBufAllocator.DEFAULT.buffer(msg.length);
    message.writeBytes(msg);
    Notification notification = MessageFactory.parse(Version.Version4, message);
    message.release();
    assertTrue(notification instanceof OpenMessage);
    assertEquals(MessageType.OpenResp, ((OpenMessage) notification).getType());
    msg = new byte[] { 0, 0, 0, 32, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 2, 80, 6, 6, 3, 0, 2, 0, 1, 0, 80, 7, 4, 0,
            120, 0, -106 };
    message = PooledByteBufAllocator.DEFAULT.buffer(msg.length);
    message.writeBytes(msg);
    notification = MessageFactory.parse(Version.Version3, message);
    message.release();
    assertTrue(notification instanceof OpenMessageLegacy);
    assertEquals(MessageType.OpenResp, ((OpenMessageLegacy) notification).getType());

}

From source file:org.opendaylight.sxp.core.messaging.MessageFactoryTest.java

License:Open Source License

@Test
public void testParseUpdate() throws Exception {
    byte[] msg = new byte[] { 0, 0, 0, 68, 0, 0, 0, 3, 16, 13, 5, 32, -64, -88, 0, 1, 16, 14, 17, -128, 32, 2,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 16, 16, 4, -64, -88, 0, 1, 16, 17, 2, 78, 32, 16, 11, 5,
            30, 10, 10, 10, 10, 16, 12, 9, 64, 32, 1, 0, 0, 0, 0, 0, 0 };
    ByteBuf message = PooledByteBufAllocator.DEFAULT.buffer(msg.length);
    message.writeBytes(msg);//from  ww w . j a  v  a  2  s.c o  m
    Notification notification = MessageFactory.parse(Version.Version4, message);
    message.release();
    assertTrue(notification instanceof UpdateMessage);
    assertEquals(MessageType.Update, ((UpdateMessage) notification).getType());
    msg = new byte[] { 0, 0, 0, 51, 0, 0, 0, 3, 0, 0, 0, 1, 0, 0, 0, 14, -64, -88, 0, 1, 0, 0, 0, 1, 0, 0, 0, 2,
            39, 16, 0, 0, 0, 3, 0, 0, 0, 13, -64, -88, 0, 2, 0, 0, 0, 2, 0, 0, 0, 1, 32 };
    message = PooledByteBufAllocator.DEFAULT.buffer(msg.length);
    message.writeBytes(msg);
    notification = MessageFactory.parse(Version.Version3, message);
    assertTrue(notification instanceof UpdateMessageLegacy);
    assertEquals(MessageType.Update, ((UpdateMessageLegacy) notification).getType());

}

From source file:org.opendaylight.sxp.core.messaging.MessageFactoryTest.java

License:Open Source License

@Test
public void testParseError() throws Exception {
    byte[] msg = new byte[] { 0, 0, 0, 12, 0, 0, 0, 4, -125, 3, 0, 0 };
    ByteBuf message = PooledByteBufAllocator.DEFAULT.buffer(msg.length);
    message.writeBytes(msg);//from   w  ww .  j  a  v a 2s  .  c  o  m
    Notification notification = MessageFactory.parse(Version.Version4, message);
    message.release();
    assertTrue(notification instanceof ErrorMessage);
    assertEquals(MessageType.Error, ((ErrorMessage) notification).getType());
    msg = new byte[] { 0, 0, 0, 12, 0, 0, 0, 4, -125, 3, 0, 0 };
    message = PooledByteBufAllocator.DEFAULT.buffer(msg.length);
    message.writeBytes(msg);
    notification = MessageFactory.parse(Version.Version3, message);
    message.release();
    assertTrue(notification instanceof ErrorMessage);
    assertEquals(MessageType.Error, ((ErrorMessage) notification).getType());
}

From source file:org.opendaylight.sxp.core.messaging.MessageFactoryTest.java

License:Open Source License

@Test
public void testParsePurgeAll() throws Exception {
    byte[] msg = new byte[] { 0, 0, 0, 8, 0, 0, 0, 5 };
    ByteBuf message = PooledByteBufAllocator.DEFAULT.buffer(msg.length);
    message.writeBytes(msg);/* w  w w .  j  ava2  s.c o m*/
    Notification notification = MessageFactory.parse(Version.Version4, message);
    message.release();
    assertTrue(notification instanceof PurgeAllMessage);
    assertEquals(MessageType.PurgeAll, ((PurgeAllMessage) notification).getType());
    msg = new byte[] { 0, 0, 0, 8, 0, 0, 0, 5 };
    message = PooledByteBufAllocator.DEFAULT.buffer(msg.length);
    message.writeBytes(msg);
    notification = MessageFactory.parse(Version.Version3, message);
    message.release();
    assertTrue(notification instanceof PurgeAllMessage);
    assertEquals(MessageType.PurgeAll, ((PurgeAllMessage) notification).getType());
}

From source file:org.opendaylight.sxp.core.messaging.MessageFactoryTest.java

License:Open Source License

@Test
public void testParse() throws Exception {
    //KEEPALIVE//from   w w w  .  j  ava 2  s .  c  om
    byte[] msg = new byte[] { 0, 0, 0, 8, 0, 0, 0, 6 };
    ByteBuf message = PooledByteBufAllocator.DEFAULT.buffer(msg.length);
    message.writeBytes(msg);
    Notification notification = MessageFactory.parse(Version.Version4, message);
    message.release();
    assertTrue(notification instanceof KeepaliveMessage);
    assertEquals(MessageType.Keepalive, ((KeepaliveMessage) notification).getType());
}