eap.freeciv
Class PacketConnection

java.lang.Object
  extended byjava.lang.Thread
      extended byeap.freeciv.PacketConnection
All Implemented Interfaces:
Runnable

public class PacketConnection
extends Thread

Represents a socket connection through which freeciv packets will flow. The main purpose of this class is to run a thread which listens for packets from the socket and passes them to registered PacketListener objects. Such a connection may be on either the client or server side.

The typical way to use this class is to create a Socket, use it to create a PacketConnection, register listeners with addPacketListener(PacketListener), and then call beginListening() to start the packet listening thread. Usually, a Client or Server will handle much of this for you.


Field Summary
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
PacketConnection(Socket socket)
          Create a new connection associated with the given socket.
 
Method Summary
 void addPacketListener(PacketListener l)
          Register a listener to receive the packets that this class reads from the socket.
 void beginListening()
          Starts the listening thread.
 void run()
          This method is required by the Thread class.
 void sendPacket(Packet packet)
          Send a packet through the socket.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

PacketConnection

public PacketConnection(Socket socket)
                 throws IOException
Create a new connection associated with the given socket.

Method Detail

sendPacket

public void sendPacket(Packet packet)
                throws IOException
Send a packet through the socket.

Throws:
IOException

addPacketListener

public void addPacketListener(PacketListener l)
Register a listener to receive the packets that this class reads from the socket.


beginListening

public void beginListening()
Starts the listening thread. Note that you should register all the packet listeners before calling this method. Otherwise, the connection might read some packets and discard them before you begin listening.

Here's a thought - maybe the thread should wait if there are no listeners registered. The only downside is that the a connection could never just discard packets - but I can't think of a reason you would need it to, and even if you did you could just create a "/dev/null" packet listener.


run

public void run()
This method is required by the Thread class. It implements the listening thread.