zmq.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2021 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "context"
  17. "fmt"
  18. "github.com/lf-edge/ekuiper/pkg/api"
  19. "github.com/lf-edge/ekuiper/pkg/message"
  20. zmq "github.com/pebbe/zmq4"
  21. )
  22. type zmqSource struct {
  23. subscriber *zmq.Socket
  24. srv string
  25. topic string
  26. messageFormat string
  27. cancel context.CancelFunc
  28. }
  29. func (s *zmqSource) Configure(topic string, props map[string]interface{}) error {
  30. s.topic = topic
  31. srv, ok := props["server"]
  32. if !ok {
  33. return fmt.Errorf("zmq source is missing property server")
  34. }
  35. s.srv = srv.(string)
  36. f, ok := props["format"]
  37. if !ok {
  38. s.messageFormat = message.FormatJson
  39. } else {
  40. s.messageFormat = f.(string)
  41. }
  42. return nil
  43. }
  44. func (s *zmqSource) Open(ctx api.StreamContext, consumer chan<- api.SourceTuple, errCh chan<- error) {
  45. logger := ctx.GetLogger()
  46. var err error
  47. s.subscriber, err = zmq.NewSocket(zmq.SUB)
  48. if err != nil {
  49. errCh <- fmt.Errorf("zmq source fails to create socket: %v", err)
  50. }
  51. err = s.subscriber.Connect(s.srv)
  52. if err != nil {
  53. errCh <- fmt.Errorf("zmq source fails to connect to %s: %v", s.srv, err)
  54. }
  55. s.subscriber.SetSubscribe(s.topic)
  56. logger.Debugf("zmq source subscribe to topic %s", s.topic)
  57. exeCtx, cancel := ctx.WithCancel()
  58. s.cancel = cancel
  59. logger.Debugf("start to listen")
  60. for {
  61. msgs, err := s.subscriber.RecvMessageBytes(0)
  62. if err != nil {
  63. id, err := s.subscriber.GetIdentity()
  64. errCh <- fmt.Errorf("zmq source getting message %s error: %v", id, err)
  65. } else {
  66. logger.Debugf("zmq source receive %v", msgs)
  67. var m []byte
  68. for i, msg := range msgs {
  69. if i == 0 && s.topic != "" {
  70. continue
  71. }
  72. m = append(m, msg...)
  73. }
  74. meta := make(map[string]interface{})
  75. if s.topic != "" {
  76. meta["topic"] = string(msgs[0])
  77. }
  78. result, e := message.Decode(m, s.messageFormat)
  79. if e != nil {
  80. logger.Errorf("Invalid data format, cannot decode %v to %s format with error %s", m, s.messageFormat, e)
  81. } else {
  82. consumer <- api.NewDefaultSourceTuple(result, meta)
  83. }
  84. }
  85. select {
  86. case <-exeCtx.Done():
  87. logger.Infof("zmq source done")
  88. if s.subscriber != nil {
  89. s.subscriber.Close()
  90. }
  91. return
  92. default:
  93. //do nothing
  94. }
  95. }
  96. }
  97. func (s *zmqSource) Close(ctx api.StreamContext) error {
  98. if s.cancel != nil {
  99. s.cancel()
  100. }
  101. return nil
  102. }
  103. func Zmq() api.Source {
  104. return &zmqSource{}
  105. }