Improved unique id generation for server

This commit is contained in:
Shoghi Cervantes 2015-05-21 12:59:33 +02:00
parent cde2d39029
commit 46f20d36b3
No known key found for this signature in database
GPG Key ID: 78464DB0A7837F89
4 changed files with 44 additions and 12 deletions

View File

@ -346,6 +346,10 @@ class Server{
return $this->getConfigString("motd", "Minecraft: PE Server");
}
public function getServerUniqueId(){
return $this->serverID;
}
/**
* @return bool
*/
@ -1638,7 +1642,10 @@ class Server{
$this->logger->info($this->getLanguage()->translateString("pocketmine.server.networkStart", [$this->getIp() === "" ? "*" : $this->getIp(), $this->getPort()]));
define("BOOTUP_RANDOM", @Utils::getRandomBytes(16));
$this->serverID = Utils::getServerUniqueId($this->getIp() . $this->getPort());
$this->serverID = Utils::getMachineUniqueId($this->getIp() . $this->getPort());
$this->getLogger()->debug("Server unique id: " . $this->getServerUniqueId());
$this->getLogger()->debug("Machine unique id: " . Utils::getMachineUniqueId());
$this->network = new Network($this);
$this->network->setName($this->getMotd());

View File

@ -45,6 +45,8 @@ class GarbageCollectorCommand extends VanillaCommand{
$entitiesCollected = 0;
$tilesCollected = 0;
$memory = memory_get_usage();
foreach($sender->getServer()->getLevels() as $level){
$diff = [count($level->getChunks()), count($level->getEntities()), count($level->getTiles())];
$level->doChunkGarbageCollection();
@ -62,6 +64,7 @@ class GarbageCollectorCommand extends VanillaCommand{
$sender->sendMessage(TextFormat::GOLD . "Tiles: " . TextFormat::RED . number_format($tilesCollected));
$sender->sendMessage(TextFormat::GOLD . "Cycles: " . TextFormat::RED . number_format($cyclesCollected));
$sender->sendMessage(TextFormat::GOLD . "Memory freed: " . TextFormat::RED . number_format(round((($memory - memory_get_usage()) / 1024) / 1024, 2))." MB");
return true;
}
}

View File

@ -40,8 +40,9 @@ class SendUsageTask extends AsyncTask{
$path = "post";
$data = [];
$data["uniqueServerId"] = Utils::getServerUniqueId();
$data["uniqueRequestId"] = Utils::dataToUUID(Utils::getServerUniqueId(), microtime(true));
$data["uniqueServerId"] = $server->getServerUniqueId();
$data["uniqueMachineId"] = Utils::getMachineUniqueId();
$data["uniqueRequestId"] = Utils::dataToUUID($server->getServerUniqueId(), microtime(true));
switch($type){
case self::TYPE_OPEN:

View File

@ -78,13 +78,13 @@ class Utils{
*
* @return string
*/
public static function getServerUniqueId($extra = ""){
if(self::$serverUniqueId !== null){
public static function getMachineUniqueId($extra = ""){
if(self::$serverUniqueId !== null and $extra === ""){
return self::$serverUniqueId;
}
$machine = php_uname("a");
$machine .= file_exists("/proc/cpuinfo") ? implode(preg_grep("/model name/", file("/proc/cpuinfo"))) : "";
$machine .= file_exists("/proc/cpuinfo") ? implode(preg_grep("/(model name|Processor|Serial)/", file("/proc/cpuinfo"))) : "";
$machine .= sys_get_temp_dir();
$machine .= $extra;
$os = Utils::getOS();
@ -110,6 +110,11 @@ class Utils{
}
$machine .= implode(" ", $matches[1]); //Mac Addresses
}
$machine .= file_exists("/etc/machine-id") ? file_get_contents("/etc/machine-id") : "";
}elseif($os === "android"){
$machine .= @file_get_contents("/system/build.prop");
}elseif($os === "mac"){
$machine .= `system_profiler SPHardwareDataType | grep UUID`;
}
$data = $machine . PHP_MAXPATHLEN;
$data .= PHP_INT_MAX;
@ -119,7 +124,13 @@ class Utils{
$data .= $ext . ":" . phpversion($ext);
}
return self::$serverUniqueId = Utils::dataToUUID($machine, $data);
$uuid = Utils::dataToUUID($machine, $data);
if($extra === ""){
self::$serverUniqueId = $uuid;
}
return $uuid;
}
/**
@ -245,8 +256,15 @@ class Utils{
return count(ThreadManager::getInstance()->getAll()) + 3; //RakLib + MainLogger + Main Thread
}
public static function getCoreCount(){
public static function getCoreCount($recalculate = false){
static $processors = 0;
if($processors > 0 and !$recalculate){
return $processors;
}else{
$processors = 0;
}
switch(Utils::getOS()){
case "linux":
case "android":
@ -256,13 +274,16 @@ class Utils{
++$processors;
}
}
}else{
if(preg_match("/^([0-9]+)\\-([0-9]+)$/", trim(@file_get_contents("/sys/devices/system/cpu/present")), $matches) > 0){
$processors = (int) ($matches[2] - $matches[1]);
}
}
break;
case "bsd":
$processors = (int) `sysctl hw.ncpu | awk '{ print $2+1 }'`;
break;
case "mac":
$processors = (int) `sysctl hw.availcpu | awk '{ print $2+1 }'`;
$processors = (int) `sysctl -n hw.ncpu`;
$processors = (int) `sysctl -n hw.ncpu`;
break;
case "win":
$processors = (int) getenv("NUMBER_OF_PROCESSORS");