Example #1
0
		/**
		Select optimal server for group
		@access public
		@param string $group
		@return array $server
		*/
		public function GetOptimalServer($group)
		{	
			foreach ($this->Servers as $server=>$score)
			{
			    $score = (int)$score;
				if ($score > 1)
				{
					if($this->NNTP->Connect($server, $this->AuthData[$server]["port"], $this->AuthData[$server]["login"], $this->AuthData[$server]["password"], 5))
					{
					    Log::Log("Connect to {$server} on port {$this->AuthData[$server]["port"]} established ({$this->AuthData[$server]["login"]}:{$this->AuthData[$server]["password"]})", 1, "NNTPLog");
						$res = $this->NNTP->SelectGroup($group);
						
						Log::Log("Found {$res["count"]} messages in group {$group}", 1, "NNTPLog");
						
						$results[$server]["score"] = $score;	
						$results[$server]["load"] = $this->DB->GetOne("SELECT COUNT(*) FROM newsgroups WHERE serverid='{$this->AuthData[$server]['id']}'");	
						
						if ($res)
							$results[$server]["count"] = $res["count"];
						else
							$results[$server]["count"] = 0;
						
						$this->NNTP->Disconnect();
					}
					else
					{ 
						Log::Log("Cannot connect to {$server} on port {$this->AuthData[$server]["port"]} ({$this->AuthData[$server]["login"]}:{$this->AuthData[$server]["password"]})", 1, "NNTPLog");
					    $results[$server]["count"] = 0;
					}
				}
			}
			
			$total = array();
			foreach ((array)$results as $server=>$v)
			{
				if ($v["count"]>0)
				{
					$score = $v["count"]*$v["score"];
					
					if ($v["load"] > 0)
						$score = $score/$v["load"];
						
					$total[$server] = $score;
				}
			}
						
			if (count($total)>0)
			{
				asort($total);
				$total = array_reverse($total);
				$servers = array_keys($total);
				$srv = array_shift($servers);
				
				return $this->AuthData[$srv];
			}
			
			return false;
		}