]> git.mxchange.org Git - hub.git/blob - udp-client.php
moved for svn-git transission
[hub.git] / udp-client.php
1 <?php
2 error_reporting(E_ALL | E_STRICT);
3
4 define('ROUNDS', 10);
5 define('MAX_FAILURES', 3000);
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 function sendBye ($socket) {
34         $replied = FALSE;
35         while (!$replied) {
36                 // Try to send BYE
37                 stream_socket_sendto($socket, 'BYE');
38
39                 // Wait for an answer
40                 $read = trim(stream_socket_recvfrom($socket, 1500, 0, $peer));
41                 if (!empty($read)) {
42                         // Did send BYE as well?
43                         if ($read == 'BYE') {
44                                 // Okay
45                                 $replied = true;
46                         } // END - if
47                 } // END - if
48
49                 // Sleep a little
50                 if (function_exists('time_nanosleep')) time_nanosleep(0, 500000);
51         } // END - while
52 }
53
54 $data = array();
55 $cnt = 0;
56 $invalid = 0;
57 $failed = 0;
58
59 $ip = '127.0.0.1';
60 if (isset($_SERVER['argv'][1])) {
61         $ip = $_SERVER['argv'][1];
62 }
63
64 $socket = stream_socket_client("udp://${ip}:9060", $errno, $errstr);
65
66 if ((!is_resource($socket)) || ($errno > 0)) {
67         out(__FILE__, __LINE__, "ERROR: $errno - $errstr");
68         exit;
69 }
70
71 if (!stream_set_blocking($socket, FALSE)) {
72         out(__FILE__, __LINE__, "ERROR: Cannot set non-blocking mode!");
73         exit;
74 }
75
76 out(__FILE__, __LINE__, "Negotiating with host ${ip} ...");
77 while (!feof($socket)) {
78         //*DEBUG: */ out(__FILE__, __LINE__, "Sending ping...");
79         stream_socket_sendto($socket, 'PING');
80
81         //*DEBUG: */ out(__FILE__, __LINE__, "Reading reply...");
82         $read = trim(stream_socket_recvfrom($socket, 1500, 0, $peer));
83
84         if ($failed == constant('MAX_FAILURES')) {
85                 //*DEBUG: */ out(__FILE__, __LINE__, "Too many failures! (failed={$failed})");
86                 break;
87         } // END - if
88
89         // Is the peer the same?
90         if (empty($peer)) {
91                 out(__FILE__, __LINE__, "Connection lost? read={$read} (".strlen($read).")");
92                 $failed++;
93                 continue;
94         } elseif ($peer != "$ip:9060") {
95                 out(__FILE__, __LINE__, "Peer mismatch: {$ip}!={$peer}");
96                 $failed++;
97                 continue;
98         }
99
100         if (empty($read)) {
101                 $failed++;
102                 //*DEBUG: */ out(__FILE__, __LINE__, "Empty line received. Is the server there?");
103                 continue;
104         } elseif ($read == 'INVALID') {
105                 $failed++;
106                 out(__FILE__, __LINE__, "Server has not accepted our message.");
107                 continue;
108         } else {
109                 $failed = 0;
110                 //*DEBUG: */ out(__FILE__, __LINE__, "Response {$read} received.");
111         }
112
113         $rec  = explode(':', $read);
114
115         if (count($rec) < 2) {
116                 out(__FILE__, __LINE__, "Invalid packet {$read} received. count=".count($rec));
117                 $invalid++;
118                 continue;
119         } // END - if
120
121         $time   = trim($rec[0]);
122         $right  = explode('=', trim($rec[1]));
123         $hash   = trim($right[1]);
124         $hasher = trim($right[0]);
125
126         if (validate($time) != $hash) {
127                 out(__FILE__, __LINE__, "Invalid: ({$time}/{$hash}/{$hasher})");
128                 $invalid++;
129                 continue;
130         } // END - if
131
132         if (!isset($data[$rec[0]])) {
133                 if (count($data) > 0) {
134                         print $data[$rec[0]-1]."\n";
135                         $cnt++;
136                         if ($cnt > constant('ROUNDS')) break;
137                 }
138                 $data[$rec[0]] = 0;
139         }
140         $data[$rec[0]]++;
141
142         // Sleep a little
143         if (function_exists('time_nanosleep')) time_nanosleep(0, 500000);
144 } // END - while
145
146 array_shift($data);
147
148 // Send BYE
149 sendBye($socket);
150
151 stream_socket_shutdown($socket, STREAM_SHUT_RDWR);
152
153 $avg = 0;
154 $min = 0;
155 $max = 0;
156
157 foreach ($data as $cnt) {
158         if ($cnt > $max) {
159                 $max = $cnt;
160         }
161         if (($cnt < $min) || ($min == 0)) {
162                 $min = $cnt;
163         }
164         $avg += $cnt;
165 } // END - foreach
166
167 if (count($data) > 0) {
168         $avg = round($avg / count($data));
169 } // END - if
170
171 out(__FILE__, __LINE__, 'MIN/AVG/MAX=' . $min . '/' . $avg . '/' . $max . '');
172 out(__FILE__, __LINE__, 'INVALID=' . $invalid . '');
173 out(__FILE__, __LINE__, 'FAILED=' . $failed . '');
174
175 ?>