aboutsummaryrefslogtreecommitdiff
path: root/src/callnotifier/Source/Remote.php
blob: be7604f357f0513b81b9fbb16f17c403ee0f43a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
namespace callnotifier;

class Source_Remote
{
    protected $socket;

    public function __construct($config, $handler)
    {
        $this->config  = $config;
        $this->handler = $handler;
    }

    public function run()
    {
        do {
            try {
                $tryAgain = false;
                $this->connect($this->config->host, $this->config->port);
                $this->init();
                $this->loop();
            } catch (Exception_ConnectionReset $e) {
                $tryAgain = true;
                //connection is refused directly after a connection reset
                //hopefully waiting a bit will help
                sleep(10);
            }
        } while ($tryAgain);
        $this->disconnect();
    }

    public function connect($ip, $port)
    {
        if ($ip == '') {
            throw new \Exception('No remote IP or hostname given.');
        }

        $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
        if ($socket === false) {
            throw new \Exception(
                'socket_create() failed: reason: '
                . socket_strerror(socket_last_error())
            );
        }
        //echo "Attempting to connect to '$ip' on port '$port'...";
        $result = socket_connect($socket, $ip, $port);
        if ($result === false) {
            throw new \Exception(
                "socket_connect() failed. Reason: "
                . socket_strerror(socket_last_error($socket))
            );
        }

        $this->socket = $socket;
    }

    function init()
    {
        $msg = "\x00\x01DecoderV=1\n";
        socket_write($this->socket, $msg, strlen($msg));
        $res = $this->read_response();
        socket_write($this->socket, "\x00\x02", 2);
    }

    function loop()
    {
        while (true) {
            $dbgmsg = $this->read_response();
            //echo $dbgmsg . "\n";
            $this->handler->handle($dbgmsg);
        }
    }

    function read_response()
    {
        $res = socket_read($this->socket, 2048, PHP_NORMAL_READ);
        if ($res === false) {
            //handle "Connection reset by peer" that appears nightly since
            // version 4.0N
            throw new Exception_ConnectionReset();
        }
        return substr($res, 2, -1);
    }

    function disconnect()
    {
        socket_write($this->socket, "\x00\x03", 2);
        socket_close($this->socket);
    }

}

?>