fix copied error message
[auerswald-callnotifier.git] / src / callnotifier / MessageHandler.php
index 6181e3a39a5e15545c17d49415278e7ce852e877..78a726de60ada2566d71b3bc4263a07e11b0844a 100644 (file)
@@ -5,48 +5,13 @@ class MessageHandler
 {
     protected $dumpHdl;
 
 {
     protected $dumpHdl;
 
-    /**
-     * Array of logger object arrays.
-     * Key is the notification type, value is an array of logger objects
-     * that want to get notified about the type.
-     *
-     * @var array
-     */
-    protected $logger = array(
-        'msgData' => array(),
-        'incomingCall' => array(),
-        'edss1msg' => array(),
-    );
-
 
 
-    public function __construct($config)
+    public function __construct($config, $log, $callMonitor)
     {
         $this->config = $config;
         $this->prepareDump();
     {
         $this->config = $config;
         $this->prepareDump();
-    }
-
-    /**
-     * Add a logger
-     *
-     * @param Logger       $logger Logger object to register
-     * @param array|string $types  Single notification type or array of such
-     *                             types. "*" means "register for all types".
-     *
-     * @return self
-     */
-    public function addLogger(Logger $logger, $types)
-    {
-        if ($types == '*') {
-            $types = array_keys($this->logger);
-        }
-        $types = (array)$types;
-
-        foreach ($types as $type) {
-            if (!isset($this->logger[$type])) {
-                throw new \Exception('Unknown log type: ' . $type);
-            }
-            $this->logger[$type][] = $logger;
-        }
+        $this->log = $log;
+        $this->callMonitor = $callMonitor;
     }
 
     public function handle($msg)
     }
 
     public function handle($msg)
@@ -64,7 +29,7 @@ class MessageHandler
             return false;
         }
         list(, $type, $someid, $details) = $matches;
             return false;
         }
         list(, $type, $someid, $details) = $matches;
-        $this->log(
+        $this->log->log(
             'msgData',
             array(
                 'type' => $type,
             'msgData',
             array(
                 'type' => $type,
@@ -73,15 +38,12 @@ class MessageHandler
             )
         );
 
             )
         );
 
-        if ($type != 'Info') {
-            $this->parseEDSS1($details);
-            return;
-        }
-        //Vegw/Ets-Cref:[0xffef]/[0x64] - VEGW_SETUP from upper layer to internal destination: CGPN[**22]->CDPN[41], 
-        $regex = '#CGPN\\[([^\\]]+)\\]->CDPN\\[([^\\]]+)\\]#';
-        if (preg_match($regex, $details, $matches)) {
-            list(, $from, $to) = $matches;
-            $this->log('incomingCall', array('from' => $from, 'to' => $to));
+        if ($type == 'Debug') {
+            $msg = $this->parseEDSS1($details);
+            if (is_object($msg)) {
+                $this->log->log('edss1msg', array('msg' => $msg));
+                $this->callMonitor->handle($msg);
+            }
         }
     }
 
         }
     }
 
@@ -90,6 +52,7 @@ class MessageHandler
      *
      * @param string $details Detail string of a debug message
      *
      *
      * @param string $details Detail string of a debug message
      *
+     * @return EDSS1_Message The retrieved message, NULL if none.
      */
     protected function parseEDSS1($details)
     {
      */
     protected function parseEDSS1($details)
     {
@@ -103,38 +66,19 @@ class MessageHandler
         }
         
         $bytestring = substr($details, 5);
         }
         
         $bytestring = substr($details, 5);
-        $bytes = '';
-        foreach (explode(' ', $bytestring) as $strbyte) {
-            $bytes .= chr(hexdec($strbyte));
-        }
-
-        $msgtype = $bytes{7};
-        static $interestingTyps = array(
-            EDSS1_Message::SETUP,
-            EDSS1_Message::CONNECT,
-            EDSS1_Message::INFORMATION
-        );
-        if (!in_array($msgtype, $interestingTyps)) {
-            //return;
-        }
+        $bytes = static::getBytesFromHexString($bytestring);
 
         $mp = new EDSS1_Parser();
 
         $mp = new EDSS1_Parser();
-        $msg = $mp->parse($bytes);
-
-        $this->log('edss1msg', array('msg' => $msg));
+        return $mp->parse($bytes);
     }
 
     }
 
-    protected function log($type, $arData)
+    public static function getBytesFromHexString($bytestring)
     {
     {
-        if (!isset($this->logger[$type])) {
-            throw new \Exception('Unknown log type: ' . $type);
-        }
-        
-        if (count($this->logger[$type])) {
-            foreach ($this->logger[$type] as $logger) {
-                $logger->log($type, $arData);
-            }
+        $bytes = '';
+        foreach (explode(' ', $bytestring) as $strbyte) {
+            $bytes .= chr(hexdec($strbyte));
         }
         }
+        return $bytes;
     }
 
     protected function prepareDump()
     }
 
     protected function prepareDump()
@@ -144,7 +88,7 @@ class MessageHandler
         }
         $this->dumpHdl = fopen($this->config->dumpFile, 'w');
         if (!$this->dumpHdl) {
         }
         $this->dumpHdl = fopen($this->config->dumpFile, 'w');
         if (!$this->dumpHdl) {
-            throw new Exception('Cannot open replay file for reading');
+            throw new Exception('Cannot open dump file for writing');
         }
     }
 
         }
     }