Skip to content

Commit

Permalink
分布式文件服务器
Browse files Browse the repository at this point in the history
分布式文件服务器
  • Loading branch information
qieangel2013 committed Sep 13, 2016
1 parent cfe9ef8 commit ea8a163
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
8 changes: 4 additions & 4 deletions server/FileDistributedClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,11 @@ public function onReceive($client, $data)
$data_l = $process->read();
$extends = explode("/", $data_l);
$vas = count($extends) - 1;
//$pre_dir = substr($data_l, 0, strripos($data_l.'/', "/") + 1);
$pre_dir = substr($data_l, strlen($listenpath), strlen($data_l));
if (empty($pre_dir)) {
$data = array(
'type' => 'asyncfileclient',
'data' => array(
//'path' => iconv('GB2312', 'UTF-8', $data_l),
'path' => rawurlencode(str_replace("_", "@", $data_l)),
'fileex' => rawurlencode(str_replace("_", "@", $extends[$vas])),
'pre' => ''
Expand All @@ -145,6 +143,8 @@ public function onReceive($client, $data)


$client->send($this->packmes($data));
sleep(1);
//usleep(300000);
});
break;
case 'asyncfile':
Expand Down Expand Up @@ -259,7 +259,7 @@ public function getlistDir($dir)
return $dirInfo;
}
//解包装数据
public function unpackmes($data, $format = '\r\n\r\n', $preformat = '###')
public function unpackmes($data, $format = '\r\n\r\n', $preformat = '######')
{
$pos = strpos($data, $format);
$resultdata = array();
Expand Down Expand Up @@ -291,7 +291,7 @@ public function unpackmes($data, $format = '\r\n\r\n', $preformat = '###')
}
}
//包装数据
public function packmes($data, $format = '\r\n\r\n', $preformat = '###')
public function packmes($data, $format = '\r\n\r\n', $preformat = '######')
{
return $preformat . json_encode($data, true) . $format;
}
Expand Down
16 changes: 9 additions & 7 deletions server/FileDistributedServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ public function onStart($serv)
$wd = inotify_add_watch($this->filefd, $listenpath, IN_CREATE | IN_MOVED_TO | IN_CLOSE_WRITE); //IN_MODIFY、IN_ALL_EVENTS、IN_CLOSE_WRITE
$this->wd[$wd] = array(
'wd' => $wd,
'path' => $listenpath
'path' => $listenpath,
'pre' => ''
);
$lisrdir = FileDistributedClient::getInstance()->getlistDir($listenpath);
if ($lisrdir) {
Expand All @@ -113,14 +114,13 @@ public function onStart($serv)
$this->wd[$wd] = array(
'wd' => $wd,
'path' => $v,
'pre' => ''
'pre' => substr($v, strlen($listenpathex), strlen($v))
);
}
}
swoole_event_add($this->filefd, function($fd) use ($localclient, $listenpath, $listenpathex)
{
$events = inotify_read($fd);

if ($events) {
foreach ($events as $kk => $vv) {
if (isset($vv['name']) && $vv['mask'] != 256) {
Expand All @@ -129,7 +129,7 @@ public function onStart($serv)
$listenpathx = substr($this->wd[$vv['wd']]['path'], 0, strripos($this->wd[$vv['wd']]['path'] . '/', "/") + 1);
$listenpathx .= '/' . $vv['name'];

$wd = inotify_add_watch($fd, $listenpathx, IN_CREATE | IN_MOVED_TO | IN_CLOSE_WRITE);
$wd = inotify_add_watch($this->filefd, $listenpathx, IN_CREATE | IN_MOVED_TO | IN_CLOSE_WRITE);
$this->wd[$wd] = array(
'wd' => $wd,
'path' => $listenpathx,
Expand All @@ -138,7 +138,7 @@ public function onStart($serv)
} else {
$listenpath .= '/' . $vv['name'];

$wd = inotify_add_watch($fd, $listenpath, IN_CREATE | IN_MOVED_TO | IN_CLOSE_WRITE);
$wd = inotify_add_watch($this->filefd, $listenpath, IN_CREATE | IN_MOVED_TO | IN_CLOSE_WRITE);
$this->wd[$wd] = array(
'wd' => $wd,
'path' => $listenpath,
Expand Down Expand Up @@ -174,6 +174,8 @@ public function onStart($serv)
);
}
$localclient->send(FileDistributedClient::getInstance()->packmes($data));
//sleep(1);
usleep(300000);
}

}
Expand Down Expand Up @@ -299,7 +301,7 @@ public function onReceive($serv, $fd, $from_id, $data)
array_push($this->client_pool_ser, $this->connectioninfo['remote_ip']);
}
}
echo date('[ c ]') . str_replace("\n", "", var_export($remote_info, true));
echo date('[ c ]') . str_replace("\n", "", var_export($val, true));
} else {
if (isset($val['type'])) {
switch ($val['type']) {
Expand Down Expand Up @@ -372,7 +374,7 @@ public function onReceive($serv, $fd, $from_id, $data)
default:
break;
}
echo date('[ c ]') . str_replace("\n", "", var_export($remote_info, true));
echo date('[ c ]') . str_replace("\n", "", var_export($val, true));
} else {
if (!$this->tmpdata_flag) {
$tdf = array_shift($this->client_pool_ser_c);
Expand Down

0 comments on commit ea8a163

Please sign in to comment.