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 concreteAFSocketAddress
that 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
Modifier and TypeClassDescriptionstatic interface
AFServerSocket.Constructor<A extends AFSocketAddress>
The constructor of the concrete subclass. -
Constructor Summary
ModifierConstructorDescriptionprotected
Constructs a new, unconnected instance.protected
AFServerSocket
(FileDescriptor fdObj) Constructs a new instance, optionally associated with the given file descriptor. -
Method Summary
Modifier and TypeMethodDescriptionaccept()
final void
addCloseable
(Closeable closeable) Registers aCloseable
that should be closed when this socket is closed.protected final AFAddressFamily
<A> Returns the address family supported by this implementation.final void
bind
(SocketAddress endpoint) final void
bind
(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 newServerSocket
that is bound to the givenAFSocketAddress
.protected static <A extends AFSocketAddress>
AFServerSocket<A> bindOn
(AFServerSocket.Constructor<A> instanceSupplier, AFSocketAddress addr) Returns a newServerSocket
that is bound to the givenAFSocketAddress
.void
close()
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, unboundServerSocket
that will always bind to the given address, regardless of any socket address used in a call tobind
.final FileDescriptor
Returns the correspondingFileDescriptor
.final int
final @Nullable A
Returns the socket's local socket address, ornull
if unavailable or if there was a problem retrieving it.<T> T
getOption
(SocketOption<T> name) int
boolean
int
final boolean
isBound()
final boolean
isClosed()
final boolean
Checks if thisAFServerSocket
's file should be removed uponclose()
.boolean
Checks if the local socket address returned bygetLocalSocketAddress()
is still valid.static boolean
Checks 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 newAFSocket
instance.final void
removeCloseable
(Closeable closeable) Unregisters a previously registeredCloseable
.final void
setDeleteOnClose
(boolean b) Enables/disables deleting thisAFServerSocket
's file (or other resource type) uponclose()
.<T> ServerSocket
setOption
(SocketOption<T> name, T value) void
setPerformancePreferences
(int connectionTime, int latency, int bandwidth) void
setReceiveBufferSize
(int size) void
setReuseAddress
(boolean on) void
setShutdownOnClose
(boolean enabled) Configures whether the socket should be shutdown uponCloseable.close()
, which is the default.void
setSoTimeout
(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 concreteAFSocketAddress
that 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 concreteAFSocketAddress
that 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 newServerSocket
that is bound to the givenAFSocketAddress
.- Type Parameters:
A
- The concreteAFSocketAddress
that 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 newServerSocket
that is bound to the givenAFSocketAddress
.- Type Parameters:
A
- The concreteAFSocketAddress
that 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, unboundServerSocket
that will always bind to the given address, regardless of any socket address used in a call tobind
.- Type Parameters:
A
- The concreteAFSocketAddress
that 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:
bind
in classServerSocket
- Throws:
IOException
-
bind
- Overrides:
bind
in classServerSocket
- Throws:
IOException
-
isBound
public final boolean isBound()- Overrides:
isBound
in classServerSocket
-
isClosed
public final boolean isClosed()- Overrides:
isClosed
in classServerSocket
-
accept
- Overrides:
accept
in classServerSocket
- Throws:
IOException
-
newSocketInstance
Returns a newAFSocket
instance.- Returns:
- The new instance.
- Throws:
IOException
- on error.
-
toString
- Overrides:
toString
in classServerSocket
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classServerSocket
- Throws:
IOException
-
addCloseable
-
removeCloseable
-
isSupported
public static boolean isSupported()Checks whether everything is setup to support junixsocket sockets.- Returns:
true
if supported.
-
getLocalSocketAddress
Description copied from interface:AFSomeSocketThing
Returns the socket's local socket address, ornull
if unavailable or if there was a problem retrieving it.- Specified by:
getLocalSocketAddress
in interfaceAFSomeSocketThing
- Overrides:
getLocalSocketAddress
in 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:
true
iff still valid.
-
getLocalPort
public final int getLocalPort()- Overrides:
getLocalPort
in 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:
true
if 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:
getChannel
in classServerSocket
-
getFileDescriptor
Description copied from interface:FileDescriptorAccess
Returns the correspondingFileDescriptor
.- Specified by:
getFileDescriptor
in 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 eachbind
call.- Returns:
- This instance.
-
getInetAddress
- Overrides:
getInetAddress
in classServerSocket
-
setReceiveBufferSize
- Overrides:
setReceiveBufferSize
in classServerSocket
- Throws:
SocketException
-
getReceiveBufferSize
- Overrides:
getReceiveBufferSize
in classServerSocket
- Throws:
SocketException
-
setSoTimeout
- Overrides:
setSoTimeout
in classServerSocket
- Throws:
SocketException
-
getSoTimeout
- Overrides:
getSoTimeout
in classServerSocket
- Throws:
IOException
-
setReuseAddress
- Overrides:
setReuseAddress
in classServerSocket
- Throws:
SocketException
-
getReuseAddress
- Overrides:
getReuseAddress
in classServerSocket
- Throws:
SocketException
-
setPerformancePreferences
public void setPerformancePreferences(int connectionTime, int latency, int bandwidth) - Overrides:
setPerformancePreferences
in classServerSocket
-
getOption
- Overrides:
getOption
in classServerSocket
- Throws:
IOException
-
setOption
- Overrides:
setOption
in classServerSocket
- Throws:
IOException
-
supportedOptions
- Overrides:
supportedOptions
in classServerSocket
-
setShutdownOnClose
public void setShutdownOnClose(boolean enabled) Description copied from interface:AFSomeSocketThing
Configures whether the socket should be shutdown uponCloseable.close()
, which is the default.- Specified by:
setShutdownOnClose
in interfaceAFSomeSocketThing
- Parameters:
enabled
-true
if enabled.
-