preliminary memcache support for PHP layer
[plcapi.git] / php / plc_api.php
1 <?php
2 //
3 // PlanetLab Central Slice API (PLCAPI) PHP interface
4 //
5 // DO NOT EDIT. This file was automatically generated at
6 // @DATE@.
7 //
8 // Mark Huang <mlhuang@cs.princeton.edu>
9 // Copyright (C) 2005-2006 The Trustees of Princeton University
10 //
11 // $Id$
12 // $URL$
13 //
14 //
15
16 require_once 'plc_config.php';
17
18 class PLCAPI
19 {
20   var $auth;
21   var $server;
22   var $port;
23   var $path;
24   var $errors;
25   var $trace;
26   var $calls;
27   var $multicall;
28
29   function PLCAPI($auth = NULL,
30                   $server = PLC_API_HOST,
31                   $port = PLC_API_PORT,
32                   $path = PLC_API_PATH,
33                   $cainfo = NULL)
34   {
35     $this->auth = $auth;
36     $this->server = $server;
37     $this->port = $port;
38     $this->path = $path;
39     $this->cainfo = $cainfo;
40     $this->errors = array();
41     $this->trace = array();
42     $this->calls = array();
43     $this->multicall = false;
44   }
45
46   function error_log($error_msg, $backtrace_level = 1)
47   {
48     $backtrace = debug_backtrace();
49     $file = $backtrace[$backtrace_level]['file'];
50     $line = $backtrace[$backtrace_level]['line'];
51
52     $this->errors[] = 'PLCAPI error:  ' . $error_msg . ' in ' . $file . ' on line ' . $line;
53     error_log(end($this->errors));
54   }
55
56   function error()
57   {
58     if (empty($this->trace)) {
59       return NULL;
60     } else {
61       $last_trace = end($this->trace);
62       return implode("\\n", $last_trace['errors']);
63     }
64   }
65
66   function trace()
67   {
68     return $this->trace;
69   }
70
71   function microtime_float()
72   {
73     list($usec, $sec) = explode(" ", microtime());
74     return ((float) $usec + (float) $sec);
75   }
76
77   function call($method, $args = NULL)
78   {
79     $memcache = new Memcache;
80     $memcache->connect($this->server, 11211) or die ("Could not connect");
81
82     $key = $method;
83     if (gettype($args[1]) == "array")
84     {
85         foreach(array_values($args[1]) as $arg)
86            $key .= $arg;
87     }
88     else
89         $key .= $args[1];
90
91     $result = $memcache->get($key);
92     if ($result != FALSE) {
93         if ($result == "NULL")
94             return NULL;
95         if (gettype($result) == "array")
96             return $result;
97         else
98             return unserialize($result);
99     }
100
101     if ($this->multicall) {
102       $this->calls[] = array ('methodName' => $method,
103                                 'params' => $args);
104       return NULL;
105     } else {
106       $result = $this->internal_call ($method, $args, 3);
107       if ($result == NULL)
108           $result = "NULL";
109       $memcache->set($key, $result, false, 500);
110       return $result;
111     }
112   }
113
114   function internal_call($method, $args = NULL, $backtrace_level = 2)
115   {
116     $curl = curl_init();
117
118     // Verify peer certificate if talking over SSL
119     if ($this->port == 443) {
120       curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 2);
121       if (!empty($this->cainfo)) {
122         curl_setopt($curl, CURLOPT_CAINFO, $this->cainfo);
123       } elseif (defined('PLC_API_CA_SSL_CRT')) {
124         curl_setopt($curl, CURLOPT_CAINFO, PLC_API_CA_SSL_CRT);
125       }
126       $url = 'https://';
127     } else {
128       $url = 'http://';
129     }
130
131     // Set the URL for the request
132     $url .= $this->server . ':' . $this->port . '/' . $this->path;
133     curl_setopt($curl, CURLOPT_URL, $url);
134
135     // Marshal the XML-RPC request as a POST variable. <nil/> is an
136     // extension to the XML-RPC spec that is supported in our custom
137     // version of xmlrpc.so via the 'allow_null' output_encoding key.
138     $request = xmlrpc_encode_request($method, $args, array('allow_null' => TRUE));
139     curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
140
141     // Construct the HTTP header
142     $header[] = 'Content-type: text/xml';
143     $header[] = 'Content-length: ' . strlen($request);
144     curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
145
146     // Set some miscellaneous options
147     curl_setopt($curl, CURLOPT_TIMEOUT, 180);
148
149     // Get the output of the request
150     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
151     $t0 = $this->microtime_float();
152     $output = curl_exec($curl);
153     $t1 = $this->microtime_float();
154
155     if (curl_errno($curl)) {
156       $this->error_log('curl: ' . curl_error($curl), true);
157       $ret = NULL;
158     } else {
159       $ret = xmlrpc_decode($output);
160       if (is_array($ret) && xmlrpc_is_fault($ret)) {
161         $this->error_log('Fault Code ' . $ret['faultCode'] . ': ' .
162                          $ret['faultString'], $backtrace_level, true);
163         $ret = NULL;
164       }
165     }
166
167     curl_close($curl);
168
169     $this->trace[] = array('method' => $method,
170                            'args' => $args,
171                            'runtime' => $t1 - $t0,
172                            'return' => $ret,
173                            'errors' => $this->errors);
174     $this->errors = array();
175
176     return $ret;
177   }
178
179   function begin()
180   {
181     if (!empty($this->calls)) {
182       $this->error_log ('Warning: multicall already in progress');
183     }
184
185     $this->multicall = true;
186   }
187
188   function commit()
189   {
190     if (!empty ($this->calls)) {
191       $ret = array();
192       $results = $this->internal_call ('system.multicall', array ($this->calls));
193       foreach ($results as $result) {
194         if (is_array($result)) {
195           if (xmlrpc_is_fault($result)) {
196             $this->error_log('Fault Code ' . $result['faultCode'] . ': ' .
197                              $result['faultString'], 1, true);
198             $ret[] = NULL;
199             // Thierry - march 30 2007 
200             // using $adm->error() is broken with begin/commit style 
201             // this is because error() uses last item in trace and checks for ['errors']
202             // when using begin/commit we do run internal_call BUT internal_call checks for 
203             // multicall's result globally, not individual results, so ['errors'] comes empty
204             // I considered hacking internal_call 
205             // to *NOT* maintain this->trace at all when invoked with multicall
206             // but it is too complex to get all values right
207             // so let's go for the hacky way, and just record individual errors at the right place
208             $this->trace[count($this->trace)-1]['errors'][] = end($this->errors);
209           } else {
210             $ret[] = $result[0];
211           }
212         } else {
213           $ret[] = $result;
214         }
215       }
216     } else {
217       $ret = NULL;
218     }
219
220     $this->calls = array();
221     $this->multicall = false;
222
223     return $ret;
224   }
225
226   //
227   // PLCAPI Methods
228   //
229
230   function __call($name, $args)
231   {
232      array_unshift($args, $this->auth);
233      return $this->call($name, $args);
234   }
235 }
236
237 global $adm;
238
239 $adm = new PLCAPI(array('AuthMethod' => "capability",
240                         'Username' => PLC_API_MAINTENANCE_USER,
241                         'AuthString' => PLC_API_MAINTENANCE_PASSWORD));
242
243 ?>