本文整理汇总了Java中io.netty.bootstrap.ChannelFactory类的典型用法代码示例。如果您正苦于以下问题:Java ChannelFactory类的具体用法?Java ChannelFactory怎么用?Java ChannelFactory使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ChannelFactory类属于io.netty.bootstrap包,在下文中一共展示了ChannelFactory类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: start
import io.netty.bootstrap.ChannelFactory; //导入依赖的package包/类
/**
* Start proxy server
* */
public void start()
throws InterruptedException {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(_acceptorGroup, _upstreamWorkerGroup);
serverBootstrap.channelFactory(new ChannelFactory<ServerChannel>() {
@Override
public ServerChannel newChannel() {
return new NioServerSocketChannel();
}
});
serverBootstrap.childHandler(new ProxyInitializer(this));
//bind
ChannelFuture future = serverBootstrap.bind(_host, _port);
//wait for the future
future.awaitUninterruptibly();
if (!future.isSuccess()) {
future.channel().closeFuture().awaitUninterruptibly();
throw new ChannelException(String.format("Failed to bind to: %s:%d", _host, _port), future.cause());
} else {
_allChannels.add(future.channel());
}
}
示例2: execute
import io.netty.bootstrap.ChannelFactory; //导入依赖的package包/类
@Override
protected Future<?> execute() {
Bootstrap cb = new Bootstrap().group(proxyServer.getProxyToServerWorkerFor(transportProtocol));
switch (transportProtocol) {
case TCP:
LOG.debug("Connecting to server with TCP");
cb.channelFactory(new ChannelFactory<Channel>() {
@Override
public Channel newChannel() {
return new NioSocketChannel();
}
});
break;
case UDT:
LOG.debug("Connecting to server with UDT");
cb.channelFactory(NioUdtProvider.BYTE_CONNECTOR)
.option(ChannelOption.SO_REUSEADDR, true);
break;
default:
throw new UnknownTransportProtocolException(transportProtocol);
}
cb.handler(new ChannelInitializer<Channel>() {
protected void initChannel(Channel ch) throws Exception {
initChannelPipeline(ch.pipeline(), initialRequest);
};
});
cb.option(ChannelOption.CONNECT_TIMEOUT_MILLIS,
proxyServer.getConnectTimeout());
if (localAddress != null) {
return cb.connect(remoteAddress, localAddress);
} else {
return cb.connect(remoteAddress);
}
}
示例3: execute
import io.netty.bootstrap.ChannelFactory; //导入依赖的package包/类
@Override
protected Future<?> execute() {
Bootstrap cb = new Bootstrap().group(proxyServer
.getProxyToServerWorkerFor(transportProtocol));
switch (transportProtocol) {
case TCP:
LOG.debug("Connecting to server with TCP");
cb.channelFactory(new ChannelFactory<Channel>() {
@Override
public Channel newChannel() {
return new NioSocketChannel();
}
});
break;
case UDT:
LOG.debug("Connecting to server with UDT");
cb.channelFactory(NioUdtProvider.BYTE_CONNECTOR)
.option(ChannelOption.SO_REUSEADDR, true);
break;
default:
throw new UnknownTransportProtocolError(transportProtocol);
}
cb.handler(new ChannelInitializer<Channel>() {
protected void initChannel(Channel ch) throws Exception {
initChannelPipeline(ch.pipeline(), initialRequest);
};
});
cb.option(ChannelOption.CONNECT_TIMEOUT_MILLIS,
proxyServer.getConnectTimeout());
if (localAddress != null) {
return cb.connect(remoteAddress, localAddress);
} else {
return cb.connect(remoteAddress);
}
}
示例4: datagram
import io.netty.bootstrap.ChannelFactory; //导入依赖的package包/类
public List<BootstrapComboFactory<Bootstrap, Bootstrap>> datagram() {
// Make the list of Bootstrap factories.
List<BootstrapFactory<Bootstrap>> bfs = Arrays.asList(
new BootstrapFactory<Bootstrap>() {
@Override
public Bootstrap newInstance() {
return new Bootstrap().group(nioWorkerGroup).channelFactory(new ChannelFactory<Channel>() {
@Override
public Channel newChannel() {
return new NioDatagramChannel(InternetProtocolFamily.IPv4);
}
@Override
public String toString() {
return NioDatagramChannel.class.getSimpleName() + ".class";
}
});
}
},
new BootstrapFactory<Bootstrap>() {
@Override
public Bootstrap newInstance() {
return new Bootstrap().group(oioWorkerGroup).channel(OioDatagramChannel.class);
}
}
);
// Populare the combinations.
return combo(bfs, bfs);
}
示例5: datagram
import io.netty.bootstrap.ChannelFactory; //导入依赖的package包/类
@Override
public List<TestsuitePermutation.BootstrapComboFactory<Bootstrap, Bootstrap>> datagram() {
// Make the list of Bootstrap factories.
List<BootstrapFactory<Bootstrap>> bfs = Arrays.asList(
new BootstrapFactory<Bootstrap>() {
@Override
public Bootstrap newInstance() {
return new Bootstrap().group(nioWorkerGroup).channelFactory(new ChannelFactory<Channel>() {
@Override
public Channel newChannel() {
return new NioDatagramChannel(InternetProtocolFamily.IPv4);
}
@Override
public String toString() {
return NioDatagramChannel.class.getSimpleName() + ".class";
}
});
}
},
new BootstrapFactory<Bootstrap>() {
@Override
public Bootstrap newInstance() {
return new Bootstrap().group(EPOLL_WORKER_GROUP).channel(EpollDatagramChannel.class);
}
}
);
return combo(bfs, bfs);
}
示例6: DataServerThread
import io.netty.bootstrap.ChannelFactory; //导入依赖的package包/类
public DataServerThread() {
dataBossGroup = new NioEventLoopGroup(DATA_BOSS_THREADS);
dataWorkerGroup = new NioEventLoopGroup(DATA_WORKER_THREADS);
try {
Bootstrap b = new Bootstrap();
b.group(dataWorkerGroup)
.channelFactory(new ChannelFactory<Channel>() {
@Override
public Channel newChannel() {
return new NioDatagramChannel(InternetProtocolFamily.IPv4);
}
@Override
public String toString() {
return NioDatagramChannel.class.getSimpleName() + ".class";
}
}).handler(new ChannelInitializer<DatagramChannel>() {
@Override
public void initChannel(DatagramChannel ch) throws Exception {
ch.config().setAllocator(UnpooledByteBufAllocator.DEFAULT);
ch.pipeline().addLast(new DataMessageHandler());
if (LOG.isTraceEnabled()) {
ch.pipeline().addLast(new LoggingHandler(LogLevel.TRACE));
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
LOG.error("Cannot initialize data server.", cause);
}
});
// Bind and start to accept incoming connections.
f = b.bind(getIp(), getDataPort()).sync();
} catch (InterruptedException ex) {
LOG.error("Message data interrupted.", ex);
}
}
示例7: execute
import io.netty.bootstrap.ChannelFactory; //导入依赖的package包/类
@Override
protected Future<?> execute() {
Bootstrap cb = new Bootstrap().group(proxyServer
.getProxyToServerWorkerFor(transportProtocol));
switch (transportProtocol) {
case TCP:
LOG.debug("Connecting to server with TCP");
cb.channelFactory(new ChannelFactory<Channel>() {
@Override
public Channel newChannel() {
return new NioSocketChannel();
}
});
break;
/*case UDT:
LOG.debug("Connecting to server with UDT");
cb.channelFactory(NioUdtProvider.BYTE_CONNECTOR)
.option(ChannelOption.SO_REUSEADDR, true);
break;*/
default:
throw new UnknownTransportProtocolError(transportProtocol);
}
cb.handler(new ChannelInitializer<Channel>() {
protected void initChannel(Channel ch) throws Exception {
initChannelPipeline(ch.pipeline(), initialRequest);
};
});
cb.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 40 * 1000);
if (localAddress != null) {
return cb.connect(remoteAddress, localAddress);
} else {
return cb.connect(remoteAddress);
}
}
示例8: UDPMulticastBeacon
import io.netty.bootstrap.ChannelFactory; //导入依赖的package包/类
/**
* Creates a new UDPMulticastBeacon.
*
* @param factory
* a ChannelFactory
* @param group
* the EventLoopGroup to use for channels and the timer
* @param executor
* the executor for application code and a timer for regularly sending the beacon
* @param moduleID
* the ModuleID to announce
* @param interval
* the interval at which to send beacons
* @param unit
* the unit for interval
*/
public UDPMulticastBeacon(final ChannelFactory<? extends DatagramChannel> factory, final EventLoopGroup group,
final ScheduledExecutorService executor, final ModuleID moduleID, final long interval, final TimeUnit unit) {
beacon =
new AtomicReference<BeaconMessage>(new BeaconMessage(moduleID,
Collections.<InetSocketAddress> emptyList()));
executor.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
sendBeacon();
}
}, 0, interval, unit);
final MessageAdapter messageAdapter = new MessageAdapter();
messageAdapter.addMessageType(BeaconMessage.class);
final GsonCodec gsonCodec = new GsonCodec(Message.class);
gsonCodec.registerTypeAdapter(Message.class, messageAdapter);
gsonCodec.registerTypeAdapter(InetSocketAddress.class, new InetSocketAddressAdapter());
gsonCodec.registerTypeAdapter(BeaconMessage.class, new BeaconMessageDeserializer());
this.channelFactory = new UDPMulticastChannelFactory(factory, group, new ChannelInitializer<DatagramChannel>() {
private final DatagramPacketWrapper datagramPacketWrapper = new DatagramPacketWrapper();
private final StringEncoder stringEncoder = new StringEncoder();
private final StringDecoder stringDecoder = new StringDecoder();
private final ChannelHandler beaconHandler = new BeaconHandler();
@Override
protected void initChannel(final DatagramChannel channel) {
channel.pipeline().addLast(datagramPacketWrapper).addLast(stringEncoder).addLast(stringDecoder)
.addLast(gsonCodec).addLast(beaconHandler);
// Move TARGET_ADDRESS from channel context to handler context
channel.pipeline().context(DatagramPacketWrapper.class).attr(DatagramPacketWrapper.TARGET_ADDRESS)
.set(channel.attr(DatagramPacketWrapper.TARGET_ADDRESS).getAndRemove());
}
});
}
示例9: main
import io.netty.bootstrap.ChannelFactory; //导入依赖的package包/类
public static void main(String[] args) {
Stopwatch watch = new Stopwatch();
watch.start();
GlydarBootstrap bootstrap = new GlydarBootstrap(args);
server = new GServer(bootstrap);
ParaGlydar.setServer(server);
serverThread = new Thread(server);
serverBootstrap = new ServerBootstrap();
serverBootstrap.childHandler(new ProtocolInitializer())
.option(ChannelOption.TCP_NODELAY, true)
.option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 32 * 1024)
.option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 64 * 1024)
.group(new NioEventLoopGroup())
.channelFactory(new ChannelFactory<ServerChannel>() {
@Override
public ServerChannel newChannel() {
return new NioServerSocketChannel();
}
})
.bind(new InetSocketAddress(server.getConfig().getPort()));
server.setUpWorlds();
try {
server.getPluginLoader().loadPlugins();
} catch (Exception exc) {
server.getLogger().warning(exc, "Error while loading plugins");
}
server.getLogger().info("Server ready on port {0}", server.getConfig().getPort());
server.getLogger().info("This server is running {0} version {1}", server.getName(), server.getVersion());
watch.stop();
server.getLogger().info("Server started in {0}ms", watch.elapsed(TimeUnit.MILLISECONDS));
server.getCommandReader().start();
serverThread.start();
}
示例10: getChannelFactory
import io.netty.bootstrap.ChannelFactory; //导入依赖的package包/类
public ChannelFactory<Channel> getChannelFactory() {
return channelFactory;
}
示例11: setChannelFactory
import io.netty.bootstrap.ChannelFactory; //导入依赖的package包/类
public void setChannelFactory(ChannelFactory<Channel> channelFactory) {
this.channelFactory = channelFactory;
}
示例12: datagram
import io.netty.bootstrap.ChannelFactory; //导入依赖的package包/类
static List<Entry<Factory<Bootstrap>, Factory<Bootstrap>>> datagram() {
List<Entry<Factory<Bootstrap>, Factory<Bootstrap>>> list =
new ArrayList<Entry<Factory<Bootstrap>, Factory<Bootstrap>>>();
// Make the list of Bootstrap factories.
List<Factory<Bootstrap>> bfs =
new ArrayList<Factory<Bootstrap>>();
bfs.add(new Factory<Bootstrap>() {
@Override
public Bootstrap newInstance() {
return new Bootstrap().group(nioWorkerGroup).channelFactory(new ChannelFactory<Channel>() {
@Override
public Channel newChannel() {
return new NioDatagramChannel(InternetProtocolFamily.IPv4);
}
@Override
public String toString() {
return NioDatagramChannel.class.getSimpleName() + ".class";
}
});
}
});
bfs.add(new Factory<Bootstrap>() {
@Override
public Bootstrap newInstance() {
return new Bootstrap().group(oioWorkerGroup).channel(OioDatagramChannel.class);
}
});
// Populate the combinations
for (Factory<Bootstrap> sbf: bfs) {
for (Factory<Bootstrap> cbf: bfs) {
final Factory<Bootstrap> sbf0 = sbf;
final Factory<Bootstrap> cbf0 = cbf;
list.add(new Entry<Factory<Bootstrap>, Factory<Bootstrap>>() {
@Override
public Factory<Bootstrap> getKey() {
return sbf0;
}
@Override
public Factory<Bootstrap> getValue() {
return cbf0;
}
@Override
public Factory<Bootstrap> setValue(Factory<Bootstrap> value) {
throw new UnsupportedOperationException();
}
});
}
}
return list;
}
示例13: doStart
import io.netty.bootstrap.ChannelFactory; //导入依赖的package包/类
private void doStart() {
ServerBootstrap serverBootstrap = new ServerBootstrap().group(
serverGroup.clientToProxyBossPools.get(transportProtocol),
serverGroup.clientToProxyWorkerPools.get(transportProtocol));
ChannelInitializer<Channel> initializer = new ChannelInitializer<Channel>() {
protected void initChannel(Channel ch) throws Exception {
new ClientToProxyConnection(
DefaultHttpProxyServer.this,
sslEngineSource,
authenticateSslClients,
ch.pipeline());
};
};
switch (transportProtocol) {
case TCP:
Log.i(TAG, "Proxy listening with TCP transport");
serverBootstrap.channelFactory(new ChannelFactory<ServerChannel>() {
@Override
public ServerChannel newChannel() {
return new NioServerSocketChannel();
}
});
break;
/*case UDT:
LOG.info("Proxy listening with UDT transport");
serverBootstrap.channelFactory(NioUdtProvider.BYTE_ACCEPTOR)
.option(ChannelOption.SO_BACKLOG, 10)
.option(ChannelOption.SO_REUSEADDR, true);
break;*/
default:
throw new UnknownTransportProtocolError(transportProtocol);
}
serverBootstrap.childHandler(initializer);
serverBootstrap.bind(address).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future)
throws Exception {
registerChannel(future.channel());
}
}).awaitUninterruptibly();
}
示例14: doStart
import io.netty.bootstrap.ChannelFactory; //导入依赖的package包/类
private void doStart() {
ServerBootstrap serverBootstrap = new ServerBootstrap().group(
serverGroup.clientToProxyBossPools.get(transportProtocol),
serverGroup.clientToProxyWorkerPools.get(transportProtocol));
ChannelInitializer<Channel> initializer = new ChannelInitializer<Channel>() {
protected void initChannel(Channel ch) throws Exception {
new ClientToProxyConnection(
DefaultHttpProxyServer.this,
sslEngineSource,
authenticateSslClients,
ch.pipeline());
};
};
switch (transportProtocol) {
case TCP:
LOG.info("Proxy listening with TCP transport");
serverBootstrap.channelFactory(new ChannelFactory<ServerChannel>() {
@Override
public ServerChannel newChannel() {
return new NioServerSocketChannel();
}
});
break;
/*case UDT:
LOG.info("Proxy listening with UDT transport");
serverBootstrap.channelFactory(NioUdtProvider.BYTE_ACCEPTOR)
.option(ChannelOption.SO_BACKLOG, 10)
.option(ChannelOption.SO_REUSEADDR, true);
break;*/
default:
throw new UnknownTransportProtocolError(transportProtocol);
}
serverBootstrap.childHandler(initializer);
serverBootstrap.bind(address).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future)
throws Exception {
registerChannel(future.channel());
}
}).awaitUninterruptibly();
}
示例15: datagram
import io.netty.bootstrap.ChannelFactory; //导入依赖的package包/类
static List<Entry<Factory<Bootstrap>, Factory<Bootstrap>>> datagram() {
List<Entry<Factory<Bootstrap>, Factory<Bootstrap>>> list =
new ArrayList<Entry<Factory<Bootstrap>, Factory<Bootstrap>>>();
// Make the list of Bootstrap factories.
List<Factory<Bootstrap>> bfs = new ArrayList<Factory<Bootstrap>>();
bfs.add(new Factory<Bootstrap>() {
@Override
public Bootstrap newInstance() {
return new Bootstrap().group(nioWorkerGroup).channelFactory(new ChannelFactory<Channel>() {
@Override
public Channel newChannel(EventLoop eventLoop) {
return new NioDatagramChannel(eventLoop, InternetProtocolFamily.IPv4);
}
@Override
public String toString() {
return NioDatagramChannel.class.getSimpleName() + ".class";
}
});
}
});
bfs.add(new Factory<Bootstrap>() {
@Override
public Bootstrap newInstance() {
return new Bootstrap().group(oioWorkerGroup).channel(OioDatagramChannel.class);
}
});
// Populate the combinations
for (Factory<Bootstrap> sbf: bfs) {
for (Factory<Bootstrap> cbf: bfs) {
final Factory<Bootstrap> sbf0 = sbf;
final Factory<Bootstrap> cbf0 = cbf;
list.add(new Entry<Factory<Bootstrap>, Factory<Bootstrap>>() {
@Override
public Factory<Bootstrap> getKey() {
return sbf0;
}
@Override
public Factory<Bootstrap> getValue() {
return cbf0;
}
@Override
public Factory<Bootstrap> setValue(Factory<Bootstrap> value) {
throw new UnsupportedOperationException();
}
});
}
}
return list;
}