/[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.14 - (show annotations)
Wed Apr 9 02:06:45 2003 UTC (21 years, 4 months ago) by joko
Branch: MAIN
Changes since 1.13: +6 -2 lines
errormessage now shown preformatted

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

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