mil.navy.nps.dis
Class BehaviorStreamBufferFile

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

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

BehaviorStreamBufferFile is a class responsible for interpreting data received via a file and interpreting it as DIS PDUs.

DIS is by nature packet-oriented, while files are stream-oriented. this means that we must parse the data as it comes in, to determine how many bytes to read. This can be done via the PDU length field in the PDU header. This is always at the same point in the packet, and describes how many bytes to read. By nature, if this count is off, the stream will get scrambled.

Note that if we're reading from a file, we don't want to get TOO carried away with this whole reading thing. This might read the entire contents of a file in before anyone consumes any PDUs. This would be a disaster for big files. We do some thread synchronization with wait() and notify() in the methods that read data and consume PDUs. This regulates the number of pdus read in beforehand to DEFAULT_READAHEAD_SIZE.

The bsbf has prepended data that says something about the stream saved inside. This consists of a version number, a byte count, and a string that describes the data. this string right now contains a URL and a flag that tells whether the saved PDUs are using RTP headers.

Author:
DMcG

Field Summary
 boolean atEOF
          Set to true if the file is at EOF.
static boolean DEBUG
           
static int DEFAULT_READAHEAD_SIZE
           
static int HEADER_VERSION
           
static int MAX_DATAGRAM_SIZE
           
 
Fields inherited from class mil.navy.nps.dis.BehaviorStreamBuffer
info, inputThreadStarted, readThreadRunning, rtpEnabled, runContinue, strategy
 
Constructor Summary
BehaviorStreamBufferFile(java.io.File pFile, BehaviorStreamBufferInfo pInfo, boolean pIsReader)
          All-signing, all-dancing constructor
BehaviorStreamBufferFile(java.io.File pFile, boolean pIsReader)
          Constructor, takes a file.
BehaviorStreamBufferFile(java.lang.String pFileName, BehaviorStreamBufferInfo pInfo, boolean pIsReader)
          yet another constructor
BehaviorStreamBufferFile(java.lang.String pFileName, boolean pIsReader)
          Constructor; takes a string filename.
BehaviorStreamBufferFile(java.lang.String pFileName, java.lang.String readerOrWriter)
          Constructor, takes a fileName and a reader/writer configuration string ("reader" or "writer").
 
Method Summary
 void cleanup()
          Closes down input buffers, sockets, or open files nicely
 void debug(java.lang.String pMessage)
          Debugging output
protected  void finalize()
          Finalize method--used to clean up any files that are still open
 ProtocolDataUnit getNextPdu()
          Get the next PDU from the input stream.
static void main(java.lang.String[] args)
          Used to debug/test the class
 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 until shutdown.
 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 setIsReader(boolean pIsReader)
          sets whether we're a reader or writer.
 void setReadAheadSize(int pNewReadAheadSize)
          Set the max readahead size.
 void shutdown()
          Terminate the run loop and shutdown the thread.
 void startInputThread()
          Simple method to launch thread.
 void suspendReading()
          suspend reading in the DatagramStreamBuffer
 void trace(java.lang.String pMessage)
          tracing output
 
Methods inherited from class mil.navy.nps.dis.BehaviorStreamBuffer
checkForThreadStart, getDEBUG, getInfo, getRtpEnabled, sendPdu, setDEBUG, setInfo, setRtpEnabled, startInputThreadWithSecurity
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX_DATAGRAM_SIZE

public static final int MAX_DATAGRAM_SIZE
See Also:
Constant Field Values

DEFAULT_READAHEAD_SIZE

public static final int DEFAULT_READAHEAD_SIZE
See Also:
Constant Field Values

HEADER_VERSION

public static final int HEADER_VERSION
See Also:
Constant Field Values

DEBUG

public static boolean DEBUG

atEOF

public boolean atEOF
Set to true if the file is at EOF.

Constructor Detail

BehaviorStreamBufferFile

public BehaviorStreamBufferFile(java.io.File pFile,
                                boolean pIsReader)
Constructor, takes a file. Also starts a thread running to read from the file. Can take a string if this is a writer, which does double duty. Passing in the string says both that this is a writer, and prepends the data to the output stream. If the string is null, it's assumed that this is a reader. Kinda questionable to kick of the thread in the constuructor.

Parameters:
pFile - file object to read or write from
pIsReader - are we a reader or writer?

BehaviorStreamBufferFile

public BehaviorStreamBufferFile(java.lang.String pFileName,
                                boolean pIsReader)
Constructor; takes a string filename. we open the file ourselves. Also starts an input thread.


BehaviorStreamBufferFile

public BehaviorStreamBufferFile(java.lang.String pFileName,
                                BehaviorStreamBufferInfo pInfo,
                                boolean pIsReader)
yet another constructor


BehaviorStreamBufferFile

public BehaviorStreamBufferFile(java.io.File pFile,
                                BehaviorStreamBufferInfo pInfo,
                                boolean pIsReader)
All-signing, all-dancing constructor


BehaviorStreamBufferFile

public BehaviorStreamBufferFile(java.lang.String pFileName,
                                java.lang.String readerOrWriter)
Constructor, takes a fileName and a reader/writer configuration string ("reader" or "writer").

Method Detail

setReadAheadSize

public void setReadAheadSize(int pNewReadAheadSize)
Set the max readahead size. This is the number of PDUs the class will Read from the file before stopping. This prevents big gobs of PDUs from being read and blowing out VM.


setIsReader

public void setIsReader(boolean pIsReader)
sets whether we're a reader or writer.


startInputThread

public void startInputThread()
Simple method to launch thread. This kicks of the subclass of BSB and starts it reading in its own thread. This is required for some subclasses, such as sockets; for files, this is optional-- we can read one PDU at a time from them without blocking.

Specified by:
startInputThread in class BehaviorStreamBuffer

shutdown

public void shutdown()
Terminate the run loop and shutdown the thread. This should be used only for threaded readers.

Specified by:
shutdown in class BehaviorStreamBuffer

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

run

public void run()
Threading method to read/write 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 input buffer for this information.\ this is generally used with threaded readers.

Specified by:
receivedPdus in class BehaviorStreamBuffer

getNextPdu

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

Specified by:
getNextPdu in class BehaviorStreamBuffer

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!) In this case we simply write to the default file. In the future this might be changed to some other file.

Specified by:
sendPdu in class BehaviorStreamBuffer
Parameters:
pPdu - protocol data unit being sent
pAddress1 - ignored
pAddress2 - ignored

cleanup

public void cleanup()
Closes down input buffers, sockets, or open files nicely

Specified by:
cleanup in class BehaviorStreamBuffer

finalize

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

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

trace

public void trace(java.lang.String pMessage)
tracing output

Overrides:
trace in class BehaviorStreamBuffer

debug

public void debug(java.lang.String pMessage)
Debugging output

Overrides:
debug in class BehaviorStreamBuffer

main

public static void main(java.lang.String[] args)
Used to debug/test the class