introduce classes
authorChristian Weiske <cweiske@cweiske.de>
Sat, 14 Jul 2012 19:14:29 +0000 (21:14 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Sat, 14 Jul 2012 19:14:29 +0000 (21:14 +0200)
README.rst
callnotifier.php
src/callnotifier/CLI.php [new file with mode: 0644]
src/callnotifier/Socket.php [new file with mode: 0644]

index 188b8783fd7226884cad266b960d8bebef3cd33d..f31a2996b38ebcdd63ebeb318424f42448d77512 100644 (file)
@@ -19,3 +19,10 @@ Issues
 Ctrl+C does not send the disconnect command.
 This is a problem with PHP since pcntl_signal handling and blocking sockets
 do not work together. The signal will not be handled.
+
+
+Source
+======
+Original git website: http://git.cweiske.de/?p=auerswald-callnotifier.git
+
+Mirror: https://github.com/cweiske/auerswald-callnotifier
index a481b1f3391b2fcdf162c03827aff769b8adc6e0..9093691e67cc20b8041a747a9ece551d417effb8 100644 (file)
@@ -1,84 +1,23 @@
 #!/usr/bin/env php
 <?php
-$ip = '192.168.3.95';
-$port = 42225;
-
-$socket = awcn_connect($ip, $port);
-awcn_init($socket);
-awcn_loop($socket);
-awcn_disconnect($socket);
-
-function awcn_connect($ip, $port) {
-    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
-    if ($socket === false) {
-        echo "socket_create() failed: reason: "
-            . socket_strerror(socket_last_error()) . "\n";
-    } else {
-        echo "OK.\n";
-    }
-    echo "Attempting to connect to '$ip' on port '$port'...";
-    $result = socket_connect($socket, $ip, $port);
-    if ($result === false) {
-        echo "socket_connect() failed.\nReason: ($result) "
-            . socket_strerror(socket_last_error($socket)) . "\n";
-    } else {
-        echo "OK.\n";
-    }
-    return $socket;
-}
-
-function awcn_init($socket)
-{
-    $msg = "\x00\x01DecoderV=1\n";
-    socket_write($socket, $msg, strlen($msg));
-    $res = awcn_read_response($socket);
-    socket_write($socket, "\x00\x02", 2);
-}
-
-function awcn_loop($socket)
-{
-    while (true) {
-        $dbgmsg = awcn_read_response($socket);
-        //echo $dbgmsg . "\n";
-        awcn_handle_msg($dbgmsg);
+namespace callnotifier;
+
+set_include_path(
+    __DIR__ . '/src/'
+    . PATH_SEPARATOR . get_include_path()
+);
+spl_autoload_register(
+    function ($class) {
+        $file = str_replace(array('\\', '_'), '/', $class) . '.php';
+        $hdl = @fopen($file, 'r', true);
+        if ($hdl !== false) {
+            fclose($hdl);
+            require $file;
+        }
     }
-}
-function awcn_read_response($socket)
-{
-    $res = socket_read($socket, 2048, PHP_NORMAL_READ);
-    return substr($res, 2, -1);
-}
+);
 
-function awcn_disconnect($socket)
-{
-    socket_write($socket, "\x00\x03", 2);
-    socket_close($socket);
-}
-
-function awcn_handle_msg($msg)
-{
-    if (substr($msg, 0, 9) != '[DKANPROT') {
-        //unknown message type
-        return;
-    }
-    $regex = '#^\\[DKANPROT-([^ ]+) ([0-9]+)\\] (.*)$#';
-    if (!preg_match($regex, $msg, $matches)) {
-        //message should always be that way
-        return false;
-    }
-    list(, $type, $someid, $details) = $matches;
-
-    if ($type != 'Info') {
-        //we only want info messages
-        var_dump($type . ': ' . $details);
-        return;
-    }
-    //Vegw/Ets-Cref:[0xffef]/[0x64] - VEGW_SETUP from upper layer to internal destination: CGPN[**22]->CDPN[41], 
-    var_dump($details);
-    $regex = '#CGPN\\[([^\\]]+)\\]->CDPN\\[([^\\]]+)\\]#';
-    if (preg_match($regex, $details, $matches)) {
-        var_dump('a call!', $matches);
-    }
-}
+$cli = new CLI();
+$cli->run();
 
 ?>
\ No newline at end of file
diff --git a/src/callnotifier/CLI.php b/src/callnotifier/CLI.php
new file mode 100644 (file)
index 0000000..4bc0e60
--- /dev/null
@@ -0,0 +1,14 @@
+<?php
+namespace callnotifier;
+
+
+class CLI
+{
+    public function run()
+    {
+        $s = new Socket('192.168.3.95');
+        $s->run();
+    }
+}
+
+?>
\ No newline at end of file
diff --git a/src/callnotifier/Socket.php b/src/callnotifier/Socket.php
new file mode 100644 (file)
index 0000000..1130bd8
--- /dev/null
@@ -0,0 +1,101 @@
+<?php
+namespace callnotifier;
+
+class Socket
+{
+    protected $socket;
+    public $ip = null;
+    public $port = 42225;
+
+    public function __construct($ip)
+    {
+        $this->ip = $ip;
+    }
+
+    public function run()
+    {
+        $this->connect($this->ip, $this->port);
+        $this->init();
+        $this->loop();
+        $this->disconnect();
+    }
+
+    public function connect($ip, $port)
+    {
+        $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
+        if ($socket === false) {
+            echo "socket_create() failed: reason: "
+                . socket_strerror(socket_last_error()) . "\n";
+        } else {
+            echo "OK.\n";
+        }
+        echo "Attempting to connect to '$ip' on port '$port'...";
+        $result = socket_connect($socket, $ip, $port);
+        if ($result === false) {
+            echo "socket_connect() failed.\nReason: ($result) "
+                . socket_strerror(socket_last_error($socket)) . "\n";
+        } else {
+            echo "OK.\n";
+        }
+
+        $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->handle_msg($dbgmsg);
+        }
+    }
+    function read_response()
+    {
+        $res = socket_read($this->socket, 2048, PHP_NORMAL_READ);
+        return substr($res, 2, -1);
+    }
+
+    function disconnect()
+    {
+        socket_write($this->socket, "\x00\x03", 2);
+        socket_close($this->socket);
+    }
+
+    function handle_msg($msg)
+    {
+        if (substr($msg, 0, 9) != '[DKANPROT') {
+            //unknown message type
+            return;
+        }
+        $regex = '#^\\[DKANPROT-([^ ]+) ([0-9]+)\\] (.*)$#';
+        if (!preg_match($regex, $msg, $matches)) {
+            //message should always be that way
+            return false;
+        }
+        list(, $type, $someid, $details) = $matches;
+
+        if ($type != 'Info') {
+            //we only want info messages
+            var_dump($type . ': ' . $details);
+            return;
+        }
+        //Vegw/Ets-Cref:[0xffef]/[0x64] - VEGW_SETUP from upper layer to internal destination: CGPN[**22]->CDPN[41], 
+        var_dump($details);
+        $regex = '#CGPN\\[([^\\]]+)\\]->CDPN\\[([^\\]]+)\\]#';
+        if (preg_match($regex, $details, $matches)) {
+            var_dump('a call!', $matches);
+        }
+    }
+
+
+}
+
+?>