async
Class ASync

java.lang.Object
  extended by async.ASync
All Implemented Interfaces:
ASyncServiceInspector, ASyncServiceManager

public class ASync
extends java.lang.Object
implements ASyncServiceManager

A library for performing all network I/O over a single thread using the New I/O packages of Java.

Author:
Michael Parker

Constructor Summary
ASync()
          Creates a new ASync instance that is responsible for managing SelectableChannel objects and events belonging to registered ASyncService objects.
 
Method Summary
 boolean addEvent(ASyncEvent event)
          Enqueues the event so that its run method will be invoked at the earliest convenient moment by the thread running the main loop of this ASync instance.
 boolean addEvent(ASyncEvent event, long time_ms)
          Enqueues the event so that its run method will be invoked at time time_ms by the thread running the main loop of this ASync instance.
 boolean addService(ASyncService service)
          Adds the provided service to this ASync instance.
 boolean containsService(ASyncService service)
          Returns whether the provided service is registered with this ASync instance.
 boolean containsServiceKey(ASyncServiceKey key)
          Returns whether a service with the provided key is registered with this ASync instance.
 java.util.List<ASyncServiceKey> getActiveServiceKeys()
          Returns a list of the keys belonging to all active services registered with this ASync instance.
 java.util.List<ASyncService> getActiveServices()
          Returns a list of all active services registered with this ASync instance.
 java.util.List<ASyncServiceKey> getInactiveServiceKeys()
          Returns a list of the keys belonging to all inactive services registered with this ASync instance.
 java.util.List<ASyncService> getInactiveServices()
          Returns a list of all inactive services registered with this ASync instance.
 boolean isRunning()
          Returns whether another thread is currently running the main loop of this ASync instance.
 boolean removeService(ASyncService service)
          Removes the provided service from this ASync instance.
 boolean run(ASyncStartedCallback assc)
          Runs the main loop of this ASync instance.
 boolean stop()
          Stops another thread from running the main loop of this ASync instance.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ASync

public ASync()
Creates a new ASync instance that is responsible for managing SelectableChannel objects and events belonging to registered ASyncService objects.

Method Detail

addService

public final boolean addService(ASyncService service)
Description copied from interface: ASyncServiceManager
Adds the provided service to this ASync instance. This method will only succeed if ASync is not running, and the service is not already registered.

Specified by:
addService in interface ASyncServiceManager
Parameters:
service - the service to add
Returns:
true if ASync is not running and the service is not already added, false otherwise

removeService

public final boolean removeService(ASyncService service)
Description copied from interface: ASyncServiceManager
Removes the provided service from this ASync instance. This method will only succeed if ASync is not running, and the service is already registered.

Specified by:
removeService in interface ASyncServiceManager
Parameters:
service - the service to remove
Returns:
true if ASync is not running and the service is registered, false otherwise

containsService

public final boolean containsService(ASyncService service)
Description copied from interface: ASyncServiceManager
Returns whether the provided service is registered with this ASync instance. If this is true, it is either in the list returned by getActiveServices or the list returned by getInactiveServices.

Specified by:
containsService in interface ASyncServiceManager
Parameters:
service - the service to query as registered
Returns:
true if the service is registered, false otherwise

getActiveServices

public final java.util.List<ASyncService> getActiveServices()
Description copied from interface: ASyncServiceManager
Returns a list of all active services registered with this ASync instance. Note that the list is immutable, and so attempting to add or remove services through the list will not change the services registered with the corresponding ASync instance.

Specified by:
getActiveServices in interface ASyncServiceManager
Returns:
a list of all active and registered services

getInactiveServices

public final java.util.List<ASyncService> getInactiveServices()
Description copied from interface: ASyncServiceManager
Returns a list of all inactive services registered with this ASync instance. Note that the list is immutable, and so attempting to add or remove services through the list will not change the services registered with the corresponding ASync instance.

Specified by:
getInactiveServices in interface ASyncServiceManager
Returns:
a list of all inactive and registered services

containsServiceKey

public boolean containsServiceKey(ASyncServiceKey key)
Description copied from interface: ASyncServiceInspector
Returns whether a service with the provided key is registered with this ASync instance. If this is true, its key is either in the list returned by getActiveServiceKeys or the list returned by getInactiveServiceKeys.

Specified by:
containsServiceKey in interface ASyncServiceInspector
Parameters:
key - the key of the service to query as registered
Returns:
true if the service with the key is registered, false otherwise

getActiveServiceKeys

public java.util.List<ASyncServiceKey> getActiveServiceKeys()
Description copied from interface: ASyncServiceInspector
Returns a list of the keys belonging to all active services registered with this ASync instance. Note that the list is immutable, and so attempting to add or remove keys through the list will not change the services and keys registered with the corresponding ASync instance.

Specified by:
getActiveServiceKeys in interface ASyncServiceInspector
Returns:
a list of keys belonging to all active and registered services

getInactiveServiceKeys

public java.util.List<ASyncServiceKey> getInactiveServiceKeys()
Description copied from interface: ASyncServiceInspector
Returns a list of the keys belonging to all inactive services registered with this ASync instance. Note that the list is immutable, and so attempting to add or remove keys through the list will not change the services and keys registered with the corresponding ASync instance.

Specified by:
getInactiveServiceKeys in interface ASyncServiceInspector
Returns:
a list of keys belonging to all inactive and registered services

addEvent

public final boolean addEvent(ASyncEvent event)
Enqueues the event so that its run method will be invoked at the earliest convenient moment by the thread running the main loop of this ASync instance. Note that the thread executing the main loop may exit before this time, in which case method run is never invoked. This method will only succeed if ASync is currently running.

Parameters:
event - the event to invoke at the earliest convenient moment
Returns:
true if another thread is running the main loop, false otherwise

addEvent

public final boolean addEvent(ASyncEvent event,
                              long time_ms)
Enqueues the event so that its run method will be invoked at time time_ms by the thread running the main loop of this ASync instance. Note that the thread executing the main loop may exit before time_ms, in which case method run is never invoked. This method will only succeed if ASync is currently running.

Parameters:
event - the event to invoke at the specified time
time_ms - the time at which to run method run of argument event
Returns:
true if another thread is running the main loop, false otherwise

run

public final boolean run(ASyncStartedCallback assc)
Runs the main loop of this ASync instance. Note that the loop is run by the thread that invokes this method. If the loop is not being run by another thread, this thread executes the loop and once stop is invoked by another thread, this method returns true. If the loop is already being run by another thread, however, this method immediately returns false.

Parameters:
assc - callback method to invoke once started
Returns:
true if the main loop was run by this thread, false otherwise

stop

public final boolean stop()
Stops another thread from running the main loop of this ASync instance. If the loop is being run by another thread, this method returns true; otherwise, this method returns false.

Returns:
true if some other thread has stopped executing the main loop, false otherwise

isRunning

public final boolean isRunning()
Returns whether another thread is currently running the main loop of this ASync instance.

Returns:
true if another thread is running the main loop, false otherwise