mil.navy.nps.dis
Class BehaviorStreamBufferUDP

java.lang.Object
  extended bymil.navy.nps.dis.BehaviorStreamBuffer
      extended bymil.navy.nps.dis.BehaviorStreamBufferUDP
All Implemented Interfaces:
AllPermissionsBadge, java.lang.Runnable

public class BehaviorStreamBufferUDP
extends BehaviorStreamBuffer
implements java.lang.Runnable, AllPermissionsBadge

BehaviorStreamBufferUDP is responsible for reading from the network, specifically from packet-oriented (UDP and multicast) sockets. it is a subclass of the abstract class BehaviorStreamBuffer.

In general, this should be run via a thread--create a thread with an instance of this class in it, and run the thread. This will prevent blocking IO on the socket from stopping the application.

Author:
DMcG

Field Summary
protected  DatagramStreamBuffer datagramStreamBuffer
           
 
Fields inherited from class mil.navy.nps.dis.BehaviorStreamBuffer
DEBUG, info, inputThreadStarted, readThreadRunning, rtpEnabled, runContinue, strategy
 
Constructor Summary
BehaviorStreamBufferUDP()
          A constructor that creates a new unicast socket datagramStreamBuffer on an ephemeral (system-chosen) port.
BehaviorStreamBufferUDP(DatagramStreamBuffer pDatagramStreamBuffer)
          Creates a BehaviorStreamBufferNetwork with the specified DatagramStream.
BehaviorStreamBufferUDP(int pDatagramPort)
          A constructor that creates a new unicast socket.
BehaviorStreamBufferUDP(java.lang.String pMulticastAddress, int pDatagramPort)
          A constructor that creates a multicast socket datagramStreamBuffer If the netscape security classes are visible, this instantiates itself as a netscape-specific datagramStreamBuffer, able to bypass the Java sandbox.
 
Method Summary
 void cleanup()
          Closes down sockets nicely
protected  void finalize()
          Finalize method--used to clean up any sockets that are still open
 java.net.InetAddress getAddress()
          Accessor method to provide current multicast address.
 ProtocolDataUnit getNextPdu()
          Get the next PDU from the input stream.
 int getPort()
          Accessor method to provide current multicast port.
 java.util.Vector receivedPdus()
          Returns a vector of all the PDUs received since the last time we asked.
 void resumeReading()
          Start reading packets from the datagramStreamBuffer again.
 void run()
          Threading method to read/write via datagramStreamBuffer until shutdown.
 void sendDatagram(java.net.InetAddress pDestHost, int pPort, java.lang.String pCommand)
           
 void sendPdu(ProtocolDataUnit pPdu)
          Sends a PDU.
 void sendPdu(ProtocolDataUnit pPdu, java.lang.Object pAddress1, java.lang.Object pAddress2)
          Send a PDU to an address.
 void sendPdu(ProtocolDataUnit pdu, java.lang.String pDestinationHost, int pDestinationPort)
           
 void sendPduMulticast(ProtocolDataUnit pPdu)
           
 void setTimeToLive(int pTTL)
          JDK 1.2 version: set the multicast socket time-to-live, with 15=> local distribution, 63=>regional distribution, 127=>global distribution.
 void setTTL(byte pTTL)
          (Deprecated) JDK 1.1 version: set the multicast socket time-to-live, with 15=> local distribution, 63=>regional distribution, 127=>global distribution.
 void shutdown()
          Terminate the run loop and shutdown the thread.
 void startInputThread()
          Simple method to launch thread because Microsoft puts security restraints on everything.
 void suspendReading()
          suspend reading in the DatagramStreamBuffer
 
Methods inherited from class mil.navy.nps.dis.BehaviorStreamBuffer
checkForThreadStart, debug, getDEBUG, getInfo, getRtpEnabled, sendPdu, setDEBUG, setInfo, setRtpEnabled, startInputThreadWithSecurity, trace
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

datagramStreamBuffer

protected DatagramStreamBuffer datagramStreamBuffer
Constructor Detail

BehaviorStreamBufferUDP

public BehaviorStreamBufferUDP(int pDatagramPort)
A constructor that creates a new unicast socket. this creates an internal object responsible for reading from the socket, in this case a UDP unicast socket. It also starts a thread that reads from the socket.

Parameters:
pDatagramPort - the port we read from on this side

BehaviorStreamBufferUDP

public BehaviorStreamBufferUDP()
A constructor that creates a new unicast socket datagramStreamBuffer on an ephemeral (system-chosen) port.

If the Netscape security classes are visible, this instantiates itself with an underlying Netscape-specific DatagramStreamBuffer. Otherwise, it uses an generic, security-free datagramStreamBuffer. This also starts a thread responsible for reading from the socket.


BehaviorStreamBufferUDP

public BehaviorStreamBufferUDP(java.lang.String pMulticastAddress,
                               int pDatagramPort)
A constructor that creates a multicast socket datagramStreamBuffer If the netscape security classes are visible, this instantiates itself as a netscape-specific datagramStreamBuffer, able to bypass the Java sandbox. Otherwise, a generic DSB is used that has no security calls.

