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->event = $this->server->event("server.tick", array($this, "handle"));
$this->loop = new EventBase(); $this->loop = new ConsoleLoop();
$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->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,29 +48,11 @@ 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(); $this->loop->notify();
}else{ //$this->loop->join();
$this->server->deleteEvent($this->event);
$this->loop->stop = true;
$this->loop->notify();
//$this->loop->join();
}
} }
public function defaultCommands($cmd, $params, $issuer, $alias){ public function defaultCommands($cmd, $params, $issuer, $alias){
@ -247,22 +223,18 @@ class ConsoleAPI{
} }
public function handle($time){ public function handle($time){
if(HAS_EVENT){ if($this->loop->line !== false){
$this->loop->loop(EventBase::LOOP_NONBLOCK); $line = trim($this->loop->line);
}else{ $this->loop->line = false;
if($this->loop->line !== false){ $output = $this->run($line, "console");
$line = trim($this->loop->line); if($output != ""){
$this->loop->line = false; $mes = explode("\n", trim($output));
$output = $this->run($line, "console"); foreach($mes as $m){
if($output != ""){ console("[CMD] ".$m);
$mes = explode("\n", trim($output));
foreach($mes as $m){
console("[CMD] ".$m);
}
} }
}else{
$this->loop->notify();
} }
}else{
$this->loop->notify();
} }
} }
@ -271,20 +243,43 @@ class ConsoleAPI{
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;
} }
@fclose($fp); }
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);
}
exit(0); exit(0);
} }
} }