one little bloody return statement
[ga4php.git] / gaas / lib / gaasdClient.php
1 <?php
2
3
4 require_once("globalLib.php");
5 require_once("gaasClientMessages.php");
6
7 // I am the gaasd client.. i know all, i see all... I am the "only" way to interact with the gaasd server.
8
9 class GAASClient {
10         
11         // the main send/receive functions. Communicates with gaasd
12         // we always expect one send followed by one receive message
13         function sendReceive($message_type, $message)
14         {
15                 // yeah... this is totally gunna work
16                 global $TCP_PORT_NUMBER;
17                 
18                 $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
19                 $res = socket_connect($socket, "127.0.0.1", $TCP_PORT_NUMBER);
20                 if(!$res) {
21                         socket_close($socket);
22                         return false;
23                 }
24                 
25                 $msg["type"] = $message_type;
26                 $msg["data"] = $message;
27                 
28                 $datacomp = base64_encode(serialize($msg));
29                 $tosend = "AC:$datacomp:EOD";
30                 
31                 socket_send($socket, $tosend, strlen($tosend), 0);
32                 
33                 // get up to one meg of data - this is bad... i can feel this function
34                 // hurting alot
35                 // TODO FIX THIS - its garbage code... im not really sure how to handle this really
36                 // we need to read back as AS:data:EOD - i think it now does.. i hope, tho we need
37                 // timeouts now.
38                 $recvd = "";
39                 $continue = true;
40                 while($continue) {
41                         $size = socket_recv($socket, $recvd_a, 1024, 0);
42                         $recvd .= $recvd_a;
43                         if(preg_match("/.*\:EOD$/", $recvd)) {
44                                 // we have a full string... break out
45                                 $continue = false;
46                                 break;
47                         }
48                 }
49                 
50                 
51                 // first check we got something that makes sense
52                 if(preg_match("/^AS:.*:EOD/", $recvd) < 1) {
53                         socket_close($socket);
54                         echo "Returned data is not in right format\n";
55                         // we have a problem jim
56                         return false;
57                 }
58                 
59                 $xps = explode(":", $recvd);
60                 
61                 $component =  unserialize(base64_decode($xps[1]));
62                 
63                 if($component["type"] != $message_type) {
64                         echo "Message type was not the same as original message\n";
65                         // we have a problem jim
66                         socket_close($socket);
67                         return false;
68                 }
69                 
70                 //echo "component\n";
71                 //print_r($component);
72                 
73                 socket_close($socket);
74                 
75                 return $component["data"];
76         }
77         
78         // this is one thing i love about php... how truely dynamic it can be in very easy to do ways.
79         // im not entirely sure what im doing with this bit yet
80         function __call($func, $params)
81         {
82                 // im a little stuck here.
83                 //  want messages to be defineable in terms of plugins
84                 // but i dont think this is the way to do it
85                 global $MESSAGES;
86                 $st_defined = constant($func);
87                 //echo "func is $st_defined\n";
88                 $function_send = $MESSAGES[$st_defined]."_clientsend";
89                 $function_recv = $MESSAGES[$st_defined]."_clientrecv";
90                 //echo "real function is $function_send, $function_recv\n";
91                 
92                 if(function_exists($function_send) && function_exists($function_recv)) {
93                         return $function_recv($this->sendReceive($st_defined, $function_send($params)));
94                 } else {
95                         error_log("Function, $function does not exist!");
96                 }
97         }
98 }
99
100 ?>