throw exception when connection to switchboard cannot be established
authorChristian Weiske <cweiske@cweiske.de>
Wed, 8 Aug 2012 04:42:59 +0000 (06:42 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Wed, 8 Aug 2012 04:42:59 +0000 (06:42 +0200)
src/callnotifier/CLI.php
src/callnotifier/Source/Remote.php

index 7109e7382a0747aec082fea6ef1efdf3017f71fd..d0b15acf420ba4ab154c215c4403c40e1c504fea 100644 (file)
@@ -5,7 +5,6 @@ namespace callnotifier;
 class CLI
 {
     protected $cliParser;
-    protected $config;
 
     public function __construct()
     {
@@ -14,14 +13,14 @@ class CLI
 
     public function run()
     {
-        $this->config = new Config();
+        $config = new Config();
         try {
             $result = $this->cliParser->parse();
         } catch (\Exception $exc) {
             $this->cliParser->displayError($exc->getMessage());
         }
 
-        $this->fillConfig($this->config, $result);
+        $this->fillConfig($config, $result);
 
         $log = new Log();
         if ($result->options['debug'] || $result->options['debugEdss1']) {
@@ -31,57 +30,32 @@ class CLI
                 $debugLogger->edss1MsgOnly = true;
             }
         }
-        $log->addLogger(
-            new Logger_CallEcho(), array('startingCall', 'finishedCall')
-        );
 
-        $callMonitor = new CallMonitor($this->config, $log);
-        /*
-        $callMonitor->addDetailler(
-            new CallMonitor_Detailler_LDAP(
-                array(
-                    'host' => 'ldap.home.cweiske.de',
-                    'basedn' => 'ou=adressbuch,dc=cweiske,dc=de',
-                    'binddn' => 'cn=readonly,ou=users,dc=cweiske,dc=de',
-                    'bindpw' => 'readonly'
-                )
-            )
-        );
-        $callMonitor->addDetailler(
-            new CallMonitor_Detailler_OpenGeoDb(
-                'mysql:host=dojo;dbname=opengeodb',
-                'opengeodb-read',
-                'opengeodb'
-            )
-        );
-        */
+        $callMonitor = new CallMonitor($config, $log);
 
-        $log->addLogger(
-            new Logger_CallFile('incoming.log', 'i', '40862'),
-            array('finishedCall')
-        );
-        $log->addLogger(
-            new Logger_CallFile('all.log'),
-            array('finishedCall')
-        );
-        $log->addLogger(
-            new Logger_CallDb(
-                'mysql:host=localhost;dbname=callnotifier',
-                'callnotifier',
-                'callnotifier'
-            ),
-            array('finishedCall')
-        );
+        $configFile = $this->getConfigFile();
+        if ($configFile !== null) {
+            include $configFile;
+        }
 
-        $handler = new MessageHandler($this->config, $log, $callMonitor);
+        $handler = new MessageHandler($config, $log, $callMonitor);
 
-        if ($this->config->replayFile !== null) {
+        if ($config->replayFile !== null) {
             $sourceClass = 'callnotifier\Source_File';
         } else {
             $sourceClass = 'callnotifier\Source_Remote';
         }
-        $source = new $sourceClass($this->config, $handler);
-        $source->run();
+
+        try {
+            $source = new $sourceClass($config, $handler);
+            $source->run();
+        } catch (\Exception $e) {
+            $msg = 'Callnotifier error!' . "\n"
+                . 'Code: ' . $e->getCode() . "\n"
+                . 'Message: ' . $e->getMessage() . "\n";
+            file_put_contents('php://stderr', $msg);
+            exit(1);
+        }
     }
 
     public function setupCli()
@@ -148,6 +122,39 @@ class CLI
         $config->setIfNotEmpty('dumpFile', $result->options['dumpFile']);
         $config->setIfNotEmpty('replayFile', $result->options['replayFile']);
     }
+
+    /**
+     * Finds the path to the configuration file.
+     *
+     * The following locations are tried:
+     * - Git checkout: data/callnotifier.config.php
+     * - ~/.config/callnotifier.config.php
+     * - /etc/callnotifier.config.php
+     *
+     * @return string Full path of config file or NULL if no file found
+     */
+    protected function getConfigFile()
+    {
+        if (basename(dirname(__DIR__)) == 'src'
+            && file_exists(__DIR__ . '/../../data/callnotifier.config.php')
+        ) {
+            return __DIR__ . '/../../data/callnotifier.config.php';
+        }
+
+        if (isset($_ENV['HOME'])) {
+            $file = $_ENV['HOME'] . '/.config/callnotifier.config.php';
+            if (file_exists($file)) {
+                return $file;
+            }
+        }
+
+        $file = '/etc/callnotifier.config.php';
+        if (file_exists($file)) {
+            return $file;
+        }
+
+        return null;
+    }
 }
 
 ?>
\ No newline at end of file
index 36e468dc1b5b98b0d698ba688d0e3c187647d9ea..39b042c966de7a3f1958ad5b4c80026e3c2e4e1f 100644 (file)
@@ -21,20 +21,24 @@ class Source_Remote
 
     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) {
-            echo "socket_create() failed: reason: "
-                . socket_strerror(socket_last_error()) . "\n";
-        } else {
-            echo "OK.\n";
+            throw new \Exception(
+                'socket_create() failed: reason: '
+                . socket_strerror(socket_last_error())
+            );
         }
-        echo "Attempting to connect to '$ip' on port '$port'...";
+        //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";
+            throw new \Exception(
+                "socket_connect() failed. Reason: "
+                . socket_strerror(socket_last_error($socket))
+            );
         }
 
         $this->socket = $socket;