Search in sources :

Example 41 with ConnectionlessBootstrap

use of org.jboss.netty.bootstrap.ConnectionlessBootstrap in project traccar by tananaev.

the class VtfmsProtocol method initTrackerServers.

@Override
public void initTrackerServers(List<TrackerServer> serverList) {
    serverList.add(new TrackerServer(new ServerBootstrap(), getName()) {

        @Override
        protected void addSpecificHandlers(ChannelPipeline pipeline) {
            pipeline.addLast("frameDecoder", new VtfmsFrameDecoder());
            pipeline.addLast("stringDecoder", new StringDecoder());
            pipeline.addLast("objectDecoder", new VtfmsProtocolDecoder(VtfmsProtocol.this));
        }
    });
    serverList.add(new TrackerServer(new ConnectionlessBootstrap(), getName()) {

        @Override
        protected void addSpecificHandlers(ChannelPipeline pipeline) {
            pipeline.addLast("stringDecoder", new StringDecoder());
            pipeline.addLast("objectDecoder", new VtfmsProtocolDecoder(VtfmsProtocol.this));
        }
    });
}
Also used : StringDecoder(org.jboss.netty.handler.codec.string.StringDecoder) TrackerServer(org.traccar.TrackerServer) ServerBootstrap(org.jboss.netty.bootstrap.ServerBootstrap) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) ConnectionlessBootstrap(org.jboss.netty.bootstrap.ConnectionlessBootstrap)

Example 42 with ConnectionlessBootstrap

use of org.jboss.netty.bootstrap.ConnectionlessBootstrap in project traccar by tananaev.

the class AstraProtocol method initTrackerServers.

@Override
public void initTrackerServers(List<TrackerServer> serverList) {
    serverList.add(new TrackerServer(new ServerBootstrap(), getName()) {

        @Override
        protected void addSpecificHandlers(ChannelPipeline pipeline) {
            pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(1024, 1, 2, -3, 0));
            pipeline.addLast("objectDecoder", new AstraProtocolDecoder(AstraProtocol.this));
        }
    });
    serverList.add(new TrackerServer(new ConnectionlessBootstrap(), getName()) {

        @Override
        protected void addSpecificHandlers(ChannelPipeline pipeline) {
            pipeline.addLast("objectDecoder", new AstraProtocolDecoder(AstraProtocol.this));
        }
    });
}
Also used : TrackerServer(org.traccar.TrackerServer) LengthFieldBasedFrameDecoder(org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder) ServerBootstrap(org.jboss.netty.bootstrap.ServerBootstrap) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) ConnectionlessBootstrap(org.jboss.netty.bootstrap.ConnectionlessBootstrap)

Example 43 with ConnectionlessBootstrap

use of org.jboss.netty.bootstrap.ConnectionlessBootstrap in project camel by apache.

the class NettyProducer method openConnection.

