/[cvs]/nfo/php/libs/org.netfrag.glib/Data/Driver/Proxy.php
ViewVC logotype

Annotation of /nfo/php/libs/org.netfrag.glib/Data/Driver/Proxy.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations)
Sun Feb 9 17:23:21 2003 UTC (21 years, 7 months ago) by joko
Branch: MAIN
+ refactored from flib/Application/RPC/ProxyObject.php

1 joko 1.1 <?
2     /*
3     ## -------------------------------------------------------------------------------------
4     ## $Id: ProxyObject.php,v 1.7 2003/02/03 03:31:38 jonen Exp $
5     ## -------------------------------------------------------------------------------------
6     ## $Log: ProxyObject.php,v $
7     ## Revision 1.7 2003/02/03 03:31:38 jonen
8     ## - moved '$encoder->toISO()' to 'Remote.php'
9     ##
10     ## Revision 1.6 2002/12/22 13:26:20 jonen
11     ## + added support of tangram independent id (e.g. Data::UUID) toggled by option at conrtuctor
12     ##
13     ## Revision 1.5 2002/12/18 22:36:49 jonen
14     ## + added support to get remote objects via backend via 'guid'
15     ## + renamed '_loadBackend' to '_loadRemote'
16     ## + constructor now accepts additional options:
17     ## + remote: try to get object via '_loadRemote' ...
18     ## + oid: use the given identifier as an 'oid' when doing '_loadRemote'
19     ## + guid: use the given identifier as a 'guid' when doing '_loadRemote'
20     ##
21     ## Revision 1.4 2002/12/12 02:46:31 joko
22     ## + state (session) gets flushed when data was successfully loaded from remote side
23     ## + fixed _loadBackend
24     ## + fixed flushState
25     ##
26     ## Revision 1.3 2002/12/06 04:12:54 joko
27     ## + replaced 'xyzCache' through 'xyzProxy'
28     ## + function store(...)
29     ##
30     ## Revision 1.2 2002/12/05 21:44:09 joko
31     ## + debugging
32     ##
33     ## Revision 1.1 2002/12/01 17:23:58 joko
34     ## + initial check-in
35     ##
36     ## Revision 1.11 2002/11/12 06:05:58 cvsjoko
37     ## + renamed class: Text_Encode -> TextEncode
38     ##
39     ## Revision 1.10 2002/10/29 19:15:33 cvsjoko
40     ## - moved utf8/iso-conversion to lib/utils/Text_Encode.php
41     ##
42     ## Revision 1.9 2002/10/26 12:32:36 cvsjoko
43     ## - removed debugging via "print"
44     ## + added generic logging via PEAR::Log
45     ##
46     ## Revision 1.8 2002/10/22 09:51:38 cvsmax
47     ## + moved semi-method 'createBackend' to method $site->user->create at User.class.php
48     ##
49     ## Revision 1.7 2002/10/21 18:27:09 cvsjoko
50     ## - manually disabled any form of caching
51     ##
52     ## Revision 1.6 2002/10/17 03:48:47 cvsmax
53     ## + new
54     ## + function _createBackend create new Backend
55     ## + function _create api-create
56     ##
57     ## Revision 1.5 2002/10/16 03:37:54 cvsjoko
58     ## + bugfix: wrong comparison in restriction to save to array
59     ##
60     ## Revision 1.4 2002/10/10 03:04:23 cvsjoko
61     ## + no debug-output
62     ##
63     ## Revision 1.3 2002/10/10 03:03:27 cvsjoko
64     ## + bugfix: save object to cache only if payload from backend isn't empty
65     ##
66     ## Revision 1.2 2002/10/10 02:40:06 cvsjoko
67     ## + new level of data-caching (session and persistant)
68     ## + function _loadState loads data from session
69     ## + function _saveState saves data to session
70     ## + function save api-save (session & backend)
71     ## + function _commit saves data to backend
72     ## + handy utils
73     ## + function _setAttributes
74     ## + function flushState
75     ##
76     ## Revision 1.1 2002/10/09 00:51:39 cvsjoko
77     ## + new
78     ## -------------------------------------------------------------------------------------
79     */
80    
81    
82     class Data_Driver_Proxy {
83    
84     var $objectId;
85     // purpose ...
86     var $meta;
87     var $payload;
88     var $attributes;
89     var $backend;
90    
91     function Data_Driver_Proxy($objectId = "", $options = array() ) {
92     logp(get_class($this) . "->new()", PEAR_LOG_INFO);
93     global $proxy;
94     //print Dumper($objectId, $options);
95     session_register_safe("proxy");
96     if ($options[rpcinfo]) {
97     $this->backend = mkObject('Data::Driver::RPC::Remote', $options[rpcinfo]);
98     }
99     $this->_init($objectId, $options);
100     }
101    
102     function _init($objectId="", $options = array() ) {
103     if ($objectId) {
104     $this->objectId = $objectId;
105     $this->meta = $options;
106    
107     // set default load mechanism
108     if( $this->meta[remote] && ((!$this->meta[oid] && !$this->meta[guid]) || ($this->meta[oid] && $this->meta[guid])) && (!$this->meta[key]) ) {
109     $this->meta[oid] = 1;
110     }
111    
112     $this->load();
113     //$this->_saveProxy();
114     }
115     }
116    
117     function load() {
118     logp(get_class($this) . "->load()", PEAR_LOG_INFO);
119     if (!$this->_loadState()) {
120     if (!$this->_loadProxy()) {
121    
122     // just load object from remote side if its flagged to be a remote one ...
123     if ($this->meta[remote]) {
124     $this->_loadRemote();
125     }
126    
127     }
128     }
129     }
130    
131     function save($data, $type) {
132     $this->_setAttributes($data);
133     $this->_saveState();
134     if ($type == 'commit') {
135     $this->_commit();
136     }
137     if ($type == 'create') {
138     $this->_create();
139     }
140     }
141    
142     function flush() {
143     $this->flushState();
144     $this->flushProxy();
145     }
146    
147     function _commit() {
148     $this->_saveBackend($this->attributes);
149     $this->flushState();
150     $this->flushProxy();
151     }
152    
153    
154     // TODO: make this work
155     /*
156     function _create() {
157     $this->_createBackend($this->attributes);
158     $this->flushState();
159     $this->flushProxy();
160     }
161     */
162    
163     function getAttributes() {
164     if (!$this->meta[decoded]) {
165     $this->_decode();
166     $this->_saveState();
167     }
168     return $this->attributes;
169     }
170    
171     function _setAttributes($data) {
172     $this->attributes = $data;
173     }
174    
175     function flushProxy() {
176     connectdb();
177     $sql = "DELETE FROM f_proxy WHERE oid='$this->objectId'";
178     send_sql($sql);
179     }
180    
181     function flushState() {
182     global $proxy;
183     unset($proxy[$this->objectId]);
184     $this->meta[decoded] = 0;
185     }
186    
187     function _loadState() {
188     global $proxy;
189     //print Dumper($this);
190     logp(get_class($this) . "->_loadState()");
191     if ($this->attributes = $proxy[$this->objectId]) {
192     //print "_loadState:" . dumpVar($this->attributes);
193     $this->meta[decoded] = 1;
194     // TODO: make a parameter from this (0 deactivates session-layer)
195     return 0;
196     }
197     }
198    
199     function _saveState() {
200     global $proxy;
201     logp(get_class($this) . "->_saveState()");
202     $proxy[$this->objectId] = $this->attributes;
203     //print "_saveState: " . dumpVar($this->attributes);
204     // TODO: throw exception-message back to user if operation fails
205     }
206    
207     function _loadProxy() {
208     logp(get_class($this) . "->_loadProxy()");
209     connectdb();
210     $sql = "SELECT payload FROM f_proxy WHERE oid='$this->objectId'";
211     if ($res = send_sql($sql)) {
212     $row = mysql_fetch_array($res, MYSQL_ASSOC);
213     if ($row) {
214     $this->payload = $row[payload];
215     // TODO: make a parameter from this (0 deactivates mysqldb-layer)
216     return 0;
217     }
218     }
219     }
220    
221     // TODO: use PEAR here
222     function _saveProxy() {
223     logp(get_class($this) . "->_saveProxy()");
224     connectdb();
225     if ($this->payload) {
226     //$sql = "INSERT INTO f_proxy SET payload='$this->payload' WHERE oid='$this->objectId'";
227     $sql = "INSERT INTO f_proxy SET oid='$this->objectId', payload='$this->payload'";
228     if (!send_sql($sql)) {
229     $sql = "UPDATE f_proxy SET payload='$this->payload' WHERE oid='$this->objectId'";
230     send_sql($sql);
231     }
232     }
233     }
234    
235     function _loadRemote() {
236     logp(get_class($this) . "->_loadRemote()");
237    
238     //print Dumper($this->meta);
239    
240     // TODO: test backend for reachability first (eventually cache this information and "reset" it by another party)
241    
242     // 1. check backend-handle
243     if (!$this->backend) {
244     logp(get_class($this) . "->_loadRemote: no backend handle, please check argument 'rpcinfo'", PEAR_LOG_CRIT);
245     return;
246     }
247    
248     // determine backend action by metadata
249     // check for guid or oid
250     if ($this->meta[guid]) {
251     if (!$this->objectId) {
252     logp(get_class($this) . "->_loadRemote: argument 'guid' requires valid objectId", PEAR_LOG_WARNING);
253     return;
254     }
255     if (!$this->meta[classname]) {
256     logp(get_class($this) . "->_loadRemote: argument 'guid' requires 'classname'", PEAR_LOG_WARNING);
257     return;
258     }
259     $args = array( guid => $this->objectId, classname => $this->meta[classname] );
260     $result = $this->backend->send('getObjectByGuid', $args );
261    
262     } elseif ($this->meta[oid]) {
263     if (!$this->objectId) {
264     logp(get_class($this) . "->_loadRemote: argument 'oid' requires valid objectId", PEAR_LOG_WARNING);
265     return;
266     }
267     $result = $this->backend->send('getObject', $this->objectId);
268    
269     } elseif ($this->meta[key]) {
270     if (!$this->meta[command]) {
271     logp(get_class($this) . "->_loadRemote: argument 'key' requires 'command'", PEAR_LOG_WARNING);
272     return;
273     }
274     if (!$this->meta[query]) {
275     logp(get_class($this) . "->_loadRemote: argument 'key' requires 'query'", PEAR_LOG_WARNING);
276     return;
277     }
278     $result = $this->backend->send($this->meta[command], $this->meta[query]);
279    
280     }
281    
282    
283    
284     if ($result) {
285     //print "result: " . dumpVar($result) . "<br>";
286     if (count($result) == 0) { return; }
287    
288     // FIXME: this is dangerous!
289     if ($_GET[debug]) {
290     print Dumper($result);
291     }
292    
293     $this->payload = serialize($result);
294     // ----- move this to _encode some times
295    
296     $this->_saveProxy();
297     //print "oid: $this->objectId<br>";
298     $this->flushState();
299     } else {
300     //print "Error in _loadRemote!!!<br>";
301     logp(get_class($this) . "->_loadRemote: error while trying to talk to remote side", PEAR_LOG_CRIT);
302     }
303    
304     }
305    
306     function _saveBackend($result) {
307     logp(get_class($this) . "->_saveBackend()");
308    
309     //$encoder = new TextEncode($result);
310     //$encoder->toUTF8();
311    
312     // check for guid or oid
313     if($this->meta[guid]) {
314     $args = array( 'guid' => $this->objectId, 'classname' => $this->meta[classname], 'data' => $result );
315     $response = $this->backend->send('saveObjectByGuid', $args, array( utf8 => 1) );
316     }
317     if($this->meta[oid]) {
318     $response = $this->backend->send('saveObject', array('oid' => $this->objectId, 'data' => $result), array( utf8 => 1) );
319     }
320     }
321    
322     function _decode() {
323     // fill attributes-hashtable from message-hashtable
324     if (!$this->payload) { return; }
325     //if ($this->attributes = $backend->decodeData($this->payload)) {
326     if ($this->attributes = unserialize($this->payload)) {
327     $this->meta[decoded] = 1;
328     }
329     }
330    
331     function store($struct) {
332     $this->payload = serialize($struct);
333     $this->_saveProxy();
334     }
335    
336     }
337    
338     ?>

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