-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added getUriList and setUriList method.
- Loading branch information
Showing
1 changed file
with
39 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ | |
* * | ||
* hprose client class for php 5.3+ * | ||
* * | ||
* LastModified: Sep 4, 2016 * | ||
* LastModified: Sep 5, 2016 * | ||
* Author: Ma Bingyao <[email protected]> * | ||
* * | ||
\**********************************************************/ | ||
|
@@ -31,9 +31,9 @@ | |
|
||
abstract class Client extends HandlerManager { | ||
private $index = -1; | ||
private $uriList = null; | ||
protected $async = true; | ||
public $uri = null; | ||
public $uris = null; | ||
public $filters = array(); | ||
public $timeout = 30000; | ||
public $retry = 10; | ||
|
@@ -71,32 +71,29 @@ private static function initClientFactories() { | |
self::$clientFactoriesInited = true; | ||
} | ||
|
||
public static function create($uris, $async = true) { | ||
public static function create($uriList, $async = true) { | ||
if (!self::$clientFactoriesInited) self::initClientFactories(); | ||
if (is_string($uris)) $uris = array($uris); | ||
$scheme = strtolower(parse_url($uris[0], PHP_URL_SCHEME)); | ||
$n = count($uris); | ||
if (is_string($uriList)) $uriList = array($uriList); | ||
$scheme = strtolower(parse_url($uriList[0], PHP_URL_SCHEME)); | ||
$n = count($uriList); | ||
for ($i = 1; $i < $n; ++$i) { | ||
if (strtolower(parse_url($uris[$i], PHP_URL_SCHEME)) != $scheme) { | ||
if (strtolower(parse_url($uriList[$i], PHP_URL_SCHEME)) != $scheme) { | ||
throw new Exception("Not support multiple protocol."); | ||
} | ||
} | ||
$clientFactory = self::$clientFactories[$scheme]; | ||
if (empty($clientFactory)) { | ||
throw new Exception("This client doesn't support $scheme scheme."); | ||
} | ||
return new $clientFactory($uris, $async); | ||
return new $clientFactory($uriList, $async); | ||
} | ||
|
||
public function __construct($uris = null, $async = true) { | ||
public function __construct($uriList = null, $async = true) { | ||
parent::__construct(); | ||
if ($uris != null) { | ||
if (is_string($uris)) $uris = array($uris); | ||
if (is_array($uris)) { | ||
$this->useService($uris); | ||
} | ||
if (is_bool($uris)) { | ||
$async = $uris; | ||
if ($uriList != null) { | ||
$this->setUriList($uriList); | ||
if (is_bool($uriList)) { | ||
$async = $uriList; | ||
} | ||
} | ||
$this->async = $async; | ||
|
@@ -202,18 +199,29 @@ protected function setUri($uri) { | |
$this->uri = $uri; | ||
} | ||
|
||
public function useService($uris = array(), $namespace = '') { | ||
if (!empty($uris)) { | ||
if (is_string($uris)) { | ||
$uris = array($uris); | ||
} | ||
else { | ||
shuffle($uris); | ||
} | ||
$this->index = 0; | ||
$this->failround = 0; | ||
$this->uris = $uris; | ||
$this->setUri($uris[$this->index]); | ||
public function getUriList() { | ||
return $this->uriList; | ||
} | ||
|
||
public function setUriList($uriList) { | ||
if (is_string($uriList)) { | ||
$uriList = array($uriList); | ||
} | ||
else if (is_array($uriList)) { | ||
shuffle($uriList); | ||
} | ||
else { | ||
return; | ||
} | ||
$this->index = 0; | ||
$this->failround = 0; | ||
$this->uriList = $uriList; | ||
$this->setUri($uriList[$this->index]); | ||
} | ||
|
||
public function useService($uriList = array(), $namespace = '') { | ||
if (!empty($uriList)) { | ||
$this->setUriList($uriList); | ||
} | ||
if ($namespace) { | ||
$namespace .= "_"; | ||
|
@@ -252,15 +260,15 @@ protected function wait($interval, $callback) { | |
} | ||
|
||
private function failswitch() { | ||
$n = count($this->uris); | ||
$n = count($this->uriList); | ||
if ($n > 1) { | ||
$i = $this->index + 1; | ||
if ($i >= $n) { | ||
$i = 0; | ||
$this->failround++; | ||
} | ||
$this->index = $i; | ||
$this->setUri($this->uris[$i]); | ||
$this->setUri($this->uriList[$i]); | ||
} | ||
else { | ||
$this->failround++; | ||
|
@@ -283,7 +291,7 @@ private function failswitch() { | |
if ($context->idempotent && ($context->retried < $context->retry)) { | ||
$interval = ++$context->retried * 0.5; | ||
if ($context->failswitch) { | ||
$interval -= (count($this->uris) - 1) * 0.5; | ||
$interval -= (count($this->uriList) - 1) * 0.5; | ||
} | ||
if ($interval > 5) $interval = 5; | ||
$self = $this; | ||
|