protected ChannelFuture openConnection() throws Exception {
    ChannelFuture answer;
    if (isTcp()) {
        // its okay to create a new bootstrap for each new channel
        ClientBootstrap clientBootstrap = new ClientBootstrap(channelFactory);
        clientBootstrap.setOption("keepAlive", configuration.isKeepAlive());
        clientBootstrap.setOption("tcpNoDelay", configuration.isTcpNoDelay());
        clientBootstrap.setOption("reuseAddress", configuration.isReuseAddress());
        clientBootstrap.setOption("connectTimeoutMillis", configuration.getConnectTimeout());
        // set any additional netty options
        if (configuration.getOptions() != null) {
            for (Map.Entry<String, Object> entry : configuration.getOptions().entrySet()) {
                clientBootstrap.setOption(entry.getKey(), entry.getValue());
            }
        }
        // set the pipeline factory, which creates the pipeline for each newly created channels
        clientBootstrap.setPipelineFactory(pipelineFactory);
        answer = clientBootstrap.connect(new InetSocketAddress(configuration.getHost(), configuration.getPort()));
        if (LOG.isDebugEnabled()) {
            LOG.debug("Created new TCP client bootstrap connecting to {}:{} with options: {}", new Object[] { configuration.getHost(), configuration.getPort(), clientBootstrap.getOptions() });
        }
        return answer;
    } else {
        // its okay to create a new bootstrap for each new channel
        ConnectionlessBootstrap connectionlessClientBootstrap = new ConnectionlessBootstrap(datagramChannelFactory);
        connectionlessClientBootstrap.setOption("child.keepAlive", configuration.isKeepAlive());
        connectionlessClientBootstrap.setOption("child.tcpNoDelay", configuration.isTcpNoDelay());
        connectionlessClientBootstrap.setOption("child.reuseAddress", configuration.isReuseAddress());
        connectionlessClientBootstrap.setOption("child.connectTimeoutMillis", configuration.getConnectTimeout());
        connectionlessClientBootstrap.setOption("child.broadcast", configuration.isBroadcast());
        connectionlessClientBootstrap.setOption("sendBufferSize", configuration.getSendBufferSize());
        connectionlessClientBootstrap.setOption("receiveBufferSize", configuration.getReceiveBufferSize());
        // set any additional netty options
        if (configuration.getOptions() != null) {
            for (Map.Entry<String, Object> entry : configuration.getOptions().entrySet()) {
                connectionlessClientBootstrap.setOption(entry.getKey(), entry.getValue());
            }
        }
        // set the pipeline factory, which creates the pipeline for each newly created channels
        connectionlessClientBootstrap.setPipelineFactory(pipelineFactory);
        // if no one is listen on the port
        if (!configuration.isUdpConnectionlessSending()) {
            answer = connectionlessClientBootstrap.connect(new InetSocketAddress(configuration.getHost(), configuration.getPort()));
        } else {
            // bind and store channel so we can close it when stopping
            Channel channel = connectionlessClientBootstrap.bind(new InetSocketAddress(0));
            allChannels.add(channel);
            answer = new SucceededChannelFuture(channel);
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Created new UDP client bootstrap connecting to {}:{} with options: {}", new Object[] { configuration.getHost(), configuration.getPort(), connectionlessClientBootstrap.getOptions() });
        }
        return answer;
    }
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) SucceededChannelFuture(org.jboss.netty.channel.SucceededChannelFuture) SucceededChannelFuture(org.jboss.netty.channel.SucceededChannelFuture) ClientBootstrap(org.jboss.netty.bootstrap.ClientBootstrap) InetSocketAddress(java.net.InetSocketAddress) Channel(org.jboss.netty.channel.Channel) Map(java.util.Map) ConnectionlessBootstrap(org.jboss.netty.bootstrap.ConnectionlessBootstrap)

Example 44 with ConnectionlessBootstrap

use of org.jboss.netty.bootstrap.ConnectionlessBootstrap in project camel by apache.

the class SingleUDPNettyServerBootstrapFactory method startServerBootstrap.

protected void startServerBootstrap() throws Exception {
    // create non-shared worker pool
    int count = configuration.getWorkerCount() > 0 ? configuration.getWorkerCount() : NettyHelper.DEFAULT_IO_THREADS;
    workerPool = new NioDatagramWorkerPool(Executors.newCachedThreadPool(), count);
    datagramChannelFactory = new NioDatagramChannelFactory(workerPool);
    connectionlessBootstrap = new ConnectionlessBootstrap(datagramChannelFactory);
    connectionlessBootstrap.setOption("child.keepAlive", configuration.isKeepAlive());
    connectionlessBootstrap.setOption("child.tcpNoDelay", configuration.isTcpNoDelay());
    connectionlessBootstrap.setOption("reuseAddress", configuration.isReuseAddress());
    connectionlessBootstrap.setOption("child.reuseAddress", configuration.isReuseAddress());
    connectionlessBootstrap.setOption("child.connectTimeoutMillis", configuration.getConnectTimeout());
    connectionlessBootstrap.setOption("child.broadcast", configuration.isBroadcast());
    connectionlessBootstrap.setOption("sendBufferSize", configuration.getSendBufferSize());
    connectionlessBootstrap.setOption("receiveBufferSize", configuration.getReceiveBufferSize());
    // only set this if user has specified
    if (configuration.getReceiveBufferSizePredictor() > 0) {
        connectionlessBootstrap.setOption("receiveBufferSizePredictorFactory", new FixedReceiveBufferSizePredictorFactory(configuration.getReceiveBufferSizePredictor()));
    }
    if (configuration.getBacklog() > 0) {
        connectionlessBootstrap.setOption("backlog", configuration.getBacklog());
    }
    // set any additional netty options
    if (configuration.getOptions() != null) {
        for (Map.Entry<String, Object> entry : configuration.getOptions().entrySet()) {
            connectionlessBootstrap.setOption(entry.getKey(), entry.getValue());
        }
    }
    LOG.debug("Created ConnectionlessBootstrap {} with options: {}", connectionlessBootstrap, connectionlessBootstrap.getOptions());
    // set the pipeline factory, which creates the pipeline for each newly created channels
    connectionlessBootstrap.setPipelineFactory(pipelineFactory);
    InetSocketAddress hostAddress = new InetSocketAddress(configuration.getHost(), configuration.getPort());
    IpV4Subnet multicastSubnet = new IpV4Subnet(MULTICAST_SUBNET);
    if (multicastSubnet.contains(configuration.getHost())) {
        datagramChannel = (DatagramChannel) connectionlessBootstrap.bind(hostAddress);
        String networkInterface = configuration.getNetworkInterface() == null ? LOOPBACK_INTERFACE : configuration.getNetworkInterface();
        multicastNetworkInterface = NetworkInterface.getByName(networkInterface);
        ObjectHelper.notNull(multicastNetworkInterface, "No network interface found for '" + networkInterface + "'.");
        LOG.info("ConnectionlessBootstrap joining {}:{} using network interface: {}", new Object[] { configuration.getHost(), configuration.getPort(), multicastNetworkInterface.getName() });
        datagramChannel.joinGroup(hostAddress, multicastNetworkInterface).syncUninterruptibly();
        allChannels.add(datagramChannel);
    } else {
        LOG.info("ConnectionlessBootstrap binding to {}:{}", configuration.getHost(), configuration.getPort());
        channel = connectionlessBootstrap.bind(hostAddress);
        allChannels.add(channel);
    }
}
Also used : NioDatagramChannelFactory(org.jboss.netty.channel.socket.nio.NioDatagramChannelFactory) InetSocketAddress(java.net.InetSocketAddress) NioDatagramWorkerPool(org.jboss.netty.channel.socket.nio.NioDatagramWorkerPool) FixedReceiveBufferSizePredictorFactory(org.jboss.netty.channel.FixedReceiveBufferSizePredictorFactory) Map(java.util.Map) IpV4Subnet(org.jboss.netty.handler.ipfilter.IpV4Subnet) ConnectionlessBootstrap(org.jboss.netty.bootstrap.ConnectionlessBootstrap)

