simnet
Class DatagramSocket

java.lang.Object
  extended bysimnet.DatagramSocket
All Implemented Interfaces:
PlugoutListener

public class DatagramSocket
extends java.lang.Object
implements PlugoutListener

The Simnet implementation of a UDP Socket.


Field Summary
protected  boolean closed
          If this socket has been closed
(package private)  java.lang.Object consumer
          The object that will be calling recv on us (usually an Application)
static int CONSUMER_ID
          The notification code when the consumer gets plugged out (randomly chosen to avoid conflicts)
static int DEFAULT_QUEUE_SIZE
          The default size of the queue (0 = infinite)
protected  int ip
          This ip this socket is connected to, -1 for unconnected socket
protected  int local_port
          The local port this this socket is bound to, -1 if not bound
protected  int port
          This port this socket is connected to, -1 for unconnected socket
protected  int q_size
          The maximum permitted size of the queue
protected  int q_used
          The current size of the rxQueue
protected  java.util.LinkedList queue
          The linked list of DatagramPackets that were received
protected  Simulator sim
          A reference to the simulator
protected  long timeout
          The timeout for receiving packets, in ms (0=infinite)
(package private)  UDP udp
          The UDP transport we are using
(package private)  UDPStatistics us
          The statistics for this socket
 
Constructor Summary
(package private) DatagramSocket(UDP udp, java.lang.Object consumer)
          Create a new socket using the specified udp implementation.
 
Method Summary
 int available()
          How many objects are available for immediate reading.
 void bind(int port)
          Bind this socket to the specified port.
 void close()
          Close this socket (closed sockets may not be used again).
 void connect(int ip, int port)
          Connects this socket to the specified ip and port.
 void disconnect()
          Turns this socket into an unconnected socket (removes remote binding).
 void finalize()
           
 int getLocalPort()
          Returns the local port that this socket is bound to, or -1 if this socket is not bound.
(package private)  void in(DatagramPacket p)
          Called by udp when it receives a packet for the port this socket is bound to.
 boolean isBound()
          Returns whether or not this socket is bound.
 boolean isClosed()
          Returns whether or not this socket is closed.
 boolean isConnected()
          Returns whether or not this socket is connected.
 void plugoutNotification(int id, java.lang.Object replacement, java.lang.Object[] args)
          When the consumer is getting plugged out, close this socket (or transfer to our replacement).
 java.lang.Object recv()
          Wait for an object to be received on this socket, can only be used on connected sockets.
 DatagramPacket recvfrom()
          Receives a datagram from this socket.
 boolean send(java.lang.Object data)
          Send the given object on this socket, can only be used on connected sockets.
 boolean sendto(int dest, int dest_port, java.lang.Object data)
          Send the given data to the specified destination.
 void setTimeout(long timeout)
          Set the receive timeout for this socket to be the specified value (0=infinite)
 java.lang.String toString()
          Returns a string representation of this DatagramSocket
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

CONSUMER_ID

public static final int CONSUMER_ID
The notification code when the consumer gets plugged out (randomly chosen to avoid conflicts)

See Also:
Constant Field Values

DEFAULT_QUEUE_SIZE

public static final int DEFAULT_QUEUE_SIZE
The default size of the queue (0 = infinite)

See Also:
Constant Field Values

local_port

protected int local_port
The local port this this socket is bound to, -1 if not bound


ip

protected int ip
This ip this socket is connected to, -1 for unconnected socket


port

protected int port
This port this socket is connected to, -1 for unconnected socket


queue

protected java.util.LinkedList queue
The linked list of DatagramPackets that were received


q_size

protected int q_size
The maximum permitted size of the queue


q_used

protected int q_used
The current size of the rxQueue


timeout

protected long timeout
The timeout for receiving packets, in ms (0=infinite)


closed

protected boolean closed
If this socket has been closed


udp

UDP udp
The UDP transport we are using


consumer

java.lang.Object consumer
The object that will be calling recv on us (usually an Application)


us

UDPStatistics us
The statistics for this socket


sim

protected Simulator sim
A reference to the simulator

Constructor Detail

DatagramSocket

DatagramSocket(UDP udp,
               java.lang.Object consumer)
Create a new socket using the specified udp implementation. Should not be called directly, use UDP.createDatagramSocket.

Method Detail

setTimeout

public void setTimeout(long timeout)
                throws SimnetSocketException
Set the receive timeout for this socket to be the specified value (0=infinite)

Throws:
SimnetSocketException - if the timeout is negative

bind

public void bind(int port)
          throws SimnetSocketException
Bind this socket to the specified port. If -1 is specified, the socket will be bound to any free local port.

Parameters:
port - The port number to bind to
Throws:
SimnetSocketException - if this socket is already bound, is an invalid port or the port is already in use.

close

public void close()
Close this socket (closed sockets may not be used again). It unbinds, disconnects, and clears the queue.


connect

public void connect(int ip,
                    int port)
             throws SimnetSocketException
Connects this socket to the specified ip and port. Note that no messages are actually sent.

Throws:
SimnetSocketException - if the port is not postive, or this socket is already connected.

disconnect

public void disconnect()
Turns this socket into an unconnected socket (removes remote binding).


send

public boolean send(java.lang.Object data)
             throws SimnetSocketException
Send the given object on this socket, can only be used on connected sockets.

Parameters:
data - the object to send
Throws:
SimnetSocketException - if this socket is not connected.

recv

public java.lang.Object recv()
                      throws SimnetSocketException,
                             java.lang.InterruptedException
Wait for an object to be received on this socket, can only be used on connected sockets.

Returns:
the object received
Throws:
SimnetSocketException - if this socket is not connected.
java.lang.InterruptedException - if this thread is interrupted.

sendto

public boolean sendto(int dest,
                      int dest_port,
                      java.lang.Object data)
               throws SimnetSocketException
Send the given data to the specified destination.

Parameters:
dest - the destination ip address
dest_port - the destination port
data - the outgoing data
Throws:
SimnetSocketException - if this socket is connected

recvfrom

public DatagramPacket recvfrom()
                        throws SimnetSocketException,
                               java.lang.InterruptedException
Receives a datagram from this socket.

Returns:
the datagram received on this socket
Throws:
SimnetSocketException - if the socket is not bound, the socket is closed while waiting for a packet, or the timeout expired
java.lang.InterruptedException - if this thread is interrupted.

available

public int available()
How many objects are available for immediate reading.

Returns:
the number of packets that can be read now

in

void in(DatagramPacket p)
Called by udp when it receives a packet for the port this socket is bound to.

Parameters:
p - the packet received

isConnected

public boolean isConnected()
Returns whether or not this socket is connected.


isBound

public boolean isBound()
Returns whether or not this socket is bound.


getLocalPort

public int getLocalPort()
Returns the local port that this socket is bound to, or -1 if this socket is not bound.


isClosed

public boolean isClosed()
Returns whether or not this socket is closed.


toString

public java.lang.String toString()
Returns a string representation of this DatagramSocket


plugoutNotification

public void plugoutNotification(int id,
                                java.lang.Object replacement,
                                java.lang.Object[] args)
When the consumer is getting plugged out, close this socket (or transfer to our replacement).

Specified by:
plugoutNotification in interface PlugoutListener
Parameters:
id - The id passed to registerPlugoutNotification
replacement - the object that will be replacing the object that is being plugged out. It will be null if the object is getting plugged out, rather then being replaced with a new plugin.
args - were the args passed to registerPlugoutNotification

finalize

public void finalize()