New Event ConsoleLoop

This commit is contained in:
Shoghi Cervantes Pueyo 2013-04-27 00:39:59 +02:00
parent 3cae25cebd
commit d7a54123e2

View File

@ -36,14 +36,8 @@ class ConsoleAPI{
} }
public function init(){ public function init(){
if(HAS_EVENT){
$this->loop = new EventBase();
$event = new Event($this->loop, STDIN, Event::READ | Event::PERSIST, array($this, "readLine"));
$event->add();
}else{
$this->event = $this->server->event("server.tick", array($this, "handle")); $this->event = $this->server->event("server.tick", array($this, "handle"));
$this->loop = new ConsoleLoop(); $this->loop = new ConsoleLoop();
}
$this->register("help", "[page|command name]", array($this, "defaultCommands")); $this->register("help", "[page|command name]", array($this, "defaultCommands"));
$this->register("status", "", array($this, "defaultCommands")); $this->register("status", "", array($this, "defaultCommands"));
$this->register("difficulty", "<0|1|2>", array($this, "defaultCommands")); $this->register("difficulty", "<0|1|2>", array($this, "defaultCommands"));
@ -54,30 +48,12 @@ class ConsoleAPI{
$this->server->api->ban->cmdWhitelist("help"); $this->server->api->ban->cmdWhitelist("help");
} }
public function readLine($fd, $events){
$line = trim(fgets($fd));
if($line != ""){
$output = $this->run($line, "console");
if($output != ""){
$mes = explode("\n", trim($output));
foreach($mes as $m){
console("[CMD] ".$m);
}
}
}
}
function __destruct(){ function __destruct(){
if(HAS_EVENT){
$this->server->deleteEvent($this->event); $this->server->deleteEvent($this->event);
$this->loop->stop(); $this->loop->stop();
}else{
$this->server->deleteEvent($this->event);
$this->loop->stop = true;
$this->loop->notify(); $this->loop->notify();
//$this->loop->join(); //$this->loop->join();
} }
}
public function defaultCommands($cmd, $params, $issuer, $alias){ public function defaultCommands($cmd, $params, $issuer, $alias){
$output = ""; $output = "";
@ -247,9 +223,6 @@ class ConsoleAPI{
} }
public function handle($time){ public function handle($time){
if(HAS_EVENT){
$this->loop->loop(EventBase::LOOP_NONBLOCK);
}else{
if($this->loop->line !== false){ if($this->loop->line !== false){
$line = trim($this->loop->line); $line = trim($this->loop->line);
$this->loop->line = false; $this->loop->line = false;
@ -264,27 +237,49 @@ class ConsoleAPI{
$this->loop->notify(); $this->loop->notify();
} }
} }
}
} }
class ConsoleLoop extends Thread{ class ConsoleLoop extends Thread{
public $line; public $line;
public $stop; public $stop;
public $base;
public $ev;
public function __construct(){ public function __construct(){
$this->line = false; $this->line = false;
$this->stop = false; $this->stop = false;
$this->start(); $this->start();
} }
public function run(){ public function stop(){
$fp = fopen("php://stdin", "r"); $this->stop = true;
while($this->stop === false and ($line = fgets($fp)) !== false){ if(HAS_EVENT){
$this->base->stop();
}
}
public function readLine($fp, $events = null){
$line = trim(fgets($fp));
if($line != ""){
$this->line = $line; $this->line = $line;
$this->wait(); $this->wait();
$this->line = false; $this->line = false;
} }
}
public function run(){
if(HAS_EVENT){
$this->base = new EventBase();
$this->ev = new Event($this->base, STDIN, Event::READ | Event::PERSIST, array($this, "readLine"));
$this->ev->add();
$this->base->loop();
}else{
$fp = fopen("php://stdin", "r");
while($this->stop === false){
$this->readLine($fp);
}
@fclose($fp); @fclose($fp);
}
exit(0); exit(0);
} }
} }