/[cvs]/nfo/php/libs/org.netfrag.glib/DataSource/Proxy/XMLRPC.php
ViewVC logotype

Annotation of /nfo/php/libs/org.netfrag.glib/DataSource/Proxy/XMLRPC.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (hide annotations)
Fri Mar 28 03:00:12 2003 UTC (21 years, 5 months ago) by joko
Branch: MAIN
Changes since 1.3: +65 -20 lines
enhanced error- and exception-handling

1 joko 1.1 <?
2 joko 1.2 /**
3     * This file contains the DataSource::Proxy::XMLRPC module.
4     *
5     * @author Andreas Motl <andreas.motl@ilo.de>
6     * @package org.netfrag.glib
7     * @name DataSource::Proxy::XMLRPC
8     *
9     */
10    
11    
12 joko 1.1 /*
13     ## --------------------------------------------------------------------------
14 joko 1.4 ## $Id: XMLRPC.php,v 1.3 2003/03/27 16:05:01 joko Exp $
15 joko 1.1 ## --------------------------------------------------------------------------
16 joko 1.2 ## $Log: XMLRPC.php,v $
17 joko 1.4 ## Revision 1.3 2003/03/27 16:05:01 joko
18     ## enhanced 'function _call': debugging, tracing and autodisconnect now parametrized
19     ##
20 joko 1.3 ## Revision 1.2 2003/03/05 23:16:48 joko
21     ## updated docu - phpDocumentor is very strict about its 'blocks'...
22     ##
23 joko 1.2 ## Revision 1.1 2003/03/03 21:53:52 joko
24     ## refactored from Data::Driver::RPC::Remote
25     ##
26 joko 1.1 ## Revision 1.4 2003/02/20 21:42:43 joko
27     ## + fix: always converting from utf8 into latin here (for now)
28     ##
29     ## Revision 1.3 2003/02/13 21:51:29 joko
30     ## + now can remember its connection-state
31     ## + sub ping
32     ##
33     ## Revision 1.2 2003/02/13 00:42:44 joko
34     ## +- renamed modules
35     ##
36     ## Revision 1.1 2003/02/09 17:25:38 joko
37     ## + refactored from flib/Application/RPC/Remote.php
38     ##
39     ## Revision 1.6 2003/02/03 03:37:45 jonen
40     ## - removed unused argument '$decode' at function '_call()'
41     ## + added argument '$options=array()' at function '_call()'
42     ## which will be passed to function '&decodeData()' for e.g. utf8 decoding
43     ## + added '$encoder->toISO()' at '&decodeData()', (re)moved from 'ProxyObject.php'
44     ##
45     ## Revision 1.5 2002/12/22 13:24:09 jonen
46     ## + added utf8 encoding at 'send()' toggled by option
47     ##
48     ## Revision 1.4 2002/12/19 02:02:25 jonen
49     ## + minor changes
50     ##
51     ## Revision 1.3 2002/12/19 01:59:37 jonen
52     ## + minor changes: coment debug prints
53     ##
54     ## Revision 1.2 2002/12/05 21:45:31 joko
55     ## + debugging
56     ##
57     ## Revision 1.1 2002/12/01 17:23:58 joko
58     ## + initial check-in
59     ##
60     ## Revision 1.3 2002/12/01 06:22:57 cvsjoko
61     ## + minor update: now can use config defaults or given args
62     ##
63     ## Revision 1.2 2002/10/29 19:14:45 cvsjoko
64     ## - bugfix: dont' do utf8-encoding here
65     ##
66     ## Revision 1.1 2002/10/09 00:51:39 cvsjoko
67     ## + new
68     ##
69     ##
70     ## -------------------------------------------------------------------------
71     */
72    
73    
74     // V1: normal require
75     //require_once 'XML/RPC/RPC.php';
76    
77     // V2: first time load of foreign module (PEAR::XML::RPC)
78     //require_once 'XML/RPC/RPC.php';
79     loadModule('XML::RPC::RPC');
80    
81     loadModule('Class::Logger');
82    
83 joko 1.2
84     /**
85     * This is the DataSource::Proxy::XMLRPC module.
86     *
87     * @author Andreas Motl <andreas.motl@ilo.de>
88     * @package org.netfrag.glib
89     * @subpackage DataSource
90     * @name DataSource::Proxy::XMLRPC
91     *
92     * @todo SOAP?
93     *
94     */
95 joko 1.1 class DataSource_Proxy_XMLRPC extends Class_Logger {
96    
97     var $configured;
98 joko 1.4 var $meta = array();
99     var $response = null;
100     var $errors = array();
101 joko 1.1
102     function DataSource_Proxy_XMLRPC($args = array()) {
103    
104     parent::constructor();
105    
106     //print Dumper($this, $args);
107 joko 1.4 //print Dumper($args);
108 joko 1.1
109     global $Data_Driver_RPC_Remote_meta;
110     session_register_safe("Data_Driver_RPC_Remote_meta");
111     $this->meta = $Data_Driver_RPC_Remote_meta;
112    
113 joko 1.4 // force connect?
114    
115     // V1
116     /*
117 joko 1.1 if (!isset($this->meta[connected]) || $args[connect]) {
118     $this->meta[connected] = 1;
119     }
120 joko 1.4 */
121    
122     // V2
123     $this->FORCE_CONNECT = $args[connect];
124    
125 joko 1.1 $this->_save_meta();
126    
127 joko 1.4
128 joko 1.3 // merge args - V1
129     //if ($args['Host']) { $this->HOST = $args['Host']; }
130     //if ($args['Port']) { $this->PORT = $args['Port']; }
131    
132     // merge args - V2
133     // TODO: php::merge_to($this, $args);
134     //php::merge_to($this, $args);
135     //print Dumper($args);
136     if (is_array($args)) {
137     foreach ($args as $key => $val) {
138     $key = strtoupper($key);
139     $this->$key = $val;
140     }
141     }
142 joko 1.1
143     // check if host is valid
144 joko 1.3 if (!$this->HOST) {
145 joko 1.1 $this->_raiseException( "->constructor: attribute 'host' is empty, please check your settings");
146     return;
147     }
148    
149 joko 1.3 if (!$this->PORT) {
150 joko 1.1 $this->_raiseException( "->constructor: attribute 'port' is empty, please check your settings");
151     return;
152     }
153    
154     $this->configured = 1;
155    
156     }
157    
158     function _save_meta() {
159     global $Data_Driver_RPC_Remote_meta;
160     $Data_Driver_RPC_Remote_meta = $this->meta;
161     }
162    
163     function send($command, $data = "", $options = array()) {
164    
165     $this->log(get_class($this) . "->send: " . $command, PEAR_LOG_DEBUG);
166    
167     if (!$this->isConnected()) {
168 joko 1.3 $this->_raiseException( "->send[ command=$command ]: not connected while trying to send command!");
169 joko 1.1 return;
170     }
171    
172     // do 'encode' here and ...
173     if ($options[utf8]) {
174     $encoder = new Data_Encode($data);
175     $encoder->toUTF8();
176     }
177     // call '_call' with 'decode'
178     return $this->_call($command, $data, $options);
179     }
180    
181 joko 1.3
182 joko 1.1 function _call($command, $data = "", $options = array() ) {
183    
184     if (!$this->configured) {
185     $this->_raiseException( "->_call: class not configured properly");
186     return;
187     }
188    
189     // populate options list - mostly for debugging purposes
190     $options_list = array();
191     foreach ($options as $key => $value) {
192     array_push($options_list, "$key=$value");
193     }
194    
195     $data_debug = $data;
196     if (is_array($data_debug)) { $data_debug = join(", ", $data_debug); }
197     $options_debug = join(", ", $options_list);
198 joko 1.4 $this->log(get_class($this) . "->_call: " . $command . "(" . $data_debug . ") [" . $options_debug . "]", PEAR_LOG_DEBUG);
199 joko 1.1
200     // trace
201     //print "call: $command<hr>";
202     //print Dumper($data);
203     //print Dumper($this);
204    
205     // data
206 joko 1.3 $data_enc = XML_RPC_encode($data);
207 joko 1.1
208     // message - request
209 joko 1.3 $msg = new XML_RPC_Message($command);
210     $msg->addParam($data_enc);
211    
212     // ???
213     //print htmlentities($msg->serialize());
214    
215 joko 1.1 // remote procedure call
216 joko 1.3 $rpc = new XML_RPC_Client("/", $this->HOST, $this->PORT);
217    
218     // TODO: detect highlevel errors, raise proper exceptions on them (e.g. 'Unknown method', 'Method signature error(s)')
219     // done: now possible to declare tracing at this point in configuration
220     // Please look inside your {appname}/etc/hosts/{hostname}.php
221     $rpc->setDebug($this->TRACE);
222 joko 1.4 //$rpc->setDebug(1);
223 joko 1.3
224 joko 1.4 if ( $this->response = $rpc->send($msg) ) {
225     // intermediate result error checking
226     $this->_checkException();
227     } else {
228 joko 1.3 // TODO: redirect this error elsewhere!
229     //print "RPC-error!<br>";
230     $this->_raiseException( "->_call: no response");
231     return;
232     }
233 joko 1.4
234 joko 1.1 // message - response
235 joko 1.4 $response_enc = $this->response->value();
236 joko 1.3
237     // TODO: what's this? prematurely returning here should not be considered "stable"....
238     return $this->decodeData($response_enc, $options);
239 joko 1.1
240     }
241    
242 joko 1.3
243 joko 1.1 function &decodeData(&$payload, $options = array() ) {
244     //if (!is_object($payload)) { return; }
245     if ($payload) {
246     // data
247     $data = XML_RPC_decode($payload);
248     //print "data: " . dumpVar($response);
249    
250     // decode UTF8 to ISO if wanted
251     //if ($options[to_latin]) {
252     $encoder = new Data_Encode($data);
253     $encoder->toISO();
254     //}
255    
256 joko 1.4 $this->_be_connected();
257    
258 joko 1.1 return $data;
259     } else {
260     //print "ERROR!<br>";
261     return 0;
262     }
263     }
264    
265 joko 1.4 function _be_connected() {
266     $this->meta[connected] = 1;
267     $this->_save_meta();
268     }
269    
270     function _raiseException($message, $code = null) {
271    
272     // aggregate errors for this run/query
273     $this->_add_error($message, $code);
274 joko 1.3
275     // spout out the error message of the raised exception
276 joko 1.1 $this->log(get_class($this) . $message, PEAR_LOG_ERR);
277 joko 1.3
278 joko 1.4 // handle some stuff regarding more special behaviour (will this get rule-based sometimes?)
279    
280     // handle 'FORCE_CONNECT'
281     $connect_condition = $this->FORCE_CONNECT = ($this->isConnected() && $this->DISCONNECT_ON_ERROR);
282     if ($connect_condition) {
283 joko 1.3 $message = '->_raiseException: [DISCONNECT_ON_ERROR] done transparently. Please reconnect manually.';
284 joko 1.4 $this->_add_error($message, $code);
285     //$this->log(get_class($this) . $message, PEAR_LOG_WARNING);
286     $this->log(get_class($this) . $message, PEAR_LOG_ERR);
287 joko 1.3 $this->meta[connected] = 0;
288     $this->_save_meta();
289     }
290 joko 1.1 }
291    
292     function isConnected() {
293     return $this->meta[connected];
294     }
295    
296     function ping() {
297     $this->_call('ping');
298 joko 1.4 }
299    
300    
301     function _checkException() {
302     // Please look inside your {appname}/etc/hosts/{hostname}.php for toggling $this->DEBUG
303     if ($error_code = $this->response->faultCode()) {
304     //print $msg_response->faultString();
305     $this->_raiseException( "->_call: " . $this->response->faultString(), $error_code );
306     }
307     }
308    
309     //if ($this->DEBUG && $error_code = $msg_response->faultCode()) {
310    
311    
312     function _add_error($message, $code) {
313     array_push( $this->errors, array(code => $code, message => $message) );
314     }
315    
316     function getStatus() {
317     return array( connected => $this->isConnected(), errors => $this->errors );
318 joko 1.1 }
319    
320     }
321    
322     ?>

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed