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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.8 - (show annotations)
Sun Feb 9 17:35:46 2003 UTC (21 years, 7 months ago) by joko
Branch: MAIN
CVS Tags: HEAD
Changes since 1.7: +4 -1 lines
FILE REMOVED
- moved to org.netfrag.glib/Data/Driver/

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

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