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

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.20

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