func (api *API) SubscribeData(query string, cb func(ts messages.QueryTimeseriesResult)) error { nonce := rand.Uint32() msg := messages.KeyValueQuery{ Query: query, Nonce: nonce, } fmt.Printf("Subscribe to %v\n", api.uri+fmt.Sprintf("/signal/%s,all", api.vk[:len(api.vk)-1])) c, err := api.client.Subscribe(&bw.SubscribeParams{ URI: api.uri + fmt.Sprintf("/signal/%s,all", api.vk[:len(api.vk)-1]), }) if err != nil { return err } err = api.client.Publish(&bw.PublishParams{ URI: api.uri + "/slot/subscribe", PayloadObjects: []bw.PayloadObject{msg.ToMsgPackBW()}, }) fmt.Printf("Publish to %v\n", api.uri+"/slot/subscribe") for msg := range c { found, timeseries, err := GetTimeseries(nonce, msg) if err == nil && found { cb(timeseries) } else if err != nil { return err } } return nil }
func (api *API) Query(query string) error { if len(query) == 0 { return nil } var wg sync.WaitGroup nonce := rand.Uint32() msg := messages.KeyValueQuery{ Query: query, Nonce: nonce, } wg.Add(1) fmt.Printf("Subscribe to %v\n", api.uri+fmt.Sprintf("/signal/%s,queries", api.vk[:len(api.vk)-1])) c, err := api.client.Subscribe(&bw.SubscribeParams{ URI: api.uri + fmt.Sprintf("/signal/%s,queries", api.vk[:len(api.vk)-1]), }) if err != nil { return err } go func() { for msg := range c { var isMyResponse bool = false // check for error found, err := GetError(nonce, msg) isMyResponse = isMyResponse || found if found { fmt.Println(err) } // check for metadata found, metadata, err := GetMetadata(nonce, msg) isMyResponse = isMyResponse || found if err == nil && found { fmt.Println(metadata.Dump()) } else if found && err != nil { fmt.Println(err) } // check for timeseries found, timeseries, err := GetTimeseries(nonce, msg) isMyResponse = isMyResponse || found if err == nil && found { fmt.Println(timeseries.Dump()) } else if found && err != nil { fmt.Println(err) } // check for lists found, listdata, err := GetDistinctList(nonce, msg) isMyResponse = isMyResponse || found if err == nil && found { fmt.Println(listdata.Dump()) } else if found && err != nil { fmt.Println(err) } if isMyResponse { wg.Done() } } }() err = api.client.Publish(&bw.PublishParams{ URI: api.uri + "/slot/query", PayloadObjects: []bw.PayloadObject{msg.ToMsgPackBW()}, }) fmt.Printf("Publish to %v\n", api.uri+"/slot/query") wg.Wait() return nil }