some fun data.
authorpaulr <me@pjr.cc>
Thu, 17 Mar 2011 17:24:56 +0000 (04:24 +1100)
committerpaulr <me@pjr.cc>
Thu, 17 Mar 2011 17:24:56 +0000 (04:24 +1100)
lib/interComms.php
lib/lib.php
unittests/commrecv.php [new file with mode: 0644]
unittests/commsend.php [new file with mode: 0644]

index 52d874d..f53a4a0 100644 (file)
@@ -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;
        
 }
 
index 63c697a..1fde0d7 100644 (file)
@@ -1,5 +1,7 @@
 <?php 
 
+require_once("interComms.php");
+
 // first and foremost, load the plugins
 $basedir = dirname(__FILE__);
 if(is_dir("$basedir/plugins")) {
@@ -20,7 +22,7 @@ if(is_dir("$basedir/plugins")) {
 $storeLocation = "$basedir/../store/";
 global $storeLocation;
 
-if(!isdir($storeLocation)) {
+if(!is_dir($storeLocation)) {
        mkdir($storeLocation, 0770);
 }
 ?>
\ No newline at end of file
diff --git a/unittests/commrecv.php b/unittests/commrecv.php
new file mode 100644 (file)
index 0000000..16fea30
--- /dev/null
@@ -0,0 +1,13 @@
+<?php 
+require_once("../lib/lib.php");
+
+$mn = new netCom(true);
+
+$mn->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 (file)
index 0000000..cdd8e4e
--- /dev/null
@@ -0,0 +1,13 @@
+<?php
+require_once("../lib/lib.php");
+
+$mn = new netCom();
+$mn->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