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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.4 by joko, Wed Mar 5 16:32:19 2003 UTC revision 1.19 by joko, Mon Jun 7 16:56:45 2004 UTC
# Line 5  Line 5 
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>
# Line 15  Line 18 
18   *    $Id$   *    $Id$
19   * -------------------------------------------------------------------------   * -------------------------------------------------------------------------
20   *    $Log$   *    $Log$
21     *    Revision 1.19  2004/06/07 16:56:45  joko
22     *    enabled caching-layers
23     *    minor fixes regarding caching
24     *
25     *    Revision 1.18  2004/05/13 19:17:55  jonen
26     *    + bugfix: utf8 conversion was missing at some backend-calls
27     *
28     *    Revision 1.17  2003/07/14 10:05:23  jonen
29     *    bugfix: added *needed* function 'getAttributes'
30     *
31     *    Revision 1.16  2003/07/02 13:51:38  jonen
32     *    removed debug dumper
33     *
34     *    Revision 1.15  2003/04/11 01:32:21  joko
35     *    renamed logging function
36     *
37     *    Revision 1.14  2003/04/09 02:06:45  joko
38     *    errormessage now shown preformatted
39     *
40     *    Revision 1.13  2003/04/04 17:38:03  joko
41     *    modifications regarding error-/exception-handling and -tracing
42     *
43     *    Revision 1.12  2003/03/29 08:01:21  joko
44     *    modified ErrorBoxing
45     *
46     *    Revision 1.11  2003/03/28 06:44:51  joko
47     *    VERBOSE mode
48     *
49     *    Revision 1.10  2003/03/28 03:05:54  joko
50     *    more fancy debugging-output
51     *
52     *    Revision 1.9  2003/03/10 23:05:25  joko
53     *    + fixed metadata for phpDocumentor
54     *
55     *    Revision 1.8  2003/03/10 22:31:56  joko
56     *    + fixed metadata for phpDocumentor
57     *
58     *    Revision 1.7  2003/03/09 15:51:44  joko
59     *    + additional metadata for Autodia
60     *
61     *    Revision 1.6  2003/03/05 17:28:43  joko
62     *    updated docu (phpDocumentor testing....)
63     *
64     *    Revision 1.5  2003/03/05 17:02:22  joko
65     *    updated docu (phpDocumentor testing....)
66     *
67   *    Revision 1.4  2003/03/05 16:32:19  joko   *    Revision 1.4  2003/03/05 16:32:19  joko
68   *    updated docu (phpDocumentor testing....)   *    updated docu (phpDocumentor testing....)
69   *   *
# Line 127  loadModule('DesignPattern::Proxy'); Line 176  loadModule('DesignPattern::Proxy');
176    
177    
178  /**  /**
179   * Data::Driver::Proxy  --  Multiple stage data fetching and caching   * DesignPattern::RemoteProxy  --  Multiple stage data fetching and caching
180   *   *
181   *   *
182   * This class (Data::Driver::Proxy) provides an abstract framework   * This class (DesignPattern::RemoteProxy) provides an abstract framework
183   * for loading/saving arbitrary data from/to data storages interfaced   * for loading/saving arbitrary data from/to data storages interfaced
184   * by storage *proxy*-drivers.   * by storage *proxy*-drivers.
185   * Don't mix these up with the concrete storage *handle*-drivers   * Don't mix these up with the concrete storage *handle*-drivers
# Line 139  loadModule('DesignPattern::Proxy'); Line 188  loadModule('DesignPattern::Proxy');
188   * providing a more highlevel, consistent API making   * providing a more highlevel, consistent API making
189   * it easier for Data::Driver::Proxy to do its main work:   * it easier for Data::Driver::Proxy to do its main work:
190   *   *
191     * quote from: http://home.earthlink.net/~huston2/dp/proxy.html
192     * "A remote proxy provides a local representative for an
193     * object that resides in a different address space. This is
194     * what the "stub" code in RPC and CORBA provides."
195     *
196     *
197   * Multiple stage data fetching and caching:   * Multiple stage data fetching and caching:
198   *   *
199   * <pre>   * <pre>
# Line 191  loadModule('DesignPattern::Proxy'); Line 246  loadModule('DesignPattern::Proxy');
246   * An attempt to implement some software design patterns   * An attempt to implement some software design patterns
247   * --- RemoteProxyPattern   * --- RemoteProxyPattern
248   *   *
249     * @link http://www.agcs.com/supportv2/techpapers/patterns/papers/tutnotes/sld017.htm
250     * @link http://home.earthlink.net/~huston2/dp/proxy.html
251     * @link http://wiki.cs.uiuc.edu/PatternStories/RemoteObject
252   * @link http://c2.com/cgi-bin/wiki?ProxyPattern   * @link http://c2.com/cgi-bin/wiki?ProxyPattern
253     * @link http://c2.com/cgi-bin/wiki?LazyProxies
254   *   *
255   * @author Andreas Motl <andreas.motl@ilo.de>   * @author Andreas Motl <andreas.motl@ilo.de>
256   * @link http://www.netfrag.org/~joko/   * @link http://www.netfrag.org/~joko/
# Line 201  loadModule('DesignPattern::Proxy'); Line 260  loadModule('DesignPattern::Proxy');
260   * @link http://www.gnu.org/licenses/lgpl.txt   * @link http://www.gnu.org/licenses/lgpl.txt
261   *   *
262   * @package org.netfrag.glib   * @package org.netfrag.glib
263     * @subpackage DesignPattern
264   * @name DesignPattern::RemoteProxy   * @name DesignPattern::RemoteProxy
265   *   *
266   *   *
267   * @todo   * @todo extend options to en-/disable caching via a) session and/or b) database
268   * <pre>   *           make feature available via runtime setter-method to these options
269   *  x extend options to en-/disable caching via a) session and/or b) database   * @todo PEAR::Cache for caching purposes!!!
270   *    o make feature available via runtime setter-method to these options   * @todo refactor database access: use PEAR for this! no more 'connectdb' here!!!
271   *  o use PEAR::Cache for caching purposes!!!   * @todo make database connection more flexible to make possible
272   *  o refactor database access: use PEAR for this! no more 'connectdb' here!!!   *           to have different (probably named) proxy databases (besides a "main database")
273   *  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?
274   *      to have different (probably named) proxy databases (besides a "main database")   * @todo refactor this to a "RemoteObject" class!!! (inheriting from DesignPattern::RemoteObject)
275   *  o rename this to Data::Proxy? or split into Data::Query, Data::Result and Data::Wrapper?   * @todo rename this to "DesignPattern::LazyRemoteProxy"???
  * </pre>  
276   *   *
277   */   */
278  class DesignPattern_RemoteProxy extends DesignPattern_Proxy {  class DesignPattern_RemoteProxy extends DesignPattern_Proxy {
# Line 226  class DesignPattern_RemoteProxy extends Line 285  class DesignPattern_RemoteProxy extends
285    var $backend;    var $backend;
286    
287    function DesignPattern_RemoteProxy($objectId = "", $options = array() ) {    function DesignPattern_RemoteProxy($objectId = "", $options = array() ) {
288      logp(get_class($this) . "->new()", PEAR_LOG_INFO);      php::log(get_class($this) . "->new(objectId=$objectId)", PEAR_LOG_INFO);
289      global $proxy;      global $proxy;
290    
291      // 2003-03-05 - modified constructor      // 2003-03-05 - modified constructor
# Line 268  class DesignPattern_RemoteProxy extends Line 327  class DesignPattern_RemoteProxy extends
327        session_register_safe("proxy");        session_register_safe("proxy");
328      }        }  
329    
330        /**
331         * <!-- Autodia -->
332         * can do: (this is metadata supplied for Autodia, don't delete!)
333         *  $this->backend = new DataSource_Proxy_XMLRPC()
334         *
335         */
336    
337      if ($this->meta[remote]) {      if ($this->meta[remote]) {
338        //$this->backend = mkObject('Data::Driver::RPC::Remote', $this->meta[rpcinfo]);        //$this->backend = mkObject('Data::Driver::RPC::Remote', $this->meta[rpcinfo]);
339        $this->backend = php::mkComponent('DataSource::Proxy::XMLRPC', $this->meta[rpcinfo]);        $this->backend = php::mkComponent('DataSource::Proxy::XMLRPC', $this->meta[rpcinfo]);
# Line 283  class DesignPattern_RemoteProxy extends Line 349  class DesignPattern_RemoteProxy extends
349    
350    
351    function load() {    function load() {
352      logp(get_class($this) . "->load()", PEAR_LOG_INFO);      php::log(get_class($this) . "->load()", PEAR_LOG_INFO);
353      if (!$this->_loadState()) {      if (!$this->_loadState()) {
354        if (!$this->_loadProxy()) {        if (!$this->_loadProxy()) {
355    
# Line 340  class DesignPattern_RemoteProxy extends Line 406  class DesignPattern_RemoteProxy extends
406      $this->attributes = $data;      $this->attributes = $data;
407    }    }
408    
409      function getAttributes() {
410        return $this->attributes;
411      }
412    
413    function flushProxy() {    function flushProxy() {
414          connectdb();          connectdb();
415      $sql = "DELETE FROM f_proxy WHERE oid='$this->objectId'";      $sql = "DELETE FROM f_proxy WHERE oid='$this->objectId'";
# Line 355  class DesignPattern_RemoteProxy extends Line 425  class DesignPattern_RemoteProxy extends
425    function _loadState() {    function _loadState() {
426      global $proxy;      global $proxy;
427    
428        // just do session-caching if requested
429        if (!$this->meta[cache][session]) { return; }
430    
431      // trace      // trace
432        //print Dumper($this);        //print Dumper($this);
433    
434      // debug      // debug
435        logp(get_class($this) . "->_loadState()");        php::log(get_class($this) . "->_loadState()");
436                
437      if ($this->attributes = $proxy[$this->objectId]) {      if ($this->attributes = $proxy[$this->objectId]) {
438        //print "_loadState:" . dumpVar($this->attributes);        //print "_loadState:" . dumpVar($this->attributes);
439        $this->meta[decoded] = 1;        $this->meta[decoded] = 1;
440        // TODO: make a parameter from this (0 deactivates session-layer)        // TODO: make a parameter from this (0 deactivates session-layer)
441        return 0;        return 1;
442      }      }
443    }    }
444    
445    function _saveState() {    function _saveState() {
446      global $proxy;      global $proxy;
447      logp(get_class($this) . "->_saveState()");      
448        // just do session-caching if requested
449        if (!$this->meta[cache][session]) { return; }
450        
451        php::log(get_class($this) . "->_saveState()");
452      $proxy[$this->objectId] = $this->attributes;      $proxy[$this->objectId] = $this->attributes;
453      //print "_saveState: " . dumpVar($this->attributes);      //print "_saveState: " . dumpVar($this->attributes);
454      // TODO: throw exception-message back to user if operation fails      // TODO: throw exception-message back to user if operation fails
# Line 384  class DesignPattern_RemoteProxy extends Line 461  class DesignPattern_RemoteProxy extends
461            
462      // trace & debug      // trace & debug
463        //print Dumper($this);        //print Dumper($this);
464        logp(get_class($this) . "->_loadProxy()");        php::log(get_class($this) . "->_loadProxy()");
465                
466      connectdb();      connectdb();
467      $sql = "SELECT payload FROM f_proxy WHERE oid='$this->objectId'";      $sql = "SELECT payload FROM f_proxy WHERE oid='$this->objectId'";
# Line 393  class DesignPattern_RemoteProxy extends Line 470  class DesignPattern_RemoteProxy extends
470        if ($row) {        if ($row) {
471          $this->payload = $row[payload];          $this->payload = $row[payload];
472          // TODO: make a parameter from this (0 deactivates mysqldb-layer)          // TODO: make a parameter from this (0 deactivates mysqldb-layer)
473          return 0;          return 1;
474        }        }
475      }      }
476    }    }
# Line 404  class DesignPattern_RemoteProxy extends Line 481  class DesignPattern_RemoteProxy extends
481      // FIXME!      // FIXME!
482      if (!$this->meta[cache][db]) { return; }      if (!$this->meta[cache][db]) { return; }
483    
484      logp(get_class($this) . "->_saveProxy()");      php::log(get_class($this) . "->_saveProxy()");
485          connectdb();          connectdb();
486          if ($this->payload) {          if ($this->payload) {
487        //$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'";
# Line 417  class DesignPattern_RemoteProxy extends Line 494  class DesignPattern_RemoteProxy extends
494    }    }
495        
496    function _loadRemote() {    function _loadRemote() {
497      logp(get_class($this) . "->_loadRemote()");      php::log(get_class($this) . "->_loadRemote()");
498            
499      // trace      // trace
500        //print Dumper($this->meta);        //print Dumper($this->meta);
# Line 426  class DesignPattern_RemoteProxy extends Line 503  class DesignPattern_RemoteProxy extends
503            
504      // 1. check backend-handle      // 1. check backend-handle
505      if (!$this->backend) {      if (!$this->backend) {
506        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);
507        return;        return;
508      }      }
509            
# Line 434  class DesignPattern_RemoteProxy extends Line 511  class DesignPattern_RemoteProxy extends
511        // check for guid or oid        // check for guid or oid
512        if ($this->meta[guid]) {        if ($this->meta[guid]) {
513          if (!$this->objectId) {          if (!$this->objectId) {
514            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);
515            return;            return;
516          }          }
517          if (!$this->meta[classname]) {          if (!$this->meta[classname]) {
518            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);
519            return;            return;
520          }          }
521            php::log(get_class($this) . "->_loadRemote: getObjectByGuid", PEAR_LOG_DEBUG);
522          $args = array( guid => $this->objectId, classname => $this->meta[classname] );          $args = array( guid => $this->objectId, classname => $this->meta[classname] );
523          $result = $this->backend->send('getObjectByGuid', $args );          $result = $this->backend->send('getObjectByGuid', $args, array( utf8 => 1)  );
524    
525        } elseif ($this->meta[oid]) {        } elseif ($this->meta[oid]) {
526          if (!$this->objectId) {          if (!$this->objectId) {
527            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);
528            return;            return;
529          }          }
530          $result = $this->backend->send('getObject', $this->objectId);          php::log(get_class($this) . "->_loadRemote: getObject", PEAR_LOG_DEBUG);
531            $result = $this->backend->send('getObject', $this->objectId, array( utf8 => 1) );
532    
533        } elseif ($this->meta[key]) {        } elseif ($this->meta[key]) {
534          if (!$this->meta[command]) {          if (!$this->meta[command]) {
535            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);
536            return;            return;
537          }          }
538          /*          /*
539          if (!$this->meta[query]) {          if (!$this->meta[query]) {
540            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);
541            return;            return;
542          }          }
543          */          */
544          $result = $this->backend->send($this->meta[command], $this->meta[query]);          //php::log(get_class($this) . "->_loadRemote: $this->meta[command](" . join(' ', $this->meta[query]) . ")", PEAR_LOG_DEBUG);
545            //print Dumper(array($this->meta[command], $this->meta[query]));
546            $result = $this->backend->send($this->meta[command], $this->meta[query], array( utf8 => 1) );
547                    
548        }        }
549    
550        //print "result: " . dumpVar($result) . "<br>";
551    
552        $status = $this->backend->status();
553        //print Dumper($status);
554            
555      if ($result) {      $good = is_array($result) && sizeof($result) && $status[connected];
556        //print "result: " . dumpVar($result) . "<br>";  
557        if (count($result) == 0) { return; }      if ($good) {
558                
559        // FIXME: this is dangerous!        // FIXME: this is dangerous!
560          /*
561        if ($_GET[debug]) {        if ($_GET[debug]) {
562          print Dumper($result);          print Dumper($result);
563        }        }
564          */
565                
566        $this->payload = serialize($result);        $this->payload = serialize($result);
567        // ----- move this to _encode some times        // ----- move this to _encode some times
# Line 483  class DesignPattern_RemoteProxy extends Line 569  class DesignPattern_RemoteProxy extends
569        $this->_saveProxy();        $this->_saveProxy();
570        //print "oid: $this->objectId<br>";        //print "oid: $this->objectId<br>";
571        $this->flushState();        $this->flushState();
572        
573      } else {      } else {
574        //print "Error in _loadRemote!!!<br>";        
575        logp(get_class($this) . "->_loadRemote: error while trying to talk to remote side", PEAR_LOG_CRIT);        if (constants::get('APP_MODE_DEBUG')) {
576            $this->draw_error_box($status);
577          } else {
578            php::maintenance('rpc', array( status => $status ) );
579          }
580        
581      }      }
582            
583    }    }
584    
585    function draw_error_box($status) {
586      $style = html_style("text/css", '.boxlabel_yellow { color: yellow; font-weight:bold; }');
587      $statusbox = html_div();
588      $statusbox->set_style('background: red; border: 2px black groove; width:640px; padding:10px; margin:40px;');
589      $statusbox->add( html_span('boxlabel_yellow', "Method:"), get_class($this) . "->_loadRemote", html_br() );
590      $statusbox->add( html_span('boxlabel_yellow', "Connected:"), $status[connected], html_br() );
591      $statusbox->add( html_span('boxlabel_yellow', "RPCSESSID:"), $status[RPCSESSID], html_br() );
592      foreach ($status[errors] as $error) {
593        $msg = html_pre($error[message]);
594        $statusbox->add( html_span('boxlabel_yellow', "Error($error[code]):"), $msg );
595      }
596        
597      $message = "Error while talking to remote side. Please check wire, socket or api.";
598      php::log($message, PEAR_LOG_CRIT);
599      $statusbox->add( html_span('boxlabel_yellow', "Critical:"), $message, html_br() );
600    
601      // V1
602      /*
603      if (constants::get('VERBOSE') || constants::get('ERRORS_ONLY')) {
604        print $style->render();
605        print $statusbox->render();
606      } else {
607        foreach ($status[errors] as $error) {
608          print Dumper($error);
609        }
610      }
611      */
612    
613      // V2
614      trace( container($style, $statusbox) );
615    
616    }  
617    
618    
619    function _saveBackend($result) {    function _saveBackend($result) {
620      logp(get_class($this) . "->_saveBackend()");      php::log(get_class($this) . "->_saveBackend()");
621    
622      //$encoder = new TextEncode($result);      //$encoder = new TextEncode($result);
623      //$encoder->toUTF8();      //$encoder->toUTF8();
# Line 522  class DesignPattern_RemoteProxy extends Line 648  class DesignPattern_RemoteProxy extends
648    
649  }  }
650    
 ?>  
651    ?>

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.19

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