1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Pomm's Foundation package. |
4
|
|
|
* |
5
|
|
|
* (c) 2014 - 2015 Grégoire HUBERT <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace PommProject\Foundation\Listener; |
11
|
|
|
|
12
|
|
|
use PommProject\Foundation\Client\ClientPoolerInterface; |
13
|
|
|
use PommProject\Foundation\Client\ClientPooler; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* ListenerPooler |
17
|
|
|
* |
18
|
|
|
* Pooler for listener clients. |
19
|
|
|
* |
20
|
|
|
* @package Foundation |
21
|
|
|
* @copyright 2014 - 2015 Grégoire HUBERT |
22
|
|
|
* @author Grégoire HUBERT |
23
|
|
|
* @license X11 {@link http://opensource.org/licenses/mit-license.php} |
24
|
|
|
* @see ClientPooler |
25
|
|
|
*/ |
26
|
|
|
class ListenerPooler extends ClientPooler |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* getPoolerType |
30
|
|
|
* |
31
|
|
|
* @see ClientPoolerInterface |
32
|
|
|
*/ |
33
|
|
|
public function getPoolerType() |
34
|
|
|
{ |
35
|
|
|
return 'listener'; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* createClient |
41
|
|
|
* |
42
|
|
|
* See @ClientPooler |
43
|
|
|
*/ |
44
|
|
|
protected function createClient($identifier) |
45
|
|
|
{ |
46
|
|
|
return new Listener($identifier); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* notify |
51
|
|
|
* |
52
|
|
|
* Send a notification to clients. |
53
|
|
|
* Client identifiers may be a single client name, an array of client or |
54
|
|
|
* '*' to notify all clients. |
55
|
|
|
* Event name may use ':' to split indicate additional information (ie type |
56
|
|
|
* of payload). Events sent to 'pika', 'pika:chu' will both notify client |
57
|
|
|
* 'pika'. |
58
|
|
|
* |
59
|
|
|
* @access public |
60
|
|
|
* @param string|array $identifiers |
61
|
|
|
* @param array $data |
62
|
|
|
* @return ListenerPooler $this |
63
|
|
|
*/ |
64
|
|
|
public function notify($identifiers, array $data) |
65
|
|
|
{ |
66
|
|
|
$this->getSession()->hasLogger() && |
67
|
|
|
$this->getSession()->getLogger()->debug( |
68
|
|
|
"Pomm: ListenerPooler: notification received.", |
69
|
|
|
[ |
70
|
|
|
'receivers' => $identifiers, |
71
|
|
|
] |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
if (is_scalar($identifiers)) { |
75
|
|
|
if ($identifiers === '*') { |
76
|
|
|
return $this->notifyAll($data); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$identifiers = [ $identifiers ]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $this->notifyClients($identifiers, $data); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* notifyAll |
87
|
|
|
* |
88
|
|
|
* Notify all existing clients. |
89
|
|
|
* |
90
|
|
|
* @access protected |
91
|
|
|
* @param array $data |
92
|
|
|
* @return ListenerPooler $this |
93
|
|
|
*/ |
94
|
|
|
protected function notifyAll(array $data) |
95
|
|
|
{ |
96
|
|
|
foreach ($this |
97
|
|
|
->getSession() |
98
|
|
|
->getAllClientForType($this->getPoolerType()) as $client) { |
99
|
|
|
$client->notify('*', $data); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* notifyClients |
107
|
|
|
* |
108
|
|
|
* Send a notification to the specified clients. |
109
|
|
|
* |
110
|
|
|
* @access protected |
111
|
|
|
* @param array $identifiers |
112
|
|
|
* @param array $data |
113
|
|
|
* @return ListenerPooler $this |
114
|
|
|
*/ |
115
|
|
|
protected function notifyClients(array $identifiers, array $data) |
116
|
|
|
{ |
117
|
|
|
foreach ($identifiers as $identifier) { |
118
|
|
|
$client_name = strpos($identifier, ':') !== false |
119
|
|
|
? substr($identifier, 0, strpos($identifier, ':')) |
120
|
|
|
: $identifier |
121
|
|
|
; |
122
|
|
|
|
123
|
|
|
$client = $this |
124
|
|
|
->getSession() |
125
|
|
|
->getClient($this->getPoolerType(), $client_name) |
126
|
|
|
; |
127
|
|
|
|
128
|
|
|
if ($client !== null) { |
129
|
|
|
$client->notify($identifier, $data); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|