Java Code Examples for org.jboss.netty.bootstrap.ConnectionlessBootstrap#setPipelineFactory()

The following examples show how to use org.jboss.netty.bootstrap.ConnectionlessBootstrap#setPipelineFactory() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UdpServer.java    From feeyo-hlsserver with Apache License 2.0 6 votes vote down vote up
public void startup(int port) {
	
	ChannelFactory channelFactory = new NioDatagramChannelFactory(Executors.newCachedThreadPool());
	
	bootstrap = new ConnectionlessBootstrap( channelFactory );
	bootstrap.setOption("reuseAddress", false);
	bootstrap.setOption("child.reuseAddress", false);		
	bootstrap.setOption("readBufferSize", 1024 * 1024 * 15); //15M
	bootstrap.setOption("writeBufferSize", 1024 * 20);		
	bootstrap.setOption("receiveBufferSizePredictor", new FixedReceiveBufferSizePredictor(1024 * 3));
	bootstrap.setOption("receiveBufferSizePredictorFactory", new FixedReceiveBufferSizePredictorFactory(1024 * 3));
	bootstrap.setPipelineFactory( new ChannelPipelineFactory() {
		@Override
		public ChannelPipeline getPipeline() throws Exception {
			ChannelPipeline pipeline = Channels.pipeline();
			pipeline.addLast("handler", new UdpServerChannelHandler());
			return pipeline;
		}			
	});		
	datagramChannel = (DatagramChannel) bootstrap.bind( new InetSocketAddress( port ) );
}
 
Example 2
Source File: NettyUdpReceiverTest.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
private ConnectionlessBootstrap createUdpServer() {
        DatagramChannelFactory udpFactory = new NioDatagramChannelFactory(Executors.newCachedThreadPool(), 4);
        ChannelPipelineFactory pipelineFactory = new ChannelPipelineFactory() {
            @Override
            public ChannelPipeline getPipeline() throws Exception {
                ChannelPipeline pipeline = Channels.pipeline();
                pipeline.addLast("test", new SimpleChannelHandler() {
                    @Override
                    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
                        String name = Thread.currentThread().getName();
                        logger.debug("sleep:{}", name);
                        Thread.sleep(10000);
//                        if (!name.equals("New I/O worker #1")) {
                            logger.debug("messageReceived thread-{} message:", Thread.currentThread().getName());
//                        }
                    }
                });
                return pipeline;
            }
        };
        ConnectionlessBootstrap udpBootstrap = new ConnectionlessBootstrap(udpFactory);
        udpBootstrap.setPipelineFactory(pipelineFactory);
        return udpBootstrap;
    }
 
Example 3
Source File: SyslogUDPSource.java    From mt-flume with Apache License 2.0 6 votes vote down vote up
@Override
public void start() {
  // setup Netty server
  ConnectionlessBootstrap serverBootstrap = new ConnectionlessBootstrap
      (new OioDatagramChannelFactory(Executors.newCachedThreadPool()));
  final syslogHandler handler = new syslogHandler();
  handler.setFormater(formaterProp);
  serverBootstrap.setPipelineFactory(new ChannelPipelineFactory() {
    @Override
    public ChannelPipeline getPipeline() {
     return Channels.pipeline(handler);
    }
   });

  if (host == null) {
    nettyChannel = serverBootstrap.bind(new InetSocketAddress(port));
  } else {
    nettyChannel = serverBootstrap.bind(new InetSocketAddress(host, port));
  }

  super.start();
}
 
Example 4
Source File: UdpClient.java    From feeyo-hlsserver with Apache License 2.0 5 votes vote down vote up
public UdpClient(final UdpClientChannelHandler channelHandler) {
	
	bootstrap = new ConnectionlessBootstrap(new NioDatagramChannelFactory());
	bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
		@Override
		public ChannelPipeline getPipeline() throws Exception {
			ChannelPipeline pipeline = Channels.pipeline();
			pipeline.addLast("handler", channelHandler);
			return pipeline;
		}
	});
	
	bootstrap.setOption("localAddress", new InetSocketAddress(10002));
	channel = bootstrap.bind();
}
 
Example 5
Source File: CarbonTextServer.java    From kairos-carbon with Apache License 2.0 5 votes vote down vote up
@Override
public void start() throws KairosDBException
{
	// Configure the server.
	m_serverBootstrap = new ServerBootstrap(
			new NioServerSocketChannelFactory(
					Executors.newCachedThreadPool(),
					Executors.newCachedThreadPool()));

	// Configure the pipeline factory.
	m_serverBootstrap.setPipelineFactory(this);
	m_serverBootstrap.setOption("child.tcpNoDelay", true);
	m_serverBootstrap.setOption("child.keepAlive", true);
	m_serverBootstrap.setOption("reuseAddress", true);

	// Bind and start to accept incoming connections.
	m_serverBootstrap.bind(new InetSocketAddress(m_address, m_port));


	m_udpBootstrap = new ConnectionlessBootstrap(
			new NioDatagramChannelFactory());

	m_udpBootstrap.setOption("receiveBufferSizePredictorFactory", new FixedReceiveBufferSizePredictorFactory(m_maxSize));

	m_udpBootstrap.setPipelineFactory(this);

	m_udpBootstrap.bind(new InetSocketAddress(m_port));
}
 
