1
|
|
|
<?php |
2
|
|
|
namespace PHPDaemon\Examples; |
3
|
|
|
|
4
|
|
|
use PHPDaemon\Clients\Asterisk\Pool; |
5
|
|
|
use PHPDaemon\Core\AppInstance; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @package Examples |
9
|
|
|
* @subpackage Asterisk |
10
|
|
|
* |
11
|
|
|
* @author TyShkan <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class ExampleAsteriskClient extends AppInstance |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
public $asteriskclient; |
17
|
|
|
|
18
|
|
|
public $asteriskconn; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Constructor. |
22
|
|
|
* @return void |
23
|
|
|
*/ |
24
|
|
|
public function init() |
25
|
|
|
{ |
26
|
|
|
if ($this->isEnabled()) { |
27
|
|
|
$this->asteriskclient = Pool::getInstance($this->config->asteriskclientname->value); |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Called when the worker is ready to go. |
33
|
|
|
* @return void |
34
|
|
|
*/ |
35
|
|
|
public function onReady() |
36
|
|
|
{ |
37
|
|
|
if ($this->asteriskclient) { |
38
|
|
|
$this->asteriskclient->onReady(); |
39
|
|
|
$this->connect(); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function connect() |
44
|
|
|
{ |
45
|
|
|
$this->asteriskclient->getConnection($this->config->url->value, function ($conn) { |
46
|
|
|
$this->asteriskconn = $conn; |
47
|
|
|
if ($conn->connected) { |
48
|
|
|
$conn->bind('disconnect', function ($conn) { |
|
|
|
|
49
|
|
|
\PHPDaemon\Core\Daemon::log('Connection lost... Reconnect in ' . $this->config->reconnect->value . ' sec'); |
50
|
|
|
$this->connect(); |
51
|
|
|
}); |
52
|
|
|
} else { |
53
|
|
|
\PHPDaemon\Core\Daemon::log(get_class($this) . ': couldn\'t connect to ' . $this->config->url->value); |
54
|
|
|
} |
55
|
|
|
}); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Called when application instance is going to shutdown. |
60
|
|
|
* @return boolean Ready to shutdown? |
61
|
|
|
*/ |
62
|
|
|
public function onShutdown($graceful = false) |
63
|
|
|
{ |
64
|
|
|
if ($this->asteriskclient) { |
65
|
|
|
return $this->asteriskclient->onShutdown(); |
66
|
|
|
} |
67
|
|
|
return true; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Setting default config options |
72
|
|
|
* Overriden from AppInstance::getConfigDefaults |
73
|
|
|
* @return array|false |
74
|
|
|
*/ |
75
|
|
|
protected function getConfigDefaults() |
76
|
|
|
{ |
77
|
|
|
return [ |
78
|
|
|
'url' => 'tcp://user:password@localhost:5038', |
79
|
|
|
'reconnect' => 1, |
80
|
|
|
'asteriskclient-name' => '' |
81
|
|
|
]; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.