edu.nps.moves.net
Class BehaviorProducerUDP

java.lang.Object
  extended by edu.nps.moves.net.BehaviorProducerUDP
All Implemented Interfaces:
BehaviorProducerIF, BehaviorWriterIF, java.lang.Runnable

public class BehaviorProducerUDP
extends java.lang.Object
implements BehaviorProducerIF, BehaviorWriterIF, java.lang.Runnable

This implements an object that can read and write DIS PDUs from a unicast or multicast UDP socket. It implements the BehaviorProducer interface, which allows objects to register as listeners for PDU arrival events, and the BehaviorWriter interface, which allows PDUs to be written. It's a bit complex internally, but not all that bad from an interface standpoint.

This runs in a thread of its own. The listeners for PDU events can either run in threads of their own, which is a bit complex, or simply process PDU objects as they come in, which is simple but may have performance problems if processing a PDU takes a long time.

Author:
DMcG

Field Summary
 
Fields inherited from interface edu.nps.moves.net.BehaviorWriterIF
MTU_SIZE
 
Constructor Summary
BehaviorProducerUDP(java.net.DatagramSocket pSocket)
           
 
Method Summary
 void addListener(BehaviorConsumerIF consumer)
          Add a listener that will be notified when a PDU is ready.
 void removeListener(BehaviorConsumerIF consumer)
          Remove a listener/consumer of PDUs from the notification list.
 void run()
          Entry point for thread
 void setDefaultDestination(java.net.InetAddress addr, int port)
          Set the default destination that the plain write(pdu) method will send data to.
 void setUseCopies(boolean shouldCreateCopy)
          This is a performance option.
 void write(java.nio.ByteBuffer buffer)
          Write PDU information to the default destination.
 void write(java.nio.ByteBuffer buffer, int numberOfBytes)
          If we have a byte buffer we are marshalling to, it may be bigger than the actuall size of the marshalled PDU.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BehaviorProducerUDP

public BehaviorProducerUDP(java.net.DatagramSocket pSocket)
Method Detail

addListener

public void addListener(BehaviorConsumerIF consumer)
Description copied from interface: BehaviorProducerIF
Add a listener that will be notified when a PDU is ready.

Specified by:
addListener in interface BehaviorProducerIF
Parameters:
consumer - the object that will be notified of the PDU

removeListener

public void removeListener(BehaviorConsumerIF consumer)
Description copied from interface: BehaviorProducerIF
Remove a listener/consumer of PDUs from the notification list.

Specified by:
removeListener in interface BehaviorProducerIF
Parameters:
consumer - to be removed from the notification list

setDefaultDestination

public void setDefaultDestination(java.net.InetAddress addr,
                                  int port)
Description copied from interface: BehaviorWriterIF
Set the default destination that the plain write(pdu) method will send data to.

Specified by:
setDefaultDestination in interface BehaviorWriterIF
Parameters:
addr - first object that describes destination (eg, IP)
port - second object that describes destination (eg, port number)

write

public void write(java.nio.ByteBuffer buffer)
Description copied from interface: BehaviorWriterIF
Write PDU information to the default destination. The user must clear the buffer if it is desired to be reused.

Specified by:
write in interface BehaviorWriterIF
Parameters:
buffer - the DIS PDU infomation to be written

write

public void write(java.nio.ByteBuffer buffer,
                  int numberOfBytes)
If we have a byte buffer we are marshalling to, it may be bigger than the actuall size of the marshalled PDU. This writes only the first numberOfBytes bytes of the ByteBuffer to the network.

Parameters:
buffer -
numberOfBytes -

setUseCopies

public void setUseCopies(boolean shouldCreateCopy)
Description copied from interface: BehaviorProducerIF
This is a performance option. When a PDU arrives we want to distribute it to all listeners. If we use a single copy of the object distributed to all listeners this may cause problems if one listener modifies the object and undermines the expectations of another listener. to avoid this we can create a new copy of the PDU and hand off a new, unique copy of the object to each listener. But this may cause some performance problems, since it takes a while to allocate a new object.

The default behavior should be to distribute a new, unqiue copy to each listener. this allows the user to override this behavior for better performance.

Specified by:
setUseCopies in interface BehaviorProducerIF
Parameters:
shouldCreateCopy - true to create a new copy for each listener, false for a shared copy for each listener

run

public void run()
Entry point for thread

Specified by:
run in interface java.lang.Runnable