Details | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1872 | runge | 1 | <? |
| 2 | |||
| 3 | error_reporting(E_ALL); |
||
| 4 | |||
| 5 | function atomd_connect($host, $port) |
||
| 6 | { |
||
| 1880 | arune | 7 | $socket = pfsockopen($host, $port, $errno, $errstr); |
| 1872 | runge | 8 | |
| 9 | if (!$socket) |
||
| 10 | { |
||
| 11 | throw new Exception("$errstr ($errno)"); |
||
| 12 | } |
||
| 13 | |||
| 14 | return $socket; |
||
| 15 | } |
||
| 16 | |||
| 17 | function atomd_read_packet($socket) |
||
| 18 | { |
||
| 19 | if (feof($socket)) |
||
| 20 | { |
||
| 21 | throw new Exception("socket not open"); |
||
| 22 | } |
||
| 23 | |||
| 24 | $command = fread($socket, 4); |
||
| 25 | |||
| 26 | if ($command === FALSE) |
||
| 27 | { |
||
| 28 | throw new Exception("got EOF while reading command from socket"); |
||
| 29 | } |
||
| 30 | |||
| 31 | $payload_length = fread($socket, 4); |
||
| 32 | |||
| 33 | if ($payload_length === FALSE) |
||
| 34 | { |
||
| 35 | throw new Exception("got EOF while reading payload_length from socket"); |
||
| 36 | } |
||
| 37 | |||
| 38 | $payload = ""; |
||
| 39 | |||
| 40 | if ($payload_length > 0) |
||
| 41 | { |
||
| 42 | $payload = fread($socket, intval($payload_length)); |
||
| 43 | |||
| 44 | if ($payload === FALSE) |
||
| 45 | { |
||
| 46 | throw new Exception("got EOF while reading payload from socket"); |
||
| 47 | } |
||
| 48 | } |
||
| 49 | |||
| 50 | return $command . $payload_length . $payload; |
||
| 51 | } |
||
| 52 | |||
| 53 | function atomd_write_packet($socket, $command, $payload) |
||
| 54 | { |
||
| 55 | if (feof($socket)) |
||
| 56 | { |
||
| 57 | throw new Exception("socket not open"); |
||
| 58 | } |
||
| 59 | |||
| 60 | $packet = $command . str_pad(strlen($payload) + 1, 4, "0", STR_PAD_LEFT) . $payload . "\0"; |
||
| 61 | |||
| 62 | if (fwrite($socket, $packet) === FALSE) |
||
| 63 | { |
||
| 64 | throw new Exception("got error while writing payload to socket"); |
||
| 65 | } |
||
| 66 | } |
||
| 67 | |||
| 68 | function atomd_data_available($socket) |
||
| 69 | { |
||
| 2051 | linlun | 70 | $read = array($socket); |
| 71 | $write = NULL; |
||
| 72 | $except = NULL; |
||
| 73 | |||
| 74 | if (false === ($num_changed_streams = stream_select($read, $write, $except, 0))) |
||
| 75 | { |
||
| 76 | throw new Exception("could not do select on socket"); |
||
| 77 | } |
||
| 78 | |||
| 79 | return $num_changed_streams > 0; |
||
| 80 | /* |
||
| 81 | |||
| 82 | |||
| 1872 | runge | 83 | $position = ftell($socket); |
| 84 | |||
| 85 | fseek($socket, -1, SEEK_END); |
||
| 86 | |||
| 87 | $has_data = ftell($socket) > $position; |
||
| 88 | |||
| 89 | fseek($socket, $position, SEEK_SET); |
||
| 90 | |||
| 2051 | linlun | 91 | return $has_data;*/ |
| 1872 | runge | 92 | } |
| 93 | |||
| 2051 | linlun | 94 | /* |
| 1872 | runge | 95 | |
| 2051 | linlun | 96 | function atomd_data_available($socket) |
| 97 | { |
||
| 98 | $position = ftell($socket); |
||
| 99 | |||
| 100 | fseek($socket, -1, SEEK_END); |
||
| 1872 | runge | 101 | |
| 2051 | linlun | 102 | $has_data = ftell($socket) > $position; |
| 103 | |||
| 104 | fseek($socket, $position, SEEK_SET); |
||
| 105 | |||
| 106 | return $has_data; |
||
| 107 | } |
||
| 108 | */ |
||
| 109 | |||
| 1872 | runge | 110 | function atomd_initialize($host, $port) |
| 111 | { |
||
| 1879 | arune | 112 | $socket = atomd_connect($host, $port); |
| 1872 | runge | 113 | |
| 114 | while (atomd_data_available($socket)) |
||
| 115 | { |
||
| 116 | $packet = atomd_read_packet($socket); // Read initial prompt |
||
| 117 | } |
||
| 118 | |||
| 119 | return $socket; |
||
| 120 | } |
||
| 121 | |||
| 122 | function atomd_send_command($socket, $command) |
||
| 123 | { |
||
| 124 | atomd_write_packet($socket, "RESP", $command); |
||
| 125 | } |
||
| 126 | |||
| 2051 | linlun | 127 | |
| 1872 | runge | 128 | function atomd_read_command_response($socket) |
| 129 | { |
||
| 130 | $response = ""; |
||
| 131 | |||
| 132 | while (true) |
||
| 133 | { |
||
| 134 | $packet = atomd_read_packet($socket); |
||
| 135 | |||
| 136 | if (substr($packet, 0, 4) != "TEXT") |
||
| 137 | { |
||
| 138 | break; |
||
| 139 | } |
||
| 140 | |||
| 141 | $response .= substr($packet, 8) . "\n"; |
||
| 142 | } |
||
| 143 | |||
| 144 | return $response; |
||
| 145 | } |
||
| 146 | |||
| 147 | |||
| 148 | /* |
||
| 149 | $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); |
||
| 150 | |||
| 151 | if ($socket === false) |
||
| 152 | { |
||
| 153 | die("Socket Creation Failed"); |
||
| 154 | } |
||
| 155 | |||
| 156 | $result = socket_connect($socket, "127.0.0.1", 1202); |
||
| 157 | |||
| 158 | if ($result === false) |
||
| 159 | { |
||
| 160 | die("Connection Failed"); |
||
| 161 | } |
||
| 162 | |||
| 163 | |||
| 164 | $command = "help"; |
||
| 165 | $length = strlen($command) + 1; |
||
| 166 | |||
| 167 | $data = "RESP" . str_pad($length, 4, "0", STR_PAD_LEFT) . $command . "\0\n"; |
||
| 168 | |||
| 169 | echo "Sending:" . $data . "<br/>"; |
||
| 170 | |||
| 171 | socket_write($socket, $data, strlen($data)); |
||
| 172 | |||
| 173 | do |
||
| 174 | { |
||
| 175 | $line = socket_read($socket, 1024, PHP_BINARY_READ); |
||
| 176 | $output .= $line; |
||
| 177 | } |
||
| 178 | while ($line != ""); |
||
| 179 | |||
| 180 | echo $output . "<br/>"; |
||
| 181 | $output = ""; |
||
| 182 | |||
| 183 | echo "Done first read!<br/>"; |
||
| 184 | |||
| 185 | do |
||
| 186 | { |
||
| 187 | $line = socket_read($socket, 1024, PHP_BINARY_READ); |
||
| 188 | $output .= $line; |
||
| 189 | } |
||
| 190 | while ($line != ""); |
||
| 191 | |||
| 192 | echo $output . "<br/>"; |
||
| 193 | $output = ""; |
||
| 194 | |||
| 195 | echo "Done!<br/>"; |
||
| 196 | |||
| 197 | socket_close($socket); |
||
| 198 | */ |
||
| 199 | ?> |