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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.15 - (show annotations)
Fri Apr 11 01:32:21 2003 UTC (21 years, 3 months ago) by joko
Branch: MAIN
Changes since 1.14: +22 -19 lines
renamed logging function

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

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