Package org.omegazero.net.client
Class TCPClientManager
java.lang.Object
org.omegazero.net.common.SelectorHandler
org.omegazero.net.common.ConnectionSelectorHandler
org.omegazero.net.client.TCPClientManager
- All Implemented Interfaces:
Runnable
,NetClientManager
,NetworkApplication
- Direct Known Subclasses:
PlainTCPClientManager
,TLSClientManager
public abstract class TCPClientManager
extends ConnectionSelectorHandler
implements NetClientManager
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes this application, closing all bound and connected sockets and stopping the main loop, causing a call toNetworkApplication.start()
to return.connection
(ConnectionParameters params) Creates a new connection instance based on the given parameters to be managed by thisNetClientManager
.
SocketConnection.connect(int)
will need to be called on the returned connection instance to initiate the connection.protected abstract ChannelConnection
createConnection
(SelectionKey selectionKey, ConnectionParameters params) protected abstract void
Called when a connection was established.protected void
Called when a key was selected in a select call in theSelectorHandler.runSelectorLoop()
method.void
init()
Initializes this application.protected void
Called every loop iteration inSelectorHandler.runSelectorLoop()
, which is every timeSelector.select()
returns, either because aSelectionKey
was selected or the selector was woken up for any other reason (for example after a call toSelectorHandler.selectorWakeup()
).
This method does nothing by default.void
start()
Runs the main loop of this instance.Methods inherited from class org.omegazero.net.common.ConnectionSelectorHandler
handleConnectionClosed, onConnectionClosed
Methods inherited from class org.omegazero.net.common.SelectorHandler
closeSelector, initSelector, isRunning, registerChannel, registerChannel, runSelectorLoop, selectorKeys, selectorWakeup
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.omegazero.net.common.NetworkApplication
run
-
Field Details
-
worker
-
-
Constructor Details
-
TCPClientManager
public TCPClientManager() -
TCPClientManager
-
-
Method Details
-
createConnection
protected abstract ChannelConnection createConnection(SelectionKey selectionKey, ConnectionParameters params) throws IOException - Throws:
IOException
-
handleConnect
Called when a connection was established.- Parameters:
conn
- TheChannelConnection
object representing the connection that was established
-
init
Description copied from interface:NetworkApplication
Initializes this application.- Specified by:
init
in interfaceNetworkApplication
- Throws:
IOException
- If an IO error occurs during initialization
-
close
Description copied from interface:NetworkApplication
Closes this application, closing all bound and connected sockets and stopping the main loop, causing a call toNetworkApplication.start()
to return.- Specified by:
close
in interfaceNetworkApplication
- Throws:
IOException
- If an IO error occurs
-
start
Description copied from interface:NetworkApplication
Runs the main loop of this instance. This loop processes incoming or outgoing connection requests and network traffic.
Under normal circumstances, should never return beforeNetworkApplication.close()
is called. Afterclose()
is called, this function should return as soon as possible.
If this method is called beforeNetworkApplication.init()
, the behavior is undefined.- Specified by:
start
in interfaceNetworkApplication
- Throws:
IOException
- If an IO error occurs during any networking operation
-
connection
Description copied from interface:NetClientManager
Creates a new connection instance based on the given parameters to be managed by thisNetClientManager
.
SocketConnection.connect(int)
will need to be called on the returned connection instance to initiate the connection.- Specified by:
connection
in interfaceNetClientManager
- Parameters:
params
- Parameters for this connection- Returns:
- The new connection instance
- Throws:
IOException
-
loopIteration
Description copied from class:SelectorHandler
Called every loop iteration inSelectorHandler.runSelectorLoop()
, which is every timeSelector.select()
returns, either because aSelectionKey
was selected or the selector was woken up for any other reason (for example after a call toSelectorHandler.selectorWakeup()
).
This method does nothing by default. Subclasses may override this method to perform additional required operations.- Overrides:
loopIteration
in classConnectionSelectorHandler
- Throws:
IOException
- If an IO error occurs
-
handleSelectedKey
Description copied from class:SelectorHandler
Called when a key was selected in a select call in theSelectorHandler.runSelectorLoop()
method.- Specified by:
handleSelectedKey
in classSelectorHandler
- Parameters:
key
- The selected key- Throws:
IOException
- If an IO error occurs
-