1 |
joko |
1.1 |
<? |
2 |
|
|
/* |
3 |
|
|
## -------------------------------------------------------------------------- |
4 |
joko |
1.5 |
## $Id: Remote.php,v 1.4 2003/02/20 21:42:43 joko Exp $ |
5 |
joko |
1.1 |
## -------------------------------------------------------------------------- |
6 |
|
|
## $Log: Remote.php,v $ |
7 |
joko |
1.5 |
## Revision 1.4 2003/02/20 21:42:43 joko |
8 |
|
|
## + fix: always converting from utf8 into latin here (for now) |
9 |
|
|
## |
10 |
joko |
1.4 |
## Revision 1.3 2003/02/13 21:51:29 joko |
11 |
|
|
## + now can remember its connection-state |
12 |
|
|
## + sub ping |
13 |
|
|
## |
14 |
joko |
1.3 |
## Revision 1.2 2003/02/13 00:42:44 joko |
15 |
|
|
## +- renamed modules |
16 |
|
|
## |
17 |
joko |
1.2 |
## Revision 1.1 2003/02/09 17:25:38 joko |
18 |
|
|
## + refactored from flib/Application/RPC/Remote.php |
19 |
|
|
## |
20 |
joko |
1.1 |
## Revision 1.6 2003/02/03 03:37:45 jonen |
21 |
|
|
## - removed unused argument '$decode' at function '_call()' |
22 |
|
|
## + added argument '$options=array()' at function '_call()' |
23 |
|
|
## which will be passed to function '&decodeData()' for e.g. utf8 decoding |
24 |
|
|
## + added '$encoder->toISO()' at '&decodeData()', (re)moved from 'ProxyObject.php' |
25 |
|
|
## |
26 |
|
|
## Revision 1.5 2002/12/22 13:24:09 jonen |
27 |
|
|
## + added utf8 encoding at 'send()' toggled by option |
28 |
|
|
## |
29 |
|
|
## Revision 1.4 2002/12/19 02:02:25 jonen |
30 |
|
|
## + minor changes |
31 |
|
|
## |
32 |
|
|
## Revision 1.3 2002/12/19 01:59:37 jonen |
33 |
|
|
## + minor changes: coment debug prints |
34 |
|
|
## |
35 |
|
|
## Revision 1.2 2002/12/05 21:45:31 joko |
36 |
|
|
## + debugging |
37 |
|
|
## |
38 |
|
|
## Revision 1.1 2002/12/01 17:23:58 joko |
39 |
|
|
## + initial check-in |
40 |
|
|
## |
41 |
|
|
## Revision 1.3 2002/12/01 06:22:57 cvsjoko |
42 |
|
|
## + minor update: now can use config defaults or given args |
43 |
|
|
## |
44 |
|
|
## Revision 1.2 2002/10/29 19:14:45 cvsjoko |
45 |
|
|
## - bugfix: dont' do utf8-encoding here |
46 |
|
|
## |
47 |
|
|
## Revision 1.1 2002/10/09 00:51:39 cvsjoko |
48 |
|
|
## + new |
49 |
|
|
## |
50 |
|
|
## |
51 |
|
|
## ------------------------------------------------------------------------- |
52 |
|
|
*/ |
53 |
|
|
|
54 |
|
|
|
55 |
|
|
/* |
56 |
|
|
|
57 |
|
|
TODO: |
58 |
|
|
o SOAP? |
59 |
|
|
|
60 |
|
|
|
61 |
|
|
*/ |
62 |
|
|
|
63 |
|
|
|
64 |
|
|
class Data_Driver_RPC_Remote extends DesignPattern_Logger { |
65 |
|
|
|
66 |
|
|
var $configured; |
67 |
joko |
1.3 |
var $meta; |
68 |
joko |
1.1 |
|
69 |
|
|
function Data_Driver_RPC_Remote($args = array()) { |
70 |
joko |
1.3 |
|
71 |
joko |
1.1 |
parent::constructor(); |
72 |
joko |
1.3 |
|
73 |
joko |
1.1 |
//print Dumper($this, $args); |
74 |
joko |
1.3 |
|
75 |
|
|
global $Data_Driver_RPC_Remote_meta; |
76 |
|
|
session_register_safe("Data_Driver_RPC_Remote_meta"); |
77 |
|
|
$this->meta = $Data_Driver_RPC_Remote_meta; |
78 |
|
|
|
79 |
|
|
if (!isset($this->meta[connected]) || $args[connect]) { |
80 |
|
|
$this->meta[connected] = 1; |
81 |
|
|
} |
82 |
|
|
$this->_save_meta(); |
83 |
|
|
|
84 |
joko |
1.1 |
if ($args['Host']) { $this->host = $args['Host']; } |
85 |
|
|
if ($args['Port']) { $this->port = $args['Port']; } |
86 |
joko |
1.3 |
|
87 |
joko |
1.1 |
// check if host is valid |
88 |
|
|
if (!$this->host) { |
89 |
|
|
$this->_raiseException( "->constructor: attribute 'host' is empty, please check your settings"); |
90 |
|
|
return; |
91 |
|
|
} |
92 |
|
|
|
93 |
|
|
if (!$this->port) { |
94 |
|
|
$this->_raiseException( "->constructor: attribute 'port' is empty, please check your settings"); |
95 |
|
|
return; |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
$this->configured = 1; |
99 |
|
|
|
100 |
|
|
} |
101 |
|
|
|
102 |
joko |
1.3 |
function _save_meta() { |
103 |
|
|
global $Data_Driver_RPC_Remote_meta; |
104 |
|
|
$Data_Driver_RPC_Remote_meta = $this->meta; |
105 |
|
|
} |
106 |
|
|
|
107 |
joko |
1.1 |
function send($command, $data = "", $options = array()) { |
108 |
joko |
1.3 |
|
109 |
|
|
$this->log(get_class($this) . "->send: " . $command, PEAR_LOG_DEBUG); |
110 |
|
|
|
111 |
|
|
if (!$this->isConnected()) { |
112 |
|
|
$this->_raiseException( "->send: not connected!"); |
113 |
|
|
return; |
114 |
|
|
} |
115 |
|
|
|
116 |
joko |
1.1 |
// do 'encode' here and ... |
117 |
|
|
if ($options[utf8]) { |
118 |
joko |
1.2 |
$encoder = new Data_Encode($data); |
119 |
joko |
1.1 |
$encoder->toUTF8(); |
120 |
|
|
} |
121 |
|
|
// call '_call' with 'decode' |
122 |
|
|
return $this->_call($command, $data, $options); |
123 |
|
|
} |
124 |
|
|
|
125 |
|
|
function _call($command, $data = "", $options = array() ) { |
126 |
|
|
|
127 |
|
|
if (!$this->configured) { |
128 |
|
|
$this->_raiseException( "->_call: class not configured properly"); |
129 |
|
|
return; |
130 |
|
|
} |
131 |
|
|
|
132 |
joko |
1.3 |
// populate options list - mostly for debugging purposes |
133 |
|
|
$options_list = array(); |
134 |
|
|
foreach ($options as $key => $value) { |
135 |
|
|
array_push($options_list, "$key=$value"); |
136 |
|
|
} |
137 |
|
|
|
138 |
|
|
$data_debug = $data; |
139 |
|
|
if (is_array($data_debug)) { $data_debug = join(", ", $data_debug); } |
140 |
|
|
$options_debug = join(", ", $options_list); |
141 |
|
|
$this->log(get_class($this) . ": " . $command . "(" . $data_debug . ") [" . $options_debug . "]", PEAR_LOG_DEBUG); |
142 |
|
|
|
143 |
joko |
1.2 |
// trace |
144 |
|
|
//print "call: $command<hr>"; |
145 |
|
|
//print Dumper($data); |
146 |
|
|
//print Dumper($this); |
147 |
joko |
1.1 |
|
148 |
|
|
// data |
149 |
|
|
$data_enc = XML_RPC_encode($data); |
150 |
|
|
|
151 |
|
|
// message - request |
152 |
|
|
$msg = new XML_RPC_Message($command); |
153 |
|
|
$msg->addParam($data_enc); |
154 |
|
|
//print htmlentities($msg->serialize()); |
155 |
|
|
// remote procedure call |
156 |
|
|
$rpc = new XML_RPC_Client("/", $this->host, $this->port); |
157 |
|
|
$rpc->setDebug(0); |
158 |
|
|
if ( !$msg_response = $rpc->send($msg) ) { |
159 |
|
|
// TODO: redirect this error elsewhere! |
160 |
|
|
//print "RPC-error!<br>"; |
161 |
|
|
$this->_raiseException( "->_call: no response"); |
162 |
|
|
return; |
163 |
|
|
} |
164 |
|
|
// message - response |
165 |
|
|
$response_enc = $msg_response->value(); |
166 |
|
|
|
167 |
|
|
// TODO: what's this? prematurely returning here should not be considered "stable".... |
168 |
|
|
return $this->decodeData($response_enc, $options); |
169 |
|
|
} |
170 |
|
|
|
171 |
|
|
function &decodeData(&$payload, $options = array() ) { |
172 |
|
|
//if (!is_object($payload)) { return; } |
173 |
|
|
if ($payload) { |
174 |
|
|
// data |
175 |
|
|
$data = XML_RPC_decode($payload); |
176 |
|
|
//print "data: " . dumpVar($response); |
177 |
|
|
|
178 |
|
|
// decode UTF8 to ISO if wanted |
179 |
joko |
1.4 |
//if ($options[to_latin]) { |
180 |
joko |
1.2 |
$encoder = new Data_Encode($data); |
181 |
joko |
1.1 |
$encoder->toISO(); |
182 |
joko |
1.4 |
//} |
183 |
joko |
1.3 |
|
184 |
|
|
$this->meta[connected] = 1; |
185 |
|
|
$this->_save_meta(); |
186 |
joko |
1.1 |
|
187 |
|
|
return $data; |
188 |
|
|
} else { |
189 |
|
|
//print "ERROR!<br>"; |
190 |
|
|
return 0; |
191 |
|
|
} |
192 |
|
|
} |
193 |
|
|
|
194 |
|
|
function _raiseException($message) { |
195 |
joko |
1.3 |
$this->meta[connected] = 0; |
196 |
|
|
$this->_save_meta(); |
197 |
|
|
$this->log(get_class($this) . $message, PEAR_LOG_ERR); |
198 |
|
|
} |
199 |
|
|
|
200 |
|
|
function isConnected() { |
201 |
|
|
return $this->meta[connected]; |
202 |
|
|
} |
203 |
|
|
|
204 |
|
|
function ping() { |
205 |
|
|
$this->_call('ping'); |
206 |
joko |
1.1 |
} |
207 |
|
|
|
208 |
|
|
} |
209 |
|
|
|
210 |
|
|
?> |