123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 2016/10/15
- * Time: 下午3:52
- */
- namespace search;
- use Log;
- abstract class tcp_client
- {
- const time_out = 3600;
- const body_header_len = 10;
- //协议打包格式,PHP序列化用于PHP进程间通信,json 格式用于PHP与C++进程间通信
- const SerializeType = 1;
- const JsonType = 2;
- private $mSocket;
- protected $mBodyType;
- private $mAutoConnect;
- protected function __construct($socket = false,$auto_connect = true)
- {
- $this->mSocket = $socket;
- $this->mAutoConnect = $auto_connect;
- }
- 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->mAutoConnect) return true;
- 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 server err host:{$host} code:{$errno} : {$err}");
- return false;
- } else {
- Log::record("connect server succ host:{$host} code:{$errno} : {$err}");
- socket_set_timeout($this->mSocket,self::time_out);
- return true;
- }
- }
- private function fini_socket()
- {
- if(!$this->mAutoConnect) return;
- 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()
- {
- do {
- $body = $this->read(self::body_header_len);
- if($body === false) return false;
- $body_len = intval($body);
- }
- while($body_len === 0);
- 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($this->mAutoConnect)
- {
- if (feof($this->mSocket) == true) {
- return false;
- } else {
- $tmp = fread($this->mSocket, $left);
- }
- }
- else {
- $tmp = @socket_read($this->mSocket,$left);
- if(strlen($tmp) == 0) return false;
- }
- $left = $left - strlen($tmp);
- $data .= $tmp;
- }
- return $data;
- }
- private function write($data,$len)
- {
- while ($len > 0)
- {
- if($this->mAutoConnect) {
- $ret = fwrite($this->mSocket,$data);
- } else {
- $ret = @socket_write($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 "tcp://{$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 "tcp://{$host}:{$port}";
- }
- public function add_inviter($param)
- {
- $param = ["act" => 'inviter','op' => 'add', "params" => $param];
- $result = $this->request($param);
- if(empty($result)) return false;
- $code = intval($result['code']);
- if($code != 200) {
- return false;
- }
- else {
- return true;
- }
- }
- public function fetch_inviters($param)
- {
- $param = ["act" => 'inviter','op' => 'list', "params" => $param];
- $result = $this->request($param);
- $code = intval($result['code']);
- if($code != 200) {
- return false;
- }
- else {
- return $result['data']['inviters'];
- }
- }
- public function fetch_invitees($param)
- {
- $param = ["act" => 'inviter','op' => 'invitees', "params" => $param];
- $result = $this->request($param);
- $code = intval($result['code']);
- if($code != 200) {
- return false;
- }
- else {
- return $result['data']['invitees'];
- }
- }
- /////////////////////////////////////////////////////////////////////////
- public function add_follow($param)
- {
- $param = ["act" => 'follow','op' => 'add', "params" => $param];
- $result = $this->request($param);
- if(empty($result)) return false;
- $code = intval($result['code']);
- if($code != 200) {
- return false;
- }
- else {
- return true;
- }
- }
- public function del_follow($param)
- {
- $param = ["act" => 'follow','op' => 'del', "params" => $param];
- $result = $this->request($param);
- if(empty($result)) return false;
- $code = intval($result['code']);
- if($code != 200) {
- return false;
- }
- else {
- return true;
- }
- }
- public function fetch_follow($param)
- {
- $param = ["act" => 'follow','op' => 'fetch', "params" => $param];
- $result = $this->request($param);
- $code = intval($result['code']);
- if($code != 200) {
- return false;
- }
- else {
- return $result['data']['follows'];
- }
- }
- public function add_special($param)
- {
- $param = ["act" => 'special','op' => 'add', "params" => $param];
- $result = $this->request($param);
- if(empty($result)) return false;
- $code = intval($result['code']);
- if($code != 200) {
- return false;
- }
- else {
- return true;
- }
- }
- public function del_special($param)
- {
- $param = ["act" => 'special','op' => 'del', "params" => $param];
- $result = $this->request($param);
- if(empty($result)) return false;
- $code = intval($result['code']);
- if($code != 200) {
- return false;
- }
- else {
- return true;
- }
- }
- public function fetch_self_special($param)
- {
- $param = ["act" => 'special','op' => 'fetch_self', "params" => $param];
- $result = $this->request($param);
- $code = intval($result['code']);
- if($code != 200) {
- return false;
- }
- else {
- return $result['data']['specials'];
- }
- }
- public function fetch_pri_special($param)
- {
- $param = ["act" => 'special','op' => 'fetch_pri', "params" => $param];
- $result = $this->request($param);
- $code = intval($result['code']);
- if($code != 200) {
- return false;
- }
- else {
- return $result['data']['specials'];
- }
- }
- public function fetch_pub_special($param)
- {
- $param = ["act" => 'special','op' => 'fetch_pub', "params" => $param];
- $result = $this->request($param);
- $code = intval($result['code']);
- if($code != 200) {
- return false;
- }
- else {
- return $result['data']['specials'];
- }
- }
- }
|