17 function __construct()
19 $this->InputStream
= false;
22 debug("A : ".gettype($this->Buffer
)."<br>");
25 function setBuffer ($Buffer)
27 $this->InputStream
= true;
28 $this->Buffer
= $Buffer;
32 function isReading () { return $this->InputStream
; }
34 function serialUInt8 (&$val)
36 if ($this->isReading())
38 $val = ord($this->Buffer
{$this->Pos++
});
39 debug(sprintf ("read uint8 '%d'<br>\n", $val));
43 debug("B".gettype($this->Buffer
)."<br>");
44 debug(sprintf ("write uint8 Buffer size before = %u<br>\n", strlen($this->Buffer
)));
45 $this->Buffer
= $this->Buffer
. chr($val & 0xFF);
47 debug("C".gettype($this->Buffer
)."<br>");
48 debug(sprintf ("write uint8 '%d' %d<br>\n", $val, $this->Pos
));
49 debug(sprintf ("write uint8 Buffer size after = %u<br>\n", strlen($this->Buffer
)));
53 function serialUInt32 (&$val)
55 if ($this->isReading())
57 $val = ord($this->Buffer
{$this->Pos++
});
58 $val +
= ord($this->Buffer
{$this->Pos++
})*256;
59 $val +
= ord($this->Buffer
{$this->Pos++
})*(double)256*256;
60 $val +
= ord($this->Buffer
{$this->Pos++
})*(double)256*256*256;
61 debug(sprintf ("read uint32 '%d'<br>\n", $val));
66 debug("D".gettype($this->Buffer
)."<br>");
67 $this->Buffer
.= chr($val & 0xFF);
68 $this->Buffer
.= chr(($val>>8) & 0xFF);
69 $this->Buffer
.= chr(($val>>16) & 0xFF);
70 $this->Buffer
.= chr(($val>>24) & 0xFF);
72 debug("E".gettype($this->Buffer
)."<br>");
73 debug(sprintf ("write uint32 '%d' %d<br>\n", $val, $this->Pos
));
77 function serialString (&$val)
79 if ($this->isReading())
81 $this->serialUInt32($size);
82 debug(sprintf ("read string : size = %u<br>\n", $size));
83 $val = substr ($this->Buffer
, $this->Pos
, $size);
84 debug(sprintf ("read string '%s'<br>\n", $val));
85 $this->Pos +
= strlen($val);
89 $valLen = strlen($val);
90 $this->serialUInt32($valLen);
91 $this->Buffer
.= $val;
92 $this->Pos +
= $valLen;
93 debug(sprintf ("write string '%s' %d<br>\n", $val, $this->Pos
));
96 function serialEnum (&$val)
98 if ($this->isReading())
101 $this->serialUInt32($intValue);
102 $val->fromInt((int)$intValue);
103 debug(sprintf ("read enum '%s'<br>\n", $val->toString()));
107 $intValue = $val->toInt();
108 $this->serialUInt32($intValue);
109 debug(sprintf ("write enum '%s' %d<br>\n", $val->toString(), $this->Pos
));
114 class CMessage
extends CMemStream
118 function setName($name)
120 $this->MsgName
= $name;
124 class CCallbackClient
126 var $ConSock = false;
130 function connect($addr, $port, &$res)
134 debug(sprintf("Connect<br>"));
137 $this->ConSock
= fsockopen ($addr, $port, $errno, $errstr, $SockTimeOut);
138 debug("H".gettype($this->ConSock
)."<br>");
142 $res = "Can't connect to the callback server '$addr:$port' ($errno: $errstr)";
148 // set time out on the socket to 2 secondes
149 stream_set_timeout($this->ConSock
, $SockTimeOut);
159 fclose($this->ConSock
);
160 debug(sprintf("Close<br>"));
163 debug(sprintf("Already Closed !<br>"));
166 function sendMessage(&$message)
170 debug(sprintf ("Socket is not valid\n"));
173 debug(sprintf ("sendMessage : message Buffer is '%d'<br>\n", $message->Pos
));
174 debug(sprintf ("sendMessage : message Buffer is '%d'<br>\n", strlen($message->Buffer
)));
175 $hd = new CMemStream
;
176 debug(sprintf("SendMessage number %u<br>", $this->MsgNum
));
177 $hd->serialUInt32 ($this->MsgNum
); // number the packet
179 debug(sprintf("After SendMessage, number %u<br>", $this->MsgNum
));
181 $hd->serialUInt8 ($messageType);
182 $hd->serialString ($message->MsgName
);
184 debug(sprintf ("sendMessage : header size is '%d'<br>\n", $hd->Pos
));
186 // $sb .= $message->Buffer;
188 $size = $hd->Pos +
$message->Pos
;
189 $Buffer = (string) chr(($size>>24)&0xFF);
190 $Buffer .= chr(($size>>16)&0xFF);
191 $Buffer .= chr(($size>>8)&0xFF);
192 $Buffer .= chr($size&0xFF);
193 debug( "E".gettype($hd->Buffer
)."<br>");
194 debug("F".gettype($message->Buffer
)."<br>");
195 $Buffer .= (string) $hd->Buffer
;
196 $Buffer .= (string) $message->Buffer
;
198 debug("G".gettype($this->ConSock
)."<br>");
200 if (!fwrite ($this->ConSock
, $Buffer))
202 debug(sprintf ("Error writing to socket\n"));
205 debug(sprintf ("sent packet size '%d' (written size = %d) <br>\n", strlen($Buffer), $size));
206 fflush ($this->ConSock
);
211 function waitMessage()
215 debug(sprintf ("Socket is not valid\n"));
221 $val = fread ($this->ConSock
, 1);
222 $info = stream_get_meta_data($this->ConSock
);
223 if ($info['timed_out'])
225 debug('Connection timed out!');
228 $size = ord($val) << 24;
229 $val = fread ($this->ConSock
, 1);
230 $info = stream_get_meta_data($this->ConSock
);
231 if ($info['timed_out'])
233 debug('Connection timed out!');
236 $size = ord($val) << 16;
237 $val = fread ($this->ConSock
, 1);
238 $info = stream_get_meta_data($this->ConSock
);
239 if ($info['timed_out'])
241 debug('Connection timed out!');
244 $size +
= ord($val) << 8;
245 $val = fread ($this->ConSock
, 1);
246 $info = stream_get_meta_data($this->ConSock
);
247 if ($info['timed_out'])
249 debug('Connection timed out!');
253 debug(sprintf ("receive packet size '%d'<br>\n", $size));
254 $fake = fread ($this->ConSock
, 5);
255 $info = stream_get_meta_data($this->ConSock
);
256 if ($info['timed_out'])
258 debug('Connection timed out!');
261 $size -= 5; // remove the fake
264 while ($size > 0 && strlen($Buffer) != $size)
266 $Buffer .= fread ($this->ConSock
, $size - strlen($Buffer));
267 $info = stream_get_meta_data($this->ConSock
);
268 if ($info['timed_out'])
270 debug('Connection timed out!');
274 $msgin = new CMemStream
;
275 $msgin->setBuffer ($Buffer);
278 $msgin->serialString($name);
280 debug(sprintf("Message name = '%s'<BR>", $name));
281 $message = new CMessage
;
282 $message->setBuffer(substr($msgin->Buffer
, $msgin->Pos
));
283 $message->setName($name);
285 debug(sprintf("In message name = '%s'<br>", $message->MsgName
));
292 // class CSessionManagerProxy
294 // function createSession($userId, $sessionType, $callbackClient)
296 // debug(sprintf("Creating session for user %u, type %s<BR>", $userId, $sessionType));
297 // $msg = new CMessage;
298 // $msg->setName("CSS");
299 // $msg->serialUInt32($userId);
300 // $msg->serialString($sessionType);
302 // $callbackClient->sendMessage($msg);
306 // class CSessionManagerClientSkel
308 // function waitCallback($callbackClient)
310 // $message = $callbackClient->waitMessage();
312 // debug(sprintf("Received message '%s'<BR>", $message->MsgName));
314 // switch($message->MsgName)
317 // debug(sprintf("Create session result<BR>"));
318 // $this->createSessionResult_skel($message);
322 // debug(sprintf("Create scenario result<BR>"));
323 // $this->createScenarioResult_skel($message);
328 // function createSessionResult_skel($message)
334 // $message->serialUInt32($userId);
335 // $message->serialUInt32($sessionId);
336 // $message->serialUInt8($result);
338 // createSessionResult($userId, $sessionId, $result);
342 // printf("creating callback client...<BR>");
344 // $cb = new CCallbackClient;
346 // $cb->connect("192.168.0.1", "8060", $ret);
348 // $smp = new CSessionManagerProxy;
350 // printf("creating a new sessions...<BR>");
351 // $smp->createSession(10, "st_edit", $cb);
353 // $smcs = new CSessionManagerClientSkel;
354 // $smcs->waitCallback($cb);
357 // function createSessionResult($userId, $sessionId, $result)
359 // echo "The session result for user $userId is the session $sessionId with a result of $result\n";
363 // This function connect to the AS.
364 // If true, $res contains the url to connect.
365 // If false, $res contains the reason why it s not okay.
367 // function connectToAS(&$fp, &$res)
369 // global $ASHost, $ASPort;
371 // $sid = session_id();
372 // $result = sqlquery("SELECT socket_id FROM resident_socket");
373 // if (!$result || sqlnumrows($result) == 0)
375 // $fp = pfsockopen ($ASHost, $ASPort, $errno, $errstr, 30);
376 // echo "opened resident socket '$fp'\n";
378 // $result = sqlquery("SELECT socket_id FROM resident_socket WHERE socket_id='$fp'");
379 // if ($result && sqlnumrows($result)>0)
380 // sqlquery("DELETE FROM resident_socket WHERE socket_id='$fp'");
382 // sqlquery("INSERT INTO resident_socket SET socket_id='$fp', session_id='$sid', last_access=NOW()");
386 // $result = sqlfetch($result);
387 // $fp = $result["socket_id"];
390 // // remove too old sockets
391 // sqlquery("SELECT socket_id FROM resident_socket WHERE NOW()-last_access > 1800");
392 // while ($result && ($arr=sqlfetch($result)))
394 // fclose((int)($arr["socket_id"]));
395 // sqlquery("DELETE FROM resident_socket WHERE socket_id='".$arr["socket_id"]."'");
398 // // update current socket last access
399 // sqlquery("UPDATE resident_socket SET last_access=NOW() WHERE socket_id='$fp' AND session_id='$sid'");
402 // // connect to the login service that must be $ASHost:$ASPort
403 // $fp = fsockopen ($ASHost, $ASPort, $errno, $errstr, 30);
406 // $res = "Can't connect to the admin service '$ASHost:$ASPort' ($errno: $errstr)";
415 // function disconnectFromAS(&$fp)
418 // $result = sqlquery("SELECT socket_id FROM resident_socket WHERE socket_id='$fp'");
419 // if (!$result || sqlnumrows($socket)==0)
425 // function sendMessage ($fp, $msgout)
427 // $size = $msgout->Pos;
428 // $Buffer = chr(($size>>24)&0xFF);
429 // $Buffer .= chr(($size>>16)&0xFF);
430 // $Buffer .= chr(($size>>8)&0xFF);
431 // $Buffer .= chr($size&0xFF);
432 // $Buffer .= $msgout->Buffer;
434 // fwrite ($fp, $Buffer);
436 // //printf ("sent packet size '%d'<br>", strlen($Buffer));
441 // function waitMessage ($fp, &$msgin)
443 // //echo "waiting a message";
445 // $val = fread ($fp, 1);
446 // $size = ord($val) << 24;
447 // $val = fread ($fp, 1);
448 // $size = ord($val) << 16;
449 // $val = fread ($fp, 1);
450 // $size += ord($val) << 8;
451 // $val = fread ($fp, 1);
452 // $size += ord($val);
453 // //printf ("receive packet size '%d'<br>", $size);
454 // $fake = fread ($fp, 4);
455 // $size -= 4; // remove the fake
457 // $Buffer = fread ($fp, $size);
458 // $msgin = new CMemStream;
459 // $msgin->setBuffer ($Buffer);
462 // function logNelQuery($query)
466 // $f = fopen("./nel_queries.log", "a");
467 // fwrite($f, date("Y/m/d H:i:s")." ".sprintf("%-16s", $admlogin)." $query\n");
471 // logUser($uid, "QUERY=".$query);
474 // function nel_query($rawvarpath, &$result)
476 // global $nel_queries;
478 // $nel_queries[] = $rawvarpath;
480 // //echo "rawvarpath=$rawvarpath<br>\n";
482 // //logNelQuery($rawvarpath);
484 // connectToAS($fp, $result);
485 // if(strlen($result) != 0)
488 // // send the message that say that we want to add a user
489 // $msgout = new CMemStream;
491 // $msgout->serialuint32 ($fake); // fake used to number the packet
493 // $msgout->serialuint8 ($messageType);
494 // $msgout->serialstring ($rawvarpath);
496 // sendMessage ($fp, $msgout);
498 // waitMessage ($fp, $msgin);
500 // $msgin->serialstring($result);
502 // if(strlen($result) == 0)
512 // //printf("receive response '$result'<br>\n");
514 // disconnectFromAS(&$fp);
515 // //echo "sent OK.<br><br>\n";