org.rlcommunity.rlglue.codec.network
Class Network

java.lang.Object
  extended by org.rlcommunity.rlglue.codec.network.Network

public class Network
extends java.lang.Object

This class does the heavy lifting of sendig and receiving data over the network. It is used by both the Java and Matlab codecs. The Socket has been changed (Feb 7 2009) to be offer a NON-BLOCKING option. The Java codec is JUST FINE using BLOCKING. NON-BLOCKING is for MATLAB. You may need NON-BLOCKING if you wanted to run an environment and/or agent and/or experiment all from the same Thread in Java. But why would you do that? You would have them in different Threads or preferably different processes, so none of this matters.

Author:
btanner

Field Summary
static int kAgentCleanup
           
static int kAgentConnection
           
static int kAgentEnd
           
static int kAgentInit
           
static int kAgentMessage
           
static int kAgentStart
           
static int kAgentStep
           
protected static int kByteBufferDefaultSize
           
protected static int kCharSize
           
static java.lang.String kDefaultHost
           
static int kDefaultPort
           
protected static int kDoubleSize
           
static int kEnvCleanup
           
static int kEnvInit
           
static int kEnvironmentConnection
           
static int kEnvMessage
           
static int kEnvStart
           
static int kEnvStep
           
static int kExperimentConnection
           
static int kIntSize
           
static int kRetryTimeout
           
static int kRLAgentEnd
           
static int kRLAgentMessage
           
static int kRLAgentStart
           
static int kRLAgentStep
           
static int kRLCleanup
           
static int kRLEnvMessage
           
static int kRLEnvStart
           
static int kRLEnvStep
           
static int kRLEpisode
           
static int kRLInit
           
static int kRLNumEpisodes
           
static int kRLNumSteps
           
static int kRLReturn
           
static int kRLStart
           
static int kRLStep
           
static int kRLTerm
           
protected  java.nio.channels.SocketChannel socketChannel
           
 
Constructor Summary
Network()
           
 
Method Summary
 void clearRecvBuffer()
           
 void clearSendBuffer()
           
protected static java.nio.ByteBuffer cloneWithCapacity(java.nio.ByteBuffer original, int capacity)
           
 void close()
           
 void connect(java.lang.String host, int port, int retryTimeout)
           
 boolean connect(java.lang.String host, int port, int retryTimeout, boolean blocking)
          Support for NON-BLOCKING added.
 boolean ensureConnected()
          This can be used in NON-BLOCKING mode to be sure that the connection was made.
protected  void ensureRecvCapacityRemains(int capacity)
           
protected  void ensureSendCapacityRemains(int capacity)
           
 void flipRecvBuffer()
           
 void flipSendBuffer()
           
 Action getAction()
           
 char getChar()
           
 double getDouble()
           
 double[] getDoubles(int howMany)
           
 int getInt()
           
 int getInt(int index)
           
 int[] getInts(int howMany)
           
 Observation getObservation()
           
 java.lang.String getString()
          Used for getting task spec and env/agent messages.
 boolean isConnected()
           
 void putAbstractType(RL_abstract_type theObject)
           
 void putAction(Action action)
           
 void putChar(char c)
          Brian Tanner adding this for RL-Glue 3.x compatibility Converts unicode (> 1 byte) char to 1 byte network char protocol
 void putDouble(double value)
           
 void putDoubles(double[] values)
          Experimental
 void putInt(int value)
           
 void putInts(int[] values)
          Experimental
 void putObservation(Observation obs)
           
 void putRewardObservation(Reward_observation_terminal rewardObservation)
           
 void putString(java.lang.String message)
           
 int recv(int size)
          This method has been updated.
 int recvNonBlock(int size)
          This method has been added.
 int send()
           
static int sizeOf(double value)
           
static int sizeOf(int value)
           
static int sizeOf(Reward_observation_terminal rewardObservation)
           
static int sizeOf(RL_abstract_type theObject)
           
static int sizeOf(java.lang.String string)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

kExperimentConnection

public static final int kExperimentConnection
See Also:
Constant Field Values

kAgentConnection

public static final int kAgentConnection
See Also:
Constant Field Values

kEnvironmentConnection

public static final int kEnvironmentConnection
See Also:
Constant Field Values

kAgentInit

public static final int kAgentInit
See Also:
Constant Field Values

kAgentStart

public static final int kAgentStart
See Also:
Constant Field Values

kAgentStep

public static final int kAgentStep
See Also:
Constant Field Values

kAgentEnd

public static final int kAgentEnd
See Also:
Constant Field Values

kAgentCleanup

public static final int kAgentCleanup
See Also:
Constant Field Values

kAgentMessage

public static final int kAgentMessage
See Also:
Constant Field Values

kEnvInit

public static final int kEnvInit
See Also:
Constant Field Values

kEnvStart

public static final int kEnvStart
See Also:
Constant Field Values

kEnvStep

public static final int kEnvStep
See Also:
Constant Field Values

kEnvCleanup

public static final int kEnvCleanup
See Also:
Constant Field Values

kEnvMessage

public static final int kEnvMessage
See Also:
Constant Field Values

kRLInit

public static final int kRLInit
See Also:
Constant Field Values

kRLStart

public static final int kRLStart
See Also:
Constant Field Values

kRLStep

public static final int kRLStep
See Also:
Constant Field Values

kRLCleanup

public static final int kRLCleanup
See Also:
Constant Field Values

kRLReturn

public static final int kRLReturn
See Also:
Constant Field Values

kRLNumSteps

public static final int kRLNumSteps
See Also:
Constant Field Values

kRLNumEpisodes

public static final int kRLNumEpisodes
See Also:
Constant Field Values

kRLEpisode

public static final int kRLEpisode
See Also:
Constant Field Values

