| | |
| | | package gopherdiscovery |
| | | |
| | | import ( |
| | | "encoding/json" |
| | | "errors" |
| | | "log" |
| | | |
| | |
| | | |
| | | // Service that needs to be discovered, for example for a web server could be |
| | | // http://192.168.1.1:8080 |
| | | service string |
| | | service ServiceInfo |
| | | |
| | | heartbeatmsg chan []byte |
| | | |
| | |
| | | changes chan []byte |
| | | } |
| | | |
| | | func Client(urlServer string, service string) (*DiscoveryClient, error) { |
| | | return ClientWithSub(urlServer, "", service) |
| | | type ServiceInfo struct { |
| | | ServiceId string `json:"serviceId"` |
| | | Info interface{} `json:"info"` |
| | | } |
| | | |
| | | func ClientWithSub(urlServer string, urlPubSub string, service string) (*DiscoveryClient, error) { |
| | | func Client(urlServer string, serviceId string) (*DiscoveryClient, error) { |
| | | return ClientWithSub(urlServer, "", serviceId) |
| | | } |
| | | |
| | | func ClientWithSub(urlServer string, urlPubSub string, serviceId string) (*DiscoveryClient, error) { |
| | | var sock mangos.Socket |
| | | var err error |
| | | var subscriber *Subscriber |
| | |
| | | return nil, err |
| | | } |
| | | |
| | | svInfo := ServiceInfo{ |
| | | ServiceId: serviceId, |
| | | } |
| | | client := &DiscoveryClient{ |
| | | urlServer: urlServer, |
| | | urlPubSub: urlPubSub, |
| | | service: service, |
| | | service: svInfo, |
| | | ctx: ctx, |
| | | cancel: cancel, |
| | | sock: sock, |
| | |
| | | d.cancel() |
| | | } |
| | | |
| | | func (d *DiscoveryClient) SetResp(i interface{}) { |
| | | d.service.Info = i |
| | | } |
| | | |
| | | func (d *DiscoveryClient) run() { |
| | | var err error |
| | | var msg []byte |
| | |
| | | log.Println("DiscoveryClient: Cannot receive the SURVEY", err.Error()) |
| | | continue |
| | | } |
| | | err = d.sock.Send([]byte(d.service)) |
| | | sendB, err := json.Marshal(d.service) |
| | | if err != nil { |
| | | log.Println("DiscoveryClient: marshal d.serviceInfo err", err.Error()) |
| | | continue |
| | | } |
| | | err = d.sock.Send(sendB) |
| | | if err != nil { |
| | | log.Println("DiscoveryClient: Cannot send the SURVEY response", err.Error()) |
| | | continue |
| | |
| | | package gopherdiscovery |
| | | |
| | | import ( |
| | | "encoding/json" |
| | | "log" |
| | | "time" |
| | | |
| | |
| | | nodes StringSet |
| | | // publisher, we are going to publish the changes of the set here |
| | | publisher *Publisher |
| | | |
| | | svInfo map[string]interface{} |
| | | } |
| | | |
| | | type Publisher struct { |
| | |
| | | var err error |
| | | var msg []byte |
| | | var responses StringSet |
| | | var si ServiceInfo |
| | | |
| | | err = d.sock.Send([]byte("are you ok?")) |
| | | if err != nil { |
| | |
| | | return |
| | | } |
| | | } else { |
| | | responses.Add(string(msg)) |
| | | if json.Unmarshal(msg, &si) == nil { |
| | | responses.Add(si.ServiceId) |
| | | d.services.svInfo[si.ServiceId] = si.Info |
| | | } |
| | | } |
| | | } |
| | | } |
| | |
| | | s := &Services{ |
| | | nodes: NewStringSet(), |
| | | publisher: publisher, |
| | | svInfo: make(map[string]interface{}, 0), |
| | | } |
| | | |
| | | return s |
| | |
| | | return d.services.nodes |
| | | } |
| | | |
| | | func (d *DiscoveryServer) SvInfo() map[string]interface{} { |
| | | m := make(map[string]interface{}) |
| | | for k,v := range d.services.svInfo { |
| | | m[k] = v |
| | | } |
| | | return m |
| | | } |
| | | |
| | | func (d *DiscoveryServer) PublishMsg(msg string) { |
| | | d.services.publisher.Publish([]byte(msg)) |
| | | } |