From: paulr Date: Thu, 17 Mar 2011 17:24:56 +0000 (+1100) Subject: some fun data. X-Git-Url: http://git.pjr.cc/?a=commitdiff_plain;h=527c1ed71988910d53837e504d5b1ba0b16888aa;p=pengine.git some fun data. --- diff --git a/lib/interComms.php b/lib/interComms.php index 52d874d..f53a4a0 100644 --- a/lib/interComms.php +++ b/lib/interComms.php @@ -2,16 +2,19 @@ define("NETCOM_PORT", 14001); define("NETCOM_PORT_SSL", 14002); + +// comms on this thing are very serial, the server are only capable of processing +// one thing at a time... for now class netCom { function __construct($am_i_a_server = false, $server_addr = "127.0.0.1") { // i have to set it to something, right? - $semKey = ftok(__FILE__, "pengine"); - $encrypt = false; + $this->semKey = ftok(__FILE__, "p"); + $this->encrypt = false; - $amserver = $am_i_a_server; - $server = $server_addr; + $this->amserver = $am_i_a_server; + $this->server = $server_addr; } @@ -19,23 +22,70 @@ class netCom { function go() { if($this->amserver) { + $this->listen_socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); + socket_bind($this->listen_socket, "127.0.0.1", NETCOM_PORT); + socket_listen($this->listen_socket); + + } else { + $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); + $res = socket_connect($this->socket, "127.0.0.1", NETCOM_PORT); + if(!$res) { + echo "fail on connect\n"; + socket_close($this->socket); + return false; + } } } - function serverLoop() + function waitForConnection() { - + socket_listen($this->listen_socket); + $this->socket = socket_accept($this->listen_socket); } - - function sendMessage() - { + + function sendMessage($message_array) + { + $datacomp = base64_encode(serialize($message_array)); + $tosend = "PEN:$datacomp:INE"; + + socket_send($this->socket, $tosend, strlen($tosend), 0); + // get up to one meg of data - this is bad... i can feel this function + // hurting alot + // TODO FIX THIS - its garbage code... im not really sure how to handle this really + // we need to read back as AS:data:EOD - i think it now does.. i hope, tho we need + // timeouts now. } function receiveMessage() { + $recvd = ""; + $continue = true; + while($continue) { + $size = socket_recv($this->socket, $recvd_a, 1024, 0); + $recvd .= $recvd_a; + if(preg_match("/.*\:INE$/", $recvd)) { + // we have a full string... break out + $continue = false; + break; + } + } + + + // first check we got something that makes sense + if(preg_match("/^PEN:.*:INE/", $recvd) < 1) { + socket_close($this->socket); + echo "Returned data is not in right format\n"; + // we have a problem jim + return false; + } + + $xps = explode(":", $recvd); + $component = unserialize(base64_decode($xps[1])); + + return $component; } @@ -46,6 +96,8 @@ class netCom { private $amserver; private $socket; private $socket_ssl; + private $listen_socket; + private $listen_socket_ssl; } diff --git a/lib/lib.php b/lib/lib.php index 63c697a..1fde0d7 100644 --- a/lib/lib.php +++ b/lib/lib.php @@ -1,5 +1,7 @@ \ No newline at end of file diff --git a/unittests/commrecv.php b/unittests/commrecv.php new file mode 100644 index 0000000..16fea30 --- /dev/null +++ b/unittests/commrecv.php @@ -0,0 +1,13 @@ +go(); +while(true) { + $mn->waitForConnection(); + while(($msg = $mn->receiveMessage()) !== false) { + print_r($msg); + } +} +?> \ No newline at end of file diff --git a/unittests/commsend.php b/unittests/commsend.php new file mode 100644 index 0000000..cdd8e4e --- /dev/null +++ b/unittests/commsend.php @@ -0,0 +1,13 @@ +go(); + +$array["asdf"] = "message"; +$array["other"] = "moogie"; +for($i = 0; $i < 10; $i++) { + $array["bl"] = $i; + $mn->sendMessage($array); +} +?> \ No newline at end of file