2.17.2: 2011-06-17

net.sf.basedb.util.jobagent
Class JobAgentConnection

java.lang.Object
  extended by net.sf.basedb.util.jobagent.JobAgentConnection

public class JobAgentConnection
extends Object

This class is used by client applications to communicate with job agents. It used to send commands to job agents. For example, it can send start, stop and info commands.

Version:
2.0
Author:
nicklas
See Also:
JobAgentServerConnection
Last modified
$Date: 2010-10-19 13:02:17 +0200 (Tue, 19 Oct 2010) $

Field Summary
private static Pattern HEADER_REGEXP
          Regexp used to parse a typical answer.
private  int port
           
private  String server
           
private  int timeout
           
 
Constructor Summary
JobAgentConnection(int port, int timeout)
          Create a connection to the local job agent running on the specified port.
JobAgentConnection(String server, int port, int timeout)
          Create a connection to the job agent running on the specified server and listening on the specified port.
 
Method Summary
 void close()
          Close the connection to the job agent.
private  Socket connect()
          Create a socket connection to the job agent.
 JobAgentInfo getInfo(boolean full)
          Send an info or status request to the job agent.
static Map<String,List<String>> parseAnswer(String answer)
          Utility method for parsing a 'typical' answer from a job agent.
static Map<String,String> parseSimpleAnswer(String answer)
          Utility method for parsing a 'typical' answer from a job agent.
 String send(String cmd)
          Send a remote control command to the job agent.
private  String sendCmd(Socket socket, String cmd)
          Send a command to the job agent and return the answer.
 String sendPause()
           
 String sendPing()
          Send the ping command to the job agent.
 String sendStart()
          Send a start request to the job agent.
 String sendStop()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

HEADER_REGEXP

private static final Pattern HEADER_REGEXP
Regexp used to parse a typical answer.


server

private final String server

port

private final int port

timeout

private final int timeout
Constructor Detail

JobAgentConnection

public JobAgentConnection(int port,
                          int timeout)
Create a connection to the local job agent running on the specified port.

Parameters:
port - The port the job agent is listening on
timeout - The timeout in milliseconds

JobAgentConnection

public JobAgentConnection(String server,
                          int port,
                          int timeout)
Create a connection to the job agent running on the specified server and listening on the specified port.

Parameters:
server - The server the job agent is running on, or null to connect to the local host
port - The port the job agent is listening on
timeout - The timeout in milliseconds
Method Detail

parseAnswer

public static Map<String,List<String>> parseAnswer(String answer)
Utility method for parsing a 'typical' answer from a job agent. The typical answer is a string that contains a key-value pair on each line, separated by a colon. This method allows multiple entries for the same key.

Parameters:
answer - The answer
Returns:
A map with the key as index and a list with all values
Since:
2.16

parseSimpleAnswer

public static Map<String,String> parseSimpleAnswer(String answer)
Utility method for parsing a 'typical' answer from a job agent. The typical answer is a string that contains a key-value pair on each line, separated by a colon. This method allows only a single entry for the same key.

Parameters:
answer - The answer
Returns:
A map with the key as index to the value
Since:
2.16

sendPing

public String sendPing()
                throws IOException
Send the ping command to the job agent. The only answer to this command is OK.

Returns:
OK if successful
Throws:
IOException - If there is an error

sendStart

public String sendStart()
                 throws IOException
Send a start request to the job agent.

Returns:
OK if successful
Throws:
IOException - If there is an error

sendStop

public String sendStop()
                throws IOException
Throws:
IOException

sendPause

public String sendPause()
                 throws IOException
Throws:
IOException

getInfo

public JobAgentInfo getInfo(boolean full)
                     throws IOException
Send an info or status request to the job agent. The difference is that a status request doesn't return information about cpu, memory or executing jobs.

Parameters:
full - If TRUE a info request is sent, othwerwise a status request is sent
Returns:
A JobAgentInfo object containing the information
Throws:
IOException - If there is an error

close

public void close()
Close the connection to the job agent. In the current implementation all connections are stateless and this method does nothing. This might change in the future if a more advanced protocol is implemented. Clients should always call this method.


send

public String send(String cmd)
            throws IOException
Send a remote control command to the job agent.

Parameters:
cmd - The command to send
Returns:
The answer
Throws:
IOException - If there is an error
Since:
2.16 (was private before that)

connect

private Socket connect()
                throws IOException
Create a socket connection to the job agent.

Returns:
The socket
Throws:
IOException

sendCmd

private String sendCmd(Socket socket,
                       String cmd)
                throws IOException
Send a command to the job agent and return the answer. If the answer is FAILED an exception will be thrown.

Parameters:
socket - The socket used for communicating with the job agent
cmd - The command to send
Returns:
The answer from the job server
Throws:
IOException - If there is a communications error

2.17.2: 2011-06-17