]> git.mxchange.org Git - hub.git/blobdiff - contrib/udp-client.php
moved for later merge to master
[hub.git] / contrib / udp-client.php
diff --git a/contrib/udp-client.php b/contrib/udp-client.php
new file mode 100644 (file)
index 0000000..d5dd361
--- /dev/null
@@ -0,0 +1,175 @@
+<?php
+error_reporting(E_ALL | E_STRICT);
+
+define('ROUNDS', 10);
+define('MAX_FAILURES', 3000);
+
+require('udp-inc.php');
+
+global $hasher;
+
+function validate ($data) {
+       global $hasher;
+
+       switch ($hasher) {
+               case 'md5':
+                       return md5($data);
+                       break;
+
+               case 'sha1':
+                       return sha1($data);
+                       break;
+
+               case 'crc32':
+                       return crc32($data);
+                       break;
+
+               default:
+                       out(__FUNCTION__, __LINE__, "Unknown hasher: ${hasher}");
+                       break;
+       }
+}
+
+function sendBye ($socket) {
+       $replied = FALSE;
+       while (!$replied) {
+               // Try to send BYE
+               stream_socket_sendto($socket, 'BYE');
+
+               // Wait for an answer
+               $read = trim(stream_socket_recvfrom($socket, 1500, 0, $peer));
+               if (!empty($read)) {
+                       // Did send BYE as well?
+                       if ($read == 'BYE') {
+                               // Okay
+                               $replied = true;
+                       } // END - if
+               } // END - if
+
+               // Sleep a little
+               if (function_exists('time_nanosleep')) time_nanosleep(0, 500000);
+       } // END - while
+}
+
+$data = array();
+$cnt = 0;
+$invalid = 0;
+$failed = 0;
+
+$ip = '127.0.0.1';
+if (isset($_SERVER['argv'][1])) {
+       $ip = $_SERVER['argv'][1];
+}
+
+$socket = stream_socket_client("udp://${ip}:9060", $errno, $errstr);
+
+if ((!is_resource($socket)) || ($errno > 0)) {
+       out(__FILE__, __LINE__, "ERROR: $errno - $errstr");
+       exit;
+}
+
+if (!stream_set_blocking($socket, FALSE)) {
+       out(__FILE__, __LINE__, "ERROR: Cannot set non-blocking mode!");
+       exit;
+}
+
+out(__FILE__, __LINE__, "Negotiating with host ${ip} ...");
+while (!feof($socket)) {
+       //*DEBUG: */ out(__FILE__, __LINE__, "Sending ping...");
+       stream_socket_sendto($socket, 'PING');
+
+       //*DEBUG: */ out(__FILE__, __LINE__, "Reading reply...");
+       $read = trim(stream_socket_recvfrom($socket, 1500, 0, $peer));
+
+       if ($failed == constant('MAX_FAILURES')) {
+               //*DEBUG: */ out(__FILE__, __LINE__, "Too many failures! (failed={$failed})");
+               break;
+       } // END - if
+
+       // Is the peer the same?
+       if (empty($peer)) {
+               out(__FILE__, __LINE__, "Connection lost? read={$read} (".strlen($read).")");
+               $failed++;
+               continue;
+       } elseif ($peer != "$ip:9060") {
+               out(__FILE__, __LINE__, "Peer mismatch: {$ip}!={$peer}");
+               $failed++;
+               continue;
+       }
+
+       if (empty($read)) {
+               $failed++;
+               //*DEBUG: */ out(__FILE__, __LINE__, "Empty line received. Is the server there?");
+               continue;
+       } elseif ($read == 'INVALID') {
+               $failed++;
+               out(__FILE__, __LINE__, "Server has not accepted our message.");
+               continue;
+       } else {
+               $failed = 0;
+               //*DEBUG: */ out(__FILE__, __LINE__, "Response {$read} received.");
+       }
+
+       $rec  = explode(':', $read);
+
+       if (count($rec) < 2) {
+               out(__FILE__, __LINE__, "Invalid packet {$read} received. count=".count($rec));
+               $invalid++;
+               continue;
+       } // END - if
+
+       $time   = trim($rec[0]);
+       $right  = explode('=', trim($rec[1]));
+       $hash   = trim($right[1]);
+       $hasher = trim($right[0]);
+
+       if (validate($time) != $hash) {
+               out(__FILE__, __LINE__, "Invalid: ({$time}/{$hash}/{$hasher})");
+               $invalid++;
+               continue;
+       } // END - if
+
+       if (!isset($data[$rec[0]])) {
+               if (count($data) > 0) {
+                       print $data[$rec[0]-1]."\n";
+                       $cnt++;
+                       if ($cnt > constant('ROUNDS')) break;
+               }
+               $data[$rec[0]] = 0;
+       }
+       $data[$rec[0]]++;
+
+       // Sleep a little
+       if (function_exists('time_nanosleep')) time_nanosleep(0, 500000);
+} // END - while
+
+array_shift($data);
+
+// Send BYE
+sendBye($socket);
+
+stream_socket_shutdown($socket, STREAM_SHUT_RDWR);
+
+$avg = 0;
+$min = 0;
+$max = 0;
+
+foreach ($data as $cnt) {
+       if ($cnt > $max) {
+               $max = $cnt;
+       }
+       if (($cnt < $min) || ($min == 0)) {
+               $min = $cnt;
+       }
+       $avg += $cnt;
+} // END - foreach
+
+if (count($data) > 0) {
+       $avg = round($avg / count($data));
+} // END - if
+
+out(__FILE__, __LINE__, 'MIN/AVG/MAX=' . $min . '/' . $avg . '/' . $max . '');
+out(__FILE__, __LINE__, 'INVALID=' . $invalid . '');
+out(__FILE__, __LINE__, 'FAILED=' . $failed . '');
+
+?>