diff --git a/src/Monitor.php b/src/Monitor.php index f4366bd..20534fc 100644 --- a/src/Monitor.php +++ b/src/Monitor.php @@ -3,25 +3,32 @@ namespace Laysense\Monitor; use WebSocket\Client; -class Detection +class Detect { var $dest='ws://172.83.153.167:2345'; var $nodeinfo; - public function getNode() + var $ping; + var $delay; + public function node() { $nodeinfo = $this->callWebSocet('{"msg":"getnodelist"}'); $this->nodeinfo=$nodeinfo; } + public function getNode(){ + if(!isset($this->nodeinfo)){ + $this->node(); + } + return $this->nodeinfo; + } public function ping($host,$count) { + if(!isset($this->nodeinfo)){ + $this->node(); + } $ping = $this->callWebSocet('{"msg":"ping","target:'.$host.'","type":"ICMP","count":'.$count.'}'); foreach($this->nodeinfo as $node){ $ping[$node->nodeid]['nodeinfo']=$node; } - $this->ping=$ping; - } - public function getDelay() - { $alldelay = 0; foreach($this->ping as $id=>$node){ if((!isset($node['nodeinfo'])) || (!isset($node['delay'])) || $node['delay']<=10){ @@ -30,8 +37,15 @@ class Detection $alldelay += $node['delay']; } } - $alldelay = $alldelay/count($this->ping); - return $alldelay; + $this->ping=$ping; + $this->delay=$alldelay/count($this->ping); + } + public function getDelay() + { + if(!isset($this->delay)){ + $this->ping(); + } + return $this->delay; } private function callWebSocet(){ $client = new Client($this->dest);