edu.cuny.cat.server
Class ConnectionManager

java.lang.Object
  extended by java.lang.Thread
      extended by edu.cuny.cat.server.ConnectionManager
All Implemented Interfaces:
AuctionEventListener, java.lang.Runnable

public final class ConnectionManager
extends java.lang.Thread
implements AuctionEventListener

A class, as a thread, accepts connection requests, creates ConnectionAdaptors, and manage them, including event passing, etc..

Version:
$Revision: 1.64 $
Author:
Jinzhong Niu

Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
protected  java.util.SortedMap<java.lang.String,ConnectionAdaptor> adaptors
          normal connections
protected  java.util.Set<ConnectionAdaptor> babyAdaptors
          connections with clients that have yet to check in.
protected  GameController controller
           
protected  Dispatcher dispatcher
          a separate thread to process event-dispatching tasks
protected  IdentityOffice identityOffice
           
protected  CatpInfrastructure infrast
           
protected  ServerConnector<CatpMessage> serverConnector
           
protected  boolean stopAcceptingConnection
           
protected  edu.cuny.random.Uniform uniform
          used to randomize event dispatching order
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
ConnectionManager()
           
 
Method Summary
 void clientCheckIn(ConnectionAdaptor adaptor)
          processes the connection adaptor over which a client checked in.
 void dispatchEvent(AuctionEvent event)
          dispatches an event to all clients
 void dispatchEvent(AuctionEvent event, java.util.Collection<java.lang.String> receiverIdColl)
          dispatches an event to specified client receivers.
 void dispatchEvent(AuctionEvent event, java.util.Collection<java.lang.String> receiverIdColl, java.util.Observer observer)
           
 void dispatchIncomingMessage(CatpMessage msg, MessageHandler<CatpMessage> handler, java.lang.String clientId, java.util.Observer observer)
          aims to enqueue messages incoming from clients and get them process in order along with events.
 void dispatchOutgoingMessage(CatpMessage msg, Session<CatpMessage> session, java.lang.String clientId, java.util.Observer observer)
          aims to enqueue all messages outgoing to clients and get them sent in order.
 void eventOccurred(AuctionEvent event)
          process event without forwarding to connection adaptors
 ConnectionAdaptor getAdaptor(java.lang.String clientId)
           
 IdentityOffice getIdentityOffice()
           
 void printPendingTasks()
          prints all the pending tasks enqueued, for debugging purpose only.
 void processDispatchingTasks()
           
 void processEventLocally(AuctionEvent event)
           
<T> T[]
randomize(T[] array)
          randomize the order of objects in an array.
 void removeAdaptor(java.lang.String clientId)
           
 void removeBabyAdaptor(ConnectionAdaptor adaptor)
           
 void run()
           
protected  void stopAcceptingConnection()
           
 void terminate()
          terminiates the connections and the event dispatcher.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

infrast

protected CatpInfrastructure infrast

controller

protected GameController controller

serverConnector

protected ServerConnector<CatpMessage> serverConnector

stopAcceptingConnection

protected boolean stopAcceptingConnection

identityOffice

protected IdentityOffice identityOffice

adaptors

protected java.util.SortedMap<java.lang.String,ConnectionAdaptor> adaptors
normal connections


babyAdaptors

protected java.util.Set<ConnectionAdaptor> babyAdaptors
connections with clients that have yet to check in.


dispatcher

protected Dispatcher dispatcher
a separate thread to process event-dispatching tasks


uniform

protected edu.cuny.random.Uniform uniform
used to randomize event dispatching order

Constructor Detail

ConnectionManager

public ConnectionManager()
Method Detail

getIdentityOffice

public IdentityOffice getIdentityOffice()

terminate

public void terminate()
terminiates the connections and the event dispatcher.


run

public void run()
Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread

stopAcceptingConnection

protected void stopAcceptingConnection()

removeBabyAdaptor

public void removeBabyAdaptor(ConnectionAdaptor adaptor)

removeAdaptor

public void removeAdaptor(java.lang.String clientId)

getAdaptor

public ConnectionAdaptor getAdaptor(java.lang.String clientId)

clientCheckIn

public void clientCheckIn(ConnectionAdaptor adaptor)
processes the connection adaptor over which a client checked in. This is processed here instead of through ClientStateUpdatedEvent because the adaptor has no reference before the client's name is allocated.

Parameters:
adaptor - the connection adaptor for the client that just checked in.

eventOccurred

public void eventOccurred(AuctionEvent event)
process event without forwarding to connection adaptors

Specified by:
eventOccurred in interface AuctionEventListener
Parameters:
event -

processEventLocally

public void processEventLocally(AuctionEvent event)

dispatchEvent

public void dispatchEvent(AuctionEvent event)
dispatches an event to all clients

Parameters:
event -

dispatchEvent

public void dispatchEvent(AuctionEvent event,
                          java.util.Collection<java.lang.String> receiverIdColl)
dispatches an event to specified client receivers. This method is called by some connection adaptor to notify other adaptors.

Parameters:
event -
receiverIdColl -

dispatchEvent

public void dispatchEvent(AuctionEvent event,
                          java.util.Collection<java.lang.String> receiverIdColl,
                          java.util.Observer observer)

dispatchOutgoingMessage

public void dispatchOutgoingMessage(CatpMessage msg,
                                    Session<CatpMessage> session,
                                    java.lang.String clientId,
                                    java.util.Observer observer)
aims to enqueue all messages outgoing to clients and get them sent in order. NOTE: This is not used currently and may be deleted in the future.

Parameters:
msg -
session -
clientId -
observer -

dispatchIncomingMessage

public void dispatchIncomingMessage(CatpMessage msg,
                                    MessageHandler<CatpMessage> handler,
                                    java.lang.String clientId,
                                    java.util.Observer observer)
aims to enqueue messages incoming from clients and get them process in order along with events.

Parameters:
msg -
handler -
clientId -
observer -

processDispatchingTasks

public void processDispatchingTasks()

printPendingTasks

public void printPendingTasks()
prints all the pending tasks enqueued, for debugging purpose only.


randomize

public <T> T[] randomize(T[] array)
randomize the order of objects in an array.

Type Parameters:
T - the type of elements in the array.
Parameters:
array - the array
Returns:
the randomized array, which is actually the same array passed in.