/
EventListenerImpl.class.php
52 lines (41 loc) · 1.22 KB
/
EventListenerImpl.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
require_once dirname(__FILE__) . "/ThreadImpl.class.php";
require_once dirname(__FILE__) . "/EventListener.php";
require_once dirname(__FILE__) . "/EventHandler.php";
class EventListenerImpl extends ThreadImpl implements EventListener {
private $eventList = array();
private $handler;
private $runListener = true;
private $sleepTime = 0;
public function __construct(EventHandler $handler){
parent::__construct();
$this->handler = $handler;
}
public function run(){
while(true){
if($this->runListener){
if(0 < count($this->eventList)){
$event = array_pop($this->eventList);
$this->handler->handle($event);
} else {
$this->sleep();
}
}
}
}
public function getName(){
return __CLASS__;
}
public function handleEvent(Event $event){
$this->eventList []= $event;
}
public function stopListener(){
$this->runListener = false;
}
public function setSleep($msecond){
$this->sleepTime = $msecond;
}
protected function sleep(){
usleep($this->sleepTime * 1000);
}
}