New file |
| | |
| | | package ruleserver |
| | | |
| | | import ( |
| | | "basic.com/pubsub/protomsg.git" |
| | | "basic.com/valib/gopherdiscovery.git" |
| | | "github.com/gogo/protobuf/proto" |
| | | "ruleprocess/logger" |
| | | "time" |
| | | ) |
| | | |
| | | type PubSubServer struct { |
| | | |
| | | } |
| | | |
| | | const ( |
| | | Discovery_Server = "tcp://0.0.0.0:40009" |
| | | Discovery_UrlPubSub = "tcp://0.0.0.0:50009" |
| | | ) |
| | | |
| | | //启动discovery的server |
| | | var discoveryServer *gopherdiscovery.DiscoveryServer |
| | | var videotapChan chan *protomsg.VideotapeInfo |
| | | func StartServer() { |
| | | var clients []string |
| | | var err error |
| | | var ( |
| | | defaultOpts = gopherdiscovery.Options{ |
| | | SurveyTime: 3 * time.Second, |
| | | //RecvDeadline: 3 * time.Second, |
| | | PollTime: 5 * time.Second, |
| | | } |
| | | ) |
| | | |
| | | discoveryServer, err = gopherdiscovery.Server(Discovery_Server, Discovery_UrlPubSub, defaultOpts) |
| | | |
| | | logger.Debug("server: ", discoveryServer) |
| | | logger.Debug("err:",err) |
| | | logger.Debug("clients: ",clients) |
| | | |
| | | videotapChan = make(chan *protomsg.VideotapeInfo) |
| | | |
| | | for { |
| | | select { |
| | | case dbMsg := <-videotapChan: |
| | | publishMessage(dbMsg) |
| | | } |
| | | } |
| | | } |
| | | |
| | | //广播数据库改变的msg |
| | | func publishMessage(msg *protomsg.VideotapeInfo) { |
| | | sendBytes,err := proto.Marshal(msg) |
| | | if err ==nil{ |
| | | logger.Debug("看看这个疑似空指针的东西",discoveryServer) |
| | | discoveryServer.PublishMsg(string(sendBytes)) |
| | | } |
| | | } |
| | | |
| | | func AddLxMessage(msg *protomsg.VideotapeInfo) { |
| | | videotapChan <- msg |
| | | } |