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.15 2003/04/11 01:32:21 joko |
22 |
|
* renamed logging function |
23 |
|
* |
24 |
|
* Revision 1.14 2003/04/09 02:06:45 joko |
25 |
|
* errormessage now shown preformatted |
26 |
|
* |
27 |
|
* Revision 1.13 2003/04/04 17:38:03 joko |
28 |
|
* modifications regarding error-/exception-handling and -tracing |
29 |
|
* |
30 |
|
* Revision 1.12 2003/03/29 08:01:21 joko |
31 |
|
* modified ErrorBoxing |
32 |
|
* |
33 |
|
* Revision 1.11 2003/03/28 06:44:51 joko |
34 |
|
* VERBOSE mode |
35 |
|
* |
36 |
|
* Revision 1.10 2003/03/28 03:05:54 joko |
37 |
|
* more fancy debugging-output |
38 |
|
* |
39 |
|
* Revision 1.9 2003/03/10 23:05:25 joko |
40 |
|
* + fixed metadata for phpDocumentor |
41 |
|
* |
42 |
|
* Revision 1.8 2003/03/10 22:31:56 joko |
43 |
|
* + fixed metadata for phpDocumentor |
44 |
|
* |
45 |
|
* Revision 1.7 2003/03/09 15:51:44 joko |
46 |
|
* + additional metadata for Autodia |
47 |
|
* |
48 |
|
* Revision 1.6 2003/03/05 17:28:43 joko |
49 |
|
* updated docu (phpDocumentor testing....) |
50 |
|
* |
51 |
|
* Revision 1.5 2003/03/05 17:02:22 joko |
52 |
|
* updated docu (phpDocumentor testing....) |
53 |
|
* |
54 |
* Revision 1.4 2003/03/05 16:32:19 joko |
* Revision 1.4 2003/03/05 16:32:19 joko |
55 |
* updated docu (phpDocumentor testing....) |
* updated docu (phpDocumentor testing....) |
56 |
* |
* |
163 |
|
|
164 |
|
|
165 |
/** |
/** |
166 |
* Data::Driver::Proxy -- Multiple stage data fetching and caching |
* DesignPattern::RemoteProxy -- Multiple stage data fetching and caching |
167 |
* |
* |
168 |
* |
* |
169 |
* This class (Data::Driver::Proxy) provides an abstract framework |
* This class (DesignPattern::RemoteProxy) provides an abstract framework |
170 |
* for loading/saving arbitrary data from/to data storages interfaced |
* for loading/saving arbitrary data from/to data storages interfaced |
171 |
* by storage *proxy*-drivers. |
* by storage *proxy*-drivers. |
172 |
* Don't mix these up with the concrete storage *handle*-drivers |
* Don't mix these up with the concrete storage *handle*-drivers |
175 |
* providing a more highlevel, consistent API making |
* providing a more highlevel, consistent API making |
176 |
* it easier for Data::Driver::Proxy to do its main work: |
* it easier for Data::Driver::Proxy to do its main work: |
177 |
* |
* |
178 |
|
* quote from: http://home.earthlink.net/~huston2/dp/proxy.html |
179 |
|
* "A remote proxy provides a local representative for an |
180 |
|
* object that resides in a different address space. This is |
181 |
|
* what the "stub" code in RPC and CORBA provides." |
182 |
|
* |
183 |
|
* |
184 |
* Multiple stage data fetching and caching: |
* Multiple stage data fetching and caching: |
185 |
* |
* |
186 |
* <pre> |
* <pre> |
233 |
* An attempt to implement some software design patterns |
* An attempt to implement some software design patterns |
234 |
* --- RemoteProxyPattern |
* --- RemoteProxyPattern |
235 |
* |
* |
236 |
|
* @link http://www.agcs.com/supportv2/techpapers/patterns/papers/tutnotes/sld017.htm |
237 |
|
* @link http://home.earthlink.net/~huston2/dp/proxy.html |
238 |
|
* @link http://wiki.cs.uiuc.edu/PatternStories/RemoteObject |
239 |
* @link http://c2.com/cgi-bin/wiki?ProxyPattern |
* @link http://c2.com/cgi-bin/wiki?ProxyPattern |
240 |
|
* @link http://c2.com/cgi-bin/wiki?LazyProxies |
241 |
* |
* |
242 |
* @author Andreas Motl <andreas.motl@ilo.de> |
* @author Andreas Motl <andreas.motl@ilo.de> |
243 |
* @link http://www.netfrag.org/~joko/ |
* @link http://www.netfrag.org/~joko/ |
247 |
* @link http://www.gnu.org/licenses/lgpl.txt |
* @link http://www.gnu.org/licenses/lgpl.txt |
248 |
* |
* |
249 |
* @package org.netfrag.glib |
* @package org.netfrag.glib |
250 |
|
* @subpackage DesignPattern |
251 |
* @name DesignPattern::RemoteProxy |
* @name DesignPattern::RemoteProxy |
252 |
* |
* |
253 |
* |
* |
254 |
* @todo |
* @todo extend options to en-/disable caching via a) session and/or b) database |
255 |
* <pre> |
* make feature available via runtime setter-method to these options |
256 |
* x extend options to en-/disable caching via a) session and/or b) database |
* @todo PEAR::Cache for caching purposes!!! |
257 |
* o make feature available via runtime setter-method to these options |
* @todo refactor database access: use PEAR for this! no more 'connectdb' here!!! |
258 |
* o use PEAR::Cache for caching purposes!!! |
* @todo make database connection more flexible to make possible |
259 |
* o refactor database access: use PEAR for this! no more 'connectdb' here!!! |
* to have different (probably named) proxy databases (besides a "main database") |
260 |
* 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? |
261 |
* to have different (probably named) proxy databases (besides a "main database") |
* @todo refactor this to a "RemoteObject" class!!! (inheriting from DesignPattern::RemoteObject) |
262 |
* o rename this to Data::Proxy? or split into Data::Query, Data::Result and Data::Wrapper? |
* @todo rename this to "DesignPattern::LazyRemoteProxy"??? |
|
* </pre> |
|
263 |
* |
* |
264 |
*/ |
*/ |
265 |
class DesignPattern_RemoteProxy extends DesignPattern_Proxy { |
class DesignPattern_RemoteProxy extends DesignPattern_Proxy { |
272 |
var $backend; |
var $backend; |
273 |
|
|
274 |
function DesignPattern_RemoteProxy($objectId = "", $options = array() ) { |
function DesignPattern_RemoteProxy($objectId = "", $options = array() ) { |
275 |
logp(get_class($this) . "->new()", PEAR_LOG_INFO); |
php::log(get_class($this) . "->new()", PEAR_LOG_INFO); |
276 |
global $proxy; |
global $proxy; |
277 |
|
|
278 |
// 2003-03-05 - modified constructor |
// 2003-03-05 - modified constructor |
314 |
session_register_safe("proxy"); |
session_register_safe("proxy"); |
315 |
} |
} |
316 |
|
|
317 |
|
/** |
318 |
|
* <!-- Autodia --> |
319 |
|
* can do: (this is metadata supplied for Autodia, don't delete!) |
320 |
|
* $this->backend = new DataSource_Proxy_XMLRPC() |
321 |
|
* |
322 |
|
*/ |
323 |
|
|
324 |
if ($this->meta[remote]) { |
if ($this->meta[remote]) { |
325 |
//$this->backend = mkObject('Data::Driver::RPC::Remote', $this->meta[rpcinfo]); |
//$this->backend = mkObject('Data::Driver::RPC::Remote', $this->meta[rpcinfo]); |
326 |
$this->backend = php::mkComponent('DataSource::Proxy::XMLRPC', $this->meta[rpcinfo]); |
$this->backend = php::mkComponent('DataSource::Proxy::XMLRPC', $this->meta[rpcinfo]); |
336 |
|
|
337 |
|
|
338 |
function load() { |
function load() { |
339 |
logp(get_class($this) . "->load()", PEAR_LOG_INFO); |
php::log(get_class($this) . "->load()", PEAR_LOG_INFO); |
340 |
if (!$this->_loadState()) { |
if (!$this->_loadState()) { |
341 |
if (!$this->_loadProxy()) { |
if (!$this->_loadProxy()) { |
342 |
|
|
412 |
//print Dumper($this); |
//print Dumper($this); |
413 |
|
|
414 |
// debug |
// debug |
415 |
logp(get_class($this) . "->_loadState()"); |
php::log(get_class($this) . "->_loadState()"); |
416 |
|
|
417 |
if ($this->attributes = $proxy[$this->objectId]) { |
if ($this->attributes = $proxy[$this->objectId]) { |
418 |
//print "_loadState:" . dumpVar($this->attributes); |
//print "_loadState:" . dumpVar($this->attributes); |
424 |
|
|
425 |
function _saveState() { |
function _saveState() { |
426 |
global $proxy; |
global $proxy; |
427 |
logp(get_class($this) . "->_saveState()"); |
php::log(get_class($this) . "->_saveState()"); |
428 |
$proxy[$this->objectId] = $this->attributes; |
$proxy[$this->objectId] = $this->attributes; |
429 |
//print "_saveState: " . dumpVar($this->attributes); |
//print "_saveState: " . dumpVar($this->attributes); |
430 |
// TODO: throw exception-message back to user if operation fails |
// TODO: throw exception-message back to user if operation fails |
437 |
|
|
438 |
// trace & debug |
// trace & debug |
439 |
//print Dumper($this); |
//print Dumper($this); |
440 |
logp(get_class($this) . "->_loadProxy()"); |
php::log(get_class($this) . "->_loadProxy()"); |
441 |
|
|
442 |
connectdb(); |
connectdb(); |
443 |
$sql = "SELECT payload FROM f_proxy WHERE oid='$this->objectId'"; |
$sql = "SELECT payload FROM f_proxy WHERE oid='$this->objectId'"; |
457 |
// FIXME! |
// FIXME! |
458 |
if (!$this->meta[cache][db]) { return; } |
if (!$this->meta[cache][db]) { return; } |
459 |
|
|
460 |
logp(get_class($this) . "->_saveProxy()"); |
php::log(get_class($this) . "->_saveProxy()"); |
461 |
connectdb(); |
connectdb(); |
462 |
if ($this->payload) { |
if ($this->payload) { |
463 |
//$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'"; |
470 |
} |
} |
471 |
|
|
472 |
function _loadRemote() { |
function _loadRemote() { |
473 |
logp(get_class($this) . "->_loadRemote()"); |
php::log(get_class($this) . "->_loadRemote()"); |
474 |
|
|
475 |
// trace |
// trace |
476 |
//print Dumper($this->meta); |
//print Dumper($this->meta); |
479 |
|
|
480 |
// 1. check backend-handle |
// 1. check backend-handle |
481 |
if (!$this->backend) { |
if (!$this->backend) { |
482 |
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); |
483 |
return; |
return; |
484 |
} |
} |
485 |
|
|
487 |
// check for guid or oid |
// check for guid or oid |
488 |
if ($this->meta[guid]) { |
if ($this->meta[guid]) { |
489 |
if (!$this->objectId) { |
if (!$this->objectId) { |
490 |
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); |
491 |
return; |
return; |
492 |
} |
} |
493 |
if (!$this->meta[classname]) { |
if (!$this->meta[classname]) { |
494 |
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); |
495 |
return; |
return; |
496 |
} |
} |
497 |
|
php::log(get_class($this) . "->_loadRemote: getObjectByGuid", PEAR_LOG_DEBUG); |
498 |
$args = array( guid => $this->objectId, classname => $this->meta[classname] ); |
$args = array( guid => $this->objectId, classname => $this->meta[classname] ); |
499 |
$result = $this->backend->send('getObjectByGuid', $args ); |
$result = $this->backend->send('getObjectByGuid', $args ); |
500 |
|
|
501 |
} elseif ($this->meta[oid]) { |
} elseif ($this->meta[oid]) { |
502 |
if (!$this->objectId) { |
if (!$this->objectId) { |
503 |
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); |
504 |
return; |
return; |
505 |
} |
} |
506 |
|
php::log(get_class($this) . "->_loadRemote: getObject", PEAR_LOG_DEBUG); |
507 |
$result = $this->backend->send('getObject', $this->objectId); |
$result = $this->backend->send('getObject', $this->objectId); |
508 |
|
|
509 |
} elseif ($this->meta[key]) { |
} elseif ($this->meta[key]) { |
510 |
if (!$this->meta[command]) { |
if (!$this->meta[command]) { |
511 |
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); |
512 |
return; |
return; |
513 |
} |
} |
514 |
/* |
/* |
515 |
if (!$this->meta[query]) { |
if (!$this->meta[query]) { |
516 |
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); |
517 |
return; |
return; |
518 |
} |
} |
519 |
*/ |
*/ |
520 |
|
//php::log(get_class($this) . "->_loadRemote: $this->meta[command](" . join(' ', $this->meta[query]) . ")", PEAR_LOG_DEBUG); |
521 |
|
//print Dumper(array($this->meta[command], $this->meta[query])); |
522 |
$result = $this->backend->send($this->meta[command], $this->meta[query]); |
$result = $this->backend->send($this->meta[command], $this->meta[query]); |
523 |
|
|
524 |
} |
} |
525 |
|
|
526 |
|
//print "result: " . dumpVar($result) . "<br>"; |
527 |
|
|
528 |
|
$status = $this->backend->status(); |
529 |
|
//print Dumper($status); |
530 |
|
|
531 |
if ($result) { |
$good = is_array($result) && sizeof($result) && $status[connected]; |
532 |
//print "result: " . dumpVar($result) . "<br>"; |
|
533 |
if (count($result) == 0) { return; } |
if ($good) { |
534 |
|
|
535 |
// FIXME: this is dangerous! |
// FIXME: this is dangerous! |
536 |
|
/* |
537 |
if ($_GET[debug]) { |
if ($_GET[debug]) { |
538 |
print Dumper($result); |
print Dumper($result); |
539 |
} |
} |
540 |
|
*/ |
541 |
|
|
542 |
$this->payload = serialize($result); |
$this->payload = serialize($result); |
543 |
// ----- move this to _encode some times |
// ----- move this to _encode some times |
545 |
$this->_saveProxy(); |
$this->_saveProxy(); |
546 |
//print "oid: $this->objectId<br>"; |
//print "oid: $this->objectId<br>"; |
547 |
$this->flushState(); |
$this->flushState(); |
548 |
|
|
549 |
} else { |
} else { |
550 |
//print "Error in _loadRemote!!!<br>"; |
|
551 |
logp(get_class($this) . "->_loadRemote: error while trying to talk to remote side", PEAR_LOG_CRIT); |
if (constants::get('APP_MODE_DEBUG')) { |
552 |
|
$this->draw_error_box($status); |
553 |
|
} else { |
554 |
|
php::maintenance('rpc', array( status => $status ) ); |
555 |
|
} |
556 |
|
|
557 |
} |
} |
558 |
|
|
559 |
} |
} |
560 |
|
|
561 |
|
function draw_error_box($status) { |
562 |
|
$style = html_style("text/css", '.boxlabel_yellow { color: yellow; font-weight:bold; }'); |
563 |
|
$statusbox = html_div(); |
564 |
|
$statusbox->set_style('background: red; border: 2px black groove; width:640px; padding:10px; margin:40px;'); |
565 |
|
$statusbox->add( html_span('boxlabel_yellow', "Method:"), get_class($this) . "->_loadRemote", html_br() ); |
566 |
|
$statusbox->add( html_span('boxlabel_yellow', "Connected:"), $status[connected], html_br() ); |
567 |
|
$statusbox->add( html_span('boxlabel_yellow', "RPCSESSID:"), $status[RPCSESSID], html_br() ); |
568 |
|
foreach ($status[errors] as $error) { |
569 |
|
$msg = html_pre($error[message]); |
570 |
|
$statusbox->add( html_span('boxlabel_yellow', "Error($error[code]):"), $msg ); |
571 |
|
} |
572 |
|
|
573 |
|
$message = "Error while talking to remote side. Please check wire, socket or api."; |
574 |
|
php::log($message, PEAR_LOG_CRIT); |
575 |
|
$statusbox->add( html_span('boxlabel_yellow', "Critical:"), $message, html_br() ); |
576 |
|
|
577 |
|
// V1 |
578 |
|
/* |
579 |
|
if (constants::get('VERBOSE') || constants::get('ERRORS_ONLY')) { |
580 |
|
print $style->render(); |
581 |
|
print $statusbox->render(); |
582 |
|
} else { |
583 |
|
foreach ($status[errors] as $error) { |
584 |
|
print Dumper($error); |
585 |
|
} |
586 |
|
} |
587 |
|
*/ |
588 |
|
|
589 |
|
// V2 |
590 |
|
trace( container($style, $statusbox) ); |
591 |
|
|
592 |
|
} |
593 |
|
|
594 |
|
|
595 |
function _saveBackend($result) { |
function _saveBackend($result) { |
596 |
logp(get_class($this) . "->_saveBackend()"); |
php::log(get_class($this) . "->_saveBackend()"); |
597 |
|
|
598 |
//$encoder = new TextEncode($result); |
//$encoder = new TextEncode($result); |
599 |
//$encoder->toUTF8(); |
//$encoder->toUTF8(); |