Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1413638
  • 博文数量: 269
  • 博客积分: 3602
  • 博客等级: 中校
  • 技术积分: 4536
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-17 21:13
文章分类

全部博文(269)

文章存档

2014年(8)

2013年(139)

2012年(122)

分类: 系统运维

2013-05-21 10:29:57

假如 你是应用 的Linux体系 ,并对性能有肯定 哀求 请看

         该客户端是用php写的,由于 php的 php-mogilefs 模块只能在Linux下应用 ,所认为利便 开拓测试,我参考 mediawiki代码库改造 了一个php语言的客户端,如下:

        
注:此代码性能较低,只做开拓测试用。

文件: mogilefs.php

/**
* 该类用于散播式文件体系 mogilefs干系 处理 赏罚
*/
class MogilefsPhpClient {
   
    /**
    * 器材
    */
    public $socket;
    /**
    * 设置
    */
    public $config;
    /**
    * domain
    *
    * @var unknown_type
    */
    public $domain;
   
    /**
    * 过错 动静
    */
    public $error;
    /**
    * 布局函数,返回器材
    * @author lengfeng
    * @return bool
    */
    public function __construct($type) {
        /*加载设置 文件*/
        require_once ('mogilefs_config.php');
        $this->config = $mogilefs_config [$type];
        $this->connect ();
    }
    /**
    * 毗连
    * @return bool
    */
    private function connect() {
        //毗连 多个traker
        foreach ( $this->config as $value ) {
            $host = $value ['MOGILEFS_HOST']; //traker 所在
            $port = $value ['MOGILEFS_PORT']; //port
            $domain = $value ['MOGILEFS_DOMAIN']; //domain
           

            $this->domain = $domain;
            $this->hosts = $host;
           
            $this->socket = fsockopen ( $host, $port );
            if ($this->socket) {
                break;
            }
        }
        return $this->socket;
    }
    /**
    * Send a request to mogilefsd and parse the result.
    * @private
    */
    private function doRequest($cmd, $args = array()) {
        $params = ' domain=' . urlencode ( $this->domain );
        foreach ( $args as $key => $value )
            $params .= '&' . urlencode ( $key ) . "=" . urlencode ( $value );
       
        if (! $this->socket) {
            $this->connect ();
        }
        fwrite ( $this->socket, $cmd . $params . "\n" );
        $line = fgets ( $this->socket );
        $words = explode ( ' ', $line );
        if ($words [0] == 'OK') {
            parse_str ( trim ( $words [1] ), $result );
        } else {
            $result = false;
            $this->error = join ( " ", $words );
        }
        return $result;
    }
    /**
    * Get an array of paths
    */
    public function getPaths($key) {
        $res = $this->doRequest ( "GET_PATHS", array ("key" => $key ) );
        unset ( $res ['paths'] );
        return $res;
    }
   
    /**
    * 插入文件
    * @var string $file 文件路径或文件内容
    * @var string $key    文件key
    * @var bool   $type 范例 ,false=>数据流 ,true=>文件路径
    * @var string $class class
    * @return bool
    *
    */
    public function put($filename, $key, $class = null, $type = true) {
       
        if (empty ( $class ))
            $class = $this->config [0] ['MOGILEFS_CLASS']; //class
       

        $res = $this->doRequest ( "CREATE_OPEN", array ("key" => $key, "class" => $class ) );
       
        if (! $res)
            return false;
       
        if (preg_match ( '/^http:\/\/([a-z0-9.-]*):([0-9]*)\/(.*)$/', $res ['path'], $matches )) {
            $host = $matches [1];
            $port = $matches [2];
            $path = $matches [3];
           
            // $fout = fopen( $res['path'], 'w' );
            $fin = fopen ( $filename, 'r' );
            $ch = curl_init ();
            curl_setopt ( $ch, CURLOPT_PUT, 1 );
            curl_setopt ( $ch, CURLOPT_URL, $res ['path'] );
            curl_setopt ( $ch, CURLOPT_VERBOSE, 0 );
            curl_setopt ( $ch, CURLOPT_INFILE, $fin );
            curl_setopt ( $ch, CURLOPT_INFILESIZE, filesize ( $filename ) );
            curl_setopt ( $ch, CURLOPT_TIMEOUT, 4 );
            curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
            if (! curl_exec ( $ch )) {
                $this->error = curl_error ( $ch );
                curl_close ( $ch );
                return false;
            }
            curl_close ( $ch );
           
            $closeres = $this->doRequest ( "CREATE_CLOSE", array ("key" => $key, "class" => $class, "devid" => $res ['devid'], "fid" => $res ['fid'], "path" => urldecode ( $res ['path'] ) ) );
            if ($closeres === false) {
                return false;
            } else {
                return true;
            }
        }
    }
   
    /**
    * 读取文件内容
    * @var string $key    文件key
    * @return bool
    */
    public function getContent($key) {
        $paths = $this->getPaths ( $key );
        if ($paths == false)
            return false;
        foreach ( $paths as $path ) {
            $fh = fopen ( $path, 'r' );
            $contents = '';
           
            if ($fh) {
                while ( ! feof ( $fh ) ) {
                    $contents .= fread ( $fh, 8192 );
                }
                fclose ( $fh );
                return $contents;
            }
        }
        return false;
    }
    /**
    * 删除文件
    * @var string $key    文件key
    * @return bool
    */
    public function del($key) {
        $res = $this->doRequest ( "DELETE", array ("key" => $key ) );
        if ($res === false)
            return false;
        return true;
    }
    /**
    * 重定名
    * @var string $from    文件旧key
    * @var string $to      文件新key
    * @return bool
    */
    public function rename($from, $to) {
        $res = $this->doRequest ( "RENAME", array ("from_key" => $from, "to_key" => $to ) );
        if ($res === false)
            return false;
        return true;
    }
    /**
    * getClient
    * @return object
    */
    public function getClient() {
        return $this->socket;
    }
    /**
    * getDomains
    * @return array
    */
    public function getDomains() {
        $res = $this->doRequest ( 'GET_DOMAINS' );
        if (! $res) {
            return false;
        }
        $domains = array ();
        for($i = 1; $i <= $res ['domains']; $i ++) {
            $dom = 'domain' . $i;
            $classes = array ();
            for($j = 1; $j <= $res [$dom . 'classes']; $j ++) {
                $classes [$res [$dom . 'class' . $j . 'name']] = $res [$dom . 'class' . $j . 'mindevcount'];
            }
            $domains [] = array ('name' => $res [$dom], 'classes' => $classes );
        }
        return $domains;
    }
}

设置 文件:mogilefs_config.php

$mogilefs_config = array(
    'test' =>array(
        array(
            'MOGILEFS_HOST' =>ip,
            'MOGILEFS_PORT' =>6001,
            'MOGILEFS_DOMAIN'=>'test_domain',   
            'MOGILEFS_CLASS' =>'test_class',
            'MOGILEFS_DEVICE_COUNT'=>1
            )
    )
);

阅读(1918) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~