Abstract.php
3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/**
* Enter description here...
*
* example:
* <pre>
*
* </pre>
*
* @name Q_Server_Abstract
* @version ( 2011-8-14 下午01:35:46 )
* @package Abstract.php
* @author liuziyang peter.zyliu@gmail.com
* @since 1.0 2011
*/
abstract class Q_Server_Abstract implements Q_Server_Interface
{
protected $config_path;
/**
*
* 是否验证服务器
* @var bool
*/
protected $check_server = false;
/**
*
* hash key
* @var String
*/
protected $hash_key;
/**
* 解析服务器字符
*
* @param String $ips
* @return Array
*/
protected function parseServerString($ips)
{
$tmpary = explode(',', str_replace(array(
' ',
"\n"
), '', $ips));
if (empty($tmpary)) {
throw new Q_Server_Exception(__CLASS__ . ': Server Config Format String Error.');
}
$serverResults = array();
foreach ($tmpary as $k => $v) {
$results = $this->makeServer($v);
if (!empty($results)) {
$serverResults[] = $results;
}
}
return $serverResults;
}
/**
* 组合服务器地址
* @param String $serverStr
* @return Array
*/
private function makeServer($serverStr)
{
$servers = explode(':', $serverStr);
$optCount = count($servers);
$results = array();
switch ($optCount) {
case 2 :
list($host, $port) = $servers;
$results = array(
'host' => (string)$host,
'port' => (int)intval($port)
);
break;
case 3 :
list($host, $port, $weight) = $servers;
$results = array(
'host' => (string)$host,
'port' => (int)intval($port),
'weight' => (int)intval($weight)
);
break;
default :
$results = array();
}
return $results;
}
public function hashServer(array $servers)
{
if (count($servers) < 3) {
throw new Q_Server_Exception(' Server count Less than 3. Can not be used HashServer ( Hash ).');
}
if (empty($this->hash_key)) {
throw new Q_Server_Exception(' Hash Key is Null. ');
}
$servers_hosts = array();
foreach ($servers as $k => $v) {
$servers_hosts[] = $v['host'] . ':' . $v['port'];
}
$hash = new Q_Core_Flexi_Hash();
$hash->addTargets($servers_hosts);
$ip = array(
'host' => '',
'port' => ''
);
$hit_ip = $hash->lookup($this->hash_key);
list($ip['host'], $ip['port']) = explode(':', $hit_ip);
return $ip;
}
/**
* 随机选择服务器并验证
*
* @param array $servers
* @param String $type
* @return Array
*/
public function randServer(array $servers)
{
$hitServer = array();
while (true) {
shuffle($servers);
if (empty($servers)) {
break;
}
$server_num = count($servers);
$randNum = $server_num > 2 ? mt_rand(0, $server_num - 1) : 0;
$hitServer = $servers[$randNum];
if ($this->check_server === false) {
break;
}
unset($servers[$randNum]);
if (Q_Server_Monitor::run()->check($hitServer["host"], $hitServer["port"])) {
break;
}
}
return $hitServer;
}
}