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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (show annotations)
Thu Feb 13 21:48:09 2003 UTC (21 years, 7 months ago) by joko
Branch: MAIN
Changes since 1.1: +63 -15 lines
+ caching mechanisms more configurable now

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

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