2 namespace callnotifier;
8 public function __construct($config, $handler)
10 $this->config = $config;
11 $this->handler = $handler;
16 $this->connect($this->config->host, $this->config->port);
22 public function connect($ip, $port)
25 throw new \Exception('No remote IP or hostname given.');
28 $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
29 if ($socket === false) {
31 'socket_create() failed: reason: '
32 . socket_strerror(socket_last_error())
35 //echo "Attempting to connect to '$ip' on port '$port'...";
36 $result = socket_connect($socket, $ip, $port);
37 if ($result === false) {
39 "socket_connect() failed. Reason: "
40 . socket_strerror(socket_last_error($socket))
44 $this->socket = $socket;
49 $msg = "\x00\x01DecoderV=1\n";
50 socket_write($this->socket, $msg, strlen($msg));
51 $res = $this->read_response();
52 socket_write($this->socket, "\x00\x02", 2);
58 $dbgmsg = $this->read_response();
59 //echo $dbgmsg . "\n";
60 $this->handler->handle($dbgmsg);
64 function read_response()
66 $res = socket_read($this->socket, 2048, PHP_NORMAL_READ);
67 return substr($res, 2, -1);
72 socket_write($this->socket, "\x00\x03", 2);
73 socket_close($this->socket);