mSocket = false; } public function __destruct() { $this->fini_socket(); } protected function request($param) { $times = 0; do { if($this->init_socket() == false) { return false; } $ret = $this->write_param($param); if($ret === false) { $this->fini_socket(); } } while($ret == false && ++$times < 2); if($ret == true) { $body = $this->read_body(); if($body != false) { if($this->mBodyType == tcp_client::SerializeType) { return unserialize($body); } else { return json_decode($body,true); } } } else { $this->fini_socket(); return false; } } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// private function init_socket() { if($this->mSocket != false) { if(feof($this->mSocket) == true) { $this->fini_socket(); } else { return true; } } $host = $this->remote_addr(); $this->mSocket = stream_socket_client ($host,$err,$errno,30); if($this->mSocket == false) { Log::record("connect search server err --- code:{$errno} : {$err}"); return false; } else { socket_set_timeout($this->mSocket,self::time_out); return true; } } private function fini_socket() { if($this->mSocket != false) { Log::record("tcp_client fini_socket",Log::DEBUG); stream_socket_shutdown($this->mSocket,STREAM_SHUT_RDWR); fclose($this->mSocket); $this->mSocket = false; } } private function read_body() { $body_len = $this->read(self::body_header_len); $body_len = intval($body_len); if($body_len > 0) { $body = $this->read($body_len); return $body; } else { return false; } } private function read($len) { $left = $len; $data = ""; while ($left > 0) { if(feof($this->mSocket) == true) { return false; } else { $tmp = fread($this->mSocket,$left); $left = $left - strlen($tmp); $data .= $tmp; } } return $data; } private function write($data,$len) { while ($len > 0) { $ret = fwrite($this->mSocket,$data); if($ret == false || $ret <= 0) { return false; } else { $len -= $ret; $count = strlen($data); $data = substr($data,$ret,$count - $ret); } } return true; } private function write_param($param) { if($this->mBodyType == tcp_client::SerializeType) { $param = serialize($param); } else { $param = json_encode($param); } $len = sprintf("%010d",strlen($param)); $data = $len . $param; return $this->write($data,strlen($data)); } abstract function remote_addr(); } class search_client extends tcp_client { const GetRelatedWord = 1; const SearchReasult = 2; const MatchPrice = 3; const PromoteGoods = 4; const ValidateArea = 20; protected static $stInstance; public function __construct() { parent::__construct(); $this->mBodyType = tcp_client::SerializeType; } public function __destruct() { parent::__destruct(); } public static function instance() { if(self::$stInstance == null) { self::$stInstance = new search_client(); } return self::$stInstance; } public function get_words($word) { $param = array("type" => self::GetRelatedWord, "keyword" => $word); return $this->request($param); } public function get_result($params) { $param = array("type" => self::SearchReasult, "params" => $params); return $this->request($param); } public function match_price($params) { $param = array("type" => self::MatchPrice, "params" => $params); return $this->request($param); } public function promote_goods($params) { $param = array("type" => self::PromoteGoods, "params" => $params); return $this->request($param); } public function get_area($area_id) { $param = array("type" => self::ValidateArea, "params" => array('area_id' => $area_id)); return $this->request($param); } public function remote_addr() { global $config; $host = $config['searcher']['host']; $port = $config['searcher']['port']; return "{$host}:{$port}"; } } class relation_client extends tcp_client { const GetRelatedWord = 1; const SearchReasult = 2; const MatchPrice = 3; protected static $stInstance; public function __construct() { parent::__construct(); $this->mBodyType = tcp_client::JsonType; } public static function instance() { if(self::$stInstance == null) { self::$stInstance = new relation_client(); } return self::$stInstance; } public function __destruct() { parent::__destruct(); } public function remote_addr() { global $config; $host = $config['relation']['host']; $port = $config['relation']['port']; return "{$host}:{$port}"; } public function add_follow($param) { $param = array("act" => 'follow','op' => 'add', "params" => $param); return $this->request($param); } public function del_follow($param) { $param = array("act" => 'follow','op' => 'del', "params" => $param); return $this->request($param); } public function fetch_follow($param) { $param = array("act" => 'follow','op' => 'fetch', "params" => $param); return $this->request($param); } public function add_special($param) { $param = array("act" => 'special','op' => 'add', "params" => $param); return $this->request($param); } public function del_special($param) { $param = array("act" => 'special','op' => 'del', "params" => $param); return $this->request($param); } public function fetch_self_special($param) { $param = array("act" => 'special','op' => 'fetch_self', "params" => $param); return $this->request($param); } public function fetch_pri_special($param) { $param = array("act" => 'special','op' => 'fetch_pri', "params" => $param); return $this->request($param); } public function fetch_pub_special($param) { $param = array("act" => 'special','op' => 'fetch_pub', "params" => $param); return $this->request($param); } }