Skip to content

Commit

Permalink
分布式文件服务器
Browse files Browse the repository at this point in the history
分布式文件服务器
  • Loading branch information
qieangel2013 committed Sep 22, 2016
1 parent 5622a83 commit d209d5e
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 123 deletions.
19 changes: 11 additions & 8 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
| 日期:2016/7/25
|---------------------------------------------------------------
*/
define('ServerIp',"0.0.0.0");
define('ServerPort',"9508");
define('ServerLog',dirname(__DIR__).'/log/FileDistributed.log');
define('redis_server','192.168.102.163');
define('redis_port','6379');
define('redis_auth','123qwe');
define('LISTENPATH',dirname(__DIR__).'/img');
define('allsysnc',true);
define('ServerIp', "0.0.0.0");
define('ServerPort', "9508");
define('ServerLog', dirname(__DIR__) . '/log/FileDistributed.log');
define('redis_server', '192.168.102.163');
define('redis_port', '6379');
define('redis_auth', '123qwe');
define('LISTENPATH', dirname(__DIR__) . '/img');
define('allsysnc', true);
define('maxpackage', 1024 * 1024 * 200);
define('Bincmd', '/usr/local/php/bin/php');
define('file_arg', 'dfs');
?>
62 changes: 29 additions & 33 deletions server.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,48 @@
|---------------------------------------------------------------
*/
// 检查扩展
if(!extension_loaded('inotify'))
{
if (!extension_loaded('inotify')) {
exit("Please install inotify extension.\n");
}
if(!extension_loaded('redis'))
{
if (!extension_loaded('redis')) {
exit("Please install redis extension.\n");
}
if(!extension_loaded('swoole'))
{
if (!extension_loaded('swoole')) {
exit("Please install swoole extension.\n");
}
//检查是否为cli模式
if(php_sapi_name() !== 'cli'){
if (php_sapi_name() !== 'cli') {
exit("Please use php cli mode.\n");
}
$cmd="/usr/local/php/bin/php";//php的绝对路径
function server_call($cmd)
{

foreach(glob(__DIR__.'/server/FileDistributedServer.php') as $start_file)
{
exec($cmd.' '.$start_file);

foreach (glob(__DIR__ . '/server/FileDistributedServer.php') as $start_file) {
exec($cmd . ' ' . $start_file);
}
}
$ser_ser=$argv;
if(!isset($ser_ser[1])){
exit("No argv.\n");
}else{
switch ($ser_ser[1]) {
case 'start':
call_user_func('server_call',$cmd);
break;
case 'stop':
exec("ps -ef | grep -E '".$cmd."' |grep -v 'grep'| awk '{print $2}'|xargs kill -9 > /dev/null 2>&1 &");
echo "Kill all process success.\n";
break;
case 'restart':
exec("ps -ef | grep -E '".$cmd."' |grep -v 'grep'| awk '{print $2}'|xargs kill -9 > /dev/null 2>&1 &");
echo "Kill all process success.\n";
call_user_func('server_call',$cmd);
break;
default:
exit("Not support this argv.\n");
break;
$ser_ser = $argv;
require_once __DIR__ . '/config/config.php';
if (!isset($ser_ser[1])) {
exit("No argv.\n");
} else {
switch ($ser_ser[1]) {
case 'start':
call_user_func('server_call', Bincmd);
break;
case 'stop':
exec("ps -ef | grep -E '" . Bincmd . "' |grep -v 'grep'| awk '{print $2}'|xargs kill -9 > /dev/null 2>&1 &");
echo "Kill all process success.\n";
break;
case 'restart':
exec("ps -ef | grep -E '" . Bincmd . "' |grep -v 'grep'| awk '{print $2}'|xargs kill -9 > /dev/null 2>&1 &");
echo "Kill all process success.\n";
call_user_func('server_call', Bincmd);
break;
default:
exit("Not support this argv.\n");
break;
}
}
}

?>
10 changes: 8 additions & 2 deletions server/FileDistributedClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function addServerClient($address)
'package_length_type' => 'N',
'package_length_offset' => 0, //第N个字节是包长度的值
'package_body_offset' => 4, //第几个字节开始计算长度
'package_max_length' => 1024 * 1024 * 20 //协议最大长度
'package_max_length' => maxpackage //协议最大长度
));
$client->on('Connect', array(
&$this,
Expand Down Expand Up @@ -222,13 +222,14 @@ public function onError($client)
$this->removeuser($this->cur_address);
$this->del_server[ip2long($this->cur_address)] = $this->cur_address;
$this->table->del(ip2long($this->cur_address));
$this->setkey($this->cur_address);
$this->setkey($this->cur_address, file_arg . 'errserfile');
unset($this->b_client_pool[$this->cur_address]);
unset($client);
}
//获取分布式服务器列表
public function getserlist($keyname = 'FileDistributed')
{
$keyname = isset($keyname) && !empty($keyname) ? $keyname : 'FileDistributed';
ob_start();
dredis::getInstance()->getfd($keyname);
$result = ob_get_contents();
Expand All @@ -238,26 +239,31 @@ public function getserlist($keyname = 'FileDistributed')
//添加到分布式服务器列表
public function appendserlist($data, $score, $keyname = 'FileDistributed')
{
$keyname = isset($keyname) && !empty($keyname) ? $keyname : 'FileDistributed';
dredis::getInstance()->savefd($data, $score, $keyname);
}
//从分布式服务器列表删除
public function removeuser($data, $keyname = 'FileDistributed')
{
$keyname = isset($keyname) && !empty($keyname) ? $keyname : 'FileDistributed';
dredis::getInstance()->removefd($data, $keyname);
}
//设置错误服务器
public function setkey($data, $keyname = 'errserfile')
{
$keyname = isset($keyname) && !empty($keyname) ? $keyname : 'errserfile';
return dredis::getInstance()->setkey($data, $keyname);
}
//获取错误服务器
public function getkey($keyname = 'errserfile')
{
$keyname = isset($keyname) && !empty($keyname) ? $keyname : 'errserfile';
return dredis::getInstance()->getkey($keyname);
}
//删除错误服务器
public function delkey($keyname = 'errserfile')
{
$keyname = isset($keyname) && !empty($keyname) ? $keyname : 'errserfile';
return dredis::getInstance()->delkey($keyname);
}
//获取目录
Expand Down
Loading

0 comments on commit d209d5e

Please sign in to comment.