Note that this creates a thread to read from the socket.


BehaviorStreamBufferUDP

public BehaviorStreamBufferUDP(DatagramStreamBuffer pDatagramStreamBuffer)
Creates a BehaviorStreamBufferNetwork with the specified DatagramStream. Does not start running a thread.

Method Detail

startInputThread

public void startInputThread()
Simple method to launch thread because Microsoft puts security restraints on everything.

EntityDispatcher is static; only one copy is shared between all instances of the EspduTransform and other Script nodes. If it's null the first time through here, create and thread it.

This is synchronized, since we may have N EspduTransforms attempting to instantiate themselves at once, all accessing this shared class variable. The synchronization prevents multiple access.

This is actually some magic code. Java keeps several mutex locks around, one for every instance of a class and one for the class itself. We can't sychronize on entityDispatcher because that's null the first time through, which will fail silently. We also can't use a java primitive type. So I created a completely arbitrary instance of an object as a class variable, and synchronize on that. It's a standin for the EntityDispatcher in a way. When done, it starts reading from the input socket.

Specified by:
startInputThread in class BehaviorStreamBuffer

shutdown

public void shutdown()
Terminate the run loop and shutdown the thread.

Specified by:
shutdown in class BehaviorStreamBuffer

getAddress

public java.net.InetAddress getAddress()
Accessor method to provide current multicast address.


getPort

public int getPort()
Accessor method to provide current multicast port.


run

public void run()
Threading method to read/write via datagramStreamBuffer until shutdown.

Specified by:
run in interface java.lang.Runnable
Specified by:
run in class BehaviorStreamBuffer

receivedPdus

public java.util.Vector receivedPdus()
Returns a vector of all the PDUs received since the last time we asked. Queries the underlying DatagramStreamBuffer for this information.

Specified by:
receivedPdus in class BehaviorStreamBuffer

getNextPdu

public ProtocolDataUnit getNextPdu()
Description copied from class: BehaviorStreamBuffer
Get the next PDU from the input stream. This is generally used with unthreaded readers.

Specified by:
getNextPdu in class BehaviorStreamBuffer

setTimeToLive

public void setTimeToLive(int pTTL)
JDK 1.2 version: set the multicast socket time-to-live, with 15=> local distribution, 63=>regional distribution, 127=>global distribution.

Note that browsers are still using JDK 1.1 while command-line applications can use JDK 1.2.

see java.net.MulticastSocket.setTTL()


setTTL

public void setTTL(byte pTTL)
(Deprecated) JDK 1.1 version: set the multicast socket time-to-live, with 15=> local distribution, 63=>regional distribution, 127=>global distribution. It seems that there is a bug under NS 4.04 (surprise, surprise) so that setTTL on the mcast socket throws an exception, so you probably shouldn't use this yet. JDK 1.2 deprecated the setTTL method on the MulticastSocket class and replaced it with the setTimeToLive method, which takes an int instead of a byte. If you have problems compiling under 1.1 this is probably the problem.

see java.net.MulticastSocket.setTimeToLive()


sendPdu

public void sendPdu(ProtocolDataUnit pPdu)
Sends a PDU. If the underlying destination address has already been set, for example in a multicast or file, we don't need to supply an address.

Specified by:
sendPdu in class BehaviorStreamBuffer

sendPdu

public void sendPdu(ProtocolDataUnit pPdu,
                    java.lang.Object pAddress1,
                    java.lang.Object pAddress2)
Send a PDU to an address. Since the address can be in many forms, for example an InetAddress and a port number, we cheat here. The destination address is passed in as generic objects; a concrete subclass, such as a Unicast UDP object, will cast the generic objects here to what it expects, such as an InetAddress. (After checking for the right type with instanceof, of course!)

Specified by:
sendPdu in class BehaviorStreamBuffer
Parameters:
pPdu - protocol data unit being sent
pAddress1 - the first part of the address, typically the inet address
pAddress2 - the second part of the address, typically null or the destiation port

sendPduMulticast

public void sendPduMulticast(ProtocolDataUnit pPdu)

sendPdu

public void sendPdu(ProtocolDataUnit pdu,
                    java.lang.String pDestinationHost,
                    int pDestinationPort)

sendDatagram

public void sendDatagram(java.net.InetAddress pDestHost,
                         int pPort,
                         java.lang.String pCommand)

suspendReading

public void suspendReading()
suspend reading in the DatagramStreamBuffer

Specified by:
suspendReading in class BehaviorStreamBuffer

resumeReading

public void resumeReading()
Start reading packets from the datagramStreamBuffer again.

Specified by:
resumeReading in class BehaviorStreamBuffer

cleanup

public void cleanup()
Closes down sockets nicely

Specified by:
cleanup in class BehaviorStreamBuffer

finalize

protected void finalize()
                 throws java.lang.Throwable
Finalize method--used to clean up any sockets that are still open

Specified by:
finalize in class BehaviorStreamBuffer
Throws:
java.lang.Throwable