/[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.11 - (show annotations)
Fri Mar 28 06:44:51 2003 UTC (21 years, 5 months ago) by joko
Branch: MAIN
Changes since 1.10: +23 -12 lines
VERBOSE mode

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

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