spellcast.server
Class ServerConnectionHandler

java.lang.Object
  extended by spellcast.server.ServerConnectionHandler
All Implemented Interfaces:
java.lang.Runnable, IPC

public class ServerConnectionHandler
extends java.lang.Object
implements IPC, java.lang.Runnable

This class handles all incoming and outgoing network traffic. Since network io can be time consuming a thread is used.

On each loop through run():

Checks for and accepts one new connection.

Each open connection is checked for a new message and only one message per connection is received.

All out going messages are sent on the caller's thread.

Author:
Barrie Treloar

Constructor Summary
ServerConnectionHandler(VersionDetails programVersionDetails, java.net.InetAddress bindAddress, int port)
           
 
Method Summary
 void connect(IPCHandle handle, Id client)
          Connect the handle with the specified Id.
 void disconnect(IPCHandle handle)
          Disconnect the client.
 IPCRequest receive()
          Removes the first event from the incoming event queue and returns it.
 void run()
          This thread will start running when start is called and continue running until the stop method is called.
 void send(Id client, GameEvent event)
          Send an event to the specified client.
 void sendToAll(GameEvent e)
          Send the event to all clients.
 void start()
          Allocate resources, create and start the thread.
 void stop()
          Sets the isRunning flag to false which lets run exit the infite loop.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ServerConnectionHandler

public ServerConnectionHandler(VersionDetails programVersionDetails,
                               java.net.InetAddress bindAddress,
                               int port)
Method Detail

receive

public IPCRequest receive()
Removes the first event from the incoming event queue and returns it. If there are no events in the queue this will wait until one is added when a client sends the server an event, at which point the thread will be notified.

Specified by:
receive in interface IPC

send

public void send(Id client,
                 GameEvent event)
Send an event to the specified client. Sending an event occurs on the caller's thread. This may block while the message is being sent.

Specified by:
send in interface IPC

sendToAll

public void sendToAll(GameEvent e)
Send the event to all clients. Equivalent to calling send multiple times for all clients.

Specified by:
sendToAll in interface IPC

run

public void run()
This thread will start running when start is called and continue running until the stop method is called.

On each loop through run():

Checks for and accepts one new connection.

Each open connection is checked for a new message and only one message per connection is received.

Specified by:
run in interface java.lang.Runnable

connect

public void connect(IPCHandle handle,
                    Id client)
Connect the handle with the specified Id.

Specified by:
connect in interface IPC

disconnect

public void disconnect(IPCHandle handle)
Disconnect the client.

Specified by:
disconnect in interface IPC

start

public void start()
           throws ServerException
Allocate resources, create and start the thread. Resources are deallocated in cleanup.

Throws:
ServerException - thrown if the server list socket could not be allocated.

stop

public void stop()
Sets the isRunning flag to false which lets run exit the infite loop. This method will wait until the thread dies before calling cleanup where resources are deallocated.



Copyright © 2001-2005 Spellcast. All Rights Reserved.