/[cvs]/nfo/php/libs/org.netfrag.flib/Application/RPC/ProxyObject.php
ViewVC logotype

Annotation of /nfo/php/libs/org.netfrag.flib/Application/RPC/ProxyObject.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (hide annotations)
Mon Feb 3 03:31:38 2003 UTC (21 years, 7 months ago) by jonen
Branch: MAIN
Changes since 1.6: +4 -4 lines
- moved '$encoder->toISO()' to 'Remote.php'

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

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