Project Moscato Team Messaging Middleware Implemetation Message Middleware by Golang Operate as Secure, Effectively
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

matching.go 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package modules
  2. import (
  3. "errors"
  4. _ "errors"
  5. "github.com/juliangruber/go-intersect"
  6. "reflect"
  7. )
  8. type match_manager struct {
  9. match_count int
  10. }
  11. // Matching -> Return (list of IP addresses of matched subs, Pub Msg, error)
  12. func (moscato *Moscato) Matching(msg MsgUnit) {
  13. //msg := moscato.queue.pop(true)
  14. topic := msg.(PublishMsg).Topic
  15. value := msg.(PublishMsg).Value
  16. sub_mng := moscato.SubscriptionManager
  17. // listß
  18. ret := make([]string, 0)
  19. // list for matched range subscriptions
  20. big := make([]int, 0)
  21. small := make([]int, 0)
  22. // 1. Find (topicNode[Topic] == msg.Topic) Node
  23. topicPtr := sub_mng.list
  24. pos := topicPtr.getTopicNodePos(topic)
  25. // Don't Exist topicNode
  26. if pos == nil {
  27. moscato.SendQueue <- myType{nil, msg, errors.New("Don't Exist Matching Topic")}
  28. } else {
  29. // 2. Traverse all valueNode -> and Match
  30. valPtr := pos.list.head
  31. for valPtr != nil {
  32. compare := Compare(valPtr.val, value)
  33. if compare < 0 { // sub.val > pub.val
  34. // single : { >, >= }
  35. // (1) case : >
  36. for i := 0; i < len(valPtr.single2sub_b); i++ {
  37. sub := valPtr.single2sub_b[i]
  38. ip := sub_mng.sub2ip[sub]
  39. ret = append(ret, ip)
  40. }
  41. // (2) case : >=
  42. for i := 0; i < len(valPtr.single2sub_eb); i++ {
  43. sub := valPtr.single2sub_eb[i]
  44. ip := sub_mng.sub2ip[sub]
  45. ret = append(ret, ip)
  46. }
  47. // range : { >, >= }
  48. // (1) case : >
  49. for i := 0; i < len(valPtr.range2sub_b); i++ {
  50. sub := valPtr.range2sub_b[i]
  51. big = append(big, sub)
  52. }
  53. // (2) case : >=
  54. for i := 0; i < len(valPtr.range2sub_eb); i++ {
  55. sub := valPtr.range2sub_eb[i]
  56. big = append(big, sub)
  57. }
  58. } else if compare > 0 { // sub.val < pub.val
  59. // single : { <, <= }
  60. // (1) case : <
  61. for i := 0; i < len(valPtr.single2sub_s); i++ {
  62. sub := valPtr.single2sub_s[i]
  63. ip := sub_mng.sub2ip[sub]
  64. ret = append(ret, ip)
  65. }
  66. // (2) case : <=
  67. for i := 0; i < len(valPtr.single2sub_es); i++ {
  68. sub := valPtr.single2sub_es[i]
  69. ip := sub_mng.sub2ip[sub]
  70. ret = append(ret, ip)
  71. }
  72. // range : { <, <= }
  73. // (1) case : <
  74. for i := 0; i < len(valPtr.range2sub_s); i++ {
  75. sub := valPtr.range2sub_s[i]
  76. small = append(small, sub)
  77. }
  78. // (2) case : <=
  79. for i := 0; i < len(valPtr.range2sub_es); i++ {
  80. sub := valPtr.range2sub_es[i]
  81. small = append(small, sub)
  82. }
  83. } else { // sub.val == pub.val
  84. // single : { <=, >=, ==}
  85. // (1) case : <=
  86. for i := 0; i < len(valPtr.single2sub_es); i++ {
  87. sub := valPtr.single2sub_es[i]
  88. ip := sub_mng.sub2ip[sub]
  89. ret = append(ret, ip)
  90. }
  91. // (2) case : >=
  92. for i := 0; i < len(valPtr.single2sub_eb); i++ {
  93. sub := valPtr.single2sub_eb[i]
  94. ip := sub_mng.sub2ip[sub]
  95. ret = append(ret, ip)
  96. }
  97. // (3) case : ==
  98. for i := 0; i < len(valPtr.single2sub_e); i++ {
  99. sub := valPtr.single2sub_e[i]
  100. ip := sub_mng.sub2ip[sub]
  101. ret = append(ret, ip)
  102. }
  103. // range : { <=, >= }
  104. // (1) case : <=
  105. for i := 0; i < len(valPtr.range2sub_es); i++ {
  106. sub := valPtr.range2sub_es[i]
  107. small = append(small, sub)
  108. }
  109. // (2) case : >=
  110. for i := 0; i < len(valPtr.range2sub_eb); i++ {
  111. sub := valPtr.range2sub_eb[i]
  112. big = append(big, sub)
  113. }
  114. }
  115. valPtr = valPtr.next
  116. }
  117. // Add the intersection IP address of two sets (large and small) to the return list
  118. hash := intersect.Hash(small, big)
  119. list := reflect.ValueOf(hash)
  120. for i := 0; i < list.Len(); i++ {
  121. sub := list.Index(i).Interface().(int)
  122. ip := sub_mng.sub2ip[sub]
  123. ret = append(ret, ip)
  124. }
  125. moscato.MatchingManager.match_count++
  126. moscato.SendQueue <- myType{ret, msg, nil}
  127. }
  128. }