dumping network data is possible now
[auerswald-callnotifier.git] / src / callnotifier / MessageHandler.php
1 <?php
2 namespace callnotifier;
3
4 class MessageHandler
5 {
6     protected $dumpHdl;
7
8
9     public function __construct($config)
10     {
11         $this->config = $config;
12         $this->prepareDump();
13     }
14
15     public function handle($msg)
16     {
17         if ($this->config->dumpFile !== null) {
18             $this->dump($msg);
19         }
20         if (substr($msg, 0, 9) != '[DKANPROT') {
21             //unknown message type
22             return;
23         }
24         $regex = '#^\\[DKANPROT-([^ ]+) ([0-9]+)\\] (.*)$#';
25         if (!preg_match($regex, $msg, $matches)) {
26             //message should always be that way
27             return false;
28         }
29         list(, $type, $someid, $details) = $matches;
30
31         if ($type != 'Info') {
32             //we only want info messages
33             var_dump($type . ': ' . $details);
34             return;
35         }
36         //Vegw/Ets-Cref:[0xffef]/[0x64] - VEGW_SETUP from upper layer to internal destination: CGPN[**22]->CDPN[41], 
37         var_dump($details);
38         $regex = '#CGPN\\[([^\\]]+)\\]->CDPN\\[([^\\]]+)\\]#';
39         if (preg_match($regex, $details, $matches)) {
40             var_dump('a call!', $matches);
41         }
42     }
43
44     protected function prepareDump()
45     {
46         if ($this->config->dumpFile === null) {
47             return;
48         }
49         $this->dumpHdl = fopen($this->config->dumpFile, 'w');
50         if (!$this->dumpHdl) {
51             throw new Exception('Cannot open replay file for reading');
52         }
53     }
54
55     protected function dump($msg)
56     {
57         fwrite($this->dumpHdl, $msg . "\n");
58     }
59 }
60
61 ?>