5 |
* @author Andreas Motl <andreas.motl@ilo.de> |
* @author Andreas Motl <andreas.motl@ilo.de> |
6 |
* @package org.netfrag.glib |
* @package org.netfrag.glib |
7 |
* @name DesignPattern::RemoteProxy |
* @name DesignPattern::RemoteProxy |
|
* @filesource |
|
8 |
* |
* |
9 |
* |
* |
10 |
|
*/ |
11 |
|
|
12 |
|
|
13 |
|
/** |
14 |
* <b>Cvs-Log:</b> |
* <b>Cvs-Log:</b> |
15 |
* |
* |
16 |
* <pre> |
* <pre> |
18 |
* $Id$ |
* $Id$ |
19 |
* ------------------------------------------------------------------------- |
* ------------------------------------------------------------------------- |
20 |
* $Log$ |
* $Log$ |
21 |
|
* Revision 1.16 2003/07/02 13:51:38 jonen |
22 |
|
* removed debug dumper |
23 |
|
* |
24 |
|
* Revision 1.15 2003/04/11 01:32:21 joko |
25 |
|
* renamed logging function |
26 |
|
* |
27 |
|
* Revision 1.14 2003/04/09 02:06:45 joko |
28 |
|
* errormessage now shown preformatted |
29 |
|
* |
30 |
|
* Revision 1.13 2003/04/04 17:38:03 joko |
31 |
|
* modifications regarding error-/exception-handling and -tracing |
32 |
|
* |
33 |
|
* Revision 1.12 2003/03/29 08:01:21 joko |
34 |
|
* modified ErrorBoxing |
35 |
|
* |
36 |
|
* Revision 1.11 2003/03/28 06:44:51 joko |
37 |
|
* VERBOSE mode |
38 |
|
* |
39 |
|
* Revision 1.10 2003/03/28 03:05:54 joko |
40 |
|
* more fancy debugging-output |
41 |
|
* |
42 |
|
* Revision 1.9 2003/03/10 23:05:25 joko |
43 |
|
* + fixed metadata for phpDocumentor |
44 |
|
* |
45 |
|
* Revision 1.8 2003/03/10 22:31:56 joko |
46 |
|
* + fixed metadata for phpDocumentor |
47 |
|
* |
48 |
|
* Revision 1.7 2003/03/09 15:51:44 joko |
49 |
|
* + additional metadata for Autodia |
50 |
|
* |
51 |
|
* Revision 1.6 2003/03/05 17:28:43 joko |
52 |
|
* updated docu (phpDocumentor testing....) |
53 |
|
* |
54 |
|
* Revision 1.5 2003/03/05 17:02:22 joko |
55 |
|
* updated docu (phpDocumentor testing....) |
56 |
|
* |
57 |
* Revision 1.4 2003/03/05 16:32:19 joko |
* Revision 1.4 2003/03/05 16:32:19 joko |
58 |
* updated docu (phpDocumentor testing....) |
* updated docu (phpDocumentor testing....) |
59 |
* |
* |
166 |
|
|
167 |
|
|
168 |
/** |
/** |
169 |
* Data::Driver::Proxy -- Multiple stage data fetching and caching |
* DesignPattern::RemoteProxy -- Multiple stage data fetching and caching |
170 |
* |
* |
171 |
* |
* |
172 |
* This class (Data::Driver::Proxy) provides an abstract framework |
* This class (DesignPattern::RemoteProxy) provides an abstract framework |
173 |
* for loading/saving arbitrary data from/to data storages interfaced |
* for loading/saving arbitrary data from/to data storages interfaced |
174 |
* by storage *proxy*-drivers. |
* by storage *proxy*-drivers. |
175 |
* Don't mix these up with the concrete storage *handle*-drivers |
* Don't mix these up with the concrete storage *handle*-drivers |
178 |
* providing a more highlevel, consistent API making |
* providing a more highlevel, consistent API making |
179 |
* it easier for Data::Driver::Proxy to do its main work: |
* it easier for Data::Driver::Proxy to do its main work: |
180 |
* |
* |
181 |
|
* 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 |
* Multiple stage data fetching and caching: |
* Multiple stage data fetching and caching: |
188 |
* |
* |
189 |
* <pre> |
* <pre> |
236 |
* An attempt to implement some software design patterns |
* An attempt to implement some software design patterns |
237 |
* --- RemoteProxyPattern |
* --- RemoteProxyPattern |
238 |
* |
* |
239 |
|
* @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 |
* @link http://c2.com/cgi-bin/wiki?ProxyPattern |
* @link http://c2.com/cgi-bin/wiki?ProxyPattern |
243 |
|
* @link http://c2.com/cgi-bin/wiki?LazyProxies |
244 |
* |
* |
245 |
* @author Andreas Motl <andreas.motl@ilo.de> |
* @author Andreas Motl <andreas.motl@ilo.de> |
246 |
* @link http://www.netfrag.org/~joko/ |
* @link http://www.netfrag.org/~joko/ |
250 |
* @link http://www.gnu.org/licenses/lgpl.txt |
* @link http://www.gnu.org/licenses/lgpl.txt |
251 |
* |
* |
252 |
* @package org.netfrag.glib |
* @package org.netfrag.glib |
253 |
|
* @subpackage DesignPattern |
254 |
* @name DesignPattern::RemoteProxy |
* @name DesignPattern::RemoteProxy |
255 |
* |
* |
256 |
* |
* |
257 |
* @todo |
* @todo extend options to en-/disable caching via a) session and/or b) database |
258 |
* <pre> |
* make feature available via runtime setter-method to these options |
259 |
* x extend options to en-/disable caching via a) session and/or b) database |
* @todo PEAR::Cache for caching purposes!!! |
260 |
* o make feature available via runtime setter-method to these options |
* @todo refactor database access: use PEAR for this! no more 'connectdb' here!!! |
261 |
* o use PEAR::Cache for caching purposes!!! |
* @todo make database connection more flexible to make possible |
262 |
* o refactor database access: use PEAR for this! no more 'connectdb' here!!! |
* to have different (probably named) proxy databases (besides a "main database") |
263 |
* o make database connection more flexible to make possible |
* @todo rename this to Data::Proxy? or split into Data::Query, Data::Result and Data::Wrapper? |
264 |
* to have different (probably named) proxy databases (besides a "main database") |
* @todo refactor this to a "RemoteObject" class!!! (inheriting from DesignPattern::RemoteObject) |
265 |
* o rename this to Data::Proxy? or split into Data::Query, Data::Result and Data::Wrapper? |
* @todo rename this to "DesignPattern::LazyRemoteProxy"??? |
|
* </pre> |
|
266 |
* |
* |
267 |
*/ |
*/ |
268 |
class DesignPattern_RemoteProxy extends DesignPattern_Proxy { |
class DesignPattern_RemoteProxy extends DesignPattern_Proxy { |
275 |
var $backend; |
var $backend; |
276 |
|
|
277 |
function DesignPattern_RemoteProxy($objectId = "", $options = array() ) { |
function DesignPattern_RemoteProxy($objectId = "", $options = array() ) { |
278 |
logp(get_class($this) . "->new()", PEAR_LOG_INFO); |
php::log(get_class($this) . "->new()", PEAR_LOG_INFO); |
279 |
global $proxy; |
global $proxy; |
280 |
|
|
281 |
// 2003-03-05 - modified constructor |
// 2003-03-05 - modified constructor |
317 |
session_register_safe("proxy"); |
session_register_safe("proxy"); |
318 |
} |
} |
319 |
|
|
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 |
|
|
327 |
if ($this->meta[remote]) { |
if ($this->meta[remote]) { |
328 |
//$this->backend = mkObject('Data::Driver::RPC::Remote', $this->meta[rpcinfo]); |
//$this->backend = mkObject('Data::Driver::RPC::Remote', $this->meta[rpcinfo]); |
329 |
$this->backend = php::mkComponent('DataSource::Proxy::XMLRPC', $this->meta[rpcinfo]); |
$this->backend = php::mkComponent('DataSource::Proxy::XMLRPC', $this->meta[rpcinfo]); |
339 |
|
|
340 |
|
|
341 |
function load() { |
function load() { |
342 |
logp(get_class($this) . "->load()", PEAR_LOG_INFO); |
php::log(get_class($this) . "->load()", PEAR_LOG_INFO); |
343 |
if (!$this->_loadState()) { |
if (!$this->_loadState()) { |
344 |
if (!$this->_loadProxy()) { |
if (!$this->_loadProxy()) { |
345 |
|
|
415 |
//print Dumper($this); |
//print Dumper($this); |
416 |
|
|
417 |
// debug |
// debug |
418 |
logp(get_class($this) . "->_loadState()"); |
php::log(get_class($this) . "->_loadState()"); |
419 |
|
|
420 |
if ($this->attributes = $proxy[$this->objectId]) { |
if ($this->attributes = $proxy[$this->objectId]) { |
421 |
//print "_loadState:" . dumpVar($this->attributes); |
//print "_loadState:" . dumpVar($this->attributes); |
427 |
|
|
428 |
function _saveState() { |
function _saveState() { |
429 |
global $proxy; |
global $proxy; |
430 |
logp(get_class($this) . "->_saveState()"); |
php::log(get_class($this) . "->_saveState()"); |
431 |
$proxy[$this->objectId] = $this->attributes; |
$proxy[$this->objectId] = $this->attributes; |
432 |
//print "_saveState: " . dumpVar($this->attributes); |
//print "_saveState: " . dumpVar($this->attributes); |
433 |
// TODO: throw exception-message back to user if operation fails |
// TODO: throw exception-message back to user if operation fails |
440 |
|
|
441 |
// trace & debug |
// trace & debug |
442 |
//print Dumper($this); |
//print Dumper($this); |
443 |
logp(get_class($this) . "->_loadProxy()"); |
php::log(get_class($this) . "->_loadProxy()"); |
444 |
|
|
445 |
connectdb(); |
connectdb(); |
446 |
$sql = "SELECT payload FROM f_proxy WHERE oid='$this->objectId'"; |
$sql = "SELECT payload FROM f_proxy WHERE oid='$this->objectId'"; |
460 |
// FIXME! |
// FIXME! |
461 |
if (!$this->meta[cache][db]) { return; } |
if (!$this->meta[cache][db]) { return; } |
462 |
|
|
463 |
logp(get_class($this) . "->_saveProxy()"); |
php::log(get_class($this) . "->_saveProxy()"); |
464 |
connectdb(); |
connectdb(); |
465 |
if ($this->payload) { |
if ($this->payload) { |
466 |
//$sql = "INSERT INTO f_proxy SET payload='$this->payload' WHERE oid='$this->objectId'"; |
//$sql = "INSERT INTO f_proxy SET payload='$this->payload' WHERE oid='$this->objectId'"; |
473 |
} |
} |
474 |
|
|
475 |
function _loadRemote() { |
function _loadRemote() { |
476 |
logp(get_class($this) . "->_loadRemote()"); |
php::log(get_class($this) . "->_loadRemote()"); |
477 |
|
|
478 |
// trace |
// trace |
479 |
//print Dumper($this->meta); |
//print Dumper($this->meta); |
482 |
|
|
483 |
// 1. check backend-handle |
// 1. check backend-handle |
484 |
if (!$this->backend) { |
if (!$this->backend) { |
485 |
logp(get_class($this) . "->_loadRemote: no backend handle, please check argument 'rpcinfo'", PEAR_LOG_CRIT); |
php::log(get_class($this) . "->_loadRemote: no backend handle, please check argument 'rpcinfo'", PEAR_LOG_CRIT); |
486 |
return; |
return; |
487 |
} |
} |
488 |
|
|
490 |
// check for guid or oid |
// check for guid or oid |
491 |
if ($this->meta[guid]) { |
if ($this->meta[guid]) { |
492 |
if (!$this->objectId) { |
if (!$this->objectId) { |
493 |
logp(get_class($this) . "->_loadRemote: argument 'guid' requires valid objectId", PEAR_LOG_WARNING); |
php::log(get_class($this) . "->_loadRemote: argument 'guid' requires valid objectId", PEAR_LOG_WARNING); |
494 |
return; |
return; |
495 |
} |
} |
496 |
if (!$this->meta[classname]) { |
if (!$this->meta[classname]) { |
497 |
logp(get_class($this) . "->_loadRemote: argument 'guid' requires 'classname'", PEAR_LOG_WARNING); |
php::log(get_class($this) . "->_loadRemote: argument 'guid' requires 'classname'", PEAR_LOG_WARNING); |
498 |
return; |
return; |
499 |
} |
} |
500 |
|
php::log(get_class($this) . "->_loadRemote: getObjectByGuid", PEAR_LOG_DEBUG); |
501 |
$args = array( guid => $this->objectId, classname => $this->meta[classname] ); |
$args = array( guid => $this->objectId, classname => $this->meta[classname] ); |
502 |
$result = $this->backend->send('getObjectByGuid', $args ); |
$result = $this->backend->send('getObjectByGuid', $args ); |
503 |
|
|
504 |
} elseif ($this->meta[oid]) { |
} elseif ($this->meta[oid]) { |
505 |
if (!$this->objectId) { |
if (!$this->objectId) { |
506 |
logp(get_class($this) . "->_loadRemote: argument 'oid' requires valid objectId", PEAR_LOG_WARNING); |
php::log(get_class($this) . "->_loadRemote: argument 'oid' requires valid objectId", PEAR_LOG_WARNING); |
507 |
return; |
return; |
508 |
} |
} |
509 |
|
php::log(get_class($this) . "->_loadRemote: getObject", PEAR_LOG_DEBUG); |
510 |
$result = $this->backend->send('getObject', $this->objectId); |
$result = $this->backend->send('getObject', $this->objectId); |
511 |
|
|
512 |
} elseif ($this->meta[key]) { |
} elseif ($this->meta[key]) { |
513 |
if (!$this->meta[command]) { |
if (!$this->meta[command]) { |
514 |
logp(get_class($this) . "->_loadRemote: argument 'key' requires 'command'", PEAR_LOG_WARNING); |
php::log(get_class($this) . "->_loadRemote: argument 'key' requires 'command'", PEAR_LOG_WARNING); |
515 |
return; |
return; |
516 |
} |
} |
517 |
/* |
/* |
518 |
if (!$this->meta[query]) { |
if (!$this->meta[query]) { |
519 |
logp(get_class($this) . "->_loadRemote: argument 'key' requires 'query'", PEAR_LOG_WARNING); |
php::log(get_class($this) . "->_loadRemote: argument 'key' requires 'query'", PEAR_LOG_WARNING); |
520 |
return; |
return; |
521 |
} |
} |
522 |
*/ |
*/ |
523 |
|
//php::log(get_class($this) . "->_loadRemote: $this->meta[command](" . join(' ', $this->meta[query]) . ")", PEAR_LOG_DEBUG); |
524 |
|
//print Dumper(array($this->meta[command], $this->meta[query])); |
525 |
$result = $this->backend->send($this->meta[command], $this->meta[query]); |
$result = $this->backend->send($this->meta[command], $this->meta[query]); |
526 |
|
|
527 |
} |
} |
528 |
|
|
529 |
|
//print "result: " . dumpVar($result) . "<br>"; |
530 |
|
|
531 |
|
$status = $this->backend->status(); |
532 |
|
//print Dumper($status); |
533 |
|
|
534 |
if ($result) { |
$good = is_array($result) && sizeof($result) && $status[connected]; |
535 |
//print "result: " . dumpVar($result) . "<br>"; |
|
536 |
if (count($result) == 0) { return; } |
if ($good) { |
537 |
|
|
538 |
// FIXME: this is dangerous! |
// FIXME: this is dangerous! |
539 |
|
/* |
540 |
if ($_GET[debug]) { |
if ($_GET[debug]) { |
541 |
print Dumper($result); |
print Dumper($result); |
542 |
} |
} |
543 |
|
*/ |
544 |
|
|
545 |
$this->payload = serialize($result); |
$this->payload = serialize($result); |
546 |
// ----- move this to _encode some times |
// ----- move this to _encode some times |
548 |
$this->_saveProxy(); |
$this->_saveProxy(); |
549 |
//print "oid: $this->objectId<br>"; |
//print "oid: $this->objectId<br>"; |
550 |
$this->flushState(); |
$this->flushState(); |
551 |
|
|
552 |
} else { |
} else { |
553 |
//print "Error in _loadRemote!!!<br>"; |
|
554 |
logp(get_class($this) . "->_loadRemote: error while trying to talk to remote side", PEAR_LOG_CRIT); |
if (constants::get('APP_MODE_DEBUG')) { |
555 |
|
$this->draw_error_box($status); |
556 |
|
} else { |
557 |
|
php::maintenance('rpc', array( status => $status ) ); |
558 |
|
} |
559 |
|
|
560 |
} |
} |
561 |
|
|
562 |
} |
} |
563 |
|
|
564 |
|
function draw_error_box($status) { |
565 |
|
$style = html_style("text/css", '.boxlabel_yellow { color: yellow; font-weight:bold; }'); |
566 |
|
$statusbox = html_div(); |
567 |
|
$statusbox->set_style('background: red; border: 2px black groove; width:640px; padding:10px; margin:40px;'); |
568 |
|
$statusbox->add( html_span('boxlabel_yellow', "Method:"), get_class($this) . "->_loadRemote", html_br() ); |
569 |
|
$statusbox->add( html_span('boxlabel_yellow', "Connected:"), $status[connected], html_br() ); |
570 |
|
$statusbox->add( html_span('boxlabel_yellow', "RPCSESSID:"), $status[RPCSESSID], html_br() ); |
571 |
|
foreach ($status[errors] as $error) { |
572 |
|
$msg = html_pre($error[message]); |
573 |
|
$statusbox->add( html_span('boxlabel_yellow', "Error($error[code]):"), $msg ); |
574 |
|
} |
575 |
|
|
576 |
|
$message = "Error while talking to remote side. Please check wire, socket or api."; |
577 |
|
php::log($message, PEAR_LOG_CRIT); |
578 |
|
$statusbox->add( html_span('boxlabel_yellow', "Critical:"), $message, html_br() ); |
579 |
|
|
580 |
|
// V1 |
581 |
|
/* |
582 |
|
if (constants::get('VERBOSE') || constants::get('ERRORS_ONLY')) { |
583 |
|
print $style->render(); |
584 |
|
print $statusbox->render(); |
585 |
|
} else { |
586 |
|
foreach ($status[errors] as $error) { |
587 |
|
print Dumper($error); |
588 |
|
} |
589 |
|
} |
590 |
|
*/ |
591 |
|
|
592 |
|
// V2 |
593 |
|
trace( container($style, $statusbox) ); |
594 |
|
|
595 |
|
} |
596 |
|
|
597 |
|
|
598 |
function _saveBackend($result) { |
function _saveBackend($result) { |
599 |
logp(get_class($this) . "->_saveBackend()"); |
php::log(get_class($this) . "->_saveBackend()"); |
600 |
|
|
601 |
//$encoder = new TextEncode($result); |
//$encoder = new TextEncode($result); |
602 |
//$encoder->toUTF8(); |
//$encoder->toUTF8(); |
627 |
|
|
628 |
} |
} |
629 |
|
|
|
?> |
|
630 |
|
?> |