edu.virginia.cs.mng.hypercast
Class IncomingSocketObject

java.lang.Object
  |
  +--edu.virginia.cs.mng.hypercast.IncomingSocketObject

class IncomingSocketObject
extends java.lang.Object

This object manages an incoming socket connection. It provides synchronization for multiple threads receive()ing. It will also interrupt and receive()ing thread when the object is close()d.

This object manages a buffer of TCP_UnicastAdapter.RECEIVE_BUFFER_SIZE. It is a limit on the maximum size of a message.

This thread calls the createMessage() function of the I_AdapterCallBack implementer.


Field Summary
(package private)  I_AdapterCallBack callback
          The object implementing the createMessage() function.
(package private)  int endValidBytes
          One beyond the index of the final byte in receiveBuffer that was read from the InputStream.
(package private)  boolean inCallBack
          True while the synchronized thread is inside an I_AdapterCallBack function.
(package private)  java.io.InputStream is
          The InputStream of the socket.
(package private)  boolean isClosed
          True if the socket has been closed.
(package private)  long lastTimeAttemptToRead
          Ideally, the last time a thread performed the read() call to the InputStream.
(package private)  long lastTimeRead
          Ideally, the last time a thread returned from the read() call to the InputStream.
(package private)  TCP_UnicastAdapter parent
          The TCP_UnicastAdapter that created this object.
(package private)  byte[] receiveBuffer
          The buffer for holding data read from the InputStream.
(package private)  java.net.Socket s
          The socket.
(package private)  int[] startValidBytes
          startValidBytes[0] holds the location in receiveBuffer of first byte read from the InputStream.
(package private)  java.lang.Thread waitingThread
          The thread currently reading from the InputStream.
(package private)  java.lang.Object waitingThreadGuard
          A lock.
 
Constructor Summary
(package private) IncomingSocketObject(java.net.Socket sock, I_AdapterCallBack cb, TCP_UnicastAdapter p)
           
 
Method Summary
(package private)  void close()
          Closes the socket and interrupts any thread currently read()ing.
(package private)  boolean dataWaiting()
          Returns true if data is in buffer or InputStream has bytes available.
(package private)  boolean isClosed()
          Returns true if the thread will never call an function in I_AdapterCallBack again.
(package private)  boolean noAttemptToReadInTime(int timePeriod, long currentTime)
          Returns true if there a read call has not been made in the last timePeriod milliseconds.
(package private)  boolean noReadInTime(int timePeriod, long currentTime)
          Returns true if there a read has not completed in the last timePeriod milliseconds.
private  void readBytes()
          Private function that adds bytes to the buffer from the InputStream.
 I_Message receive()
          Returns a new message as gotten from I_AdapterCallBack.createMessage.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

s

java.net.Socket s
The socket.

is

java.io.InputStream is
The InputStream of the socket.

callback

I_AdapterCallBack callback
The object implementing the createMessage() function.

parent

TCP_UnicastAdapter parent
The TCP_UnicastAdapter that created this object.

receiveBuffer

byte[] receiveBuffer
The buffer for holding data read from the InputStream. The portion of the buffer containing the data is in receiveBuffer[startValidBytes[0]] to receiveBuffer[endValidBytes-1]

startValidBytes

int[] startValidBytes
startValidBytes[0] holds the location in receiveBuffer of first byte read from the InputStream. startValidBytes is an array of length 1.
See Also:
I_AdapterCallBack#createMessage

endValidBytes

int endValidBytes
One beyond the index of the final byte in receiveBuffer that was read from the InputStream.

waitingThread

java.lang.Thread waitingThread
The thread currently reading from the InputStream.

waitingThreadGuard

java.lang.Object waitingThreadGuard
A lock. Used to guarantee that interrupts do affect the waitingThread while it is reading from the InputStream.

isClosed

boolean isClosed
True if the socket has been closed.

inCallBack

boolean inCallBack
True while the synchronized thread is inside an I_AdapterCallBack function.

lastTimeAttemptToRead

long lastTimeAttemptToRead
Ideally, the last time a thread performed the read() call to the InputStream. Actually, every time the call is done, the value is rest to 0. If the variable's value is read as a 0, the value is reset to the current time. By this, we avoid calling System.currentTimeMillis() for every read().

lastTimeRead

long lastTimeRead
Ideally, the last time a thread returned from the read() call to the InputStream. Actually, every time the call completes, the value is rest to 0. If the variable's value is read as a 0, the value is reset to the current time. By this, we avoid calling System.currentTimeMillis() for every read().
Constructor Detail

IncomingSocketObject

IncomingSocketObject(java.net.Socket sock,
                     I_AdapterCallBack cb,
                     TCP_UnicastAdapter p)
               throws java.io.IOException
Method Detail

noReadInTime

boolean noReadInTime(int timePeriod,
                     long currentTime)
Returns true if there a read has not completed in the last timePeriod milliseconds.

noAttemptToReadInTime

boolean noAttemptToReadInTime(int timePeriod,
                              long currentTime)
Returns true if there a read call has not been made in the last timePeriod milliseconds.

dataWaiting

boolean dataWaiting()
Returns true if data is in buffer or InputStream has bytes available.

close

void close()
Closes the socket and interrupts any thread currently read()ing.

isClosed

boolean isClosed()
Returns true if the thread will never call an function in I_AdapterCallBack again.

readBytes

private void readBytes()
                throws java.io.IOException
Private function that adds bytes to the buffer from the InputStream.
Throws:
java.io.IOException - for socket error
java.io.InterruptedIOException - if socket is closed

receive

public I_Message receive()
                  throws java.io.IOException
Returns a new message as gotten from I_AdapterCallBack.createMessage. This function will only read from the InputStream if the buffer does not contain a whole message.
Throws:
java.lang.IllegalArgumentException - if bytestream gets corrupted (passed from restoreMessage())
java.io.IOException - for socket error
java.io.InterruptedIOException - if socket is closed