Example 45 with ConnectionlessBootstrap

use of org.jboss.netty.bootstrap.ConnectionlessBootstrap in project camel by apache.

the class NettyUdpConnectionlessSendTest method createNettyUdpReceiver.

public void createNettyUdpReceiver() {
    bootstrap = new ConnectionlessBootstrap(new NioDatagramChannelFactory());
    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {

        @Override
        public ChannelPipeline getPipeline() throws Exception {
            ChannelPipeline channelPipeline = Channels.pipeline();
            channelPipeline.addLast("StringDecoder", new StringDecoder(CharsetUtil.UTF_8));
            channelPipeline.addLast("ContentHandler", new ContentHandler());
            return channelPipeline;
        }
    });
}
Also used : NioDatagramChannelFactory(org.jboss.netty.channel.socket.nio.NioDatagramChannelFactory) StringDecoder(org.jboss.netty.handler.codec.string.StringDecoder) ChannelPipelineFactory(org.jboss.netty.channel.ChannelPipelineFactory) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) ConnectionlessBootstrap(org.jboss.netty.bootstrap.ConnectionlessBootstrap)

Aggregations

ConnectionlessBootstrap (org.jboss.netty.bootstrap.ConnectionlessBootstrap)61 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)50 ServerBootstrap (org.jboss.netty.bootstrap.ServerBootstrap)47 TrackerServer (org.traccar.TrackerServer)46 StringEncoder (org.jboss.netty.handler.codec.string.StringEncoder)24 StringDecoder (org.jboss.netty.handler.codec.string.StringDecoder)20 NioDatagramChannelFactory (org.jboss.netty.channel.socket.nio.NioDatagramChannelFactory)10 CharacterDelimiterFrameDecoder (org.traccar.CharacterDelimiterFrameDecoder)10 LengthFieldBasedFrameDecoder (org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder)8 InetSocketAddress (java.net.InetSocketAddress)7 ChannelPipelineFactory (org.jboss.netty.channel.ChannelPipelineFactory)4 FixedReceiveBufferSizePredictorFactory (org.jboss.netty.channel.FixedReceiveBufferSizePredictorFactory)4 LineBasedFrameDecoder (org.jboss.netty.handler.codec.frame.LineBasedFrameDecoder)4 DatagramChannelFactory (org.jboss.netty.channel.socket.DatagramChannelFactory)3 IOException (java.io.IOException)2 Map (java.util.Map)2 Channel (org.jboss.netty.channel.Channel)2 FixedLengthFrameDecoder (org.jboss.netty.handler.codec.frame.FixedLengthFrameDecoder)2 Test (org.junit.Test)2 ServerPipelineFactory (com.jcumulus.server.rtmfp.ServerPipelineFactory)1