// Based on PHP XML-RPC http://phpxmlrpc.sourceforge.net/
require_once("run/xmlrpc-300-beta/xmlrpc.inc");
/**
* Bare-bones RPC caller, which uses XML-RPC and provides
* functionality for "disconnection" when the server is down but the
* testing must go on.
* Good if considering retrieving data in other ways in the future, but using
* XML-RPC for now.
*/
class RPC {
private $hostName = "localhost";
private $serverName = null;
private $portNumber = -1;
private $debug = 0;
private $disconnected = false;
private $dummyfunction = false;
/**
* Create a new connectio nto the data server by specifying the server name and the port number.
* The hostname can also be specified, but is assumed to be localhost otherwise.
* @param string $serverName name of XML-RPC server
* @param int $portNumber the port that the server is listening on
* @param string $hostName Default value is localhost
*/
function __construct($serverName, $portNumber, $hostName='localhost') {
$this->serverName = $serverName;
$this->portNumber = $portNumber;
$this->hostName = $hostName;
$this->debug = 0;
$this->disconnected = false;
$this->dummyfunction = null;
}
/**
* If the argument is numeric, the internal, static debug variable
* will be set to this number. 0 (and less) is silent; anything above
* that will get ccarional output, and the number will also be passed
* to setDebug in the PHP XML-RPC call, where the max verbosity is at 3.
* @param type $amt
*/
function debug($amt) {
if (is_numeric($amt))
$this->debug = $amt;
}
/**
* Disconnects the client - no server call is made even if the script
* is used, but the user-defined function by name of $func is called
* with $method, $data that one normally calls RPC::get() with. The
* get() method will also return anythign that the user-defined
* function returns.
* @param string $dummyfunction must be the name of a function that serves
* data similar to what the server would otherwise provide.
*/
function disconnect($dummyfunction) {
$this->disconnected = true;
$this->dummyfunction = $dummyfunction;
if ($this->debug > 0)
echo "RPC disconnected, re-routed to <em>$dummyfunction</em>\n";
}
/**
* Makes a call, unless this has been disconnected (see disconnect(string)),
* in which case just calls the provided user-defined function with the same parameters as here.
* @param string $method the name of the method to call on the server
* @param array $data an array of values - these must be cast to what the PHP XML RPC encoder needs to know them as!
* @return mixed Whetever the server returns, having been wrung through the PHP XML RPC decoder
*/
function get($method, $data=array()) {
if ($this->disconnected) {
return call_user_func($this->dummyfunction, $method, $data);
} else {
if ($data != null) {
if (!is_array($data)) {
$data = array($data);
}
for ($i = 0; $i < count($data); $i++) {
$data[$i] = php_xmlrpc_encode($data[$i]);
}
}
$f = new xmlrpcmsg($this->serverName . "." . $method, $data);
$c = new xmlrpc_client("/xmlrpc", $this->hostName, $this->portNumber);
$c->setDebug($this->debug);
$r = &$c->send($f);
if (!$r->faultCode()) {
return php_xmlrpc_decode($r->value());
}
}
return "";
}
}