java.lang.Object
com.aoapps.messaging.base.AbstractSocket
- All Implemented Interfaces:
Socket,Closeable,AutoCloseable
Base implementation of socket.
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractSocket(AbstractSocketContext<? extends AbstractSocket> socketContext, Identifier id, long connectTime, SocketAddress remoteSocketAddress) Creates a newAbstractSocket. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddSocketListener(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.voidclose()Any overriding implementation must call super.close() first.longgetId()abstract StringAbstractSocketContext<? extends AbstractSocket> booleanisClosed()booleanremoveSocketListener(SocketListener listener) voidsendMessage(Message message) voidsendMessages(Collection<? extends Message> messages) protected abstract voidsendMessagesImpl(Collection<? extends Message> messages) Implementation to actually enqueue and send messages.protected voidsetRemoteSocketAddress(SocketAddress newRemoteSocketAddress) Sets the most recently seen remote address.voidMakes sure the socket is not already closed then calls startImpl.protected abstract voidCalled 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) Creates a newAbstractSocket.
-
-
Method Details
-
toString
-
getSocketContext
- Specified by:
getSocketContextin interfaceSocket
-
getId
-
getConnectTime
public long getConnectTime()- Specified by:
getConnectTimein interfaceSocket
-
getCloseTime
- Specified by:
getCloseTimein interfaceSocket
-
getConnectRemoteSocketAddress
- Specified by:
getConnectRemoteSocketAddressin interfaceSocket
-
getRemoteSocketAddress
- Specified by:
getRemoteSocketAddressin 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:
startin interfaceSocket- Throws:
IllegalStateException- See Also:
-
close
Any overriding implementation must call super.close() first.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceSocket- Throws:
IOException
-
isClosed
public boolean isClosed() -
addSocketListener
public void addSocketListener(SocketListener listener, boolean synchronous) throws IllegalStateException - Specified by:
addSocketListenerin interfaceSocket- Throws:
IllegalStateException
-
removeSocketListener
- Specified by:
removeSocketListenerin 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:
sendMessagein interfaceSocket- Throws:
IllegalStateException
-
sendMessages
- Specified by:
sendMessagesin interfaceSocket- Throws:
IllegalStateException
-
getProtocol
- Specified by:
getProtocolin 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.
-
