rpc.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package rpc
  2. import (
  3. cmmdl "go-common/app/service/main/spy/model"
  4. "go-common/library/net/rpc"
  5. "go-common/library/net/rpc/context"
  6. "go-common/app/service/main/spy/conf"
  7. "go-common/app/service/main/spy/model"
  8. "go-common/app/service/main/spy/service"
  9. )
  10. // RPC server def.
  11. type RPC struct {
  12. s *service.Service
  13. }
  14. // New create instance of rpc server2 and return.
  15. func New(c *conf.Config, s *service.Service) (svr *rpc.Server) {
  16. r := &RPC{s: s}
  17. svr = rpc.NewServer(c.RPCServer)
  18. if err := svr.Register(r); err != nil {
  19. panic(err)
  20. }
  21. return
  22. }
  23. // Ping check rpc server health.
  24. func (r *RPC) Ping(c context.Context, arg *struct{}, res *struct{}) (err error) {
  25. return
  26. }
  27. // UserScore rpc req for getting user spy score.
  28. func (r *RPC) UserScore(c context.Context, arg *cmmdl.ArgUserScore, res *cmmdl.UserScore) (err error) {
  29. var (
  30. ui *model.UserInfo
  31. )
  32. if ui, err = r.s.UserInfo(c, arg.Mid, arg.IP); ui != nil && err == nil {
  33. *res = cmmdl.UserScore{Mid: ui.Mid, Score: ui.Score}
  34. }
  35. return
  36. }
  37. // HandleEvent rpc req for handling spy event , maybe from spy-job.
  38. func (r *RPC) HandleEvent(c context.Context, arg *cmmdl.ArgHandleEvent, res *struct{}) (err error) {
  39. var event = &model.EventMessage{
  40. Time: arg.Time,
  41. IP: arg.IP,
  42. Service: arg.Service,
  43. Event: arg.Event,
  44. ActiveMid: arg.ActiveMid,
  45. TargetMid: arg.TargetMid,
  46. TargetID: arg.TargetID,
  47. Args: arg.Args,
  48. Result: arg.Result,
  49. Effect: arg.Effect,
  50. RiskLevel: arg.RiskLevel,
  51. }
  52. return r.s.HandleEvent(c, event)
  53. }
  54. // ReBuildPortrait rpc.
  55. func (r *RPC) ReBuildPortrait(c context.Context, arg *cmmdl.ArgReBuild, res *struct{}) (err error) {
  56. return r.s.ReBuildPortrait(c, arg.Mid, arg.Reason)
  57. }
  58. // UpdateBaseScore rpc.
  59. func (r *RPC) UpdateBaseScore(c context.Context, arg *cmmdl.ArgReset, res *struct{}) (err error) {
  60. return r.s.UpdateBaseScore(c, arg)
  61. }
  62. // UpdateEventScore rpc.
  63. func (r *RPC) UpdateEventScore(c context.Context, arg *cmmdl.ArgReset, res *struct{}) (err error) {
  64. return r.s.UpdateEventScore(c, arg)
  65. }
  66. // UserInfo rpc.
  67. func (r *RPC) UserInfo(c context.Context, arg *cmmdl.ArgUser, res *cmmdl.UserInfo) (err error) {
  68. var (
  69. ui *model.UserInfo
  70. )
  71. if ui, err = r.s.UserInfo(c, arg.Mid, arg.IP); ui != nil && err == nil {
  72. *res = *ui
  73. }
  74. return
  75. }
  76. // ClearReliveTimes rpc.
  77. func (r *RPC) ClearReliveTimes(c context.Context, arg *cmmdl.ArgReset, res *struct{}) (err error) {
  78. return r.s.ClearReliveTimes(c, arg)
  79. }
  80. // StatByID rpc.
  81. func (r *RPC) StatByID(c context.Context, arg *cmmdl.ArgStat, res *[]*cmmdl.Statistics) (err error) {
  82. var (
  83. stat []*model.Statistics
  84. )
  85. if stat, err = r.s.StatByIDGroupEvent(c, arg.Mid, arg.ID); stat != nil && err == nil {
  86. *res = stat
  87. }
  88. return
  89. }
  90. // RefreshBaseScore rpc.
  91. func (r *RPC) RefreshBaseScore(c context.Context, arg *cmmdl.ArgReset, res *struct{}) (err error) {
  92. return r.s.RefreshBaseScore(c, arg)
  93. }