14 |
## $Id$ |
## $Id$ |
15 |
## -------------------------------------------------------------------------- |
## -------------------------------------------------------------------------- |
16 |
## $Log$ |
## $Log$ |
17 |
|
## Revision 1.4 2003/03/28 03:00:12 joko |
18 |
|
## enhanced error- and exception-handling |
19 |
|
## |
20 |
|
## Revision 1.3 2003/03/27 16:05:01 joko |
21 |
|
## enhanced 'function _call': debugging, tracing and autodisconnect now parametrized |
22 |
|
## |
23 |
## Revision 1.2 2003/03/05 23:16:48 joko |
## Revision 1.2 2003/03/05 23:16:48 joko |
24 |
## updated docu - phpDocumentor is very strict about its 'blocks'... |
## updated docu - phpDocumentor is very strict about its 'blocks'... |
25 |
## |
## |
98 |
class DataSource_Proxy_XMLRPC extends Class_Logger { |
class DataSource_Proxy_XMLRPC extends Class_Logger { |
99 |
|
|
100 |
var $configured; |
var $configured; |
101 |
var $meta; |
var $meta = array(); |
102 |
|
var $response = null; |
103 |
|
var $errors = array(); |
104 |
|
|
105 |
function DataSource_Proxy_XMLRPC($args = array()) { |
function DataSource_Proxy_XMLRPC($args = array()) { |
106 |
|
|
107 |
parent::constructor(); |
parent::constructor(); |
108 |
|
|
109 |
//print Dumper($this, $args); |
//print Dumper($this, $args); |
110 |
|
//print Dumper($args); |
111 |
|
|
112 |
global $Data_Driver_RPC_Remote_meta; |
global $Data_Driver_RPC_Remote_meta; |
113 |
session_register_safe("Data_Driver_RPC_Remote_meta"); |
session_register_safe("Data_Driver_RPC_Remote_meta"); |
114 |
$this->meta = $Data_Driver_RPC_Remote_meta; |
$this->meta = $Data_Driver_RPC_Remote_meta; |
115 |
|
|
116 |
|
// force connect? |
117 |
|
|
118 |
|
// V1 |
119 |
|
/* |
120 |
if (!isset($this->meta[connected]) || $args[connect]) { |
if (!isset($this->meta[connected]) || $args[connect]) { |
121 |
$this->meta[connected] = 1; |
$this->meta[connected] = 1; |
122 |
} |
} |
123 |
|
*/ |
124 |
|
|
125 |
|
// V2 |
126 |
|
$this->FORCE_CONNECT = $args[connect]; |
127 |
|
|
128 |
$this->_save_meta(); |
$this->_save_meta(); |
129 |
|
|
130 |
if ($args['Host']) { $this->host = $args['Host']; } |
|
131 |
if ($args['Port']) { $this->port = $args['Port']; } |
// merge args - V1 |
132 |
|
//if ($args['Host']) { $this->HOST = $args['Host']; } |
133 |
|
//if ($args['Port']) { $this->PORT = $args['Port']; } |
134 |
|
|
135 |
|
// merge args - V2 |
136 |
|
// TODO: php::merge_to($this, $args); |
137 |
|
//php::merge_to($this, $args); |
138 |
|
//print Dumper($args); |
139 |
|
if (is_array($args)) { |
140 |
|
foreach ($args as $key => $val) { |
141 |
|
$key = strtoupper($key); |
142 |
|
$this->$key = $val; |
143 |
|
} |
144 |
|
} |
145 |
|
|
146 |
// check if host is valid |
// check if host is valid |
147 |
if (!$this->host) { |
if (!$this->HOST) { |
148 |
$this->_raiseException( "->constructor: attribute 'host' is empty, please check your settings"); |
$this->_raiseException( "->constructor: attribute 'host' is empty, please check your settings"); |
149 |
return; |
return; |
150 |
} |
} |
151 |
|
|
152 |
if (!$this->port) { |
if (!$this->PORT) { |
153 |
$this->_raiseException( "->constructor: attribute 'port' is empty, please check your settings"); |
$this->_raiseException( "->constructor: attribute 'port' is empty, please check your settings"); |
154 |
return; |
return; |
155 |
} |
} |
168 |
$this->log(get_class($this) . "->send: " . $command, PEAR_LOG_DEBUG); |
$this->log(get_class($this) . "->send: " . $command, PEAR_LOG_DEBUG); |
169 |
|
|
170 |
if (!$this->isConnected()) { |
if (!$this->isConnected()) { |
171 |
$this->_raiseException( "->send: not connected!"); |
$this->_raiseException( "->send[ command=$command ]: not connected while trying to send command!"); |
172 |
return; |
return; |
173 |
} |
} |
174 |
|
|
181 |
return $this->_call($command, $data, $options); |
return $this->_call($command, $data, $options); |
182 |
} |
} |
183 |
|
|
184 |
|
|
185 |
function _call($command, $data = "", $options = array() ) { |
function _call($command, $data = "", $options = array() ) { |
186 |
|
|
187 |
if (!$this->configured) { |
if (!$this->configured) { |
198 |
$data_debug = $data; |
$data_debug = $data; |
199 |
if (is_array($data_debug)) { $data_debug = join(", ", $data_debug); } |
if (is_array($data_debug)) { $data_debug = join(", ", $data_debug); } |
200 |
$options_debug = join(", ", $options_list); |
$options_debug = join(", ", $options_list); |
201 |
$this->log(get_class($this) . ": " . $command . "(" . $data_debug . ") [" . $options_debug . "]", PEAR_LOG_DEBUG); |
$this->log(get_class($this) . "->_call: " . $command . "(" . $data_debug . ") [" . $options_debug . "]", PEAR_LOG_DEBUG); |
202 |
|
|
203 |
// trace |
// trace |
204 |
//print "call: $command<hr>"; |
//print "call: $command<hr>"; |
206 |
//print Dumper($this); |
//print Dumper($this); |
207 |
|
|
208 |
// data |
// data |
209 |
$data_enc = XML_RPC_encode($data); |
$data_enc = XML_RPC_encode($data); |
210 |
|
|
211 |
// message - request |
// message - request |
212 |
$msg = new XML_RPC_Message($command); |
$msg = new XML_RPC_Message($command); |
213 |
$msg->addParam($data_enc); |
$msg->addParam($data_enc); |
214 |
//print htmlentities($msg->serialize()); |
|
215 |
|
// ??? |
216 |
|
//print htmlentities($msg->serialize()); |
217 |
|
|
218 |
// remote procedure call |
// remote procedure call |
219 |
$rpc = new XML_RPC_Client("/", $this->host, $this->port); |
$rpc = new XML_RPC_Client("/", $this->HOST, $this->PORT); |
220 |
$rpc->setDebug(0); |
|
221 |
if ( !$msg_response = $rpc->send($msg) ) { |
// TODO: detect highlevel errors, raise proper exceptions on them (e.g. 'Unknown method', 'Method signature error(s)') |
222 |
// TODO: redirect this error elsewhere! |
// done: now possible to declare tracing at this point in configuration |
223 |
//print "RPC-error!<br>"; |
// Please look inside your {appname}/etc/hosts/{hostname}.php |
224 |
$this->_raiseException( "->_call: no response"); |
$rpc->setDebug($this->TRACE); |
225 |
return; |
//$rpc->setDebug(1); |
226 |
} |
|
227 |
|
if ( $this->response = $rpc->send($msg) ) { |
228 |
|
// intermediate result error checking |
229 |
|
$this->_checkException(); |
230 |
|
} else { |
231 |
|
// TODO: redirect this error elsewhere! |
232 |
|
//print "RPC-error!<br>"; |
233 |
|
$this->_raiseException( "->_call: no response"); |
234 |
|
return; |
235 |
|
} |
236 |
|
|
237 |
// message - response |
// message - response |
238 |
$response_enc = $msg_response->value(); |
$response_enc = $this->response->value(); |
239 |
|
|
240 |
|
// TODO: what's this? prematurely returning here should not be considered "stable".... |
241 |
|
return $this->decodeData($response_enc, $options); |
242 |
|
|
|
// TODO: what's this? prematurely returning here should not be considered "stable".... |
|
|
return $this->decodeData($response_enc, $options); |
|
243 |
} |
} |
244 |
|
|
245 |
|
|
246 |
function &decodeData(&$payload, $options = array() ) { |
function &decodeData(&$payload, $options = array() ) { |
247 |
//if (!is_object($payload)) { return; } |
//if (!is_object($payload)) { return; } |
248 |
if ($payload) { |
if ($payload) { |
256 |
$encoder->toISO(); |
$encoder->toISO(); |
257 |
//} |
//} |
258 |
|
|
259 |
$this->meta[connected] = 1; |
$this->_be_connected(); |
260 |
$this->_save_meta(); |
|
|
|
|
261 |
return $data; |
return $data; |
262 |
} else { |
} else { |
263 |
//print "ERROR!<br>"; |
//print "ERROR!<br>"; |
265 |
} |
} |
266 |
} |
} |
267 |
|
|
268 |
function _raiseException($message) { |
function _be_connected() { |
269 |
$this->meta[connected] = 0; |
$this->meta[connected] = 1; |
270 |
$this->_save_meta(); |
$this->_save_meta(); |
271 |
|
} |
272 |
|
|
273 |
|
function _raiseException($message, $code = null) { |
274 |
|
|
275 |
|
// aggregate errors for this run/query |
276 |
|
$this->_add_error($message, $code); |
277 |
|
|
278 |
|
// spout out the error message of the raised exception |
279 |
$this->log(get_class($this) . $message, PEAR_LOG_ERR); |
$this->log(get_class($this) . $message, PEAR_LOG_ERR); |
280 |
|
|
281 |
|
// handle some stuff regarding more special behaviour (will this get rule-based sometimes?) |
282 |
|
|
283 |
|
// handle 'FORCE_CONNECT' |
284 |
|
$connect_condition = $this->FORCE_CONNECT = ($this->isConnected() && $this->DISCONNECT_ON_ERROR); |
285 |
|
if ($connect_condition) { |
286 |
|
$message = '->_raiseException: [DISCONNECT_ON_ERROR] done transparently. Please reconnect manually.'; |
287 |
|
$this->_add_error($message, $code); |
288 |
|
//$this->log(get_class($this) . $message, PEAR_LOG_WARNING); |
289 |
|
$this->log(get_class($this) . $message, PEAR_LOG_ERR); |
290 |
|
$this->meta[connected] = 0; |
291 |
|
$this->_save_meta(); |
292 |
|
} |
293 |
} |
} |
294 |
|
|
295 |
function isConnected() { |
function isConnected() { |
300 |
$this->_call('ping'); |
$this->_call('ping'); |
301 |
} |
} |
302 |
|
|
303 |
|
|
304 |
|
function _checkException() { |
305 |
|
// Please look inside your {appname}/etc/hosts/{hostname}.php for toggling $this->DEBUG |
306 |
|
if ($error_code = $this->response->faultCode()) { |
307 |
|
//print $msg_response->faultString(); |
308 |
|
$this->_raiseException( "->_call: " . $this->response->faultString(), $error_code ); |
309 |
|
} |
310 |
|
} |
311 |
|
|
312 |
|
//if ($this->DEBUG && $error_code = $msg_response->faultCode()) { |
313 |
|
|
314 |
|
|
315 |
|
function _add_error($message, $code) { |
316 |
|
array_push( $this->errors, array(code => $code, message => $message) ); |
317 |
|
} |
318 |
|
|
319 |
|
function getStatus() { |
320 |
|
return array( connected => $this->isConnected(), errors => $this->errors ); |
321 |
|
} |
322 |
|
|
323 |
} |
} |
324 |
|
|
325 |
?> |
?> |