seda.sandStorm.lib.aSocket
Class AUdpSocket

java.lang.Object
  extended by seda.sandStorm.core.SimpleSink
      extended by seda.sandStorm.lib.aSocket.AUdpSocket
All Implemented Interfaces:
ProfilableIF, SinkIF

public class AUdpSocket
extends SimpleSink

An AUdpSocket implements an asynchronous datagram socket. Applications create an AUdpSocket and associate a SinkIF with it. Packets received on the socket will be pushed onto the SinkIF as AUdpInPacket objects. The AUdpSocket can also be used to send messages to the socket, and to associate a default send address using the connect() method.

See Also:
AUdpInPacket

Field Summary
private  boolean closed
           
 SinkIF compQ
           
static int DEFAULT_MAX_PACKETSIZE
          The default maximum packet size read by the socket.
(package private)  java.net.InetAddress localaddress
           
(package private)  int localport
           
 int maxPacketSize
           
private  boolean readerstarted
           
(package private)  java.net.InetAddress remaddress
           
(package private)  int remport
           
(package private)  DatagramSockState sockState
           
 int writeClogThreshold
           
 
Constructor Summary
AUdpSocket(java.net.InetAddress localaddr, int localport, SinkIF compQ, int maxPacketSize, int writeClogThreshold)
          Create a socket bound to the given local address and local port.
AUdpSocket(int localport, SinkIF compQ)
          Create a socket bound to the given local port.
AUdpSocket(SinkIF compQ)
          Create a socket bound to any available local port.
 
Method Summary
 void close(SinkIF compQ)
          Close the socket.
 void connect(java.net.InetAddress addr, int port)
          Asynchronously connect this socket to the given port.
 void disconnect()
          Asynchronously disconnect this socket from the given port.
 boolean enqueue_lossy(QueueElementIF packet)
          Enqueue an outgoing packet to be written to this socket.
 void enqueue_many(QueueElementIF[] packets)
          Enqueue an set of outgoing packets to this socket.
 void enqueue(QueueElementIF packet)
          Enqueue an outgoing packet to be written to this socket.
 void flush(SinkIF compQ)
          Flush the socket.
 java.net.InetAddress getAddress()
          Return the InetAddress that this socket is connected to; returns null if not connected.
 java.net.InetAddress getLocalAddress()
          Return the local InetAddress for this socket.
 int getLocalPort()
          Return the local port for this socket.
 int getPort()
          Return the port that this socket is connected to; returns -1 if not connected.
 long getSequenceNumber()
          Returns the next sequence number for packets arriving on this socket.
 java.net.DatagramSocket getSocket()
           
 int profileSize()
          Returns the profile size of this connection.
 int size()
          Returns the number of elements currently waiting in the sink.
 void startReader(SinkIF receiveQ)
          Associate a SinkIF with this socket and allow data to start flowing into it.
 void startReader(SinkIF receiveQ, int readClogTries)
          Associate a SinkIF with this socket and allow data to start flowing into it.
 
Methods inherited from class seda.sandStorm.core.SimpleSink
enqueue_abort, enqueue_commit, enqueue_prepare, getEnqueuePredicate, setEnqueuePredicate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_MAX_PACKETSIZE

public static final int DEFAULT_MAX_PACKETSIZE
The default maximum packet size read by the socket.

See Also:
Constant Field Values

maxPacketSize

public int maxPacketSize

writeClogThreshold

public int writeClogThreshold

compQ

public SinkIF compQ

localaddress

java.net.InetAddress localaddress

remaddress

java.net.InetAddress remaddress

localport

int localport

remport

int remport

readerstarted

private boolean readerstarted

closed

private boolean closed

sockState

DatagramSockState sockState
Constructor Detail

AUdpSocket

public AUdpSocket(SinkIF compQ)
           throws java.io.IOException
Create a socket bound to any available local port. This is mainly used to create outgoing-only sockets.

Throws:
java.io.IOException

AUdpSocket

public AUdpSocket(int localport,
                  SinkIF compQ)
           throws java.io.IOException
Create a socket bound to the given local port.

Throws:
java.io.IOException

AUdpSocket

public AUdpSocket(java.net.InetAddress localaddr,
                  int localport,
                  SinkIF compQ,
                  int maxPacketSize,
                  int writeClogThreshold)
           throws java.io.IOException
Create a socket bound to the given local address and local port.

