/[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.2 - (show annotations)
Thu Dec 5 21:44:09 2002 UTC (21 years, 9 months ago) by joko
Branch: MAIN
Changes since 1.1: +6 -3 lines
+ debugging

1 <?
2 ## -------------------------------------------------------------------------------------
3 ## $Id: ProxyObject.php,v 1.1 2002/12/01 17:23:58 joko Exp $
4 ## -------------------------------------------------------------------------------------
5 ## $Log: ProxyObject.php,v $
6 ## Revision 1.1 2002/12/01 17:23:58 joko
7 ## + initial check-in
8 ##
9 ## Revision 1.11 2002/11/12 06:05:58 cvsjoko
10 ## + renamed class: Text_Encode -> TextEncode
11 ##
12 ## Revision 1.10 2002/10/29 19:15:33 cvsjoko
13 ## - moved utf8/iso-conversion to lib/utils/Text_Encode.php
14 ##
15 ## Revision 1.9 2002/10/26 12:32:36 cvsjoko
16 ## - removed debugging via "print"
17 ## + added generic logging via PEAR::Log
18 ##
19 ## Revision 1.8 2002/10/22 09:51:38 cvsmax
20 ## + moved semi-method 'createBackend' to method $site->user->create at User.class.php
21 ##
22 ## Revision 1.7 2002/10/21 18:27:09 cvsjoko
23 ## - manually disabled any form of caching
24 ##
25 ## Revision 1.6 2002/10/17 03:48:47 cvsmax
26 ## + new
27 ## + function _createBackend create new Backend
28 ## + function _create api-create
29 ##
30 ## Revision 1.5 2002/10/16 03:37:54 cvsjoko
31 ## + bugfix: wrong comparison in restriction to save to array
32 ##
33 ## Revision 1.4 2002/10/10 03:04:23 cvsjoko
34 ## + no debug-output
35 ##
36 ## Revision 1.3 2002/10/10 03:03:27 cvsjoko
37 ## + bugfix: save object to cache only if payload from backend isn't empty
38 ##
39 ## Revision 1.2 2002/10/10 02:40:06 cvsjoko
40 ## + new level of data-caching (session and persistant)
41 ## + function _loadState loads data from session
42 ## + function _saveState saves data to session
43 ## + function save api-save (session & backend)
44 ## + function _commit saves data to backend
45 ## + handy utils
46 ## + function _setAttributes
47 ## + function flushState
48 ##
49 ## Revision 1.1 2002/10/09 00:51:39 cvsjoko
50 ## + new
51 ## -------------------------------------------------------------------------------------
52
53 class ProxyObject {
54
55 var $objectId;
56 // purpose ...
57 var $meta;
58 var $payload;
59 var $attributes;
60 var $backend;
61
62 function ProxyObject($objectId = "") {
63 session_register_safe("proxy");
64 $this->backend = new Remote;
65 $this->_init($objectId);
66 }
67
68 function _init($objectId="") {
69 if ($objectId) {
70 $this->objectId = $objectId;
71 $this->load();
72 //$this->_saveCache();
73 }
74 }
75
76 function load() {
77 if (!$this->_loadState()) {
78 if (!$this->_loadCache()) {
79 $this->_loadBackend();
80 }
81 }
82 }
83
84 function save($data, $type) {
85 $this->_setAttributes($data);
86 $this->_saveState();
87 if ($type == 'commit') {
88 $this->_commit();
89 }
90 if ($type == 'create') {
91 $this->_create();
92 }
93 }
94
95 function flush() {
96 $this->flushState();
97 $this->flushCache();
98 }
99
100 function _commit() {
101 $this->_saveBackend($this->attributes);
102 $this->flushState();
103 $this->flushCache();
104 }
105
106
107 // TODO: make this work
108 /*
109 function _create() {
110 $this->_createBackend($this->attributes);
111 $this->flushState();
112 $this->flushCache();
113 }
114 */
115
116 function getAttributes() {
117 if (!$this->meta[decoded]) {
118 $this->_decode();
119 $this->_saveState();
120 }
121 return $this->attributes;
122 }
123
124 function _setAttributes($data) {
125 $this->attributes = $data;
126 }
127
128 function flushCache() {
129 connectdb();
130 $sql = "DELETE FROM f_proxy WHERE oid='$this->objectId'";
131 send_sql($sql);
132 }
133
134 function flushState() {
135 global $proxy;
136 unset($proxy[$this->objectId]);
137 }
138
139 function _loadState() {
140 global $proxy;
141 logp(get_class($this) . "->_loadState()", LOG_DEBUG);
142 if ($this->attributes = $proxy[$this->objectId]) {
143 //print "_loadState:" . dumpVar($this->attributes);
144 $this->meta[decoded] = 1;
145 // TODO: make a parameter from this (0 deactivates session-layer)
146 return 0;
147 }
148 }
149
150 function _saveState() {
151 global $proxy;
152 logp(get_class($this) . "->_saveState()", LOG_DEBUG);
153 $proxy[$this->objectId] = $this->attributes;
154 //print "_saveState: " . dumpVar($this->attributes);
155 // TODO: throw exception-message back to user if operation fails
156 }
157
158 function _loadCache() {
159 logp(get_class($this) . "->_loadCache()", LOG_DEBUG);
160 connectdb();
161 $sql = "SELECT payload FROM f_proxy WHERE oid='$this->objectId'";
162 if ($res = send_sql($sql)) {
163 $row = mysql_fetch_array($res, MYSQL_ASSOC);
164 if ($row) {
165 $this->payload = $row[payload];
166 // TODO: make a parameter from this (0 deactivates mysqldb-layer)
167 return 0;
168 }
169 }
170 }
171
172 function _saveCache() {
173 logp(get_class($this) . "->_saveCache()", LOG_DEBUG);
174 connectdb();
175 if ($this->payload) {
176 //$sql = "INSERT INTO f_proxy SET payload='$this->payload' WHERE oid='$this->objectId'";
177 $sql = "INSERT INTO f_proxy SET oid='$this->objectId', payload='$this->payload'";
178 if (!send_sql($sql)) {
179 $sql = "UPDATE f_proxy SET payload='$this->payload' WHERE oid='$this->objectId'";
180 send_sql($sql);
181 }
182 }
183 }
184
185 function _loadBackend() {
186 logp(get_class($this) . "->_loadBackend()", LOG_DEBUG);
187 // TODO: test backend for reachability first (eventually cache this information and "reset" it by another party)
188 if ($result = $this->backend->call('getObject', $this->objectId)) {
189 //print "result: " . dumpVar($result) . "<br>";
190 if (count($result) == 0) { return; }
191 $encoder = new TextEncode($result);
192 $encoder->toISO();
193 if ($_GET[debug]) {
194 print dumpVar($result);
195 }
196 $this->payload = serialize($result);
197 $this->_saveCache();
198 }
199 }
200
201 function _saveBackend($result) {
202 logp(get_class($this) . "->_saveBackend()", LOG_DEBUG);
203 $encoder = new TextEncode($result);
204 $encoder->toUTF8();
205 $response = $this->backend->send('saveObject', array('oid' => $this->objectId, 'data' => $result) );
206 }
207
208 function _decode() {
209 // fill attributes-hashtable from message-hashtable
210 if (!$this->payload) { return; }
211 //if ($this->attributes = $backend->decodeData($this->payload)) {
212 if ($this->attributes = unserialize($this->payload)) {
213 $this->meta[decoded] = 1;
214 }
215 }
216
217
218 }
219
220 ?>

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