tcp_client.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2016/10/15
  6. * Time: 下午3:52
  7. */
  8. namespace search;
  9. use Log;
  10. abstract class tcp_client
  11. {
  12. const time_out = 3600;
  13. const body_header_len = 10;
  14. //协议打包格式,PHP序列化用于PHP进程间通信,json 格式用于PHP与C++进程间通信
  15. const SerializeType = 1;
  16. const JsonType = 2;
  17. private $mSocket;
  18. protected $mBodyType;
  19. protected function __construct()
  20. {
  21. $this->mSocket = false;
  22. }
  23. public function __destruct()
  24. {
  25. $this->fini_socket();
  26. }
  27. protected function request($param)
  28. {
  29. $times = 0;
  30. do
  31. {
  32. if($this->init_socket() == false) {
  33. return false;
  34. }
  35. $ret = $this->write_param($param);
  36. if($ret === false) {
  37. $this->fini_socket();
  38. }
  39. } while($ret == false && ++$times < 2);
  40. if($ret == true)
  41. {
  42. $body = $this->read_body();
  43. if($body != false)
  44. {
  45. if($this->mBodyType == tcp_client::SerializeType) {
  46. return unserialize($body);
  47. } else {
  48. return json_decode($body,true);
  49. }
  50. }
  51. }
  52. else
  53. {
  54. $this->fini_socket();
  55. return false;
  56. }
  57. }
  58. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  59. private function init_socket()
  60. {
  61. if($this->mSocket != false)
  62. {
  63. if(feof($this->mSocket) == true) {
  64. $this->fini_socket();
  65. } else {
  66. return true;
  67. }
  68. }
  69. $host = $this->remote_addr();
  70. $this->mSocket = stream_socket_client ($host,$err,$errno,30);
  71. if($this->mSocket == false) {
  72. Log::record("connect search server err --- code:{$errno} : {$err}");
  73. return false;
  74. } else {
  75. socket_set_timeout($this->mSocket,self::time_out);
  76. return true;
  77. }
  78. }
  79. private function fini_socket()
  80. {
  81. if($this->mSocket != false) {
  82. Log::record("tcp_client fini_socket",Log::DEBUG);
  83. stream_socket_shutdown($this->mSocket,STREAM_SHUT_RDWR);
  84. fclose($this->mSocket);
  85. $this->mSocket = false;
  86. }
  87. }
  88. private function read_body()
  89. {
  90. $body_len = $this->read(self::body_header_len);
  91. $body_len = intval($body_len);
  92. if($body_len > 0) {
  93. $body = $this->read($body_len);
  94. return $body;
  95. } else {
  96. return false;
  97. }
  98. }
  99. private function read($len)
  100. {
  101. $left = $len;
  102. $data = "";
  103. while ($left > 0)
  104. {
  105. if(feof($this->mSocket) == true) {
  106. return false;
  107. } else {
  108. $tmp = fread($this->mSocket,$left);
  109. $left = $left - strlen($tmp);
  110. $data .= $tmp;
  111. }
  112. }
  113. return $data;
  114. }
  115. private function write($data,$len)
  116. {
  117. while ($len > 0)
  118. {
  119. $ret = fwrite($this->mSocket,$data);
  120. if($ret == false || $ret <= 0) {
  121. return false;
  122. }
  123. else {
  124. $len -= $ret;
  125. $count = strlen($data);
  126. $data = substr($data,$ret,$count - $ret);
  127. }
  128. }
  129. return true;
  130. }
  131. private function write_param($param)
  132. {
  133. if($this->mBodyType == tcp_client::SerializeType) {
  134. $param = serialize($param);
  135. }
  136. else {
  137. $param = json_encode($param);
  138. }
  139. $len = sprintf("%010d",strlen($param));
  140. $data = $len . $param;
  141. return $this->write($data,strlen($data));
  142. }
  143. abstract function remote_addr();
  144. }
  145. class search_client extends tcp_client
  146. {
  147. const GetRelatedWord = 1;
  148. const SearchReasult = 2;
  149. const MatchPrice = 3;
  150. const PromoteGoods = 4;
  151. const ValidateArea = 20;
  152. protected static $stInstance;
  153. public function __construct()
  154. {
  155. parent::__construct();
  156. $this->mBodyType = tcp_client::SerializeType;
  157. }
  158. public function __destruct()
  159. {
  160. parent::__destruct();
  161. }
  162. public static function instance()
  163. {
  164. if(self::$stInstance == null) {
  165. self::$stInstance = new search_client();
  166. }
  167. return self::$stInstance;
  168. }
  169. public function get_words($word)
  170. {
  171. $param = array("type" => self::GetRelatedWord, "keyword" => $word);
  172. return $this->request($param);
  173. }
  174. public function get_result($params)
  175. {
  176. $param = array("type" => self::SearchReasult, "params" => $params);
  177. return $this->request($param);
  178. }
  179. public function match_price($params)
  180. {
  181. $param = array("type" => self::MatchPrice, "params" => $params);
  182. return $this->request($param);
  183. }
  184. public function promote_goods($params)
  185. {
  186. $param = array("type" => self::PromoteGoods, "params" => $params);
  187. return $this->request($param);
  188. }
  189. public function get_area($area_id)
  190. {
  191. $param = array("type" => self::ValidateArea, "params" => array('area_id' => $area_id));
  192. return $this->request($param);
  193. }
  194. public function remote_addr()
  195. {
  196. global $config;
  197. $host = $config['searcher']['host'];
  198. $port = $config['searcher']['port'];
  199. return "{$host}:{$port}";
  200. }
  201. }
  202. class relation_client extends tcp_client
  203. {
  204. const GetRelatedWord = 1;
  205. const SearchReasult = 2;
  206. const MatchPrice = 3;
  207. protected static $stInstance;
  208. public function __construct()
  209. {
  210. parent::__construct();
  211. $this->mBodyType = tcp_client::JsonType;
  212. }
  213. public static function instance()
  214. {
  215. if(self::$stInstance == null) {
  216. self::$stInstance = new relation_client();
  217. }
  218. return self::$stInstance;
  219. }
  220. public function __destruct()
  221. {
  222. parent::__destruct();
  223. }
  224. public function remote_addr()
  225. {
  226. global $config;
  227. $host = $config['relation']['host'];
  228. $port = $config['relation']['port'];
  229. return "{$host}:{$port}";
  230. }
  231. public function add_inviter($param)
  232. {
  233. $param = ["act" => 'inviter','op' => 'add', "params" => $param];
  234. $result = $this->request($param);
  235. if(empty($result)) return false;
  236. $code = intval($result['code']);
  237. if($code != 200) {
  238. return false;
  239. }
  240. else {
  241. return true;
  242. }
  243. }
  244. public function fetch_inviters($param)
  245. {
  246. $param = ["act" => 'inviter','op' => 'list', "params" => $param];
  247. $result = $this->request($param);
  248. $code = intval($result['code']);
  249. if($code != 200) {
  250. return false;
  251. }
  252. else {
  253. return $result['data']['inviters'];
  254. }
  255. }
  256. public function fetch_invitees($param)
  257. {
  258. $param = ["act" => 'inviter','op' => 'invitees', "params" => $param];
  259. $result = $this->request($param);
  260. $code = intval($result['code']);
  261. if($code != 200) {
  262. return false;
  263. }
  264. else {
  265. return $result['data']['invitees'];
  266. }
  267. }
  268. /////////////////////////////////////////////////////////////////////////
  269. public function add_follow($param)
  270. {
  271. $param = ["act" => 'follow','op' => 'add', "params" => $param];
  272. $result = $this->request($param);
  273. if(empty($result)) return false;
  274. $code = intval($result['code']);
  275. if($code != 200) {
  276. return false;
  277. }
  278. else {
  279. return true;
  280. }
  281. }
  282. public function del_follow($param)
  283. {
  284. $param = ["act" => 'follow','op' => 'del', "params" => $param];
  285. $result = $this->request($param);
  286. if(empty($result)) return false;
  287. $code = intval($result['code']);
  288. if($code != 200) {
  289. return false;
  290. }
  291. else {
  292. return true;
  293. }
  294. }
  295. public function fetch_follow($param)
  296. {
  297. $param = ["act" => 'follow','op' => 'fetch', "params" => $param];
  298. $result = $this->request($param);
  299. $code = intval($result['code']);
  300. if($code != 200) {
  301. return false;
  302. }
  303. else {
  304. return $result['data']['follows'];
  305. }
  306. }
  307. public function add_special($param)
  308. {
  309. $param = ["act" => 'special','op' => 'add', "params" => $param];
  310. $result = $this->request($param);
  311. if(empty($result)) return false;
  312. $code = intval($result['code']);
  313. if($code != 200) {
  314. return false;
  315. }
  316. else {
  317. return true;
  318. }
  319. }
  320. public function del_special($param)
  321. {
  322. $param = ["act" => 'special','op' => 'del', "params" => $param];
  323. $result = $this->request($param);
  324. if(empty($result)) return false;
  325. $code = intval($result['code']);
  326. if($code != 200) {
  327. return false;
  328. }
  329. else {
  330. return true;
  331. }
  332. }
  333. public function fetch_self_special($param)
  334. {
  335. $param = ["act" => 'special','op' => 'fetch_self', "params" => $param];
  336. $result = $this->request($param);
  337. $code = intval($result['code']);
  338. if($code != 200) {
  339. return false;
  340. }
  341. else {
  342. return $result['data']['specials'];
  343. }
  344. }
  345. public function fetch_pri_special($param)
  346. {
  347. $param = ["act" => 'special','op' => 'fetch_pri', "params" => $param];
  348. $result = $this->request($param);
  349. $code = intval($result['code']);
  350. if($code != 200) {
  351. return false;
  352. }
  353. else {
  354. return $result['data']['specials'];
  355. }
  356. }
  357. public function fetch_pub_special($param)
  358. {
  359. $param = ["act" => 'special','op' => 'fetch_pub', "params" => $param];
  360. $result = $this->request($param);
  361. $code = intval($result['code']);
  362. if($code != 200) {
  363. return false;
  364. }
  365. else {
  366. return $result['data']['specials'];
  367. }
  368. }
  369. }