public class NoThreadSocketExecuter
extends org.threadly.util.AbstractService
The NoThreadSocketExecuter is a simpler implementation of a SocketExecuter
that does not create any threads. Since there are no threads operations happen on whatever thread
calls .select(), and only 1 thread at a time should ever call it at a time. Other then
that it should be completely thread safe.
This is generally the implementation used by clients. It can be used for servers but only when not servicing many connections at once. How many connections is hardware and OS defendant. For an average multi-core x86 linux server I a connections not to much more then 1000 connections would be its limit, though alot depends on how active those connections are.
It should also be noted that all client read/close callbacks happen on the thread that calls select().
| Modifier and Type | Field and Description |
|---|---|
static int |
SELECT_TIME_MS |
| Constructor and Description |
|---|
NoThreadSocketExecuter()
Constructs a NoThreadSocketExecuter.
|
NoThreadSocketExecuter(org.threadly.concurrent.NoThreadScheduler scheduler)
Constructs a NoThreadSocketExecuter.
|
| Modifier and Type | Method and Description |
|---|---|
TCPClient |
createTCPClient(java.nio.channels.SocketChannel sc)
|
TCPClient |
createTCPClient(java.lang.String host,
int port)
This will create a
TCPClient on the specified SocketExecuter. |
TCPServer |
createTCPServer(java.nio.channels.ServerSocketChannel ssc)
This will crate a
TCPServer from a given ServerSocketChannel object. |
TCPServer |
createTCPServer(java.lang.String host,
int port)
This will create a
TCPServer on the specified SocketExecuter. |
UDPServer |
createUDPServer(java.lang.String host,
int port)
This will create a UDPServer on the specified
SocketExecuter. |
int |
getClientCount()
Get the count of
Client on this SocketExecuter. |
org.threadly.concurrent.SubmitterExecutor |
getExecutorFor(java.lang.Object obj)
This allows you to get the
SubmitterExecutor for a specified object. |
int |
getServerCount()
Get the count of
Server from the SocketExecuter. |
SimpleByteStats |
getStats()
This will give you read and write stats for the SocketExecuter.
|
org.threadly.concurrent.SubmitterScheduler |
getThreadScheduler()
This returns the current
SubmitterScheduler for this SocketExecuter. |
long |
getTotalPendingReadBytes()
Check the total amount of pending reads across all associated clients.
|
long |
getTotalPendingWriteBytes()
Check the total number of bytes pending to be sent by clients.
|
void |
select()
This will run all ExecuterTasks, check for pending network operations,
then run those operations.
|
void |
select(int delay)
This is the same as the
select() but it allows you to set a delay. |
void |
setClientOperations(Client client)
This is called when the a clients state needs to be rechecked.
|
void |
setPerConnectionStatsEnabled(boolean enabled)
By default per-connection stats will be enabled.
|
void |
setUDPServerOperations(UDPServer udpServer,
boolean enable) |
void |
startListening(Server server)
This is used by
Server to tell the SocketExecuter to start allowing new clients to be accepted. |
void |
stopListening(Server server)
This will stop tell the executer to stop listening for new connections on the specified
Server. |
void |
wakeup()
This is used to wakeup the
Selector assuming it was called with a timeout on it. |
void |
watchFuture(org.threadly.concurrent.future.ListenableFuture<?> lf,
long delay)
This allows you to put a timer on a
ListenableFuture. |
hasStopped, isRunning, start, startIfNotStarted, stop, stopIfRunningpublic static final int SELECT_TIME_MS
public NoThreadSocketExecuter()
AbstractService.start() must still be called before using it.public NoThreadSocketExecuter(org.threadly.concurrent.NoThreadScheduler scheduler)
AbstractService.start() must still be called before using it.public void wakeup()
Selector assuming it was called with a timeout on it.
Most all methods in this class that need to do a wakeup do it automatically, but
there are situations where you might want to wake up the thread we are blocked on
manually.public void setClientOperations(Client client)
SocketExecuterClient
must have been created using this SocketExecuter or from a Server that was created with it.client - The Client object to check the state of.public void setUDPServerOperations(UDPServer udpServer, boolean enable)
public void select()
public void select(int delay)
select() but it allows you to set a delay.
This delay is the time to wait for socket operations to happen. It will
block the calling thread for up to this amount of time, but it could be less
if any network operation happens (including another thread adding a client/server).delay - Max time in milliseconds to block for.public org.threadly.concurrent.SubmitterExecutor getExecutorFor(java.lang.Object obj)
SocketExecuterSubmitterExecutor for a specified object.obj - The Object whose SubmitterExecutor you are looking forSubmitterExecutor for that object.public long getTotalPendingWriteBytes()
SocketExecutergetTotalPendingWriteBytes in interface SocketExecuterpublic long getTotalPendingReadBytes()
SocketExecutergetTotalPendingReadBytes in interface SocketExecuterpublic void setPerConnectionStatsEnabled(boolean enabled)
SocketExecutersetPerConnectionStatsEnabled in interface SocketExecuterenabled - false if stats should not be collected per-connection by defaultpublic TCPClient createTCPClient(java.lang.String host, int port) throws java.io.IOException
SocketExecuterTCPClient on the specified SocketExecuter.createTCPClient in interface SocketExecuterhost - This is the remote host or IP to connect this client too.port - This is the remote port to connect this client too.TCPClient object.java.io.IOException - This is thrown if we can not make the socket or can not connect to the remote host.public TCPClient createTCPClient(java.nio.channels.SocketChannel sc) throws java.io.IOException
SocketExecuterTCPClient on the specified SocketExecuter with
an already created SocketChannel. This is generally how a TCPServer will create its TCPClients.createTCPClient in interface SocketExecutersc - the SocketChannel to be used for this TCPClient.TCPClientjava.io.IOException - This is thrown if there is a problem with the passed in SocketChannel.public TCPServer createTCPServer(java.lang.String host, int port) throws java.io.IOException
SocketExecuterTCPServer on the specified SocketExecuter.createTCPServer in interface SocketExecuterhost - The local host or IP the TCPServer should listen on.port - The local port the TCPServer should listen on.TCPServer.java.io.IOException - This is only thrown if we can not create the TCPServers socket.public TCPServer createTCPServer(java.nio.channels.ServerSocketChannel ssc) throws java.io.IOException
SocketExecuterTCPServer from a given ServerSocketChannel object.createTCPServer in interface SocketExecuterssc - The ServerSocketChannel object to use for this TCPServer.TCPServer.java.io.IOException - This is thrown if there is a problem with the ServerSocketChannel.public UDPServer createUDPServer(java.lang.String host, int port) throws java.io.IOException
SocketExecuterSocketExecuter.
Please note that UDPClients are made from UDPServers not directly on the SocketExecutercreateUDPServer in interface SocketExecuterhost - The local host or IP the UDPServer should listen on.port - The local port the UDPServer should listen on.UDPServer.java.io.IOException - This is only thrown if we can not create the UDPServers socket.public void startListening(Server server)
SocketExecuterServer to tell the SocketExecuter to start allowing new clients to be accepted.
The Server must have been created with this SocketExecuter.startListening in interface SocketExecuterserver - The Server to start listening on.public void stopListening(Server server)
SocketExecuterServer.
The Server will not be closed, but will no longer accept or call back in new connections till startListening
is called for it.stopListening in interface SocketExecuterserver - The Server to stop listening with.public int getClientCount()
SocketExecuterGet the count of Client on this SocketExecuter.
getClientCount in interface SocketExecuterpublic int getServerCount()
SocketExecuterGet the count of Server from the SocketExecuter.
getServerCount in interface SocketExecuterpublic org.threadly.concurrent.SubmitterScheduler getThreadScheduler()
SocketExecuterThis returns the current SubmitterScheduler for this SocketExecuter.
Every SocketExecuter must have some kind of a SubmitterScheduler for it to
execute client/server operations on.
getThreadScheduler in interface SocketExecuterSubmitterScheduler the SocketExecuter is using.public SimpleByteStats getStats()
SocketExecuterThis will give you read and write stats for the SocketExecuter. This will tell you information about the number of bytes sent/received by this SocketExecuter.
getStats in interface SocketExecuterSimpleByteStats object to allow you to get the stats for this SocketExecuter.public void watchFuture(org.threadly.concurrent.future.ListenableFuture<?> lf,
long delay)
SocketExecuterThis allows you to put a timer on a ListenableFuture. If the timer triggers before the
ListenableFuture is done it will cancel the ListenableFuture
watchFuture in interface SocketExecuterlf - The ListenableFuture to watch.delay - The delay time in Millis to wait for the ListenableFuture to finish.