Parameters:
maxPacketSize - The maximum size, in bytes, of packets that this socket will attempt to receive. The default is DEFAULT_MAX_PACKETSIZE, which is 16 KBytes.
writeClogThreshold - The maximum number of outstanding writes on this socket before a SinkCloggedEvent is pushed to the connection's completion queue. This is effectively the maximum depth threshold for this connection's SinkIF. The default value is -1, which indicates that no SinkCloggedEvents will be generated.
Throws:
java.io.IOException
Method Detail

startReader

public void startReader(SinkIF receiveQ)
Associate a SinkIF with this socket and allow data to start flowing into it. When data is read, AUdpInPacket objects will be pushed into the given SinkIF. If this queue is full, the socket will attempt to allow packets to queue up in the O/S network stack (i.e. by not issuing further read calls on the socket). Until this method is called, no data will be read from the socket.


startReader

public void startReader(SinkIF receiveQ,
                        int readClogTries)
Associate a SinkIF with this socket and allow data to start flowing into it. When data is read, AUdpInPacket objects will be pushed into the given SinkIF. If this queue is full, the socket will attempt to allow packets to queue up in the O/S network stack (i.e. by not issuing further read calls on the socket). Until this method is called, no data will be read from the socket.

Parameters:
readClogTries - The number of times the aSocket layer will attempt to push an incoming packet onto the given SinkIF while the SinkIF is full. The queue entry will be dropped after this many tries. The default value is -1, which indicates that the aSocket layer will attempt to push the queue entry indefinitely.

enqueue

public void enqueue(QueueElementIF packet)
             throws SinkException
Enqueue an outgoing packet to be written to this socket. The packet must be of type BufferElement or AUdpPacket.

Specified by:
enqueue in interface SinkIF
Specified by:
enqueue in class SimpleSink
Parameters:
packet - The QueueElementIF to enqueue
Throws:
SinkFullException - Indicates that the sink is temporarily full.
SinkClosedException - Indicates that the sink is no longer being serviced.
SinkException

enqueue_lossy

public boolean enqueue_lossy(QueueElementIF packet)
Enqueue an outgoing packet to be written to this socket. The packet must be of type BufferElement or AUdpPacket. Drops the packet if it cannot be enqueued.

Specified by:
enqueue_lossy in interface SinkIF
Overrides:
enqueue_lossy in class SimpleSink
Parameters:
packet - The QueueElementIF to enqueue
Returns:
true if the element was enqueued, false otherwise.

enqueue_many

public void enqueue_many(QueueElementIF[] packets)
                  throws SinkException
Enqueue an set of outgoing packets to this socket. Each packet must be of type BufferElement or AUdpPacket.

Specified by:
enqueue_many in interface SinkIF
Overrides:
enqueue_many in class SimpleSink
Parameters:
packets - The element array to enqueue
Throws:
SinkFullException - Indicates that the sink is temporarily full.
SinkClosedException - Indicates that the sink is no longer being serviced.
SinkException

close

public void close(SinkIF compQ)
           throws SinkClosedException
Close the socket. A SinkClosedEvent will be posted on the given compQ when the close is complete.

Throws:
SinkClosedException

flush

public void flush(SinkIF compQ)
           throws SinkClosedException
Flush the socket. A SinkFlushedEvent will be posted on the given compQ when the close is complete.

Throws:
SinkClosedException

size

public int size()
Returns the number of elements currently waiting in the sink.

Specified by:
size in interface SinkIF
Overrides:
size in class SimpleSink

getSequenceNumber

public long getSequenceNumber()
Returns the next sequence number for packets arriving on this socket. Returns 0 if this socket is not active. Note that this method may return an inaccurate sequence number since the call is not synchronized with new message arrivals that may increment the sequence number.


profileSize

public int profileSize()
Returns the profile size of this connection.

Specified by:
profileSize in interface ProfilableIF
Overrides:
profileSize in class SimpleSink

connect

public void connect(java.net.InetAddress addr,
                    int port)
Asynchronously connect this socket to the given port. All send requests enqueued after this given connect call will use the given address and port as the default address. An AUdpConnectEvent will be pushed to the user when the connect has completed.


disconnect

public void disconnect()
Asynchronously disconnect this socket from the given port. An AUdpDisconnectEvent will be enqueued to the user when the disconnect has completed. If this socket is not connected then an AUdpDisconnectEvent will be pushed to the user regardless.


getAddress

public java.net.InetAddress getAddress()
Return the InetAddress that this socket is connected to; returns null if not connected.


getPort

public int getPort()
Return the port that this socket is connected to; returns -1 if not connected.


getLocalAddress

public java.net.InetAddress getLocalAddress()
Return the local InetAddress for this socket.


getLocalPort

public int getLocalPort()
Return the local port for this socket.


getSocket

public java.net.DatagramSocket getSocket()