Created
November 19, 2014 04:35
-
-
Save whitmo/f7a91f6a16169ef9af27 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"github.com/apcera/nats" | |
"os" | |
"strings" | |
"time" | |
) | |
type Report struct { | |
Type string | |
Host string | |
Uid string | |
Index int | |
Credentials [2]string | |
} | |
func match(rep Report, ip string, dtype string) bool { | |
host := strings.Split(rep.Host, ":") | |
if host[0] == ip && dtype == rep.Type { | |
return true | |
} | |
return false | |
} | |
func decode(msg *nats.Msg) (Report, error) { | |
var report Report | |
err := json.Unmarshal(msg.Data, &report) | |
return report, err | |
} | |
func main() { | |
var DISCOVER = "vcap.component.discover" | |
nc, _ := nats.Connect(os.Args[1]) | |
dtype := os.Args[2] | |
ip := os.Args[3] | |
inbox := nats.NewInbox() | |
sub, _ := nc.SubscribeSync(inbox) | |
err := nc.PublishRequest(DISCOVER, inbox, []byte("")) | |
if err == nil { | |
results := make(chan []byte, 1) | |
go func() { | |
for { | |
m, _ := sub.NextMsg(time.Second) | |
if m != nil { | |
rep, _ := decode(m) | |
if match(rep, ip, dtype) { | |
results <- m.Data | |
} | |
} | |
} | |
}() | |
select { | |
case res := <-results: | |
fmt.Println(string(res)) | |
case <-time.After(time.Second * 5): | |
fmt.Println("TIMEOUT") | |
os.Exit(1) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment