]> git.mxchange.org Git - hub.git/blob - udp-client.php
Rewritten to out()
[hub.git] / udp-client.php
1 <?php
2 error_reporting(E_ALL | E_STRICT);
3
4 define('ROUNDS', 3);
5 define('MAX_FAILURES', 3);
6
7 require("udp-inc.php");
8
9 global $hasher;
10
11 function validate ($data) {
12         global $hasher;
13
14         switch ($hasher) {
15                 case "md5":
16                         return md5($data);
17                         break;
18
19                 case "sha1":
20                         return sha1($data);
21                         break;
22
23                 case "crc32":
24                         return crc32($data);
25                         break;
26
27                 default:
28                         out(__FUNCTION__, __LINE__, "Unknown hasher: ${hasher}");
29                         break;
30         }
31 }
32
33 $data = array();
34 $cnt = 0;
35 $invalid = 0;
36 $failed = 0;
37
38 $client = stream_socket_client("udp://192.168.1.1:1113", $errno, $errstr);
39
40 if ((!is_resource($client)) || ($errno > 0)) {
41         echo "ERROR: $errno - $errstr\n";
42         exit;
43 }
44
45 if (!stream_set_blocking($client, 0)) {
46         echo "ERROR: Cannot set non-blocking mode!\n";
47         exit;
48 }
49
50 out(__FILE__, __LINE__, "Starting test...");
51 while (!feof($client)) {
52         //out(__FILE__, __LINE__, "Sending ping...");
53         fwrite($client, "PING");
54
55         //out(__FILE__, __LINE__, "Reading reply...");
56         $read = trim(fread($client, 50));
57         if (empty($read)) {
58                 $failed++;
59                 out(__FILE__, __LINE__, "Empty line received. Is the server there?");
60         } elseif ($read == "INVALID") {
61                 $failed++;
62                 out(__FILE__, __LINE__, "Server has not accepted our message.");
63         } else {
64                 $failed = 0;
65                 //out(__FILE__, __LINE__, "Response ${read} received.");
66         }
67
68         if ($failed == constant('MAX_FAILURES')) {
69                 out(__FILE__, __LINE__, "Too many failures! (failed=${failed})");
70                 break;
71         } elseif ($failed < constant('MAX_FAILURES')) {
72                 continue;
73         }
74
75         $rec  = explode(":", $read);
76
77         $time   = $rec[0];
78         $right  = explode("=", $rec[1]);
79         $hash   = $right[1];
80         $hasher = $right[0];
81
82         if (validate($time) != $hash) {
83                 out(__FILE__, __LINE__, "Invalid: ${read}/{$hash}");
84                 $invalid++;
85                 continue;
86         }
87
88         if (!isset($data[$rec[0]])) {
89                 if (count($data) > 0) {
90                         echo $data[$rec[0]-1]."\n";
91                         $cnt++;
92                         if ($cnt > constant('ROUNDS')) break;
93                 }
94                 $data[$rec[0]] = 0;
95         }
96         $data[$rec[0]]++;
97 }
98
99 array_shift($data);
100
101 stream_socket_shutdown($client, STREAM_SHUT_RDWR);
102
103 $avg = 0;
104 $min = 0;
105 $max = 0;
106
107 foreach ($data as $cnt) {
108         if ($cnt > $max) {
109                 $max = $cnt;
110         }
111         if (($cnt < $min) || ($min == 0)) {
112                 $min = $cnt;
113         }
114         $avg += $cnt;
115 }
116
117 if (count($data) > 0) {
118         $avg = round($avg / count($data));
119 }
120
121 out(__FILE__, __LINE__, "MIN/AVG/MAX=${min}/${avg}/${max}");
122 out(__FILE__, __LINE__, "INVALID=${invalid}");
123
124 ?>