zmq.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2021-2022 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. zmq "github.com/pebbe/zmq4"
  20. )
  21. type zmqSource struct {
  22. subscriber *zmq.Socket
  23. srv string
  24. topic string
  25. cancel context.CancelFunc
  26. }
  27. func (s *zmqSource) Configure(topic string, props map[string]interface{}) error {
  28. s.topic = topic
  29. srv, ok := props["server"]
  30. if !ok {
  31. return fmt.Errorf("zmq source is missing property server")
  32. }
  33. s.srv = srv.(string)
  34. return nil
  35. }
  36. func (s *zmqSource) Open(ctx api.StreamContext, consumer chan<- api.SourceTuple, errCh chan<- error) {
  37. logger := ctx.GetLogger()
  38. var err error
  39. s.subscriber, err = zmq.NewSocket(zmq.SUB)
  40. if err != nil {
  41. errCh <- fmt.Errorf("zmq source fails to create socket: %v", err)
  42. }
  43. err = s.subscriber.Connect(s.srv)
  44. if err != nil {
  45. errCh <- fmt.Errorf("zmq source fails to connect to %s: %v", s.srv, err)
  46. }
  47. s.subscriber.SetSubscribe(s.topic)
  48. logger.Debugf("zmq source subscribe to topic %s", s.topic)
  49. exeCtx, cancel := ctx.WithCancel()
  50. s.cancel = cancel
  51. logger.Debugf("start to listen")
  52. for {
  53. msgs, err := s.subscriber.RecvMessageBytes(0)
  54. if err != nil {
  55. id, err := s.subscriber.GetIdentity()
  56. errCh <- fmt.Errorf("zmq source getting message %s error: %v", id, err)
  57. } else {
  58. logger.Debugf("zmq source receive %v", msgs)
  59. var m []byte
  60. for i, msg := range msgs {
  61. if i == 0 && s.topic != "" {
  62. continue
  63. }
  64. m = append(m, msg...)
  65. }
  66. meta := make(map[string]interface{})
  67. if s.topic != "" {
  68. meta["topic"] = string(msgs[0])
  69. }
  70. result, e := ctx.Decode(m)
  71. if e != nil {
  72. logger.Errorf("Invalid data format, cannot decode %v with error %s", m, e)
  73. } else {
  74. consumer <- api.NewDefaultSourceTuple(result, meta)
  75. }
  76. }
  77. select {
  78. case <-exeCtx.Done():
  79. logger.Infof("zmq source done")
  80. if s.subscriber != nil {
  81. s.subscriber.Close()
  82. }
  83. return
  84. default:
  85. //do nothing
  86. }
  87. }
  88. }
  89. func (s *zmqSource) Close(ctx api.StreamContext) error {
  90. if s.cancel != nil {
  91. s.cancel()
  92. }
  93. return nil
  94. }
  95. func Zmq() api.Source {
  96. return &zmqSource{}
  97. }