processor.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2016/10/6
  6. * Time: 下午10:35
  7. */
  8. namespace search;
  9. require_once(BASE_ROOT_PATH . '/helper/message/msgutil.php');
  10. require_once(BASE_ROOT_PATH . '/helper/message/subscriber.php');
  11. use StatesHelper;
  12. use Log;
  13. use event\IProcessor;
  14. use process_looper;
  15. class processor implements IProcessor
  16. {
  17. const GetRelatedWord = 1;
  18. const SearchReasult = 2;
  19. const MatchPrice = 3;
  20. const PromoteGoods = 4;
  21. const ValidateArea = 20;
  22. public function onStart()
  23. {
  24. }
  25. public function onConnected($bufid,$stream,$host,$port,$args)
  26. {
  27. }
  28. public function onRequest($bufid, $body)
  29. {
  30. if(StatesHelper::fetch_state('init')) {
  31. Log::record("searcher::instance reload data.",Log::DEBUG);
  32. searcher::instance()->init();
  33. }
  34. $body = unserialize($body);
  35. $type = $body['type'];
  36. Log::record("onRequest : {$type}",Log::DEBUG);
  37. if($type == self::GetRelatedWord)
  38. {
  39. $words = $body['keyword'];
  40. $result = searcher::instance()->get_word($words);
  41. $data = serialize($result);
  42. }
  43. elseif($type == self::SearchReasult) {
  44. $params = $body['params'];
  45. $result = searcher::instance()->get_result($params);
  46. $data = serialize($result);
  47. }
  48. elseif($type == self::MatchPrice) {
  49. $params = $body['params'];
  50. $result = searcher::instance()->match_price($params);
  51. $data = serialize($result);
  52. }
  53. elseif($type == self::PromoteGoods) {
  54. $params = $body['params'];
  55. $result = searcher::instance()->promote_goods($params);
  56. $data = serialize($result);
  57. }
  58. elseif ($type == self::ValidateArea)
  59. {
  60. $params = $body['params'];
  61. $area_id = $params['area_id'];
  62. $result = area_library::instance()->country($area_id);
  63. if($result == false) {
  64. $data = serialize(['result' => false]);
  65. } else {
  66. $result['result'] = true;
  67. $data = serialize($result);
  68. }
  69. }
  70. else {
  71. ob_clean();
  72. return false;
  73. }
  74. process_looper::instance()->write($bufid,$data);
  75. return true;
  76. }
  77. public function onClose($bufid)
  78. {
  79. }
  80. }