|
| 1 | +<?php |
| 2 | +include_once 'DqLoader.php'; |
| 3 | + |
| 4 | +class DqClient{ |
| 5 | + |
| 6 | + |
| 7 | + private $serverList = array(); |
| 8 | + private $fd = NULL; |
| 9 | + |
| 10 | + public function addServer($server){ |
| 11 | + if(is_string($server)){ |
| 12 | + $this->serverList[] = $server; |
| 13 | + } |
| 14 | + if(is_array($server) && !empty($server)){ |
| 15 | + $this->serverList = array_merge($this->serverList,$server); |
| 16 | + } |
| 17 | + $this->serverList = array_unique($this->serverList); |
| 18 | + } |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | + public function connect(){ |
| 23 | + if(!empty($this->fd)){ |
| 24 | + return $this->fd; |
| 25 | + } |
| 26 | + if (empty($this->serverList)){ |
| 27 | + DqLog::writeLog('empty server list'); |
| 28 | + return false; |
| 29 | + } |
| 30 | + $serverList=$this->serverList; |
| 31 | + |
| 32 | + while(count($serverList)){ |
| 33 | + try { |
| 34 | + $idx = rand(0,count($serverList)-1); |
| 35 | + $server = $serverList[$idx]; |
| 36 | + list($host,$port)= explode(':',$server); |
| 37 | + |
| 38 | + $fd = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); |
| 39 | + //1s内没处理完直接返回 |
| 40 | + socket_set_option($fd,SOL_SOCKET,SO_RCVTIMEO,array("sec"=>1, "usec"=>0)); |
| 41 | + if (!is_resource($fd)) { |
| 42 | + $strMsg = 'socket_create error:' . socket_strerror(socket_last_error()); |
| 43 | + throw new DqException($strMsg); |
| 44 | + } |
| 45 | + if (!socket_connect($fd, $host, $port)) { |
| 46 | + $strMsg = 'socket_create error:' . socket_strerror(socket_last_error().' ip='.$host.' port='.$port); |
| 47 | + throw new DqException($strMsg); |
| 48 | + } |
| 49 | + $this->fd = $fd; |
| 50 | + return $fd; |
| 51 | + } catch (DqException $e) { |
| 52 | + unset($this->serverList[$idx]); //删除无用的配置 |
| 53 | + |
| 54 | + unset($serverList[$idx]); |
| 55 | + $serverList = array_values($serverList); |
| 56 | + |
| 57 | + DqLog::writeLog($e->getDqMessage(), DqLog::LOG_TYPE_EXCEPTION); |
| 58 | + } |
| 59 | + |
| 60 | + } |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + public function parse_result($ret){ |
| 65 | + if($ret['code']==1){ |
| 66 | + return true; |
| 67 | + }else{ |
| 68 | + return false; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + public function add($topic,$data){ |
| 73 | + try{ |
| 74 | + $fd = $this->connect(); |
| 75 | + if($fd===false){ |
| 76 | + throw new DqException(' connect server faild'); |
| 77 | + } |
| 78 | + $data['cmd']='add'; |
| 79 | + $data['topic'] = $topic; |
| 80 | + if(DqComm::socket_wirte($fd,$data)){ |
| 81 | + $ret = DqComm::socket_read($fd); |
| 82 | + return $this->parse_result($ret); |
| 83 | + }else{ |
| 84 | + throw new DqException('add error,data='.json_encode($data)); |
| 85 | + } |
| 86 | + }catch (DqException $e){ |
| 87 | + DqLog::writeLog($e->getDqMessage(),DqLog::LOG_TYPE_EXCEPTION); |
| 88 | + } |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + public function del($topic,$id){ |
| 94 | + try{ |
| 95 | + $fd = $this->connect(); |
| 96 | + if($fd===false){ |
| 97 | + throw new DqException(' connect server faild'); |
| 98 | + } |
| 99 | + $data['cmd']='del'; |
| 100 | + $data['topic'] = $topic; |
| 101 | + $data['id'] = $id; |
| 102 | + if(DqComm::socket_wirte($fd,$data)){ |
| 103 | + $ret = DqComm::socket_read($fd); |
| 104 | + return $this->parse_result($ret); |
| 105 | + }else{ |
| 106 | + throw new DqException('add error,data='.json_encode($data)); |
| 107 | + } |
| 108 | + }catch (DqException $e){ |
| 109 | + DqLog::writeLog($e->getDqMessage(),DqLog::LOG_TYPE_EXCEPTION); |
| 110 | + } |
| 111 | + return false; |
| 112 | + } |
| 113 | + |
| 114 | + public function get($topic,$id){ |
| 115 | + try{ |
| 116 | + $fd = $this->connect(); |
| 117 | + if($fd===false){ |
| 118 | + throw new DqException(' connect server faild'); |
| 119 | + } |
| 120 | + $data['cmd']='get'; |
| 121 | + $data['topic'] = $topic; |
| 122 | + $data['id'] = $id; |
| 123 | + if(DqComm::socket_wirte($fd,$data)){ |
| 124 | + $ret = DqComm::socket_read($fd); |
| 125 | + return $ret; |
| 126 | + }else{ |
| 127 | + throw new DqException('add error,data='.json_encode($data)); |
| 128 | + } |
| 129 | + }catch (DqException $e){ |
| 130 | + DqLog::writeLog($e->getDqMessage(),DqLog::LOG_TYPE_EXCEPTION); |
| 131 | + return false; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + |
| 136 | + |
| 137 | +} |
| 138 | + |
| 139 | +//$server=array('10.210.234.203:6879'); |
| 140 | +// |
| 141 | +//$topic ='order_comment'; |
| 142 | +//$id = rand(1,10000); |
| 143 | +//$data=array( |
| 144 | +// 'id'=>$id, //topic+业务唯一id的组合 |
| 145 | +// 'body'=>array('a'=>1,'b'=>2,'c'=>3), |
| 146 | +//); |
| 147 | +// |
| 148 | +//$dqClient = new DqClient(); |
| 149 | +//$dqClient->addServer($server); |
| 150 | +// |
| 151 | +//var_dump($dqClient->add($topic,$data)); |
| 152 | +//var_dump($dqClient->get($topic,$id)); |
| 153 | +//var_dump($dqClient->del($topic,$id)); |
0 commit comments