mcast
Class TOMCastSocket

java.lang.Object
  extended by mcast.MCastSocket
      extended by mcast.TOMCastSocket
All Implemented Interfaces:
java.lang.Runnable

public class TOMCastSocket
extends MCastSocket
implements java.lang.Runnable

TOMCastSocket implements total ordering, on top of an MCastSocket, which provides (unordered) multicast.

Version:
$Id: TOMCastSocket.java,v 1.2 2009/01/13 05:36:20 zahorjan Exp $

Field Summary
private  int BUFFER_SIZE
          A not-very-respected limit on the number of packets to be buffered.
private  int clock
          Our Lamport clock.
private  boolean drained
          Set to true once there are no more packets in the receive queue and there will never be any again.
private  java.util.concurrent.ArrayBlockingQueue<TOMCastPacket> inComingPktBuf
          The ordered queue of packets to be given to the caller of receive().
private  java.lang.Thread receiver
          A TOMCastSocket uses a separate thread on the receive side.
 
Fields inherited from class mcast.MCastSocket
CLOSED, CONNECTED, DRAINED, STARTING
 
Constructor Summary
TOMCastSocket()
          Create a TOMCastSock socket at an ephemeral port.
 
Method Summary
private  int incrementClock()
          This is one of two routines that updates the clock in a thread safe way.
 TOMCastPacket receive()
          Receive blocks if nothing is available right now.
 void run()
          This is the receive thread loop, invoked when the thread is started.
 void send(TOMCastPacket pkt)
          This is multicast send.
private  int updateClockWithMax(int otherVal)
          This is one of two routines that updates the clock in a thread safe way.
 
Methods inherited from class mcast.MCastSocket
getClientId, getMCastGroupSize, join, leave, send
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

receiver

private java.lang.Thread receiver
A TOMCastSocket uses a separate thread on the receive side. That thread fetches packets from the underlying MCastSocket, buffers them as necesssary to achieve the total ordering property, and eventually puts them in a receive queue for the client to find on receive() calls.


inComingPktBuf

private java.util.concurrent.ArrayBlockingQueue<TOMCastPacket> inComingPktBuf
The ordered queue of packets to be given to the caller of receive(). It's synchronized because there may be concurrent injections of packets (by our receive thread) and removals (by the user thread calling receive).


BUFFER_SIZE

private final int BUFFER_SIZE
A not-very-respected limit on the number of packets to be buffered. Don't worry too much about enforcing this. It's here just in case we want to induce some packet losses, out of curiosity - we set it small.

See Also:
Constant Field Values

drained

private boolean drained
Set to true once there are no more packets in the receive queue and there will never be any again.


clock

private int clock
Our Lamport clock.

Constructor Detail

TOMCastSocket

public TOMCastSocket()
              throws java.net.SocketException
Create a TOMCastSock socket at an ephemeral port.

Throws:
java.net.SocketException
Method Detail

receive

public TOMCastPacket receive()
Receive blocks if nothing is available right now.

Overrides:
receive in class MCastSocket
Returns:
Next packet, in order. If none will ever be available again, returns null.

send

public void send(TOMCastPacket pkt)
          throws java.io.IOException
This is multicast send. It is non-blocking (we don't (and can't) wait until the data arrives at the other end before returning to the caller).

Throws:
java.io.IOException

updateClockWithMax

private int updateClockWithMax(int otherVal)
This is one of two routines that updates the clock in a thread safe way. This one implements:
clock = max(clock,otherVal)+1

Parameters:
otherVal - Some value.
Returns:
The old value of the clock.

incrementClock

private int incrementClock()
This is one of two routines that updates the clock in a thread safe way. This one does:
clock++

Returns:
the old value of the clock.

run

public void run()
This is the receive thread loop, invoked when the thread is started.

Specified by:
run in interface java.lang.Runnable