factory_processor.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 event\IProcessor;
  10. use process_looper;
  11. use errcode;
  12. use uniquer;
  13. use Log;
  14. class factory_processor implements IProcessor
  15. {
  16. const room_connection = "room_connection";
  17. private $mFactory;
  18. private $mRoomPos;
  19. private $mAccUniquer;
  20. private $mBufidRooms;
  21. public function __construct()
  22. {
  23. $this->mFactory = new factory();
  24. $this->mRoomPos = 0;
  25. $this->mAccUniquer = new uniquer();
  26. $this->mBufidRooms = [];
  27. }
  28. public function onStart()
  29. {
  30. global $config;
  31. $room_addrs = $config['room_factory']['rooms_addr'];
  32. foreach ($room_addrs as $addr) {
  33. process_looper::instance()->connect($addr['host'],$addr['port'],self::room_connection);
  34. }
  35. }
  36. public function onConnected($bufid,$stream,$host,$port,$args)
  37. {
  38. if($args == self::room_connection) {
  39. Log::record("room srv bufid={$bufid}",Log::DEBUG);
  40. $client = new room_client($host,$port,$stream,false);
  41. $this->mBufidRooms[$bufid] = $client;
  42. $this->block($bufid);
  43. $client->init_rooms($bufid);
  44. $this->unblock($bufid);
  45. }
  46. }
  47. public function onClose($bufid)
  48. {
  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. process_looper::instance()->connect($addr['host'],$addr['port'],self::room_connection);
  57. }
  58. }
  59. private function block($bufid) {
  60. process_looper::instance()->block($bufid);
  61. }
  62. private function unblock($bufid) {
  63. process_looper::instance()->unblock($bufid);
  64. }
  65. public function onRequest($bufid, $body)
  66. {
  67. $input = json_decode($body,true);
  68. if($input == false) {
  69. return false;
  70. }
  71. $act = $input['act'];
  72. if(empty($act)) return false;
  73. if($act == proto_type::act_fcgi) {
  74. $ret = $this->onFcgi($bufid,$input);
  75. process_looper::instance()->write($bufid,$ret);
  76. return true;
  77. }
  78. elseif($act == proto_type::act_access) {
  79. $ret = $this->onAccess($bufid,$input);
  80. process_looper::instance()->write($bufid,$ret);
  81. return true;
  82. }
  83. else {
  84. return false;
  85. }
  86. }
  87. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  88. private function onFcgi($bufid, $input)
  89. {
  90. $op = $input['op'];
  91. if($op == 'create')
  92. {
  93. $ret = $this->mFactory->create($input,$fnew);
  94. if($ret != false)
  95. {
  96. $invite = $input['invite'];
  97. if($invite)
  98. {
  99. $roomid = $ret['room'];
  100. $inviter = $ret['creator'];
  101. if($roomid <= 0 || $inviter <= 0) {
  102. return $this->error(errcode::ErrRoomCreate);
  103. }
  104. if($this->build($roomid,$fnew) == false) {
  105. return $this->error(errcode::ErrRoomBuild);
  106. }
  107. $result = $this->invite($roomid,$inviter,[$inviter]);
  108. if($result != false) {
  109. $ret = array_merge($ret,$result);
  110. return $this->success($ret);
  111. } else {
  112. return $this->error(errcode::ErrRoomInvite);
  113. }
  114. }
  115. else {
  116. return $this->error(errcode::ErrRoomCreate);
  117. }
  118. }
  119. return $this->error(errcode::ErrRoomCreate);
  120. }
  121. elseif($op == 'invite')
  122. {
  123. $roomid = intval($input['room']);
  124. $inviter = intval($input['inviter']);
  125. $invitees = $input['invitees'];
  126. if($roomid <= 0 || $inviter <= 0 || empty($invitees)) {
  127. return $this->error(errcode::ErrRoomInvite);
  128. }
  129. $client = $this->find_room($roomid,$bufid);
  130. if($client == false) {
  131. $this->build($roomid);
  132. }
  133. $ret = $this->invite($roomid,$inviter,$invitees);
  134. if($ret != false) {
  135. return $this->success($ret);
  136. } else {
  137. return $this->error(errcode::ErrRoomInvite);
  138. }
  139. }
  140. elseif($op == 'change')
  141. {
  142. $roomid = intval($input['room']);
  143. if($roomid <= 0) {
  144. return $this->error(errcode::ErrRoomChange);
  145. }
  146. $client = $this->find_room($roomid,$bufid);
  147. if($client == false) {
  148. $this->build($roomid);
  149. }
  150. $ret = $this->change($roomid);
  151. if($ret != false) {
  152. return $this->success($ret);
  153. } else {
  154. return $this->error(errcode::ErrRoomChange);
  155. }
  156. }
  157. elseif($op == 'leave')
  158. {
  159. $roomid = intval($input['room']);
  160. $user = intval($input['user']);
  161. if($roomid <= 0 || $user <= 0) {
  162. return $this->error(errcode::ErrRoomLeave);
  163. }
  164. $client = $this->find_room($roomid,$bufid);
  165. if($client == false) {
  166. $this->build($roomid);
  167. }
  168. $ret = $this->leave($roomid,$user);
  169. if($ret != false) {
  170. return $this->success($ret);
  171. } else {
  172. return $this->error(errcode::ErrRoomInvite);
  173. }
  174. }
  175. elseif($op == 'push')
  176. {
  177. $content = $input['content'];
  178. $ret = $this->push($content);
  179. if($ret != false) {
  180. return $this->success(NULL);
  181. } else {
  182. return $this->error(errcode::ErrRoomPush);
  183. }
  184. }
  185. else {
  186. return $this->error(errcode::ErrRoomFactoryOp);
  187. }
  188. }
  189. private function onAccess($bufid,$input)
  190. {
  191. $op = $input['op'];
  192. if($op == 'who')
  193. {
  194. $this->mAccUniquer->add_value($bufid);
  195. return $this->success(['act' => proto_type::act_access,'op' => 'who']);
  196. }
  197. elseif($op == 'build')
  198. {
  199. $roomid = intval($input['room']);
  200. $client = $this->find_room($roomid,$bufid);
  201. if($client == false) {
  202. $this->build($roomid);
  203. }
  204. return $this->success(['act' => proto_type::act_access,'op' => $op,'room' => $roomid]);
  205. }
  206. else {
  207. return $this->error(errcode::ErrParamter);
  208. }
  209. }
  210. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  211. private function find_room($roomid,&$outbufid)
  212. {
  213. foreach ($this->mBufidRooms as $bufid => $client)
  214. {
  215. if($client->contain_room($roomid)) {
  216. $outbufid = $bufid;
  217. return $client;
  218. }
  219. }
  220. return false;
  221. }
  222. private function build($roomid,$newroom = false)
  223. {
  224. $client = $this->find_room($roomid,$bufid);
  225. if($client != false) {
  226. $this->block($bufid);
  227. $ret = $client->build($roomid,$newroom);
  228. $this->unblock($bufid);
  229. return $ret;
  230. }
  231. $bufid = $this->room_bufid();
  232. if($bufid != false)
  233. {
  234. $client = $this->mBufidRooms[$bufid];
  235. $this->block($bufid);
  236. $ret = $client->build($roomid,$newroom);
  237. $this->unblock($bufid);
  238. if($ret != false) {
  239. $client->add_room($roomid);
  240. }
  241. return $ret;
  242. }
  243. else {
  244. return false;
  245. }
  246. }
  247. private function invite($roomid, $inviter,$invitees)
  248. {
  249. $client = $this->find_room($roomid,$bufid);
  250. if($client != false) {
  251. $this->block($bufid);
  252. $ret = $client->invite($roomid,$inviter,$invitees);
  253. $this->unblock($bufid);
  254. return $ret;
  255. }
  256. return false;
  257. }
  258. private function change($roomid)
  259. {
  260. $client = $this->find_room($roomid,$bufid);
  261. if($client != false) {
  262. $this->block($bufid);
  263. $ret = $client->change($roomid);
  264. $this->unblock($bufid);
  265. return $ret;
  266. }
  267. return false;
  268. }
  269. private function leave($roomid, $user)
  270. {
  271. foreach ($this->mBufidRooms as $bufid => $client)
  272. {
  273. if($client->contain_room($roomid))
  274. {
  275. $this->block($bufid);
  276. $ret = $client->leave($roomid,$user);
  277. $this->unblock($bufid);
  278. return $ret;
  279. }
  280. }
  281. }
  282. private function push($content)
  283. {
  284. $bufid = $this->room_bufid();
  285. if($bufid != false)
  286. {
  287. $client = $this->mBufidRooms[$bufid];
  288. $this->block($bufid);
  289. $ret = $client->push($content);
  290. $this->unblock($bufid);
  291. return true;
  292. }
  293. return false;
  294. }
  295. private function room_bufid()
  296. {
  297. $count = count($this->mBufidRooms);
  298. if($count <= 0) return false;
  299. $pos = $this->mRoomPos % $count;
  300. $i = 0;
  301. foreach ($this->mBufidRooms as $bufid => $client)
  302. {
  303. if($i == $pos) break;
  304. $pos++;
  305. }
  306. $this->mRoomPos++;
  307. Log::record("select bufid = {$bufid}",Log::DEBUG);
  308. return $bufid;
  309. }
  310. private function success($datas)
  311. {
  312. $code = errcode::Success;
  313. $data['code'] = $code;
  314. $data['message'] = errcode::msg($code);
  315. $data['data'] = $datas;
  316. $data['msgtype'] = "reply";
  317. return json_encode($data);
  318. }
  319. private function error($code,$message = '')
  320. {
  321. if (empty($message)) {
  322. $message = errcode::msg($code);
  323. }
  324. $data['code'] = $code;
  325. $data['message'] = $message;
  326. $data['datas'] = null;
  327. $data['msgtype'] = "reply";
  328. return json_encode($data);
  329. }
  330. }