use of org.jboss.netty.bootstrap.ConnectionlessBootstrap in project graylog2-server by Graylog2.
the class NettyTransport method launch.
@Override
public void launch(final MessageInput input) throws MisfireException {
final LinkedHashMap<String, Callable<? extends ChannelHandler>> handlerList = getBaseChannelHandlers(input);
final LinkedHashMap<String, Callable<? extends ChannelHandler>> finalHandlers = getFinalChannelHandlers(input);
handlerList.putAll(finalHandlers);
try {
bootstrap = getBootstrap();
bootstrap.setPipelineFactory(getPipelineFactory(handlerList));
// sigh, bindable bootstraps do not share a common interface
int receiveBufferSize;
if (bootstrap instanceof ConnectionlessBootstrap) {
acceptChannel = ((ConnectionlessBootstrap) bootstrap).bind(socketAddress);
final DefaultDatagramChannelConfig channelConfig = (DefaultDatagramChannelConfig) acceptChannel.getConfig();
receiveBufferSize = channelConfig.getReceiveBufferSize();
} else if (bootstrap instanceof ServerBootstrap) {
acceptChannel = ((ServerBootstrap) bootstrap).bind(socketAddress);
final ServerSocketChannelConfig channelConfig = (ServerSocketChannelConfig) acceptChannel.getConfig();
receiveBufferSize = channelConfig.getReceiveBufferSize();
} else {
log.error("Unknown Netty bootstrap class returned: {}. Cannot safely bind.", bootstrap);
throw new IllegalStateException("Unknown netty bootstrap class returned: " + bootstrap + ". Cannot safely bind.");
}
if (receiveBufferSize != getRecvBufferSize()) {
log.warn("receiveBufferSize (SO_RCVBUF) for input {} should be {} but is {}.", input, getRecvBufferSize(), receiveBufferSize);
}
} catch (Exception e) {
throw new MisfireException(e);
}
}
use of org.jboss.netty.bootstrap.ConnectionlessBootstrap in project hadoop by apache.
the class SimpleUdpServer method run.
public void run() {
// Configure the client.
DatagramChannelFactory f = new NioDatagramChannelFactory(Executors.newCachedThreadPool(), workerCount);
server = new ConnectionlessBootstrap(f);
server.setPipeline(Channels.pipeline(RpcUtil.STAGE_RPC_MESSAGE_PARSER, rpcProgram, RpcUtil.STAGE_RPC_UDP_RESPONSE));
server.setOption("broadcast", "false");
server.setOption("sendBufferSize", SEND_BUFFER_SIZE);
server.setOption("receiveBufferSize", RECEIVE_BUFFER_SIZE);
server.setOption("reuseAddress", true);
// Listen to the UDP port
ch = server.bind(new InetSocketAddress(port));
InetSocketAddress socketAddr = (InetSocketAddress) ch.getLocalAddress();
boundPort = socketAddr.getPort();
LOG.info("Started listening to UDP requests at port " + boundPort + " for " + rpcProgram + " with workerCount " + workerCount);
}
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;
}
}
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);
}
}
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;
}
});
}
Aggregations