aboutsummaryrefslogtreecommitdiff
path: root/src/callnotifier/Source/Remote.php
diff options
context:
space:
mode:
authorChristian Weiske <cweiske@cweiske.de>2012-07-14 22:28:47 +0200
committerChristian Weiske <cweiske@cweiske.de>2012-07-14 22:28:47 +0200
commit706ce1501a9ab1a33e4106774665c631a3ad3749 (patch)
tree3193e52b2388831012806db6ad331afe73db173c /src/callnotifier/Source/Remote.php
parent19e5e158f8a6e9dda406c2eb70ad035211613df8 (diff)
downloadauerswald-callnotifier-706ce1501a9ab1a33e4106774665c631a3ad3749.tar.gz
auerswald-callnotifier-706ce1501a9ab1a33e4106774665c631a3ad3749.zip
dumping network data is possible now
Diffstat (limited to 'src/callnotifier/Source/Remote.php')
-rw-r--r--src/callnotifier/Source/Remote.php74
1 files changed, 74 insertions, 0 deletions
diff --git a/src/callnotifier/Source/Remote.php b/src/callnotifier/Source/Remote.php
new file mode 100644
index 0000000..36e468d
--- /dev/null
+++ b/src/callnotifier/Source/Remote.php
@@ -0,0 +1,74 @@
+<?php
+namespace callnotifier;
+
+class Source_Remote
+{
+ protected $socket;
+
+ public function __construct($config, $handler)
+ {
+ $this->config = $config;
+ $this->handler = $handler;
+ }
+
+ public function run()
+ {
+ $this->connect($this->config->host, $this->config->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->handler->handle($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);
+ }
+
+}
+
+?>