factory_processor.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2017/12/14
  6. * Time: 下午12:07
  7. */
  8. namespace room;
  9. use search\IProcessor;
  10. use errcode;
  11. use scope_trace;
  12. use uniquer;
  13. class factory_processor implements IProcessor
  14. {
  15. const room_connection = "room_connection";
  16. private $mFactory;
  17. private $mLastBuilding;
  18. private $mAccUniquer;
  19. private $mBufidRooms;
  20. public function __construct()
  21. {
  22. $this->mFactory = new factory();
  23. $this->mLastBuilding = 0;
  24. $this->mAccUniquer = new uniquer();
  25. $this->mBufidRooms = [];
  26. }
  27. public function onStart()
  28. {
  29. global $config;
  30. $room_addrs = $config['room_factory']['rooms_addr'];
  31. foreach ($room_addrs as $addr) {
  32. factory_server::instance()->connect($addr['host'],$addr['port'],self::room_connection);
  33. }
  34. }
  35. public function onConnected($bufid,$stream,$host,$port,$args)
  36. {
  37. new scope_trace(__METHOD__);
  38. if($args == self::room_connection) {
  39. $client = new room_client($host,$port,$stream,false);
  40. $this->mBufidRooms[$bufid] = $client;
  41. $this->block($bufid);
  42. $client->init_rooms($bufid);
  43. $this->unblock($bufid);
  44. }
  45. }
  46. public function onClose($bufid)
  47. {
  48. new scope_trace(__METHOD__);
  49. //if access connection
  50. if($this->mAccUniquer->remove_value($bufid)) return;
  51. //if room connection
  52. if(array_key_exists($bufid,$this->mBufidRooms)) {
  53. $client = $this->mBufidRooms[$bufid];
  54. $addr = $client->host_port();
  55. unset($this->mBufidRooms[$bufid]);
  56. factory_server::instance()->connect($addr['host'],$addr['port'],self::room_connection);
  57. }
  58. }
  59. private function block($bufid) {
  60. factory_server::instance()->block($bufid);
  61. }
  62. private function unblock($bufid) {
  63. factory_server::instance()->unblock($bufid);
  64. }
  65. public function onRequest($bufid, $body)
  66. {
  67. new scope_trace(__METHOD__);
  68. $input = json_decode($body,true);
  69. if($input == false) {
  70. return false;
  71. }
  72. $act = $input['act'];
  73. if(empty($act)) return false;
  74. if($act == proto_type::act_fcgi) {
  75. $ret = $this->onFcgi($bufid,$input);
  76. factory_server::instance()->write($bufid,$ret);
  77. return true;
  78. }
  79. elseif($act == proto_type::act_access) {
  80. $ret = $this->onAccess($bufid,$input);
  81. factory_server::instance()->write($bufid,$ret);
  82. return true;
  83. }
  84. else {
  85. return false;
  86. }
  87. }
  88. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  89. private function onFcgi($bufid, $input)
  90. {
  91. $op = $input['op'];
  92. if($op == 'create')
  93. {
  94. $ret = $this->mFactory->create($input);
  95. if($ret != false)
  96. {
  97. $invite = $input['invite'];
  98. if($invite)
  99. {
  100. $roomid = $ret['room'];
  101. $inviter = $ret['creator'];
  102. if($roomid <= 0 || $inviter <= 0) {
  103. return $this->error(errcode::ErrRoomCreate);
  104. }
  105. $result = $this->invite($roomid,$inviter,[$inviter]);
  106. if($result != false) {
  107. $ret = array_merge($ret,$result);
  108. return $this->success($ret);
  109. } else {
  110. return $this->error(errcode::ErrRoomInvite);
  111. }
  112. }
  113. else {
  114. return $this->success($ret);
  115. }
  116. }
  117. return $this->error(errcode::ErrRoomCreate);
  118. }
  119. elseif($op == 'invite')
  120. {
  121. $roomid = intval($input['room']);
  122. $inviter = intval($input['inviter']);
  123. $invitees = $input['invitees'];
  124. if($roomid <= 0 || $inviter <= 0 || empty($invitees)) {
  125. return $this->error(errcode::ErrRoomInvite);
  126. }
  127. $ret = $this->invite($roomid,$inviter,$invitees);
  128. if($ret != false) {
  129. return $this->success($ret);
  130. } else {
  131. return $this->error(errcode::ErrRoomInvite);
  132. }
  133. }
  134. elseif($op == 'leave')
  135. {
  136. $roomid = intval($input['room']);
  137. $user = intval($input['user']);
  138. if($roomid <= 0 || $user <= 0) {
  139. return $this->error(errcode::ErrRoomInvite);
  140. }
  141. $ret = $this->leave($roomid,$user);
  142. if($ret != false) {
  143. return $this->success($ret);
  144. } else {
  145. return $this->error(errcode::ErrRoomInvite);
  146. }
  147. }
  148. elseif($op == 'push')
  149. {
  150. $content = $input['content'];
  151. $ret = $this->push($content);
  152. if($ret != false) {
  153. return $this->success(NULL);
  154. } else {
  155. return $this->error(errcode::ErrRoomPush);
  156. }
  157. }
  158. else {
  159. return $this->error(errcode::ErrRoomFactoryOp);
  160. }
  161. }
  162. private function onAccess($bufid,$input)
  163. {
  164. $op = $input['op'];
  165. if($op == 'who') {
  166. $this->mAccUniquer->add_value($bufid);
  167. return $this->success(['act' => proto_type::act_access,'op' => 'who']);
  168. }
  169. }
  170. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  171. private function invite($roomid, $inviter,$invitees)
  172. {
  173. foreach ($this->mBufidRooms as $bufid => $client)
  174. {
  175. if($client->contain_room($roomid))
  176. {
  177. $this->block($bufid);
  178. $ret = $client->invite($roomid,$inviter,$invitees);
  179. $this->unblock($bufid);
  180. return $ret;
  181. }
  182. }
  183. $bufid = $this->room_bufid();
  184. if($bufid != false)
  185. {
  186. $client = $this->mBufidRooms[$bufid];
  187. $this->block($bufid);
  188. $ret = $client->invite($roomid,$inviter,$invitees);
  189. $this->unblock($bufid);
  190. if($ret != false) {
  191. $client->add_room($roomid);
  192. }
  193. }
  194. return $ret;
  195. }
  196. private function leave($roomid, $user)
  197. {
  198. foreach ($this->mBufidRooms as $bufid => $client)
  199. {
  200. if($client->contain_room($roomid))
  201. {
  202. $this->block($bufid);
  203. $ret = $client->leave($roomid,$user);
  204. $this->unblock($bufid);
  205. return $ret;
  206. }
  207. }
  208. }
  209. private function push($content)
  210. {
  211. $bufid = $this->room_bufid();
  212. if($bufid != false)
  213. {
  214. $client = $this->mBufidRooms[$bufid];
  215. $this->block($bufid);
  216. $ret = $client->push($content);
  217. $this->unblock($bufid);
  218. return true;
  219. }
  220. return false;
  221. }
  222. private function room_bufid()
  223. {
  224. $count = count($this->mBufidRooms);
  225. if($count <= 0) return false;
  226. if($this->mLastBuilding >= $count) {
  227. $this->mLastBuilding = 0;
  228. }
  229. $pos = 0;
  230. foreach ($this->mBufidRooms as $bufid => $client)
  231. {
  232. if($pos != $this->mLastBuilding) {
  233. $pos++;
  234. continue;
  235. }
  236. else {
  237. break;
  238. }
  239. }
  240. return $bufid;
  241. }
  242. private function success($datas)
  243. {
  244. $code = errcode::Success;
  245. $data['code'] = $code;
  246. $data['message'] = errcode::msg($code);
  247. $data['data'] = $datas;
  248. $data['msgtype'] = "reply";
  249. return json_encode($data);
  250. }
  251. private function error($code,$message = '')
  252. {
  253. if (empty($message)) {
  254. $message = errcode::msg($code);
  255. }
  256. $data['code'] = $code;
  257. $data['message'] = $message;
  258. $data['datas'] = null;
  259. $data['msgtype'] = "reply";
  260. return json_encode($data);
  261. }
  262. }