some fun data.
[pengine.git] / lib / interComms.php
1 <?php
2 define("NETCOM_PORT", 14001);
3 define("NETCOM_PORT_SSL", 14002);
4
5
6 // comms on this thing are very serial, the server are only capable of processing
7 // one thing at a time... for now
8 class netCom {
9         
10         function __construct($am_i_a_server = false, $server_addr = "127.0.0.1")
11         {
12                 // i have to set it to something, right?
13                 $this->semKey = ftok(__FILE__, "p");
14                 $this->encrypt = false;
15                 
16                 $this->amserver = $am_i_a_server;
17                 $this->server = $server_addr;
18                 
19         }
20         
21         // initiates a bind if its a server, a connect if its a client
22         function go()
23         {
24                 if($this->amserver) {
25                         $this->listen_socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
26                         socket_bind($this->listen_socket, "127.0.0.1", NETCOM_PORT);
27                         socket_listen($this->listen_socket);
28
29                 } else {
30                         $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
31                         $res = socket_connect($this->socket, "127.0.0.1", NETCOM_PORT);
32                         if(!$res) {
33                                 echo "fail on connect\n";
34                                 socket_close($this->socket);
35                                 return false;
36                         }
37                         
38                 }
39         }
40         
41         function waitForConnection()
42         {
43                 socket_listen($this->listen_socket);
44                 $this->socket = socket_accept($this->listen_socket);
45         }
46
47         function sendMessage($message_array)
48         {               
49                 $datacomp = base64_encode(serialize($message_array));
50                 $tosend = "PEN:$datacomp:INE";
51                 
52                 socket_send($this->socket, $tosend, strlen($tosend), 0);
53                 
54                 // get up to one meg of data - this is bad... i can feel this function
55                 // hurting alot
56                 // TODO FIX THIS - its garbage code... im not really sure how to handle this really
57                 // we need to read back as AS:data:EOD - i think it now does.. i hope, tho we need
58                 // timeouts now.
59         }
60         
61         function receiveMessage()
62         {
63                 $recvd = "";
64                 $continue = true;
65                 while($continue) {
66                         $size = socket_recv($this->socket, $recvd_a, 1024, 0);
67                         $recvd .= $recvd_a;
68                         if(preg_match("/.*\:INE$/", $recvd)) {
69                                 // we have a full string... break out
70                                 $continue = false;
71                                 break;
72                         }
73                 }
74                 
75                 
76                 // first check we got something that makes sense
77                 if(preg_match("/^PEN:.*:INE/", $recvd) < 1) {
78                         socket_close($this->socket);
79                         echo "Returned data is not in right format\n";
80                         // we have a problem jim
81                         return false;
82                 }
83                 
84                 $xps = explode(":", $recvd);
85                 
86                 $component =  unserialize(base64_decode($xps[1]));
87
88                 return $component;
89         }
90          
91         
92         
93         private $server;
94         private $encrypt;
95         private $semKey;
96         private $amserver;
97         private $socket;
98         private $socket_ssl;
99         private $listen_socket;
100         private $listen_socket_ssl;
101         
102 }
103
104 /* packets look like
105  * PEN:base64_enoded data:INE
106  * 
107  * 
108  */
109
110 ?>