com.equilibrium.util.api
Class MGConnection

java.lang.Object
  extended by com.equilibrium.util.api.MGConnection

public class MGConnection
extends java.lang.Object

A connection to a MediaGenerator.

Creates a persistent socket connection to the Media Generator which can be used to service multiple MediaGenerator requests.

A connection is established to a Media Generator by instantiating a MGConnection object. The constructor can be called with no arguments to connect to a Media Generator running on the same machine as the client program. To connect to a Generator on another machine, the name or IP of that machine is passed to the constructor. A port number can also be passed to the constructor. This is only necessary if the MediaGenerator is listening for requests on a non-standard port number.

Two kinds of jobs can be submitted to the MediaGenerator; 1) simple requests and 2) batch jobs.

Simple Requests

Simple requests consist of the execution of a single script. These requests are synchronous. That is, the client must block while the requested operation is performed if it wishes to receive back any results from the request.

Simple requests begin by calling the createRequest() method on the MGConnection object. This method returns a MGRequest object. Refer to the documentation for that object for more information on submitting simple requests.

Batch Requests

Batch requests consist of a series of individual script executions that together define a single batch operation. Batch operations are executed asynchronously. That is, once the client has fully defined and submitted a batch request, it can go on and do other things. In this case, the client will normally contact the Media Generator periodically to check on the status of the batch jobs it has submitted. When it sees that a batch job has been completed, it may choose to make additional requests to the Media Generator to obtain the results of the batch operation.

To submit a batch job, the client code calls the createBatch() method on the MGConnection object. This method returns a MGBatch object. Refer to the documentation for that object for more information on submitting batch jobs.

See Also:
MGRequest, MGBatch

Nested Class Summary
static class MGConnection.Flags
          Flags intended for use with various methods on the MGConnection class.
 
Constructor Summary
MGConnection()
          Construct a default (localhost:9877) connection
MGConnection(java.lang.String hostname)
          Construct a connection with the specified host and default port.
MGConnection(java.lang.String hostname, int port)
          Construct a connection to the specified host and port.
MGConnection(java.lang.String hostname, int port, int timeout)
          Construct a connection to the specified host and port with the specified timeout.
 
Method Summary
 void batchAbort(int batchId, java.lang.String batchName)
          Abort the specified batch.
 MGBatchesExecutingResponse batchesExecuting()
          Return the ids of the batches currently executing.
 void batchForget(int batchID)
          Tell the server to forget about the batch with the specified id.
 MGBatchStatusResponse batchStatus(int batchID)
          Return the batch status of the batch with the specified id.
 MGBatchStatusResponse batchStatus(int batchID, int flags)
          Return the batch status of the batch with the specified id based on the specified flags.
 void close()
          Close and cleanup this connection.
 MGBatch createBatch()
          Create an unnamed Batch.
 MGBatch createBatch(java.lang.String batchName)
          Create a named Batch.
 MGBatch createBatch(java.lang.String batchName, boolean trackCompletedJobs)
          Create a named Batch and specify whether to track completed jobs.
 MGRequest createRequest()
          Create a new request.
 int getConnectionTimeout()
          Return this connection's timeout value.
 java.lang.String getHostName()
          Return the host name for this connection.
 int getPort()
          Return the port for this connection.
 MGFileResponse requestFile(java.lang.String path)
          Create a request for a file from the MediaGenerator.
 MGFileResponse requestFile(java.lang.String path, int offset)
          Create a request for a file from the MediaGenerator.
 long requestSessionId()
          Return the session ID for this request.
 MGFileResponse requestSharedFile(java.lang.String path, int offset)
          Create a request for a file from the MediaGenerator.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MGConnection

public MGConnection()
             throws java.io.IOException
Construct a default (localhost:9877) connection

Throws:
java.io.IOException

MGConnection

public MGConnection(java.lang.String hostname)
             throws java.io.IOException
Construct a connection with the specified host and default port.

Parameters:
hostname - the host to connect to.
Throws:
java.io.IOException

MGConnection

public MGConnection(java.lang.String hostname,
                    int port)
             throws java.io.IOException
Construct a connection to the specified host and port.

Parameters:
hostname - the host.
port - the port.
Throws:
java.io.IOException

MGConnection

public MGConnection(java.lang.String hostname,
                    int port,
                    int timeout)
             throws java.io.IOException
Construct a connection to the specified host and port with the specified timeout. Wait only timeout milliseconds for the MediaGenerator to respond. Specify a negative value to wait forever.

Parameters:
hostname - the hostname for the MediaGenerator.
port - the port
timeout - the number of milliseconds to wait.
Throws:
java.io.IOException
Method Detail

getHostName

public java.lang.String getHostName()
Return the host name for this connection.

Returns:
the host name.

