mil.navy.nps.util
Class DatagramStreamBuffer

java.lang.Object
  extended bymil.navy.nps.util.DatagramStreamBuffer
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
DatagramStreamBufferNetscape

public class DatagramStreamBuffer
extends java.lang.Object
implements java.lang.Runnable


Field Summary
(package private)  java.util.Vector datagramBuffer
           
(package private)  int datagramPort
           
(package private)  java.net.DatagramSocket datagramSocket
           
(package private)  java.net.InetAddress multicastAddress
           
(package private)  java.net.MulticastSocket multicastSocket
           
(package private)  boolean readingActive
           
(package private)  int ttl
           
(package private)  boolean usingMulticast
           
 
Constructor Summary
DatagramStreamBuffer()
          Create a new unicast DSB on an ephemeral port, one picked by the system.
DatagramStreamBuffer(int pDatagramPort)
           
DatagramStreamBuffer(java.lang.String pMulticastAddress, int pDatagramPort)
          multicast constructor.
 
Method Summary
 void cleanup()
          Closes down sockets nicely
protected static void debug(java.lang.String pDiagnostic)
          Debugging output.
protected  void finalize()
          Finalize method--used to clean up any sockets that are still open
 int getDatagramPort()
           
static boolean getDEBUG()
           
 java.net.InetAddress getMulticastAddress()
           
 boolean getReadingActive()
          Returns the run status
 boolean getUsingMulticast()
           
static void main(java.lang.String[] args)
          For testing purposes.
 java.util.Vector receivedDatagrams()
          Returns a vector of datagrams that have been received by this object since the last time this method was called.
 void run()
          Implements the runnable interface, so this class can optionally be used in threads.
 void sendDatagram(java.net.DatagramPacket pDatagram)
          Sends out a datagram.
 void sendDatagram(java.net.DatagramPacket pDatagram, java.lang.String pDestinationHost, int pDestinationSocket)
          Sends out a datagram to the designated destination and desitnation port.
static void setDEBUG(boolean pDEBUG)
           
 void setReadingActive(boolean pReadingActive)
          When this is set to false, the run() loop will terminate.
 void setTimeToLive(int pTTL)
          This is JDK 1.2 specific code; if you have problems compiling under 1.1, this is probably the cause.
 void setTTL(byte pTTL)
          Set the multicast socket time-to-live.
protected static void trace(java.lang.String pDiagnostic)
          Guaranteed debugging output.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

usingMulticast

boolean usingMulticast

datagramPort

int datagramPort

datagramSocket

java.net.DatagramSocket datagramSocket

multicastSocket

java.net.MulticastSocket multicastSocket

multicastAddress

java.net.InetAddress multicastAddress

ttl

int ttl

datagramBuffer

java.util.Vector datagramBuffer

readingActive

boolean readingActive
Constructor Detail

DatagramStreamBuffer

public DatagramStreamBuffer(int pDatagramPort)

DatagramStreamBuffer

public DatagramStreamBuffer()
Create a new unicast DSB on an ephemeral port, one picked by the system.


DatagramStreamBuffer

public DatagramStreamBuffer(java.lang.String pMulticastAddress,
                            int pDatagramPort)
multicast constructor. This has one ugly wart. We attempt a join in this class on the specified multicast group. But this requires a way to bypass the sandbox, if a sandbox is active. The subclass should handle the security calls. But one of the requirements of Java constructors is that the superclass constructor has to be called before any code in the subclass constructor--which doesn't give us a chance to do any security calls before the join() operation. We get around this by catching the failed join exception and ignoring it. Then, in the subclass, we attempt another join after we've done the security calls. That should work, but it leaves some ugly debugging messages on the console.

Method Detail

getDEBUG

public static boolean getDEBUG()

setDEBUG

public static void setDEBUG(boolean pDEBUG)

debug

protected static void debug(java.lang.String pDiagnostic)
Debugging output. Pass in a string, and it gets printed out on the console. You can pass in strings such as "foo " + bar.getName().


trace

protected static void trace(java.lang.String pDiagnostic)
Guaranteed debugging output. Pass in a string, and it gets printed out on the console. You can pass in strings such as "foo " + bar.getName().


getMulticastAddress

public java.net.InetAddress getMulticastAddress()

getDatagramPort

public int getDatagramPort()

getUsingMulticast

public boolean getUsingMulticast()

setReadingActive

public void setReadingActive(boolean pReadingActive)
When this is set to false, the run() loop will terminate.


getReadingActive

public boolean getReadingActive()
Returns the run status


run

public void run()
Implements the runnable interface, so this class can optionally be used in threads. This loops, reading packets and adding them to the list of recieved packets, until readingActive is set to false. In the course of events the receivedDatagrams() method will usually be called, which grabs the vector of recieved datagrams and replaces it with a new, empty vector.

Specified by:
run in interface java.lang.Runnable

receivedDatagrams

public java.util.Vector receivedDatagrams()
Returns a vector of datagrams that have been received by this object since the last time this method was called.


setTimeToLive

public void setTimeToLive(int pTTL)
This is JDK 1.2 specific code; if you have problems compiling under 1.1, this is probably the cause. JDK 1.2 introduced an int rather than a byte when setting the time-to-live on a socket; the setTTL(byte) method was deprecated in preference to the setTimeToLive(int) method. This just brings the code in line with the current practice. To make this work under 1.1, just comment out all of this method, recompile, and use the setTTL method instead. Set the multicast socket time-to-live. 15=> local distribution, 63=>regional distribution, 127=>global distribution.


setTTL

public void setTTL(byte pTTL)
Set the multicast socket time-to-live. 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.


sendDatagram

public void sendDatagram(java.net.DatagramPacket pDatagram,
                         java.lang.String pDestinationHost,
                         int pDestinationSocket)
Sends out a datagram to the designated destination and desitnation port.


sendDatagram

public void sendDatagram(java.net.DatagramPacket pDatagram)
Sends out a datagram. The destination should already have been set by the time it gets to this point.


cleanup

public void cleanup()
Closes down sockets nicely


finalize

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

Throws:
java.lang.Throwable

main

public static void main(java.lang.String[] args)
For testing purposes. Opens up a socket on the designated address and port, and listens for datagrams, storing them up. if you get datagrams, things are working. This also serves as an example of how to use the class.