kRLAgentMessage

public static final int kRLAgentMessage
See Also:
Constant Field Values

kRLEnvMessage

public static final int kRLEnvMessage
See Also:
Constant Field Values

kRLTerm

public static final int kRLTerm
See Also:
Constant Field Values

kRLEnvStart

public static final int kRLEnvStart
See Also:
Constant Field Values

kRLEnvStep

public static final int kRLEnvStep
See Also:
Constant Field Values

kRLAgentStart

public static final int kRLAgentStart
See Also:
Constant Field Values

kRLAgentStep

public static final int kRLAgentStep
See Also:
Constant Field Values

kRLAgentEnd

public static final int kRLAgentEnd
See Also:
Constant Field Values

kDefaultHost

public static final java.lang.String kDefaultHost
See Also:
Constant Field Values

kDefaultPort

public static final int kDefaultPort
See Also:
Constant Field Values

kRetryTimeout

public static final int kRetryTimeout
See Also:
Constant Field Values

kByteBufferDefaultSize

protected static final int kByteBufferDefaultSize
See Also:
Constant Field Values

kIntSize

public static final int kIntSize
See Also:
Constant Field Values

kDoubleSize

protected static final int kDoubleSize
See Also:
Constant Field Values

kCharSize

protected static final int kCharSize
See Also:
Constant Field Values

socketChannel

protected java.nio.channels.SocketChannel socketChannel
Constructor Detail

Network

public Network()
Method Detail

connect

public void connect(java.lang.String host,
                    int port,
                    int retryTimeout)

connect

public boolean connect(java.lang.String host,
                       int port,
                       int retryTimeout,
                       boolean blocking)
Support for NON-BLOCKING added. If you are using NON-BLOCKING, then be sure to call ensureConnected() afterward until it returns true.

Parameters:
host -
port -
retryTimeout -
blocking -

ensureConnected

public boolean ensureConnected()
This can be used in NON-BLOCKING mode to be sure that the connection was made. Probably call this in a loop. In BLOCKING mode, you don't need to call this.

Returns:
Throws:
java.io.IOException
Since:
2.02

close

public void close()
           throws java.io.IOException
Throws:
java.io.IOException

send

public int send()
         throws java.io.IOException
Throws:
java.io.IOException

recv

public int recv(int size)
         throws java.io.IOException
This method has been updated. It will work as it used to, before we added NON-BLOCKING mode. It will either return after reading size (or more) bytes or it will throw an exception.

Parameters:
size -
Returns:
Throws:
java.io.IOException

recvNonBlock

public int recvNonBlock(int size)
                 throws java.io.IOException
This method has been added. If the socket is in non-blockig mode, the It will return 0 if there is no data available. If you use the NON-BLOCKING option, it is possible that if there is no data available (for example, other components have not yet connected) then recv will return 0. If recv gets ANYTHING when it first tries, then it WILL continue and poll until it gets all the data it was asked for. It also will not throw a RLGlueDisconnectException if it socketChannel.read returns -1 before size bytes have been read. This is indicative that the other end of the socket is closed (IE, RL-Glue was quit or crashed).

Parameters:
size -
Returns:
Throws:
java.io.IOException
Since:
2.02

isConnected

public boolean isConnected()

clearSendBuffer

public void clearSendBuffer()

clearRecvBuffer

public void clearRecvBuffer()

flipSendBuffer

public void flipSendBuffer()

flipRecvBuffer

public void flipRecvBuffer()

getInts

public int[] getInts(int howMany)

getDoubles

public double[] getDoubles(int howMany)

getInt

public int getInt()

getInt

public int getInt(int index)

getDouble

public double getDouble()

getChar

public char getChar()
Returns:
ascii character encoded by the next byte.
Since:
2.0 Pull a char off the buffer. Slightly tricky because chars are unicode (more than a byte) in Java, but only 1 byte in our network protocol.

Thanks: http://bytes.com/forum/thread17160.html


getString

public java.lang.String getString()
Used for getting task spec and env/agent messages. UNLIKE the charArrays in observations/actions/etc, these strings are null terminated?

Returns:
Return a String from the network buffer

getObservation

public Observation getObservation()

getAction

public Action getAction()

putInts

public void putInts(int[] values)
Experimental

Parameters:
values -

putDoubles

public void putDoubles(double[] values)
Experimental

Parameters:
values -

putInt

public void putInt(int value)

putDouble

public void putDouble(double value)

putChar

public void putChar(char c)
Brian Tanner adding this for RL-Glue 3.x compatibility Converts unicode (> 1 byte) char to 1 byte network char protocol

Thanks http://bytes.com/forum/thread17160.html

Parameters:
c - character to put into the buffer as byte
Since:
2.0

putString

public void putString(java.lang.String message)
               throws java.io.UnsupportedEncodingException
Throws:
java.io.UnsupportedEncodingException

putAbstractType

public final void putAbstractType(RL_abstract_type theObject)

putObservation

public void putObservation(Observation obs)

putAction

public void putAction(Action action)

putRewardObservation

public void putRewardObservation(Reward_observation_terminal rewardObservation)

ensureSendCapacityRemains

protected void ensureSendCapacityRemains(int capacity)

ensureRecvCapacityRemains

protected void ensureRecvCapacityRemains(int capacity)

cloneWithCapacity

protected static java.nio.ByteBuffer cloneWithCapacity(java.nio.ByteBuffer original,
                                                       int capacity)

sizeOf

public static int sizeOf(int value)

sizeOf

public static int sizeOf(double value)

sizeOf

public static int sizeOf(java.lang.String string)

sizeOf

public static int sizeOf(RL_abstract_type theObject)

sizeOf

public static int sizeOf(Reward_observation_terminal rewardObservation)