notification_handler.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package handler
  2. import (
  3. "bet24.com/log"
  4. pb "bet24.com/servers/micros/notification/proto"
  5. "context"
  6. "fmt"
  7. )
  8. var instance *Notification
  9. func GetInstance() *Notification {
  10. if instance == nil {
  11. instance = newHandler()
  12. }
  13. return instance
  14. }
  15. func Dump(cmd, param1, param2 string) {
  16. GetInstance().dump(cmd, param1, param2)
  17. }
  18. func newHandler() *Notification {
  19. ret := new(Notification)
  20. ret.ctor()
  21. return ret
  22. }
  23. type Notification struct {
  24. mgr *manager
  25. }
  26. func (h *Notification) ctor() {
  27. h.mgr = newManager()
  28. }
  29. func (h *Notification) dump(cmd, param1, param2 string) {
  30. //log.Debug("DbEngine.Dump %s,%s,%s", cmd, param1, param2)
  31. switch cmd {
  32. case "user":
  33. h.mgr.dumpUser(param1)
  34. case "receiver":
  35. h.mgr.dumpReceiver()
  36. default:
  37. log.Debug("notification.Dump unhandled %s:%s", cmd, param1)
  38. }
  39. }
  40. func (h *Notification) SayHello(ctx context.Context, req *pb.Request, rsp *pb.Response) error {
  41. rsp.Data = fmt.Sprintf("Hello from %s:%s", pb.ServiceName, req.Name)
  42. return nil
  43. }
  44. func (h *Notification) AddNotification(ctx context.Context, req *pb.Request_AddNotification, rsp *pb.Response) error {
  45. if h.mgr.addNotification(req.UserId, req.NotificationId, req.Data) {
  46. rsp.RetCode = 1
  47. }
  48. return nil
  49. }
  50. func (h *Notification) GetNotifications(ctx context.Context, req *pb.Request, rsp *pb.Response) error {
  51. rsp.Data = h.mgr.getNotifications(req.UserId)
  52. return nil
  53. }
  54. func (h *Notification) Subscribe(ctx context.Context, req *pb.Request, rsp *pb.Response) error {
  55. h.mgr.subscribe(req.Name)
  56. return nil
  57. }
  58. func (d *Notification) AddUser(ctx context.Context, req *pb.Request, rsp *pb.Response) error {
  59. d.mgr.onUserEnter(req.UserId)
  60. return nil
  61. }
  62. func (d *Notification) RemoveUser(ctx context.Context, req *pb.Request, rsp *pb.Response) error {
  63. d.mgr.onUserExit(req.UserId)
  64. return nil
  65. }