agent_manager.lua 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. local skynet = require "skynet"
  2. local sockethelper = require "http.sockethelper"
  3. local socket = require "skynet.socket"
  4. local runconfig = require "run_config"
  5. local tools = require "tools"
  6. local CMD = {}
  7. local agents = {}
  8. function traceback(err)
  9. skynet.error(tostring(err))
  10. skynet.error(debug.traceback())
  11. end
  12. local dispatch = function(session, address, cmd, ...)
  13. local fun = CMD[cmd]
  14. if not fun then
  15. skynet.ret()
  16. return
  17. end
  18. local ret = table.pack(xpcall(fun, traceback, address, ...))
  19. local isok = ret[1]
  20. if not isok then
  21. skynet.ret()
  22. return
  23. end
  24. skynet.retpack(table.unpack(ret,2))
  25. end
  26. function getOneAgent()
  27. -- for i = 1, #agents, 1 do
  28. -- local isActive = skynet.call(agents[i],"lua",nil,nil,"isActive")
  29. -- if isActive then
  30. -- local isRunTask = skynet.call(agents[i],"lua",nil,nil,"isRunStak")
  31. -- if(isRunTask==false) then
  32. -- return agents[i]
  33. -- end
  34. -- end
  35. -- end
  36. local isActive = skynet.call(agents[1],"lua",nil,nil,"isActive")
  37. if isActive then
  38. return agents[1]
  39. end
  40. return nil
  41. end
  42. --更新筛选配置
  43. function CMD.updateFilterConfig(_,msg_body)
  44. local agent = getOneAgent()
  45. if agent~=nil then
  46. skynet.call(agent,"lua",nil,nil,"updateFilterConfig",msg_body)
  47. else
  48. skynet.error("把任务放到队列")
  49. end
  50. end
  51. --更新主体配置
  52. function CMD.updateMainConfig(_,msg_body)
  53. local agent = getOneAgent()
  54. if agent~=nil then
  55. skynet.call(agent,"lua",nil,nil,"updateMainConfig",msg_body)
  56. else
  57. skynet.error("把任务放到队列")
  58. end
  59. end
  60. --更新App配置
  61. function CMD.updateAppConfig(_,msg_body)
  62. local agent = getOneAgent()
  63. if agent~=nil then
  64. skynet.call(agent,"lua",nil,nil,"updateAppConfig",msg_body)
  65. else
  66. skynet.error("把任务放到队列")
  67. end
  68. end
  69. --更新平台配置
  70. function CMD.updatePlatformConfig(_,msg_body)
  71. local agent = getOneAgent()
  72. if agent~=nil then
  73. skynet.call(agent,"lua",nil,nil,"updatePlatformConfig",msg_body)
  74. else
  75. skynet.error("把任务放到队列")
  76. end
  77. end
  78. --添加主体
  79. function CMD.addMain(_,msg_body)
  80. local agent = getOneAgent()
  81. if agent~=nil then
  82. skynet.call(agent,"lua",nil,nil,"addMain",msg_body)
  83. else
  84. skynet.error("把任务放到队列")
  85. end
  86. end
  87. --收到一个获取黑岩书的任务
  88. function CMD.task_get_heiyan_book()
  89. end
  90. skynet.start(function()
  91. local protocol = "ws"
  92. -- for i= 1, 3 do --开启30个服务用来接收消息
  93. -- agents[i] = skynet.newservice("agent", "agent", protocol)
  94. -- end
  95. agents[1] = skynet.newservice("agent", "agent", protocol)
  96. local balance = 1
  97. local id = socket.listen("0.0.0.0",runconfig.wsProt )
  98. socket.start(id , function(id, addr)
  99. skynet.send(agents[balance], "lua", id,addr,"connect")
  100. balance = balance + 1
  101. if balance > #agents then
  102. balance = 1
  103. end
  104. end)
  105. skynet.dispatch("lua", dispatch)
  106. end)