|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + log "github.com/Sirupsen/logrus" |
| 7 | + event_rpc "github.com/docker/infrakit/pkg/rpc/event" |
| 8 | + "github.com/docker/infrakit/pkg/spi/application" |
| 9 | + "github.com/docker/infrakit/pkg/spi/event" |
| 10 | + "github.com/docker/infrakit/pkg/template" |
| 11 | + "github.com/docker/infrakit/pkg/types" |
| 12 | + MQTT "github.com/eclipse/paho.mqtt.golang" |
| 13 | + "sync" |
| 14 | +) |
| 15 | + |
| 16 | +func NewEventRepeater(eSource string, eSink string, protocol string, allowall bool) application.Plugin { |
| 17 | + sub, err := event_rpc.NewClient(eSource) |
| 18 | + if err != nil { |
| 19 | + log.Errorf("Cannot Create Event Client :%v", err) |
| 20 | + return nil |
| 21 | + } |
| 22 | + pub, err := newSinkClient(protocol, eSink) |
| 23 | + if err != nil { |
| 24 | + log.Errorf("Cannot Create Sink Client protocol :%v Server :%v :%v", protocol, eSink, err) |
| 25 | + return nil |
| 26 | + } |
| 27 | + e := &eventRepeater{ |
| 28 | + EventSource: eSource, |
| 29 | + EventSink: eSink, |
| 30 | + Events: make(map[string]*RepeatRule), |
| 31 | + Protocol: protocol, |
| 32 | + stop: make(chan bool), |
| 33 | + sourceEClient: sub.(event.Subscriber), |
| 34 | + sinkEClient: pub, |
| 35 | + allowAll: allowall, |
| 36 | + eventListMt: new(sync.Mutex), |
| 37 | + } |
| 38 | + go e.serve() |
| 39 | + return e |
| 40 | +} |
| 41 | + |
| 42 | +func newSinkClient(protocol string, broker string) (interface{}, error) { |
| 43 | + switch protocol { |
| 44 | + case "mqtt": |
| 45 | + opts := MQTT.NewClientOptions() |
| 46 | + opts.AddBroker(broker) |
| 47 | + c := MQTT.NewClient(opts) |
| 48 | + if token := c.Connect(); token.Wait() && token.Error() != nil { |
| 49 | + return nil, token.Error() |
| 50 | + } |
| 51 | + return c, nil |
| 52 | + case "stderr": |
| 53 | + log.Info("Print Event messages to stderr. This is for debug. ") |
| 54 | + return nil, nil |
| 55 | + default: |
| 56 | + return nil, fmt.Errorf("Unkown sink protocol %s", protocol) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +type eventRepeater struct { |
| 61 | + EventSource string |
| 62 | + EventSink string |
| 63 | + Events map[string]*RepeatRule |
| 64 | + Protocol string |
| 65 | + stop chan bool |
| 66 | + eventAdd chan bool |
| 67 | + eventDel chan bool |
| 68 | + sourceEClient event.Subscriber |
| 69 | + sinkEClient interface{} |
| 70 | + allowAll bool |
| 71 | + eventListMt *sync.Mutex |
| 72 | +} |
| 73 | + |
| 74 | +type RepeatRule struct { |
| 75 | + SourceTopic string |
| 76 | + SinkTopic string |
| 77 | + SourceStopCh chan<- struct{} |
| 78 | + SinkStopCh chan bool |
| 79 | + SourceStream <-chan *event.Event |
| 80 | +} |
| 81 | + |
| 82 | +type messageData struct { |
| 83 | + SourceTopic string `json:"sourcetopic",omitempty"` |
| 84 | + SinkTopic string `json:"sinktopic",omitempty"` |
| 85 | +} |
| 86 | + |
| 87 | +func (e eventRepeater) Validate(applicationProperties *types.Any) error { |
| 88 | + return nil |
| 89 | +} |
| 90 | + |
| 91 | +func (e eventRepeater) Healthy(applicationProperties *types.Any) (application.Health, error) { |
| 92 | + return application.Healthy, nil |
| 93 | +} |
| 94 | + |
| 95 | +func (e eventRepeater) AddEvent(sourcesTopic string, sinkTopic string) error { |
| 96 | + if sourcesTopic == "" { |
| 97 | + return fmt.Errorf("Error: %s", "You must have a topic of source for add repeat event.") |
| 98 | + } |
| 99 | + if sinkTopic == "" { |
| 100 | + sinkTopic = sourcesTopic |
| 101 | + } |
| 102 | + log.Debugf("Add event %s as %s", sourcesTopic, sinkTopic) |
| 103 | + e.eventListMt.Lock() |
| 104 | + defer e.eventListMt.Unlock() |
| 105 | + if _, ok := e.Events[sourcesTopic]; ok { |
| 106 | + return fmt.Errorf("Error: %s %s", "Topic already exist. :", sourcesTopic) |
| 107 | + } |
| 108 | + stream, stop, err := e.sourceEClient.SubscribeOn(types.PathFromString(sourcesTopic)) |
| 109 | + if err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + e.Events[sourcesTopic] = &RepeatRule{ |
| 113 | + SourceTopic: sourcesTopic, |
| 114 | + SinkTopic: sinkTopic, |
| 115 | + SourceStopCh: stop, |
| 116 | + SinkStopCh: make(chan bool), |
| 117 | + SourceStream: stream, |
| 118 | + } |
| 119 | + go e.publishToSink(e.Events[sourcesTopic]) |
| 120 | + return nil |
| 121 | +} |
| 122 | + |
| 123 | +func (e eventRepeater) DelEvent(sourcesTopic string) error { |
| 124 | + if sourcesTopic == "" { |
| 125 | + return fmt.Errorf("Error: %s", "You must have a topic of source for delete repeat event.") |
| 126 | + } |
| 127 | + log.Debugf("Delete event %s", sourcesTopic) |
| 128 | + e.eventListMt.Lock() |
| 129 | + defer e.eventListMt.Unlock() |
| 130 | + if _, ok := e.Events[sourcesTopic]; !ok { |
| 131 | + return fmt.Errorf("Error: %s %s", "There is no registerd topic. :", sourcesTopic) |
| 132 | + } |
| 133 | + e.Events[sourcesTopic].SinkStopCh <- true |
| 134 | + close(e.Events[sourcesTopic].SourceStopCh) |
| 135 | + delete(e.Events, sourcesTopic) |
| 136 | + return nil |
| 137 | +} |
| 138 | + |
| 139 | +func (e eventRepeater) Stop() { |
| 140 | + e.stop <- true |
| 141 | +} |
| 142 | + |
| 143 | +func (e eventRepeater) publishToSink(rr *RepeatRule) error { |
| 144 | + templateURL := "str://{{.}}" |
| 145 | + engine, err := template.NewTemplate(templateURL, template.Options{}) |
| 146 | + if err != nil { |
| 147 | + return err |
| 148 | + } |
| 149 | + for { |
| 150 | + select { |
| 151 | + case <-rr.SinkStopCh: |
| 152 | + return nil |
| 153 | + case s, ok := <-rr.SourceStream: |
| 154 | + if !ok { |
| 155 | + log.Info("Server disconnected", "topic", rr.SourceTopic) |
| 156 | + return nil |
| 157 | + } |
| 158 | + buff, err := engine.Render(s) |
| 159 | + if err != nil { |
| 160 | + return err |
| 161 | + } |
| 162 | + switch e.Protocol { |
| 163 | + case "mqtt": |
| 164 | + e.sinkEClient.(MQTT.Client).Publish(rr.SinkTopic, 0, false, buff) |
| 165 | + case "stderr": |
| 166 | + log.Infof("Publish subtopic %s gettopic %v pubtopic %v message %s\n", rr.SourceTopic, s.Topic, rr.SinkTopic, buff) |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + return nil |
| 171 | +} |
| 172 | + |
| 173 | +func (e eventRepeater) Update(message *application.Message) error { |
| 174 | + var dataStruct []messageData |
| 175 | + err := json.Unmarshal([]byte(*message.Data), &dataStruct) |
| 176 | + if err != nil { |
| 177 | + return err |
| 178 | + } |
| 179 | + switch message.Resource { |
| 180 | + case "event": |
| 181 | + log.Debugf("Update message op %v Resource %v Data %v \n", message.Op, message.Resource, dataStruct) |
| 182 | + switch message.Op { |
| 183 | + case application.ADD: |
| 184 | + for _, d := range dataStruct { |
| 185 | + log.Debugf("Add message %v \n", d) |
| 186 | + err := e.AddEvent(d.SourceTopic, d.SinkTopic) |
| 187 | + if err != nil { |
| 188 | + return err |
| 189 | + } |
| 190 | + } |
| 191 | + case application.DELETE: |
| 192 | + for _, d := range dataStruct { |
| 193 | + err := e.DelEvent(d.SourceTopic) |
| 194 | + if err != nil { |
| 195 | + return err |
| 196 | + } |
| 197 | + } |
| 198 | + case application.UPDATE: |
| 199 | + for _, d := range dataStruct { |
| 200 | + err := e.DelEvent(d.SourceTopic) |
| 201 | + if err != nil { |
| 202 | + return err |
| 203 | + } |
| 204 | + err = e.AddEvent(d.SourceTopic, d.SinkTopic) |
| 205 | + if err != nil { |
| 206 | + return err |
| 207 | + } |
| 208 | + } |
| 209 | + case application.GET: |
| 210 | + default: |
| 211 | + log.Warnf("Unknown operation\n") |
| 212 | + } |
| 213 | + default: |
| 214 | + log.Warnf("Unknown resouces\n") |
| 215 | + } |
| 216 | + return nil |
| 217 | +} |
| 218 | + |
| 219 | +func (e eventRepeater) serve() error { |
| 220 | + if e.allowAll { |
| 221 | + e.AddEvent(".", "") |
| 222 | + } |
| 223 | + for { |
| 224 | + select { |
| 225 | + case <-e.stop: |
| 226 | + return nil |
| 227 | + } |
| 228 | + } |
| 229 | +} |
0 commit comments