下面列出了怎么用io.netty.channel.kqueue.KQueueDomainSocketChannel的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
@SuppressWarnings("unchecked")
public <CHANNEL extends Channel> CHANNEL getChannel(Class<CHANNEL> channelClass) {
if (channelClass.equals(SocketChannel.class)) {
return (CHANNEL) new KQueueSocketChannel();
}
if (channelClass.equals(ServerSocketChannel.class)) {
return (CHANNEL) new KQueueServerSocketChannel();
}
if (channelClass.equals(DatagramChannel.class)) {
return (CHANNEL) new KQueueDatagramChannel();
}
if (channelClass.equals(DomainSocketChannel.class)) {
return (CHANNEL) new KQueueDomainSocketChannel();
}
if (channelClass.equals(ServerDomainSocketChannel.class)) {
return (CHANNEL) new KQueueServerDomainSocketChannel();
}
throw new IllegalArgumentException("Unsupported channel type: " + channelClass.getSimpleName());
}
private void setChannelInfo() {
if (Epoll.isAvailable()) {
this.group = new EpollEventLoopGroup();
this.channelType = EpollDomainSocketChannel.class;
LOGGER.info("Using epoll for Netty transport.");
} else {
if (!KQueue.isAvailable()) {
throw new RuntimeException("Unsupported OS '" + System.getProperty("os.name") + "', only Unix and Mac are supported");
}
this.group = new KQueueEventLoopGroup();
this.channelType = KQueueDomainSocketChannel.class;
LOGGER.info("Using KQueue for Netty transport.");
}
}
private static NettyChannelBuilder newNettyChannelBuilder(String targetUrl, String proxy)
throws IOException {
if (Strings.isNullOrEmpty(proxy)) {
return NettyChannelBuilder.forTarget(targetUrl).defaultLoadBalancingPolicy("round_robin");
}
if (!proxy.startsWith("unix:")) {
throw new IOException("Remote proxy unsupported: " + proxy);
}
DomainSocketAddress address = new DomainSocketAddress(proxy.replaceFirst("^unix:", ""));
NettyChannelBuilder builder =
NettyChannelBuilder.forAddress(address).overrideAuthority(targetUrl);
if (KQueue.isAvailable()) {
return builder
.channelType(KQueueDomainSocketChannel.class)
.eventLoopGroup(new KQueueEventLoopGroup());
}
if (Epoll.isAvailable()) {
return builder
.channelType(EpollDomainSocketChannel.class)
.eventLoopGroup(new EpollEventLoopGroup());
}
throw new IOException("Unix domain sockets are unsupported on this platform");
}
public NettyDomainSocketClient(ClientConfig config) {
super(config);
URI uri = config.baseUri();
Require.argument("URI scheme", uri.getScheme()).equalTo("unix");
if (Epoll.isAvailable()) {
this.eventLoopGroup = new EpollEventLoopGroup();
this.channelClazz = EpollDomainSocketChannel.class;
} else if (KQueue.isAvailable()) {
this.eventLoopGroup = new KQueueEventLoopGroup();
this.channelClazz = KQueueDomainSocketChannel.class;
} else {
throw new IllegalStateException("No native library for unix domain sockets is available");
}
this.path = uri.getPath();
}
public Class<? extends Channel> getClientChannel() {
if (useNativeIo && Epoll.isAvailable()) {
return uds ? EpollDomainSocketChannel.class : EpollSocketChannel.class;
} else if (useNativeIo && KQueue.isAvailable()) {
return uds ? KQueueDomainSocketChannel.class : KQueueSocketChannel.class;
}
return NioSocketChannel.class;
}
/**
* Returns the correct {@link Class} to use with the given {@link EventLoopGroup}.
*
* @param group the {@link EventLoopGroup} for which the class is needed
* @param addressClass The class of the address that to connect to.
* @return the class that should be used for bootstrapping
*/
public static Class<? extends Channel> socketChannel(EventLoopGroup group,
Class<? extends SocketAddress> addressClass) {
if (useEpoll(group)) {
return DomainSocketAddress.class.isAssignableFrom(addressClass) ? EpollDomainSocketChannel.class :
EpollSocketChannel.class;
} else if (useKQueue(group)) {
return DomainSocketAddress.class.isAssignableFrom(addressClass) ? KQueueDomainSocketChannel.class :
KQueueSocketChannel.class;
} else {
return NioSocketChannel.class;
}
}
public Class<? extends Channel> getClientChannel() {
if (useNativeIo && Epoll.isAvailable()) {
return uds ? EpollDomainSocketChannel.class : EpollSocketChannel.class;
} else if (useNativeIo && KQueue.isAvailable()) {
return uds ? KQueueDomainSocketChannel.class : KQueueSocketChannel.class;
}
return NioSocketChannel.class;
}
public static HttpBlobStore create(
DomainSocketAddress domainSocketAddress,
URI uri,
int timeoutMillis,
int remoteMaxConnections,
@Nullable final Credentials creds)
throws ConfigurationException, URISyntaxException, SSLException {
if (KQueue.isAvailable()) {
return new HttpBlobStore(
KQueueEventLoopGroup::new,
KQueueDomainSocketChannel.class,
uri,
timeoutMillis,
remoteMaxConnections,
creds,
domainSocketAddress);
} else if (Epoll.isAvailable()) {
return new HttpBlobStore(
EpollEventLoopGroup::new,
EpollDomainSocketChannel.class,
uri,
timeoutMillis,
remoteMaxConnections,
creds,
domainSocketAddress);
} else {
throw new ConfigurationException("Unix domain sockets are unsupported on this platform");
}
}
@SuppressWarnings("unchecked")
@Override
public T newChannel() {
switch (channelType) {
case ACCEPTOR:
switch (socketType) {
case JAVA_NIO:
return (T) new NioServerSocketChannel();
case NATIVE_EPOLL:
return (T) new EpollServerSocketChannel();
case NATIVE_KQUEUE:
return (T) new KQueueServerSocketChannel();
case NATIVE_EPOLL_DOMAIN:
return (T) new EpollServerDomainSocketChannel();
case NATIVE_KQUEUE_DOMAIN:
return (T) new KQueueServerDomainSocketChannel();
default:
throw new IllegalStateException("Invalid socket type: " + socketType);
}
case CONNECTOR:
switch (socketType) {
case JAVA_NIO:
return (T) new NioSocketChannel();
case NATIVE_EPOLL:
return (T) new EpollSocketChannel();
case NATIVE_KQUEUE:
return (T) new KQueueSocketChannel();
case NATIVE_EPOLL_DOMAIN:
return (T) new EpollDomainSocketChannel();
case NATIVE_KQUEUE_DOMAIN:
return (T) new KQueueDomainSocketChannel();
default:
throw new IllegalStateException("Invalid socket type: " + socketType);
}
default:
throw new IllegalStateException("Invalid channel type: " + channelType);
}
}
public static HttpCacheClient create(
DomainSocketAddress domainSocketAddress,
URI uri,
int timeoutSeconds,
int remoteMaxConnections,
boolean verifyDownloads,
ImmutableList<Entry<String, String>> extraHttpHeaders,
DigestUtil digestUtil,
@Nullable final Credentials creds)
throws Exception {
if (KQueue.isAvailable()) {
return new HttpCacheClient(
KQueueEventLoopGroup::new,
KQueueDomainSocketChannel.class,
uri,
timeoutSeconds,
remoteMaxConnections,
verifyDownloads,
extraHttpHeaders,
digestUtil,
creds,
domainSocketAddress);
} else if (Epoll.isAvailable()) {
return new HttpCacheClient(
EpollEventLoopGroup::new,
EpollDomainSocketChannel.class,
uri,
timeoutSeconds,
remoteMaxConnections,
verifyDownloads,
extraHttpHeaders,
digestUtil,
creds,
domainSocketAddress);
} else {
throw new Exception("Unix domain sockets are unsupported on this platform");
}
}