Module org.newsclub.net.unix
Package org.newsclub.net.unix
Class AFServerSocket<A extends AFSocketAddress>
java.lang.Object
java.net.ServerSocket
org.newsclub.net.unix.AFServerSocket<A>
- Type Parameters:
A- The concreteAFSocketAddressthat is supported by this type.
- All Implemented Interfaces:
Closeable,AutoCloseable,AFSomeSocketThing,FileDescriptorAccess
- Direct Known Subclasses:
AFUNIXServerSocket
public abstract class AFServerSocket<A extends AFSocketAddress>
extends ServerSocket
implements AFSomeSocketThing
The server part of a junixsocket socket.
- Author:
- Christian Kohlschütter
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceAFServerSocket.Constructor<A extends AFSocketAddress>The constructor of the concrete subclass. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedConstructs a new, unconnected instance.protectedAFServerSocket(FileDescriptor fdObj) Constructs a new instance, optionally associated with the given file descriptor. -
Method Summary
Modifier and TypeMethodDescriptionaccept()final voidaddCloseable(Closeable closeable) Registers aCloseablethat should be closed when this socket is closed.protected final AFAddressFamily<A> Returns the address family supported by this implementation.final voidbind(SocketAddress endpoint) final voidbind(SocketAddress endpoint, int backlog) final AFServerSocket<A> bindHook(SocketAddressFilter hook) Sets the hook for any subsequent call tobind(SocketAddress)andbind(SocketAddress, int)to be the given function.protected static <A extends AFSocketAddress>
AFServerSocket<A> bindOn(AFServerSocket.Constructor<A> instanceSupplier, A addr, boolean deleteOnClose) Returns a newServerSocketthat is bound to the givenAFSocketAddress.protected static <A extends AFSocketAddress>
AFServerSocket<A> bindOn(AFServerSocket.Constructor<A> instanceSupplier, AFSocketAddress addr) Returns a newServerSocketthat is bound to the givenAFSocketAddress.voidclose()final AFServerSocket<A> forceBindAddress(SocketAddress endpoint) Forces the address to be used for any subsequent call tobind(SocketAddress)to be the given one, regardless of what'll be passed tobind(SocketAddress, int), but doesn't bind yet.protected static <A extends AFSocketAddress>
AFServerSocket<A> forceBindOn(AFServerSocket.Constructor<A> instanceSupplier, A forceAddr) Returns a new, unboundServerSocketthat will always bind to the given address, regardless of any socket address used in a call tobind.final FileDescriptorReturns the correspondingFileDescriptor.final intfinal @Nullable AReturns the socket's local socket address, ornullif unavailable or if there was a problem retrieving it.<T> TgetOption(SocketOption<T> name) intbooleanintfinal booleanisBound()final booleanisClosed()final booleanChecks if thisAFServerSocket's file should be removed uponclose().booleanChecks if the local socket address returned bygetLocalSocketAddress()is still valid.static booleanChecks whether everything is setup to support junixsocket sockets.protected abstract AFServerSocketChannel<A> Creates a new AFServerSocketChannel for this socket.protected abstract AFSocketImpl<A> newImpl(FileDescriptor fdObj) Creates a new AFSocketImpl.protected static <A extends AFSocketAddress>
AFServerSocket<A> newInstance(AFServerSocket.Constructor<A> instanceSupplier) Creates a new AFServerSocket instance, using the given subclass constructor.protected static <A extends AFSocketAddress>
AFServerSocket<A> newInstance(AFServerSocket.Constructor<A> instanceSupplier, FileDescriptor fdObj, int localPort, int remotePort) Creates a new AFServerSocket instance, using the given subclass constructor.Returns a newAFSocketinstance.final voidremoveCloseable(Closeable closeable) Unregisters a previously registeredCloseable.final voidsetDeleteOnClose(boolean b) Enables/disables deleting thisAFServerSocket's file (or other resource type) uponclose().<T> ServerSocketsetOption(SocketOption<T> name, T value) voidsetPerformancePreferences(int connectionTime, int latency, int bandwidth) voidsetReceiveBufferSize(int size) voidsetReuseAddress(boolean on) voidsetShutdownOnClose(boolean enabled) Configures whether the socket should be shutdown uponCloseable.close(), which is the default.voidsetSoTimeout(int timeout) Set<SocketOption<?>> toString()Methods inherited from class java.net.ServerSocket
implAccept, setSocketFactory
-
Constructor Details
-
AFServerSocket
Constructs a new, unconnected instance.- Throws:
IOException- if the operation fails.
-
AFServerSocket
Constructs a new instance, optionally associated with the given file descriptor.- Parameters:
fdObj- The file descriptor, ornull.- Throws:
IOException- if the operation fails.
-
-
Method Details
-
newChannel
Creates a new AFServerSocketChannel for this socket.- Returns:
- The new instance.
-
newImpl
Creates a new AFSocketImpl.- Parameters:
fdObj- The file descriptor.- Returns:
- The new instance.
- Throws:
IOException- on error.
-
newInstance
protected static <A extends AFSocketAddress> AFServerSocket<A> newInstance(AFServerSocket.Constructor<A> instanceSupplier) throws IOException Creates a new AFServerSocket instance, using the given subclass constructor.- Type Parameters:
A- The concreteAFSocketAddressthat is supported by this type.- Parameters:
instanceSupplier- The subclass constructor.- Returns:
- The new instance.
- Throws:
IOException- on error.
-
newInstance
protected static <A extends AFSocketAddress> AFServerSocket<A> newInstance(AFServerSocket.Constructor<A> instanceSupplier, FileDescriptor fdObj, int localPort, int remotePort) throws IOException Creates a new AFServerSocket instance, using the given subclass constructor.- Type Parameters:
A- The concreteAFSocketAddressthat is supported by this type.- Parameters:
instanceSupplier- The subclass constructor.fdObj- The file descriptor.localPort- The local port.remotePort- The remote port.- Returns:
- The new instance.
- Throws:
IOException- on error.
-
bindOn
protected static <A extends AFSocketAddress> AFServerSocket<A> bindOn(AFServerSocket.Constructor<A> instanceSupplier, AFSocketAddress addr) throws IOException Returns a newServerSocketthat is bound to the givenAFSocketAddress.- Type Parameters:
A- The concreteAFSocketAddressthat is supported by this type.- Parameters:
instanceSupplier- The constructor of the concrete subclass.addr- The socket file to bind to.- Returns:
- The new, bound
AFServerSocket. - Throws:
IOException- if the operation fails.
-
bindOn
protected static <A extends AFSocketAddress> AFServerSocket<A> bindOn(AFServerSocket.Constructor<A> instanceSupplier, A addr, boolean deleteOnClose) throws IOException Returns a newServerSocketthat is bound to the givenAFSocketAddress.- Type Parameters:
A- The concreteAFSocketAddressthat is supported by this type.- Parameters:
instanceSupplier- The constructor of the concrete subclass.addr- The socket file to bind to.deleteOnClose- Iftrue, the socket file (if the address points to a file) will be deleted uponclose().- Returns:
- The new, bound
AFServerSocket. - Throws:
IOException- if the operation fails.
-
forceBindOn
protected static <A extends AFSocketAddress> AFServerSocket<A> forceBindOn(AFServerSocket.Constructor<A> instanceSupplier, A forceAddr) throws IOException Returns a new, unboundServerSocketthat will always bind to the given address, regardless of any socket address used in a call tobind.- Type Parameters:
A- The concreteAFSocketAddressthat is supported by this type.- Parameters:
instanceSupplier- The constructor of the concrete subclass.forceAddr- The address to use.- Returns:
- The new, yet unbound
AFServerSocket. - Throws:
IOException- if an exception occurs.
-
forceBindAddress
Forces the address to be used for any subsequent call tobind(SocketAddress)to be the given one, regardless of what'll be passed tobind(SocketAddress, int), but doesn't bind yet.- Parameters:
endpoint- The forced endpoint address.- Returns:
- This
AFServerSocket.
-
bind
- Overrides:
bindin classServerSocket- Throws:
IOException
-
bind
- Overrides:
bindin classServerSocket- Throws:
IOException
-
isBound
public final boolean isBound()- Overrides:
isBoundin classServerSocket
-
isClosed
public final boolean isClosed()- Overrides:
isClosedin classServerSocket
-
accept
- Overrides:
acceptin classServerSocket- Throws:
IOException
-
newSocketInstance
Returns a newAFSocketinstance.- Returns:
- The new instance.
- Throws:
IOException- on error.
-
toString
- Overrides:
toStringin classServerSocket
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classServerSocket- Throws:
IOException
-
addCloseable
-
removeCloseable
-
isSupported
public static boolean isSupported()Checks whether everything is setup to support junixsocket sockets.- Returns:
trueif supported.
-
getLocalSocketAddress
Description copied from interface:AFSomeSocketThingReturns the socket's local socket address, ornullif unavailable or if there was a problem retrieving it.- Specified by:
getLocalSocketAddressin interfaceAFSomeSocketThing- Overrides:
getLocalSocketAddressin classServerSocket- Returns:
- The local socket address, or
null.
-
isLocalSocketAddressValid
public boolean isLocalSocketAddressValid()Checks if the local socket address returned bygetLocalSocketAddress()is still valid. The address is no longer valid if the server socket has been closed,null, or another server socket has been bound on that address.- Returns:
trueiff still valid.
-
getLocalPort
public final int getLocalPort()- Overrides:
getLocalPortin classServerSocket
-
isDeleteOnClose
public final boolean isDeleteOnClose()Checks if thisAFServerSocket's file should be removed uponclose(). Deletion is not guaranteed, especially when not supported (e.g., addresses in the abstract namespace).- Returns:
trueif an attempt is made to delete the socket file uponclose().
-
setDeleteOnClose
public final void setDeleteOnClose(boolean b) Enables/disables deleting thisAFServerSocket's file (or other resource type) uponclose(). Deletion is not guaranteed, especially when not supported (e.g., addresses in the abstract namespace).- Parameters:
b- Enabled iftrue.
-
getChannel
- Overrides:
getChannelin classServerSocket
-
getFileDescriptor
Description copied from interface:FileDescriptorAccessReturns the correspondingFileDescriptor.- Specified by:
getFileDescriptorin interfaceFileDescriptorAccess- Returns:
- The corresponding
FileDescriptor. - Throws:
IOException- on error.
-
addressFamily
Returns the address family supported by this implementation.- Returns:
- The family.
-
bindHook
Sets the hook for any subsequent call tobind(SocketAddress)andbind(SocketAddress, int)to be the given function. The function can monitor calls or even alter the endpoint address.- Parameters:
hook- The function that gets called for eachbindcall.- Returns:
- This instance.
-
getInetAddress
- Overrides:
getInetAddressin classServerSocket
-
setReceiveBufferSize
- Overrides:
setReceiveBufferSizein classServerSocket- Throws:
SocketException
-
getReceiveBufferSize
- Overrides:
getReceiveBufferSizein classServerSocket- Throws:
SocketException
-
setSoTimeout
- Overrides:
setSoTimeoutin classServerSocket- Throws:
SocketException
-
getSoTimeout
- Overrides:
getSoTimeoutin classServerSocket- Throws:
IOException
-
setReuseAddress
- Overrides:
setReuseAddressin classServerSocket- Throws:
SocketException
-
getReuseAddress
- Overrides:
getReuseAddressin classServerSocket- Throws:
SocketException
-
setPerformancePreferences
public void setPerformancePreferences(int connectionTime, int latency, int bandwidth) - Overrides:
setPerformancePreferencesin classServerSocket
-
getOption
- Overrides:
getOptionin classServerSocket- Throws:
IOException
-
setOption
- Overrides:
setOptionin classServerSocket- Throws:
IOException
-
supportedOptions
- Overrides:
supportedOptionsin classServerSocket
-
setShutdownOnClose
public void setShutdownOnClose(boolean enabled) Description copied from interface:AFSomeSocketThingConfigures whether the socket should be shutdown uponCloseable.close(), which is the default.- Specified by:
setShutdownOnClosein interfaceAFSomeSocketThing- Parameters:
enabled-trueif enabled.
-