Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS | NEXT CLASS FRAMES  | NO FRAMES
SUMMARY:  INNER | FIELD | CONSTR | METHOD DETAIL:  FIELD | CONSTR | METHOD

Class mil.navy.nps.dis.NetworkMonitor

java.lang.Object
  |
  +--mil.navy.nps.dis.NetworkMonitor

public class NetworkMonitor
extends java.lang.Object
implements java.lang.Runnable
Version:
1.0
Author:
Don McGregor (http://www.stl.nps.navy.mil/~mcgredo)
Location:
http://www.stl.nps.navy.mil/dis-java-vrml/mil/navy/nps/dis/NetworkMonitor.java
Hierarchy Diagram:
Summary:
The NetworkMonitor provides an interface for getting PDUs from the wire, decoupled from any blocking reads on the socket, and digested from the wire format into PDU objects.
Explanation
This class contains a thread object, which is configured to read from the wire as quickly as possible, so that as few packets fall onto the floor as possible. The reads are buffered up in a Vector object of datagrams. When the DIS application comes asking for PDUs (in its own thread) the datagram buffer is swapped out and replaced by a new one. Once the DIS application thread has exclusive use of the vector buffer, it can translate the datagrams into full-fledged PDU objects, which are saved in another buffer. If duplicate ESPDUs exist, they can be culled either before translating, by directly accessing the datagram data, or culled after translating them into ESPDUs. Finally, the vector of PDUs is returned to the DIS application, to do with as it pleases.

Since there are typcially two threads operating on this data, we need to be careful about synchronized access here, so the threads don't corrupt the internal data structures.

This object is typically configured with the port it's going to be reading from. When instantiated, a socket on that port is opened, and a thread spawned to read it. It then begins to continuously read data.

Basically, this object encapsulates both a socket and a thread that reads from the socket. The advantage is that this makes the external interface very simple; as far as the consumer is concerned, he asks for and gets PDUs that have arrived since the last time he asked, without having to worry about blocking. Possible disadvantages include buffer overflow. Right now there's no guard against this, so if the consumer doesn't ask for PDUs, and the packets keep coming in, the datagram buffer will keep on storing them up. We don't worry about thread or socket cleanup, since the expectation is that the whole shebang will get flushed only when the whole application quits.

The object implements the Runnable interface. To create a new instance of the network monitor, you need to do something like this:

NetworkMonitor aNetworkMonitor = new NetworkMonitor(8008); // pass in socket no Thread aThread = new Thread(aNetworkMonitor); aNetworkMonitor.setThread(aThread); aThread.start(); There's a potential problem also if the consumer of PDU objects asks for PDUs too quickly. If the consumer thread is in a tight loop, the datagramBuffer spends so much time locked that the reader thread can't add to it. Not sure about the best way to fix this....

"Monitor" is probably a bad term, since it overloads some other terminology in Java. "NetworkListener" would probably be better. Also, this should probably be broken out so that an abstract class NetworkMonitor has two concrete sublcasses, NetworkMonitorUnicast and NetworkMonitorMulticast.

History:
15Nov96 /Don McGregor /New
10Dec96 /Don McGregor /Multicast changed over to 1.1 API spec
7Jun97 /Don Brutzman /Upgrading for JSAI use
19Aug97 /Don McGregor /Made "multicast" terminology consistent
16May98 /Don Brutzman /changes in documentation
See Also:
SimulationManager, java.lang.Object, java.lang.Thread, java.lang.Runnable, java.net.DatagramSocket, java.net.MulticastSocket

Field Summary
java.util.Vector datagramBuffer
           
int datagramPort
           
java.net.DatagramSocket datagramSocket
           
java.lang.String multicastAddress
           
java.net.MulticastSocket multicastSocket
           
netscape.security.PrivilegeManager privilegeManager
           
java.lang.Thread readerThread
           
byte ttl
           
boolean usingMulticast
           
 
Constructor Summary
NetworkMonitor(int pDatagramPort)
           
NetworkMonitor(java.lang.String pMulticastAddress, int pDatagramPort)
           
 
Method Summary
void finalize()
          Finalize method--used to clean up any sockets that are still open
java.util.Vector receivedPdus()
           
void resumeReading()
           
void run()
           
void sendPDUMcast(ProtocolDataUnit pPdu)
           
void sendPDU(ProtocolDataUnit pdu, java.lang.String pDestinationHost, int pDestinationSocket)
           
void setThread(java.lang.Thread pThread)
           
void setTTL(byte pTTL)
          Set the multicast socket time-to-live. 15=> local distribution, 63=>regional distribution, 127=>global distribution.
void suspendReading()
           
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notifyAll, notify, toString, wait, wait, wait
 

Field Detail

usingMulticast

boolean usingMulticast

datagramPort

int datagramPort

datagramSocket

java.net.DatagramSocket datagramSocket

multicastSocket

java.net.MulticastSocket multicastSocket

multicastAddress

java.lang.String multicastAddress

ttl

byte ttl

readerThread

java.lang.Thread readerThread

datagramBuffer

java.util.Vector datagramBuffer

privilegeManager

netscape.security.PrivilegeManager privilegeManager
Constructor Detail

NetworkMonitor

public NetworkMonitor(int pDatagramPort)
Throws:
java.lang.RuntimeException - when socket error occurs.

NetworkMonitor

public NetworkMonitor(java.lang.String pMulticastAddress,
                      int pDatagramPort)
Method Detail

run

public void run()
Description copied from interface:
 
Specified by:
run() in interface java.lang.Runnable

receivedPdus

public java.util.Vector receivedPdus()

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.

sendPDUMcast

public void sendPDUMcast(ProtocolDataUnit pPdu)

sendPDU

public void sendPDU(ProtocolDataUnit pdu,
                    java.lang.String pDestinationHost,
                    int pDestinationSocket)

setThread

public void setThread(java.lang.Thread pThread)

suspendReading

public void suspendReading()

resumeReading

public void resumeReading()

finalize

protected void finalize()
Finalize method--used to clean up any sockets that are still open
Overrides:
finalize in class java.lang.Object

Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS | NEXT CLASS FRAMES  | NO FRAMES
SUMMARY:  INNER | FIELD | CONSTR | METHOD DETAIL:  FIELD | CONSTR | METHOD