getPort

public int getPort()
Return the port for this connection.

Returns:
the port.

getConnectionTimeout

public int getConnectionTimeout()
Return this connection's timeout value.

Returns:
timeout in milliseconds.

createRequest

public MGRequest createRequest()
                        throws java.io.IOException
Create a new request. The resulting object is used to specify the parameters for executing a specified script on the MediaGenerator.

Returns:
the request object.
Throws:
java.io.IOException

createBatch

public MGBatch createBatch()
                    throws java.io.IOException,
                           MGException
Create an unnamed Batch. Each batch can handle the submission of multiple, related jobs. Each batch is assigned an id which can be used to identify jobs in the batch.

Returns:
the batch object.
Throws:
java.io.IOException
MGException

createBatch

public MGBatch createBatch(java.lang.String batchName,
                           boolean trackCompletedJobs)
                    throws java.io.IOException,
                           MGException
Create a named Batch and specify whether to track completed jobs. The batch name specified is attached to the batch and may be used to display information about the batch to the user.

Parameters:
batchName - the batch name.
trackCompletedJobs - if true retain status about jobs after batch ends.
Returns:
the new batch.
Throws:
java.io.IOException
MGException
See Also:
MGConnection.createBatch()

createBatch

public MGBatch createBatch(java.lang.String batchName)
                    throws java.io.IOException,
                           MGException
Create a named Batch. The batch name specified is attached to the batch and may be used to display information about the batch to the user.

Parameters:
batchName - the batch name.
Returns:
the new batch.
Throws:
java.io.IOException
MGException
See Also:
MGConnection.createBatch()

requestFile

public MGFileResponse requestFile(java.lang.String path)
                           throws java.io.IOException,
                                  MGException
Create a request for a file from the MediaGenerator. The path specifies a file using MediaRich's virtual file system.

Parameters:
path - the path to the file.
Returns:
the file response.
Throws:
java.io.IOException
MGException

requestSharedFile

public MGFileResponse requestSharedFile(java.lang.String path,
                                        int offset)
                                 throws java.io.IOException,
                                        MGException
Create a request for a file from the MediaGenerator. The path specifies a file using MediaRich's virtual file system. The offset parameter specifies an offset at which to start reading the file.

Parameters:
path - the path to the file.
offset - the offset at which to start reading the file.
Returns:
the file response.
Throws:
java.io.IOException
MGException

requestFile

public MGFileResponse requestFile(java.lang.String path,
                                  int offset)
                           throws java.io.IOException,
                                  MGException
Create a request for a file from the MediaGenerator. The path specifies a file using MediaRich's virtual file system. The offset parameter specifies an offset at which to start reading the file.

Parameters:
path - the path to the file.
offset - the offset at which to start reading the file.
Returns:
the file response.
Throws:
java.io.IOException
MGException

batchStatus

public MGBatchStatusResponse batchStatus(int batchID,
                                         int flags)
                                  throws java.io.IOException,
                                         MGException
Return the batch status of the batch with the specified id based on the specified flags.

Parameters:
batchID - the batch id for the batch.
flags - flags that determine what information is returned
Returns:
the response containing status information.
Throws:
java.io.IOException
MGException
See Also:
MGConnection.Flags

batchStatus

public MGBatchStatusResponse batchStatus(int batchID)
                                  throws java.io.IOException,
                                         MGException
Return the batch status of the batch with the specified id.

Parameters:
batchID - the batch id for the batch.
Returns:
the response containing status information.
Throws:
java.io.IOException
MGException

batchForget

public void batchForget(int batchID)
                 throws java.io.IOException,
                        MGException
Tell the server to forget about the batch with the specified id. Causes the batch's status and any batch results still on the server to be discarded.

Parameters:
batchID - the batch id for the batch.
Throws:
java.io.IOException
MGException

batchesExecuting

public MGBatchesExecutingResponse batchesExecuting()
                                            throws java.io.IOException,
                                                   MGException
Return the ids of the batches currently executing.

Returns:
the executing response containing the batch id list.
Throws:
java.io.IOException
MGException

batchAbort

public void batchAbort(int batchId,
                       java.lang.String batchName)
                throws java.io.IOException
Abort the specified batch. Both batchId and batchName must match an existing batch. All jobs relating to this job are removed from the queue.

Parameters:
batchId - the id for the batch to be aborted.
batchName - the name of the batch to be aborted.
Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException
Close and cleanup this connection.

Throws:
java.io.IOException

requestSessionId

public long requestSessionId()
                      throws MGException
Return the session ID for this request. The session ID uniquely identifies an instance of the MediaGenerator. This value changes when the MediaGenerator is started and stopped and is used to validate cached virtual file system paths.

Returns:
the MediaGenerator session id.
Throws:
MGException