3 // PlanetLab Central Slice API (PLCAPI) PHP interface
5 // DO NOT EDIT. This file was automatically generated at
8 // Mark Huang <mlhuang@cs.princeton.edu>
9 // Copyright (C) 2005-2006 The Trustees of Princeton University
16 require_once 'plc_config.php';
29 function PLCAPI($auth = NULL,
30 $server = PLC_API_HOST,
36 $this->server = $server;
39 $this->cainfo = $cainfo;
40 $this->errors = array();
41 $this->trace = array();
42 $this->calls = array();
43 $this->multicall = false;
46 function error_log($error_msg, $backtrace_level = 1)
48 $backtrace = debug_backtrace();
49 $file = $backtrace[$backtrace_level]['file'];
50 $line = $backtrace[$backtrace_level]['line'];
52 $this->errors[] = 'PLCAPI error: ' . $error_msg . ' in ' . $file . ' on line ' . $line;
53 error_log(end($this->errors));
58 if (empty($this->trace)) {
61 $last_trace = end($this->trace);
62 return implode("\\n", $last_trace['errors']);
71 function microtime_float()
73 list($usec, $sec) = explode(" ", microtime());
74 return ((float) $usec + (float) $sec);
77 function call($method, $args = NULL)
79 $memcache = new Memcache;
80 $memcache->connect($this->server, 11211) or die ("Could not connect");
83 if (gettype($args[1]) == "array")
85 foreach(array_values($args[1]) as $arg)
91 $result = $memcache->get($key);
92 if ($result != FALSE) {
93 if ($result == "NULL")
95 if (gettype($result) == "array")
98 return unserialize($result);
101 if ($this->multicall) {
102 $this->calls[] = array ('methodName' => $method,
106 $result = $this->internal_call ($method, $args, 3);
107 $memcache->set($key, $result == NULL ? "NULL" : $result, false, 600);
112 function internal_call($method, $args = NULL, $backtrace_level = 2)
116 // Verify peer certificate if talking over SSL
117 if ($this->port == 443) {
118 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 2);
119 if (!empty($this->cainfo)) {
120 curl_setopt($curl, CURLOPT_CAINFO, $this->cainfo);
121 } elseif (defined('PLC_API_CA_SSL_CRT')) {
122 curl_setopt($curl, CURLOPT_CAINFO, PLC_API_CA_SSL_CRT);
129 // Set the URL for the request
130 $url .= $this->server . ':' . $this->port . '/' . $this->path;
131 curl_setopt($curl, CURLOPT_URL, $url);
133 // Marshal the XML-RPC request as a POST variable. <nil/> is an
134 // extension to the XML-RPC spec that is supported in our custom
135 // version of xmlrpc.so via the 'allow_null' output_encoding key.
136 $request = xmlrpc_encode_request($method, $args, array('allow_null' => TRUE));
137 curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
139 // Construct the HTTP header
140 $header[] = 'Content-type: text/xml';
141 $header[] = 'Content-length: ' . strlen($request);
142 curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
144 // Set some miscellaneous options
145 curl_setopt($curl, CURLOPT_TIMEOUT, 180);
147 // Get the output of the request
148 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
149 $t0 = $this->microtime_float();
150 $output = curl_exec($curl);
151 $t1 = $this->microtime_float();
153 if (curl_errno($curl)) {
154 $this->error_log('curl: ' . curl_error($curl), true);
157 $ret = xmlrpc_decode($output);
158 if (is_array($ret) && xmlrpc_is_fault($ret)) {
159 $this->error_log('Fault Code ' . $ret['faultCode'] . ': ' .
160 $ret['faultString'], $backtrace_level, true);
167 $this->trace[] = array('method' => $method,
169 'runtime' => $t1 - $t0,
171 'errors' => $this->errors);
172 $this->errors = array();
179 if (!empty($this->calls)) {
180 $this->error_log ('Warning: multicall already in progress');
183 $this->multicall = true;
188 if (!empty ($this->calls)) {
190 $results = $this->internal_call ('system.multicall', array ($this->calls));
191 foreach ($results as $result) {
192 if (is_array($result)) {
193 if (xmlrpc_is_fault($result)) {
194 $this->error_log('Fault Code ' . $result['faultCode'] . ': ' .
195 $result['faultString'], 1, true);
197 // Thierry - march 30 2007
198 // using $adm->error() is broken with begin/commit style
199 // this is because error() uses last item in trace and checks for ['errors']
200 // when using begin/commit we do run internal_call BUT internal_call checks for
201 // multicall's result globally, not individual results, so ['errors'] comes empty
202 // I considered hacking internal_call
203 // to *NOT* maintain this->trace at all when invoked with multicall
204 // but it is too complex to get all values right
205 // so let's go for the hacky way, and just record individual errors at the right place
206 $this->trace[count($this->trace)-1]['errors'][] = end($this->errors);
218 $this->calls = array();
219 $this->multicall = false;
228 function __call($name, $args)
230 array_unshift($args, $this->auth);
231 return $this->call($name, $args);
237 $adm = new PLCAPI(array('AuthMethod' => "capability",
238 'Username' => PLC_API_MAINTENANCE_USER,
239 'AuthString' => PLC_API_MAINTENANCE_PASSWORD));