10 // $file = "logs/debug.txt";
11 // $str = file_get_contents($file);
12 // $str .= $text."\n";
13 // file_put_contents($file, $str);
22 function __construct()
24 $this->InputStream
= false;
27 debug("A : ".gettype($this->Buffer
)."<br>");
30 function setBuffer ($Buffer)
32 $this->InputStream
= true;
33 $this->Buffer
= $Buffer;
37 function isReading () { return $this->InputStream
; }
39 function serialUInt8 (&$val)
41 if ($this->isReading())
43 $val = ord($this->Buffer
{$this->Pos++
});
44 debug(sprintf ("read uint8 '%d'<br>\n", $val));
48 debug("B".gettype($this->Buffer
)."<br>");
49 debug(sprintf ("write uint8 Buffer size before = %u<br>\n", strlen($this->Buffer
)));
50 $this->Buffer
= $this->Buffer
. chr($val & 0xFF);
52 debug("C".gettype($this->Buffer
)."<br>");
53 debug(sprintf ("write uint8 '%d' %d<br>\n", $val, $this->Pos
));
54 debug(sprintf ("write uint8 Buffer size after = %u<br>\n", strlen($this->Buffer
)));
58 function serialUInt32 (&$val)
60 if ($this->isReading())
62 $val = ord($this->Buffer
{$this->Pos++
});
63 $val +
= ord($this->Buffer
{$this->Pos++
})*256;
64 $val +
= ord($this->Buffer
{$this->Pos++
})*(double)256*256;
65 $val +
= ord($this->Buffer
{$this->Pos++
})*(double)256*256*256;
66 debug(sprintf ("read uint32 '%d'<br>\n", $val));
71 debug("D".gettype($this->Buffer
)."<br>");
72 $this->Buffer
.= chr($val & 0xFF);
73 $this->Buffer
.= chr(($val>>8) & 0xFF);
74 $this->Buffer
.= chr(($val>>16) & 0xFF);
75 $this->Buffer
.= chr(($val>>24) & 0xFF);
77 debug("E".gettype($this->Buffer
)."<br>");
78 debug(sprintf ("write uint32 '%d' %d<br>\n", $val, $this->Pos
));
82 function serialString (&$val)
84 if ($this->isReading())
86 $this->serialUInt32($size);
87 debug(sprintf ("read string : size = %u<br>\n", $size));
88 $val = substr ($this->Buffer
, $this->Pos
, $size);
89 debug(sprintf ("read string '%s'<br>\n", $val));
90 $this->Pos +
= strlen($val);
94 $valLen = strlen($val);
95 $this->serialUInt32($valLen);
96 $this->Buffer
.= $val;
97 $this->Pos +
= $valLen;
98 debug(sprintf ("write string '%s' %d<br>\n", $val, $this->Pos
));
103 class CMessage
extends CMemStream
107 function setName($name)
109 $this->MsgName
= $name;
113 class CCallbackClient
115 var $ConSock = false;
119 function connect($addr, $port, &$res)
123 debug(sprintf("Connect<br>"));
126 $this->ConSock
= fsockopen ($addr, $port, $errno, $errstr, $SockTimeOut);
127 debug("H".gettype($this->ConSock
)."<br>");
131 $res = "Can't connect to the callback server '$addr:$port' ($errno: $errstr)";
137 // set time out on the socket to 2 secondes
138 stream_set_timeout($this->ConSock
, $SockTimeOut);
148 fclose($this->ConSock
);
149 debug(sprintf("Close<br>"));
152 debug(sprintf("Already Closed !<br>"));
155 function sendMessage(&$message)
159 debug(sprintf ("Socket is not valid\n"));
162 debug(sprintf ("sendMessage : message Buffer is '%d'<br>\n", $message->Pos
));
163 debug(sprintf ("sendMessage : message Buffer is '%d'<br>\n", strlen($message->Buffer
)));
164 $hd = new CMemStream
;
165 debug(sprintf("SendMessage number %u<br>", $this->MsgNum
));
166 $hd->serialUInt32 ($this->MsgNum
); // number the packet
168 debug(sprintf("After SendMessage, number %u<br>", $this->MsgNum
));
170 $hd->serialUInt8 ($messageType);
171 $hd->serialString ($message->MsgName
);
173 debug(sprintf ("sendMessage : header size is '%d'<br>\n", $hd->Pos
));
175 // $sb .= $message->Buffer;
177 $size = $hd->Pos +
$message->Pos
;
178 $Buffer = (string) chr(($size>>24)&0xFF);
179 $Buffer .= chr(($size>>16)&0xFF);
180 $Buffer .= chr(($size>>8)&0xFF);
181 $Buffer .= chr($size&0xFF);
182 debug( "E".gettype($hd->Buffer
)."<br>");
183 debug("F".gettype($message->Buffer
)."<br>");
184 $Buffer .= (string) $hd->Buffer
;
185 $Buffer .= (string) $message->Buffer
;
187 debug("G".gettype($this->ConSock
)."<br>");
189 if (!fwrite ($this->ConSock
, $Buffer))
191 debug(sprintf ("Error writing to socket\n"));
194 debug(sprintf ("sent packet size '%d' (written size = %d) <br>\n", strlen($Buffer), $size));
195 fflush ($this->ConSock
);
200 function waitMessage()
204 debug(sprintf ("Socket is not valid\n"));
210 $val = fread ($this->ConSock
, 1);
211 $info = stream_get_meta_data($this->ConSock
);
212 if ($info['timed_out'])
214 debug('Connection timed out!');
217 $size = ord($val) << 24;
218 $val = fread ($this->ConSock
, 1);
219 $info = stream_get_meta_data($this->ConSock
);
220 if ($info['timed_out'])
222 debug('Connection timed out!');
225 $size = ord($val) << 16;
226 $val = fread ($this->ConSock
, 1);
227 $info = stream_get_meta_data($this->ConSock
);
228 if ($info['timed_out'])
230 debug('Connection timed out!');
233 $size +
= ord($val) << 8;
234 $val = fread ($this->ConSock
, 1);
235 $info = stream_get_meta_data($this->ConSock
);
236 if ($info['timed_out'])
238 debug('Connection timed out!');
242 debug(sprintf ("receive packet size '%d'<br>\n", $size));
243 $fake = fread ($this->ConSock
, 5);
244 $info = stream_get_meta_data($this->ConSock
);
245 if ($info['timed_out'])
247 debug('Connection timed out!');
250 $size -= 5; // remove the fake
253 while ($size > 0 && strlen($Buffer) != $size)
255 $Buffer .= fread ($this->ConSock
, $size - strlen($Buffer));
256 $info = stream_get_meta_data($this->ConSock
);
257 if ($info['timed_out'])
259 debug('Connection timed out!');
263 $msgin = new CMemStream
;
264 $msgin->setBuffer ($Buffer);
267 $msgin->serialString($name);
269 debug(sprintf("Message name = '%s'<BR>", $name));
270 $message = new CMessage
;
271 $message->setBuffer(substr($msgin->Buffer
, $msgin->Pos
));
272 $message->setName($name);
274 debug(sprintf("In message name = '%s'<br>", $message->MsgName
));
281 // class CSessionManagerProxy
283 // function createSession($userId, $sessionType, $callbackClient)
285 // debug(sprintf("Creating session for user %u, type %s<BR>", $userId, $sessionType));
286 // $msg = new CMessage;
287 // $msg->setName("CSS");
288 // $msg->serialUInt32($userId);
289 // $msg->serialString($sessionType);
291 // $callbackClient->sendMessage($msg);
295 // class CSessionManagerClientSkel
297 // function waitCallback($callbackClient)
299 // $message = $callbackClient->waitMessage();
301 // debug(sprintf("Received message '%s'<BR>", $message->MsgName));
303 // switch($message->MsgName)
306 // debug(sprintf("Create session result<BR>"));
307 // $this->createSessionResult_skel($message);
311 // debug(sprintf("Create scenario result<BR>"));
312 // $this->createScenarioResult_skel($message);
317 // function createSessionResult_skel($message)
323 // $message->serialUInt32($userId);
324 // $message->serialUInt32($sessionId);
325 // $message->serialUInt8($result);
327 // createSessionResult($userId, $sessionId, $result);
331 // printf("creating callback client...<BR>");
333 // $cb = new CCallbackClient;
335 // $cb->connect("192.168.0.1", "8060", $ret);
337 // $smp = new CSessionManagerProxy;
339 // printf("creating a new sessions...<BR>");
340 // $smp->createSession(10, "st_edit", $cb);
342 // $smcs = new CSessionManagerClientSkel;
343 // $smcs->waitCallback($cb);
346 // function createSessionResult($userId, $sessionId, $result)
348 // echo "The session result for user $userId is the session $sessionId with a result of $result\n";
352 // This function connect to the AS.
353 // If true, $res contains the url to connect.
354 // If false, $res contains the reason why it s not okay.
356 // function connectToAS(&$fp, &$res)
358 // global $ASHost, $ASPort;
360 // $sid = session_id();
361 // $result = sqlquery("SELECT socket_id FROM resident_socket");
362 // if (!$result || sqlnumrows($result) == 0)
364 // $fp = pfsockopen ($ASHost, $ASPort, $errno, $errstr, 30);
365 // echo "opened resident socket '$fp'\n";
367 // $result = sqlquery("SELECT socket_id FROM resident_socket WHERE socket_id='$fp'");
368 // if ($result && sqlnumrows($result)>0)
369 // sqlquery("DELETE FROM resident_socket WHERE socket_id='$fp'");
371 // sqlquery("INSERT INTO resident_socket SET socket_id='$fp', session_id='$sid', last_access=NOW()");
375 // $result = sqlfetch($result);
376 // $fp = $result["socket_id"];
379 // // remove too old sockets
380 // sqlquery("SELECT socket_id FROM resident_socket WHERE NOW()-last_access > 1800");
381 // while ($result && ($arr=sqlfetch($result)))
383 // fclose((int)($arr["socket_id"]));
384 // sqlquery("DELETE FROM resident_socket WHERE socket_id='".$arr["socket_id"]."'");
387 // // update current socket last access
388 // sqlquery("UPDATE resident_socket SET last_access=NOW() WHERE socket_id='$fp' AND session_id='$sid'");
391 // // connect to the login service that must be $ASHost:$ASPort
392 // $fp = fsockopen ($ASHost, $ASPort, $errno, $errstr, 30);
395 // $res = "Can't connect to the admin service '$ASHost:$ASPort' ($errno: $errstr)";
404 // function disconnectFromAS(&$fp)
407 // $result = sqlquery("SELECT socket_id FROM resident_socket WHERE socket_id='$fp'");
408 // if (!$result || sqlnumrows($socket)==0)
414 // function sendMessage ($fp, $msgout)
416 // $size = $msgout->Pos;
417 // $Buffer = chr(($size>>24)&0xFF);
418 // $Buffer .= chr(($size>>16)&0xFF);
419 // $Buffer .= chr(($size>>8)&0xFF);
420 // $Buffer .= chr($size&0xFF);
421 // $Buffer .= $msgout->Buffer;
423 // fwrite ($fp, $Buffer);
425 // //printf ("sent packet size '%d'<br>", strlen($Buffer));
430 // function waitMessage ($fp, &$msgin)
432 // //echo "waiting a message";
434 // $val = fread ($fp, 1);
435 // $size = ord($val) << 24;
436 // $val = fread ($fp, 1);
437 // $size = ord($val) << 16;
438 // $val = fread ($fp, 1);
439 // $size += ord($val) << 8;
440 // $val = fread ($fp, 1);
441 // $size += ord($val);
442 // //printf ("receive packet size '%d'<br>", $size);
443 // $fake = fread ($fp, 4);
444 // $size -= 4; // remove the fake
446 // $Buffer = fread ($fp, $size);
447 // $msgin = new CMemStream;
448 // $msgin->setBuffer ($Buffer);
451 // function logNelQuery($query)
455 // $f = fopen("./nel_queries.log", "a");
456 // fwrite($f, date("Y/m/d H:i:s")." ".sprintf("%-16s", $admlogin)." $query\n");
460 // logUser($uid, "QUERY=".$query);
463 // function nel_query($rawvarpath, &$result)
465 // global $nel_queries;
467 // $nel_queries[] = $rawvarpath;
469 // //echo "rawvarpath=$rawvarpath<br>\n";
471 // //logNelQuery($rawvarpath);
473 // connectToAS($fp, $result);
474 // if(strlen($result) != 0)
477 // // send the message that say that we want to add a user
478 // $msgout = new CMemStream;
480 // $msgout->serialuint32 ($fake); // fake used to number the packet
482 // $msgout->serialuint8 ($messageType);
483 // $msgout->serialstring ($rawvarpath);
485 // sendMessage ($fp, $msgout);
487 // waitMessage ($fp, $msgin);
489 // $msgin->serialstring($result);
491 // if(strlen($result) == 0)
501 // //printf("receive response '$result'<br>\n");
503 // disconnectFromAS(&$fp);
504 // //echo "sent OK.<br><br>\n";