java.lang.Object
com.aoapps.messaging.base.AbstractSocket
- All Implemented Interfaces:
Socket
,Closeable
,AutoCloseable
Base implementation of socket.
-
Constructor Summary
ModifierConstructorDescriptionprotected
AbstractSocket
(AbstractSocketContext<? extends AbstractSocket> socketContext, Identifier id, long connectTime, SocketAddress remoteSocketAddress) -
Method Summary
Modifier and TypeMethodDescriptionvoid
addSocketListener
(SocketListener listener, boolean synchronous) protected Future
<?> When an error as occurred, call this to distribute to all of the listeners.protected Future
<?> callOnMessages
(List<? extends Message> messages) When one or more new messages have arrived, call this to distribute to all of the listeners.void
close()
Any overriding implementation must call super.close() first.long
getId()
abstract String
AbstractSocketContext
<? extends AbstractSocket> boolean
isClosed()
boolean
removeSocketListener
(SocketListener listener) void
sendMessage
(Message message) void
sendMessages
(Collection<? extends Message> messages) protected abstract void
sendMessagesImpl
(Collection<? extends Message> messages) Implementation to actually enqueue and send messages.protected void
setRemoteSocketAddress
(SocketAddress newRemoteSocketAddress) Sets the most recently seen remote address.void
Makes sure the socket is not already closed then calls startImpl.protected abstract void
Called once the socket is confirmed to not be closed.toString()
-
Constructor Details
-
AbstractSocket
protected AbstractSocket(AbstractSocketContext<? extends AbstractSocket> socketContext, Identifier id, long connectTime, SocketAddress remoteSocketAddress)
-
-
Method Details
-
toString
-
getSocketContext
- Specified by:
getSocketContext
in interfaceSocket
-
getId
-
getConnectTime
public long getConnectTime()- Specified by:
getConnectTime
in interfaceSocket
-
getCloseTime
- Specified by:
getCloseTime
in interfaceSocket
-
getConnectRemoteSocketAddress
- Specified by:
getConnectRemoteSocketAddress
in interfaceSocket
-
getRemoteSocketAddress
- Specified by:
getRemoteSocketAddress
in interfaceSocket
-
setRemoteSocketAddress
Sets the most recently seen remote address. If the provided value is different than the previous, will notify all listeners. -
start
public void start(Callback<? super Socket> onStart, Callback<? super Throwable> onError) throws IllegalStateException Makes sure the socket is not already closed then calls startImpl.- Specified by:
start
in interfaceSocket
- Throws:
IllegalStateException
- See Also:
-
close
Any overriding implementation must call super.close() first.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in interfaceSocket
- Throws:
IOException
-
isClosed
public boolean isClosed() -
addSocketListener
public void addSocketListener(SocketListener listener, boolean synchronous) throws IllegalStateException - Specified by:
addSocketListener
in interfaceSocket
- Throws:
IllegalStateException
-
removeSocketListener
- Specified by:
removeSocketListener
in interfaceSocket
-
callOnMessages
When one or more new messages have arrived, call this to distribute to all of the listeners. If need to wait until all of the listeners have handled the messages, can call Future.get() or Future.isDone().- Throws:
IllegalStateException
- if this socket is closed
-
callOnError
When an error as occurred, call this to distribute to all of the listeners. If need to wait until all of the listeners have handled the error, can call Future.get() or Future.isDone().- Throws:
IllegalStateException
- if this socket is closed
-
sendMessage
- Specified by:
sendMessage
in interfaceSocket
- Throws:
IllegalStateException
-
sendMessages
- Specified by:
sendMessages
in interfaceSocket
- Throws:
IllegalStateException
-
getProtocol
- Specified by:
getProtocol
in interfaceSocket
-
startImpl
protected abstract void startImpl(Callback<? super Socket> onStart, Callback<? super Throwable> onError) throws IllegalStateException Called once the socket is confirmed to not be closed.- Throws:
IllegalStateException
- if already started- See Also:
-
sendMessagesImpl
Implementation to actually enqueue and send messages. This must never block.
-