notification.pb.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package proto
  2. import (
  3. "bet24.com/log"
  4. "bet24.com/servers/micros/common"
  5. subscribe "bet24.com/servers/micros/notification/subscribe/server"
  6. "context"
  7. "fmt"
  8. "github.com/smallnest/rpcx/client"
  9. "time"
  10. )
  11. const ServiceName = "notification"
  12. var consulAddr = common.Default_Consul_Addr
  13. func getClient() client.XClient {
  14. return common.GetClientPool().GetClient(ServiceName, consulAddr)
  15. }
  16. type Request struct {
  17. Name string
  18. UserId int
  19. }
  20. type Response struct {
  21. Data string
  22. RetCode int
  23. }
  24. func SetConsulAddr(addr string) {
  25. consulAddr = addr
  26. }
  27. func SayHello(name string) string {
  28. xclient := getClient()
  29. args := &Request{
  30. Name: name,
  31. }
  32. reply := &Response{}
  33. err := xclient.Call(context.Background(), "SayHello", args, reply)
  34. if err != nil {
  35. log.Debug("notification failed to call: %v", err)
  36. common.GetClientPool().RemoveClient(ServiceName)
  37. return ""
  38. }
  39. log.Debug("SayHello return %s", reply.Data)
  40. return reply.Data
  41. }
  42. type Request_AddNotification struct {
  43. UserId int
  44. NotificationId int
  45. Data string
  46. }
  47. func AddNotification(userId int, notificationId int, data string) bool {
  48. xclient := getClient()
  49. args := &Request_AddNotification{
  50. UserId: userId,
  51. NotificationId: notificationId,
  52. Data: data,
  53. }
  54. reply := &Response{}
  55. err := xclient.Call(context.Background(), "AddNotification", args, reply)
  56. if err != nil {
  57. log.Debug("notification failed to call: %v", err)
  58. common.GetClientPool().RemoveClient(ServiceName)
  59. return false
  60. }
  61. return reply.RetCode == 1
  62. }
  63. func GetNotifications(userId int) string {
  64. xclient := getClient()
  65. args := &Request{
  66. UserId: userId,
  67. }
  68. reply := &Response{}
  69. err := xclient.Call(context.Background(), "GetNotifications", args, reply)
  70. if err != nil {
  71. log.Debug("notification failed to call: %v", err)
  72. common.GetClientPool().RemoveClient(ServiceName)
  73. return ""
  74. }
  75. return reply.Data
  76. }
  77. func Subscribe(callback func(int, string), port int) {
  78. addr := fmt.Sprintf("notification_subscriber_%s_%d", common.GetLocalIp(), port)
  79. go subscribe.Run(addr, consulAddr, callback)
  80. for {
  81. xclient := getClient()
  82. args := &Request{
  83. Name: addr,
  84. }
  85. //log.Release("notification.Subscribe %s", addr)
  86. err := xclient.Call(context.Background(), "Subscribe", args, nil)
  87. if err != nil {
  88. log.Debug("Subscribe failed to call: %v", err)
  89. common.GetClientPool().RemoveClient(ServiceName)
  90. }
  91. time.Sleep(10 * time.Second)
  92. }
  93. }
  94. func AddUser(userId int) {
  95. xclient := getClient()
  96. //defer xclient.Close()
  97. args := &Request{
  98. UserId: userId,
  99. }
  100. err := xclient.Call(context.Background(), "AddUser", args, nil)
  101. if err != nil {
  102. log.Debug("AddUser failed to call: %v", err)
  103. common.GetClientPool().RemoveClient(ServiceName)
  104. }
  105. }
  106. func RemoveUser(userId int) {
  107. xclient := getClient()
  108. //defer xclient.Close()
  109. args := &Request{
  110. UserId: userId,
  111. }
  112. err := xclient.Call(context.Background(), "RemoveUser", args, nil)
  113. if err != nil {
  114. log.Debug("RemoveUser failed to call: %v", err)
  115. common.GetClientPool().RemoveClient(ServiceName)
  116. }
  117. }