2 namespace callnotifier;
8 public function __construct($config, $handler)
10 $this->config = $config;
11 $this->handler = $handler;
19 $this->connect($this->config->host, $this->config->port);
22 } catch (Exception_ConnectionReset $e) {
29 public function connect($ip, $port)
32 throw new \Exception('No remote IP or hostname given.');
35 $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
36 if ($socket === false) {
38 'socket_create() failed: reason: '
39 . socket_strerror(socket_last_error())
42 //echo "Attempting to connect to '$ip' on port '$port'...";
43 $result = socket_connect($socket, $ip, $port);
44 if ($result === false) {
46 "socket_connect() failed. Reason: "
47 . socket_strerror(socket_last_error($socket))
51 $this->socket = $socket;
56 $msg = "\x00\x01DecoderV=1\n";
57 socket_write($this->socket, $msg, strlen($msg));
58 $res = $this->read_response();
59 socket_write($this->socket, "\x00\x02", 2);
65 $dbgmsg = $this->read_response();
66 //echo $dbgmsg . "\n";
67 $this->handler->handle($dbgmsg);
71 function read_response()
73 $res = socket_read($this->socket, 2048, PHP_NORMAL_READ);
75 //handle "Connection reset by peer" that appears nightly since
77 throw new Exception_ConnectionReset();
79 return substr($res, 2, -1);
84 socket_write($this->socket, "\x00\x03", 2);
85 socket_close($this->socket);