Example 6
Source File: CarbonPickleServer.java    From kairos-carbon with Apache License 2.0 5 votes vote down vote up
@Override
public void start() throws KairosDBException
{
	// Configure the server.
	m_serverBootstrap = new ServerBootstrap(
			new NioServerSocketChannelFactory(
					Executors.newCachedThreadPool(),
					Executors.newCachedThreadPool()));

	// Configure the pipeline factory.
	m_serverBootstrap.setPipelineFactory(this);
	m_serverBootstrap.setOption("child.tcpNoDelay", true);
	m_serverBootstrap.setOption("child.keepAlive", true);
	m_serverBootstrap.setOption("reuseAddress", true);

	// Bind and start to accept incoming connections.
	m_serverBootstrap.bind(new InetSocketAddress(m_address, m_port));


	m_udpBootstrap = new ConnectionlessBootstrap(
			new NioDatagramChannelFactory());

	m_udpBootstrap.setOption("receiveBufferSizePredictorFactory", new FixedReceiveBufferSizePredictorFactory(m_maxSize));

	m_udpBootstrap.setPipelineFactory(this);

	m_udpBootstrap.bind(new InetSocketAddress(m_port));

}
 
Example 7
Source File: RaopAudioHandler.java    From Android-Airplay-Server with MIT License 4 votes vote down vote up
/**
 * Creates an UDP socket and handler pipeline for RTP channels
 * 
 * @param local local end-point address
 * @param remote remote end-point address
 * @param channelType channel type. Determines which handlers are put into the pipeline
 * @return open data-gram channel
 */
private Channel createRtpChannel(final SocketAddress local, final SocketAddress remote, final RaopRtpChannelType channelType) {
	/* Create bootstrap helper for a data-gram socket using NIO */
	//final ConnectionlessBootstrap bootstrap = new ConnectionlessBootstrap(new NioDatagramChannelFactory(rtpExecutorService));
	final ConnectionlessBootstrap bootstrap = new ConnectionlessBootstrap(new OioDatagramChannelFactory(rtpExecutorService));
	
	
	
	/* Set the buffer size predictor to 1500 bytes to ensure that
	 * received packets will fit into the buffer. Packets are
	 * truncated if they are larger than that!
	 */
	bootstrap.setOption("receiveBufferSizePredictorFactory", new FixedReceiveBufferSizePredictorFactory(1500));
	
	/* Set the socket's receive buffer size. We set it to 1MB */
	bootstrap.setOption("receiveBufferSize", 1024 * 1024);
	
	/* Set pipeline factory for the RTP channel */
	bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
		@Override
		public ChannelPipeline getPipeline() throws Exception {
			final ChannelPipeline pipeline = Channels.pipeline();

			final AirPlayServer airPlayServer = AirPlayServer.getIstance();
			
			pipeline.addLast("executionHandler", airPlayServer.getChannelExecutionHandler());
			pipeline.addLast("exceptionLogger", exceptionLoggingHandler);
			pipeline.addLast("decoder", decodeHandler);
			pipeline.addLast("encoder", encodeHandler);
			
			/* We pretend that all communication takes place on the audio channel,
			 * and simply re-route packets from and to the control and timing channels
			 */
			if ( ! channelType.equals(RaopRtpChannelType.Audio)) {
				pipeline.addLast("inputToAudioRouter", inputToAudioRouterDownstreamHandler);
				
				/* Must come *after* the router, otherwise incoming packets are logged twice */
				pipeline.addLast("packetLogger", packetLoggingHandler);
			}
			else {
				/* Must come *before* the router, otherwise outgoing packets are logged twice */
				pipeline.addLast("packetLogger", packetLoggingHandler);
				pipeline.addLast("audioToOutputRouter", audioToOutputRouterUpstreamHandler);
				pipeline.addLast("timing", timingHandler);
				pipeline.addLast("resendRequester", resendRequestHandler);
				
				if (decryptionHandler != null){
					pipeline.addLast("decrypt", decryptionHandler);
				}
				
				if (audioDecodeHandler != null){
					pipeline.addLast("audioDecode", audioDecodeHandler);
				}
				
				pipeline.addLast("enqueue", audioEnqueueHandler);
			}

			return pipeline;
		}
	});

	Channel channel = null;
	boolean didThrow = true;
	try {
		/* Bind to local address */
		channel = bootstrap.bind(local);
		
		/* Add to group of RTP channels beloging to this RTSP connection */
		rtpChannels.add(channel);

		/* Connect to remote address if one was provided */
		if (remote != null){
			channel.connect(remote);
		}
		
		didThrow = false;
		return channel;
	}
	finally {
		if (didThrow && (channel != null)){
			channel.close();
		}
	}
}