org.newsclub.net.unix.AFGenericSelectorProvider Maven / Gradle / Ivy
Show all versions of junixsocket-common Show documentation
/*
* junixsocket
*
* Copyright 2009-2024 Christian Kohlschütter
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.newsclub.net.unix;
import java.io.IOException;
import java.net.ProtocolFamily;
import java.net.SocketAddress;
import org.eclipse.jdt.annotation.NonNull;
import com.kohlschutter.annotations.compiletime.SuppressFBWarnings;
/**
* Service-provider class for junixsocket selectors and selectable channels.
*/
final class AFGenericSelectorProvider extends AFSelectorProvider {
private static final AFGenericSelectorProvider INSTANCE = new AFGenericSelectorProvider();
@SuppressWarnings("null")
static final AFAddressFamily<@NonNull AFGenericSocketAddress> AF_GENERIC = AFAddressFamily
.registerAddressFamilyImpl("generic", AFGenericSocketAddress.addressFamily(),
new AFAddressFamilyConfig() {
@Override
protected Class extends AFSocket> socketClass() {
return AFGenericSocket.class;
}
@Override
protected AFSocket.Constructor socketConstructor() {
return AFGenericSocket::new;
}
@Override
protected Class extends AFServerSocket> serverSocketClass() {
return AFGenericServerSocket.class;
}
@Override
protected AFServerSocket.Constructor serverSocketConstructor() {
return AFGenericServerSocket::new;
}
@Override
protected Class extends AFSocketChannel> socketChannelClass() {
return AFGenericSocketChannel.class;
}
@Override
protected Class extends AFServerSocketChannel> serverSocketChannelClass() {
return AFGenericServerSocketChannel.class;
}
@Override
protected Class extends AFDatagramSocket> datagramSocketClass() {
return AFGenericDatagramSocket.class;
}
@Override
protected AFDatagramSocket.Constructor datagramSocketConstructor() {
return AFGenericDatagramSocket::new;
}
@Override
protected Class extends AFDatagramChannel> datagramChannelClass() {
return AFGenericDatagramChannel.class;
}
});
private AFGenericSelectorProvider() {
super();
}
/**
* Returns the singleton instance.
*
* @return The instance.
*/
@SuppressFBWarnings("MS_EXPOSE_REP")
public static AFGenericSelectorProvider getInstance() {
return INSTANCE;
}
/**
* Returns the singleton instance.
*
* @return The instance.
*/
public static AFGenericSelectorProvider provider() {
return getInstance();
}
/**
* Constructs a new socket pair from two sockets.
*
* @param s1 Some socket, the first one.
* @param s2 Some socket, the second one.
* @return The pair.
*/
@Override
protected AFSocketPair
newSocketPair(P s1, P s2) {
return new AFGenericSocketPair<>(s1, s2);
}
@SuppressWarnings("unchecked")
@Override
public AFGenericSocketPair openSocketChannelPair() throws IOException {
return (AFGenericSocketPair) super.openSocketChannelPair();
}
@SuppressWarnings("unchecked")
@Override
public AFGenericSocketPair openDatagramChannelPair()
throws IOException {
return (AFGenericSocketPair) super.openDatagramChannelPair();
}
@SuppressWarnings("unchecked")
@Override
public AFGenericSocketPair openDatagramChannelPair(AFSocketType type)
throws IOException {
return (AFGenericSocketPair) super.openDatagramChannelPair(type);
}
@Override
protected AFGenericSocket newSocket() throws IOException {
return AFGenericSocket.newInstance();
}
@Override
public AFGenericDatagramChannel openDatagramChannel() throws IOException {
return AFGenericDatagramSocket.newInstance().getChannel();
}
@Override
public AFGenericDatagramChannel openDatagramChannel(AFSocketType type) throws IOException {
return AFGenericDatagramSocket.newInstance(type).getChannel();
}
@Override
public AFGenericDatagramChannel openDatagramChannel(ProtocolFamily family) throws IOException {
return (AFGenericDatagramChannel) super.openDatagramChannel(family);
}
@Override
public AFGenericServerSocketChannel openServerSocketChannel() throws IOException {
return AFGenericServerSocket.newInstance().getChannel();
}
@Override
public AFGenericServerSocketChannel openServerSocketChannel(SocketAddress sa) throws IOException {
return AFGenericServerSocket.bindOn(AFGenericSocketAddress.unwrap(sa)).getChannel();
}
@Override
public AFGenericSocketChannel openSocketChannel() throws IOException {
return (AFGenericSocketChannel) super.openSocketChannel();
}
@Override
public AFGenericSocketChannel openSocketChannel(SocketAddress sa) throws IOException {
return AFGenericSocket.connectTo(AFGenericSocketAddress.unwrap(sa)).getChannel();
}
@Override
protected ProtocolFamily protocolFamily() {
return AFGenericProtocolFamily.GENERIC;
}
@Override
protected AFAddressFamily<@NonNull AFGenericSocketAddress> addressFamily() {
return AF_GENERIC;
}
}