/[cvs]/nfo/php/libs/org.netfrag.glib/DesignPattern/RemoteProxy.php
ViewVC logotype

Annotation of /nfo/php/libs/org.netfrag.glib/DesignPattern/RemoteProxy.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.17 - (hide annotations)
Mon Jul 14 10:05:23 2003 UTC (21 years ago) by jonen
Branch: MAIN
Changes since 1.16: +8 -1 lines
bugfix: added *needed* function 'getAttributes'

1 joko 1.1 <?php
2 joko 1.3 /**
3     * This file contains the DesignPattern::RemoteProxy class
4     *
5     * @author Andreas Motl <andreas.motl@ilo.de>
6     * @package org.netfrag.glib
7     * @name DesignPattern::RemoteProxy
8     *
9 joko 1.4 *
10 joko 1.9 */
11    
12    
13     /**
14 joko 1.4 * <b>Cvs-Log:</b>
15     *
16     * <pre>
17 joko 1.1 * -------------------------------------------------------------------------
18 jonen 1.17 * $Id: RemoteProxy.php,v 1.16 2003/07/02 13:51:38 jonen Exp $
19 joko 1.1 * -------------------------------------------------------------------------
20 joko 1.2 * $Log: RemoteProxy.php,v $
21 jonen 1.17 * Revision 1.16 2003/07/02 13:51:38 jonen
22     * removed debug dumper
23     *
24 jonen 1.16 * Revision 1.15 2003/04/11 01:32:21 joko
25     * renamed logging function
26     *
27 joko 1.15 * Revision 1.14 2003/04/09 02:06:45 joko
28     * errormessage now shown preformatted
29     *
30 joko 1.14 * Revision 1.13 2003/04/04 17:38:03 joko
31     * modifications regarding error-/exception-handling and -tracing
32     *
33 joko 1.13 * Revision 1.12 2003/03/29 08:01:21 joko
34     * modified ErrorBoxing
35     *
36 joko 1.12 * Revision 1.11 2003/03/28 06:44:51 joko
37     * VERBOSE mode
38     *
39 joko 1.11 * Revision 1.10 2003/03/28 03:05:54 joko
40     * more fancy debugging-output
41     *
42 joko 1.10 * Revision 1.9 2003/03/10 23:05:25 joko
43     * + fixed metadata for phpDocumentor
44     *
45 joko 1.9 * Revision 1.8 2003/03/10 22:31:56 joko
46     * + fixed metadata for phpDocumentor
47     *
48 joko 1.8 * Revision 1.7 2003/03/09 15:51:44 joko
49     * + additional metadata for Autodia
50     *
51 joko 1.7 * Revision 1.6 2003/03/05 17:28:43 joko
52     * updated docu (phpDocumentor testing....)
53     *
54 joko 1.6 * Revision 1.5 2003/03/05 17:02:22 joko
55     * updated docu (phpDocumentor testing....)
56     *
57 joko 1.5 * Revision 1.4 2003/03/05 16:32:19 joko
58     * updated docu (phpDocumentor testing....)
59     *
60 joko 1.4 * Revision 1.3 2003/03/05 16:10:17 joko
61     * updated docu (phpDocumentor testing....)
62     *
63 joko 1.3 * Revision 1.2 2003/03/05 12:14:02 joko
64     * renamed method
65     * constructor argument expansion
66     *
67 joko 1.2 * Revision 1.1 2003/03/03 22:06:46 joko
68     * refactored from Data::Driver::Proxy
69     *
70 joko 1.1 * Revision 1.3 2003/03/01 04:56:38 joko
71     * new comment style for large blocks - stolen from phpHtmlLib
72     * commit-log encapsulated into such a block
73     * introduced some documentation
74     * refactored todo (now also inside a stylish block)
75     *
76     * Revision 1.2 2003/02/13 21:48:09 joko
77     * + caching mechanisms more configurable now
78     *
79     * Revision 1.1 2003/02/09 17:23:21 joko
80     * + refactored from flib/Application/RPC/ProxyObject.php
81     *
82     * Revision 1.7 2003/02/03 03:31:38 jonen
83     * - moved '$encoder->toISO()' to 'Remote.php'
84     *
85     * Revision 1.6 2002/12/22 13:26:20 jonen
86     * + added support of tangram independent id (e.g. Data::UUID) toggled by option at conrtuctor
87     *
88     * Revision 1.5 2002/12/18 22:36:49 jonen
89     * + added support to get remote objects via backend via 'guid'
90     * + renamed '_loadBackend' to '_loadRemote'
91     * + constructor now accepts additional options:
92     * + remote: try to get object via '_loadRemote' ...
93     * + oid: use the given identifier as an 'oid' when doing '_loadRemote'
94     * + guid: use the given identifier as a 'guid' when doing '_loadRemote'
95     *
96     * Revision 1.4 2002/12/12 02:46:31 joko
97     * + state (session) gets flushed when data was successfully loaded from remote side
98     * + fixed _loadBackend
99     * + fixed flushState
100     *
101     * Revision 1.3 2002/12/06 04:12:54 joko
102     * + replaced 'xyzCache' through 'xyzProxy'
103     * + function store(...)
104     *
105     * Revision 1.2 2002/12/05 21:44:09 joko
106     * + debugging
107     *
108     * Revision 1.1 2002/12/01 17:23:58 joko
109     * + initial check-in
110     *
111     * Revision 1.11 2002/11/12 06:05:58 cvsjoko
112     * + renamed class: Text_Encode -> TextEncode
113     *
114     * Revision 1.10 2002/10/29 19:15:33 cvsjoko
115     * - moved utf8/iso-conversion to lib/utils/Text_Encode.php
116     *
117     * Revision 1.9 2002/10/26 12:32:36 cvsjoko
118     * - removed debugging via "print"
119     * + added generic logging via PEAR::Log
120     *
121     * Revision 1.8 2002/10/22 09:51:38 cvsmax
122     * + moved semi-method 'createBackend' to method $site->user->create at User.class.php
123     *
124     * Revision 1.7 2002/10/21 18:27:09 cvsjoko
125     * - manually disabled any form of caching
126     *
127     * Revision 1.6 2002/10/17 03:48:47 cvsmax
128     * + new
129     * + function _createBackend create new Backend
130     * + function _create api-create
131     *
132     * Revision 1.5 2002/10/16 03:37:54 cvsjoko
133     * + bugfix: wrong comparison in restriction to save to array
134     *
135     * Revision 1.4 2002/10/10 03:04:23 cvsjoko
136     * + no debug-output
137     *
138     * Revision 1.3 2002/10/10 03:03:27 cvsjoko
139     * + bugfix: save object to cache only if payload from backend isn't empty
140     *
141     * Revision 1.2 2002/10/10 02:40:06 cvsjoko
142     * + new level of data-caching (session and persistant)
143     * + function _loadState loads data from session
144     * + function _saveState saves data to session
145     * + function save api-save (session & backend)
146     * + function _commit saves data to backend
147     * + handy utils
148     * + function _setAttributes
149     * + function flushState
150     *
151     * Revision 1.1 2002/10/09 00:51:39 cvsjoko
152     * + new
153     * -------------------------------------------------------------------------
154 joko 1.4 * </pre>
155     *
156 joko 1.1 */
157    
158 joko 1.4
159    
160    
161     /**
162     * Load required modules:
163     *
164     */
165     loadModule('DesignPattern::Proxy');
166    
167    
168 joko 1.1 /**
169 joko 1.7 * DesignPattern::RemoteProxy -- Multiple stage data fetching and caching
170 joko 1.1 *
171     *
172 joko 1.7 * This class (DesignPattern::RemoteProxy) provides an abstract framework
173 joko 1.1 * for loading/saving arbitrary data from/to data storages interfaced
174     * by storage *proxy*-drivers.
175     * Don't mix these up with the concrete storage *handle*-drivers
176     * doing the lowlevel stuff (opening files, talking sql, etc.).
177     * These proxy-drivers are "just" wrappers around them
178     * providing a more highlevel, consistent API making
179     * it easier for Data::Driver::Proxy to do its main work:
180     *
181 joko 1.7 * quote from: http://home.earthlink.net/~huston2/dp/proxy.html
182     * "A remote proxy provides a local representative for an
183     * object that resides in a different address space. This is
184     * what the "stub" code in RPC and CORBA provides."
185     *
186     *
187 joko 1.1 * Multiple stage data fetching and caching:
188     *
189 joko 1.4 * <pre>
190     *
191 joko 1.1 * DATA, ...
192     * ... also refered to as data, should be handled as
193     * something called data.
194     *
195     * STAGES
196     * x native php4 session: serialized payload
197     * x single rdbms table: serialized payload
198     * x ... via custom database wrapper
199     * x included php-file, included php-library
200     * o object, component, package, callbacks
201     * x ... via php PEAR DB abstraction objects
202     * x remote objects (items): flat attribute hash
203     * o use Data::Object::Identity
204     * o via Data::Driver::Proxy::RPC or others
205     * o Data::Object::Encoder: encodes/decodes values
206     * o Data::Object::Interpolator: interpolates/resolves references
207     *
208     * WORK
209     * x move data transparently around between these stages
210     *
211     * TOOLS
212     *
213     * x Overview - Packages, Handlers and Stages:
214     * This is weird. ;-)
215     * - A Package can be instantiated by its namespaced classname
216     * e.g.: "mkObject('My::Package');"
217     * - A Package can be a Handler for Data::Driver::Proxy
218     * - A Handler implements exactly one Stage
219     * - Data::Driver::Proxy itself embeds these Stages:
220     * x php4-session
221     * x rdbms-table
222     * - An example Handler is Data::Driver::RPC::Remote
223     *
224     * x Handlers:
225     * x Data::Driver::RPC::Remote
226     * Interacts with a RPC::XML server (todo: talk SOAP!)
227     * using PEAR::XML::RPC.
228     * It can cache data for "disconnected mode" using the
229     * o Data::Driver::phpHtmlLib
230     * o Data::Driver::PEAR::DB
231     * x Data::Driver::PEAR::Tree (via Data::Lift)
232     *
233 joko 1.4 * </pre>
234     *
235     *
236     * An attempt to implement some software design patterns
237 joko 1.3 * --- RemoteProxyPattern
238     *
239 joko 1.7 * @link http://www.agcs.com/supportv2/techpapers/patterns/papers/tutnotes/sld017.htm
240     * @link http://home.earthlink.net/~huston2/dp/proxy.html
241     * @link http://wiki.cs.uiuc.edu/PatternStories/RemoteObject
242 joko 1.3 * @link http://c2.com/cgi-bin/wiki?ProxyPattern
243 joko 1.7 * @link http://c2.com/cgi-bin/wiki?LazyProxies
244 joko 1.3 *
245     * @author Andreas Motl <andreas.motl@ilo.de>
246     * @link http://www.netfrag.org/~joko/
247     *
248     * @copyright (c) 2003 - All Rights reserved.
249     * @license GNU LGPL (GNU Lesser General Public License)
250     * @link http://www.gnu.org/licenses/lgpl.txt
251     *
252     * @package org.netfrag.glib
253 joko 1.6 * @subpackage DesignPattern
254 joko 1.3 * @name DesignPattern::RemoteProxy
255     *
256 joko 1.4 *
257 joko 1.7 * @todo extend options to en-/disable caching via a) session and/or b) database
258     * make feature available via runtime setter-method to these options
259     * @todo PEAR::Cache for caching purposes!!!
260     * @todo refactor database access: use PEAR for this! no more 'connectdb' here!!!
261     * @todo make database connection more flexible to make possible
262     * to have different (probably named) proxy databases (besides a "main database")
263     * @todo rename this to Data::Proxy? or split into Data::Query, Data::Result and Data::Wrapper?
264     * @todo refactor this to a "RemoteObject" class!!! (inheriting from DesignPattern::RemoteObject)
265     * @todo rename this to "DesignPattern::LazyRemoteProxy"???
266 joko 1.1 *
267     */
268     class DesignPattern_RemoteProxy extends DesignPattern_Proxy {
269    
270     var $objectId;
271     // purpose ...
272     var $meta;
273     var $payload;
274     var $attributes;
275     var $backend;
276    
277     function DesignPattern_RemoteProxy($objectId = "", $options = array() ) {
278 joko 1.15 php::log(get_class($this) . "->new()", PEAR_LOG_INFO);
279 joko 1.1 global $proxy;
280    
281 joko 1.2 // 2003-03-05 - modified constructor
282     // expand objectId
283     if (is_array($objectId)) {
284     $options = $objectId[1];
285     $objectId = $objectId[0];
286     }
287    
288 joko 1.1 // trace
289     //print Dumper($objectId, $options);
290    
291     // initialization/startup
292     $this->_init_meta_options($objectId, $options);
293     $this->_init_caching();
294     $this->_init_load();
295    
296 joko 1.2 //print Dumper($this);
297    
298 joko 1.1 }
299    
300     function _init_meta_options( $objectId="", $options = array() ) {
301     $this->meta = $options;
302    
303     if ($objectId) {
304     $this->objectId = $objectId;
305    
306     // set default load mechanism
307     if ( $this->meta[remote] && ((!$this->meta[oid] && !$this->meta[guid]) || ($this->meta[oid] && $this->meta[guid])) && (!$this->meta[key]) ) {
308     $this->meta[oid] = 1;
309     }
310    
311     }
312     }
313    
314     function _init_caching() {
315    
316     if ($this->meta[cache][session]) {
317     session_register_safe("proxy");
318     }
319 joko 1.7
320     /**
321     * <!-- Autodia -->
322     * can do: (this is metadata supplied for Autodia, don't delete!)
323     * $this->backend = new DataSource_Proxy_XMLRPC()
324     *
325     */
326 joko 1.1
327     if ($this->meta[remote]) {
328     //$this->backend = mkObject('Data::Driver::RPC::Remote', $this->meta[rpcinfo]);
329 joko 1.2 $this->backend = php::mkComponent('DataSource::Proxy::XMLRPC', $this->meta[rpcinfo]);
330 joko 1.1 }
331     }
332    
333     function _init_load() {
334     if ($this->objectId) {
335     $this->load();
336     //$this->_saveProxy();
337     }
338     }
339    
340    
341     function load() {
342 joko 1.15 php::log(get_class($this) . "->load()", PEAR_LOG_INFO);
343 joko 1.1 if (!$this->_loadState()) {
344     if (!$this->_loadProxy()) {
345    
346     // just load object from remote side if its flagged to be a remote one ...
347     if ($this->meta[remote]) {
348     $this->_loadRemote();
349     }
350    
351     }
352     }
353     }
354    
355     function save($data, $type) {
356     $this->_setAttributes($data);
357     $this->_saveState();
358     if ($type == 'commit') {
359     $this->_commit();
360     }
361     if ($type == 'create') {
362     $this->_create();
363     }
364     }
365    
366     function flush() {
367     $this->flushState();
368     $this->flushProxy();
369     }
370    
371     function _commit() {
372     $this->_saveBackend($this->attributes);
373     $this->flushState();
374     $this->flushProxy();
375     }
376    
377    
378     // TODO: make this work
379     /*
380     function _create() {
381     $this->_createBackend($this->attributes);
382     $this->flushState();
383     $this->flushProxy();
384     }
385     */
386    
387 joko 1.2 function getResult() {
388 joko 1.1 if (!$this->meta[decoded]) {
389     $this->_decode();
390     $this->_saveState();
391     }
392     return $this->attributes;
393     }
394    
395     function _setAttributes($data) {
396     $this->attributes = $data;
397 jonen 1.17 }
398    
399     function getAttributes() {
400     return $this->attributes;
401 joko 1.1 }
402    
403     function flushProxy() {
404     connectdb();
405     $sql = "DELETE FROM f_proxy WHERE oid='$this->objectId'";
406     send_sql($sql);
407     }
408    
409     function flushState() {
410     global $proxy;
411     unset($proxy[$this->objectId]);
412     $this->meta[decoded] = 0;
413     }
414    
415     function _loadState() {
416     global $proxy;
417    
418     // trace
419     //print Dumper($this);
420    
421     // debug
422 joko 1.15 php::log(get_class($this) . "->_loadState()");
423 joko 1.1
424     if ($this->attributes = $proxy[$this->objectId]) {
425     //print "_loadState:" . dumpVar($this->attributes);
426     $this->meta[decoded] = 1;
427     // TODO: make a parameter from this (0 deactivates session-layer)
428     return 0;
429     }
430     }
431    
432     function _saveState() {
433     global $proxy;
434 joko 1.15 php::log(get_class($this) . "->_saveState()");
435 joko 1.1 $proxy[$this->objectId] = $this->attributes;
436     //print "_saveState: " . dumpVar($this->attributes);
437     // TODO: throw exception-message back to user if operation fails
438     }
439    
440     function _loadProxy() {
441    
442     // FIXME!
443     if (!$this->meta[cache][db]) { return; }
444    
445     // trace & debug
446     //print Dumper($this);
447 joko 1.15 php::log(get_class($this) . "->_loadProxy()");
448 joko 1.1
449     connectdb();
450     $sql = "SELECT payload FROM f_proxy WHERE oid='$this->objectId'";
451     if ($res = send_sql($sql)) {
452     $row = mysql_fetch_array($res, MYSQL_ASSOC);
453     if ($row) {
454     $this->payload = $row[payload];
455     // TODO: make a parameter from this (0 deactivates mysqldb-layer)
456     return 0;
457     }
458     }
459     }
460    
461     // TODO: use PEAR here
462     function _saveProxy() {
463    
464     // FIXME!
465     if (!$this->meta[cache][db]) { return; }
466    
467 joko 1.15 php::log(get_class($this) . "->_saveProxy()");
468 joko 1.1 connectdb();
469     if ($this->payload) {
470     //$sql = "INSERT INTO f_proxy SET payload='$this->payload' WHERE oid='$this->objectId'";
471     $sql = "INSERT INTO f_proxy SET oid='$this->objectId', payload='$this->payload'";
472     if (!send_sql($sql)) {
473     $sql = "UPDATE f_proxy SET payload='$this->payload' WHERE oid='$this->objectId'";
474     send_sql($sql);
475     }
476     }
477     }
478    
479     function _loadRemote() {
480 joko 1.15 php::log(get_class($this) . "->_loadRemote()");
481 joko 1.1
482     // trace
483     //print Dumper($this->meta);
484    
485     // TODO: test backend for reachability first (eventually cache this information and "reset" it by another party)
486    
487     // 1. check backend-handle
488     if (!$this->backend) {
489 joko 1.15 php::log(get_class($this) . "->_loadRemote: no backend handle, please check argument 'rpcinfo'", PEAR_LOG_CRIT);
490 joko 1.1 return;
491     }
492    
493     // determine backend action by metadata
494     // check for guid or oid
495     if ($this->meta[guid]) {
496     if (!$this->objectId) {
497 joko 1.15 php::log(get_class($this) . "->_loadRemote: argument 'guid' requires valid objectId", PEAR_LOG_WARNING);
498 joko 1.1 return;
499     }
500     if (!$this->meta[classname]) {
501 joko 1.15 php::log(get_class($this) . "->_loadRemote: argument 'guid' requires 'classname'", PEAR_LOG_WARNING);
502 joko 1.1 return;
503     }
504 joko 1.15 php::log(get_class($this) . "->_loadRemote: getObjectByGuid", PEAR_LOG_DEBUG);
505 joko 1.1 $args = array( guid => $this->objectId, classname => $this->meta[classname] );
506     $result = $this->backend->send('getObjectByGuid', $args );
507    
508     } elseif ($this->meta[oid]) {
509     if (!$this->objectId) {
510 joko 1.15 php::log(get_class($this) . "->_loadRemote: argument 'oid' requires valid objectId", PEAR_LOG_WARNING);
511 joko 1.1 return;
512     }
513 joko 1.15 php::log(get_class($this) . "->_loadRemote: getObject", PEAR_LOG_DEBUG);
514 joko 1.1 $result = $this->backend->send('getObject', $this->objectId);
515    
516     } elseif ($this->meta[key]) {
517     if (!$this->meta[command]) {
518 joko 1.15 php::log(get_class($this) . "->_loadRemote: argument 'key' requires 'command'", PEAR_LOG_WARNING);
519 joko 1.1 return;
520     }
521     /*
522     if (!$this->meta[query]) {
523 joko 1.15 php::log(get_class($this) . "->_loadRemote: argument 'key' requires 'query'", PEAR_LOG_WARNING);
524 joko 1.1 return;
525     }
526     */
527 joko 1.15 //php::log(get_class($this) . "->_loadRemote: $this->meta[command](" . join(' ', $this->meta[query]) . ")", PEAR_LOG_DEBUG);
528 joko 1.10 //print Dumper(array($this->meta[command], $this->meta[query]));
529 joko 1.1 $result = $this->backend->send($this->meta[command], $this->meta[query]);
530    
531     }
532    
533 joko 1.10 //print "result: " . dumpVar($result) . "<br>";
534 joko 1.1
535 joko 1.13 $status = $this->backend->status();
536 joko 1.10 //print Dumper($status);
537    
538 joko 1.13 $good = is_array($result) && sizeof($result) && $status[connected];
539 joko 1.10
540 joko 1.13 if ($good) {
541 joko 1.1
542     // FIXME: this is dangerous!
543 joko 1.13 /*
544 joko 1.1 if ($_GET[debug]) {
545     print Dumper($result);
546     }
547 joko 1.13 */
548 joko 1.1
549     $this->payload = serialize($result);
550     // ----- move this to _encode some times
551    
552     $this->_saveProxy();
553     //print "oid: $this->objectId<br>";
554     $this->flushState();
555 joko 1.10
556 joko 1.1 } else {
557 joko 1.13
558     if (constants::get('APP_MODE_DEBUG')) {
559     $this->draw_error_box($status);
560 joko 1.11 } else {
561 joko 1.13 php::maintenance('rpc', array( status => $status ) );
562 joko 1.11 }
563 joko 1.10
564 joko 1.1 }
565    
566     }
567 joko 1.13
568     function draw_error_box($status) {
569     $style = html_style("text/css", '.boxlabel_yellow { color: yellow; font-weight:bold; }');
570     $statusbox = html_div();
571     $statusbox->set_style('background: red; border: 2px black groove; width:640px; padding:10px; margin:40px;');
572     $statusbox->add( html_span('boxlabel_yellow', "Method:"), get_class($this) . "->_loadRemote", html_br() );
573     $statusbox->add( html_span('boxlabel_yellow', "Connected:"), $status[connected], html_br() );
574     $statusbox->add( html_span('boxlabel_yellow', "RPCSESSID:"), $status[RPCSESSID], html_br() );
575     foreach ($status[errors] as $error) {
576 joko 1.14 $msg = html_pre($error[message]);
577     $statusbox->add( html_span('boxlabel_yellow', "Error($error[code]):"), $msg );
578 joko 1.13 }
579 joko 1.1
580 joko 1.13 $message = "Error while talking to remote side. Please check wire, socket or api.";
581 joko 1.15 php::log($message, PEAR_LOG_CRIT);
582 joko 1.13 $statusbox->add( html_span('boxlabel_yellow', "Critical:"), $message, html_br() );
583    
584     // V1
585     /*
586     if (constants::get('VERBOSE') || constants::get('ERRORS_ONLY')) {
587     print $style->render();
588     print $statusbox->render();
589     } else {
590     foreach ($status[errors] as $error) {
591     print Dumper($error);
592     }
593     }
594     */
595    
596     // V2
597     trace( container($style, $statusbox) );
598    
599     }
600    
601    
602 joko 1.1 function _saveBackend($result) {
603 joko 1.15 php::log(get_class($this) . "->_saveBackend()");
604 joko 1.1
605     //$encoder = new TextEncode($result);
606     //$encoder->toUTF8();
607    
608     // check for guid or oid
609     if($this->meta[guid]) {
610     $args = array( 'guid' => $this->objectId, 'classname' => $this->meta[classname], 'data' => $result );
611     $response = $this->backend->send('saveObjectByGuid', $args, array( utf8 => 1) );
612     }
613     if($this->meta[oid]) {
614     $response = $this->backend->send('saveObject', array('oid' => $this->objectId, 'data' => $result), array( utf8 => 1) );
615     }
616     }
617    
618     function _decode() {
619     // fill attributes-hashtable from message-hashtable
620     if (!$this->payload) { return; }
621     //if ($this->attributes = $backend->decodeData($this->payload)) {
622     if ($this->attributes = unserialize($this->payload)) {
623     $this->meta[decoded] = 1;
624     }
625     }
626    
627     function store($struct) {
628     $this->payload = serialize($struct);
629     $this->_saveProxy();
630     }
631    
632     }
633    
634 jonen 1.16 ?>

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