diff --git a/minepig.cgi.go b/minepig.cgi.go index faf4007..2db1fd3 100644 --- a/minepig.cgi.go +++ b/minepig.cgi.go @@ -4,12 +4,13 @@ import ( "net" "fmt" "time" + "sync" ) const MAGIC = "\x00\xff\xff\x00\xfe\xfe\xfe\xfe\xfd\xfd\xfd\xfd\x12\x34\x56\x78" -func isAlive() bool { - conn, err := net.Dial("udp", "h.woozle.org:30919") +func isAlive(host string) bool { + conn, err := net.Dial("udp", host) if err != nil { return false } @@ -28,24 +29,59 @@ func isAlive() bool { return true } +var wg sync.WaitGroup + +func waitClose(c chan<- string) { + wg.Wait() + close(c) +} + +type HostEntry struct { + host string + owner string +} + +var hosts = []HostEntry{ + {"h.woozle.org:30919", "Ginnie"}, + {"h.woozle.org:44321", "Neale"}, + {"h.woozle.org:58000", "Amy"}, +} + +func ping(results chan<- string, e HostEntry) { + defer wg.Done() + if isAlive(e.host) { + results <- fmt.Sprintf("%s is playing at %s", e.owner, e.host) + } +} + func main() { + results := make(chan string, 5) + + for _, host := range hosts { + wg.Add(1) + go ping(results, host) + } + go waitClose(results) + fmt.Println("Content-type: text/html") fmt.Println("") fmt.Println("") fmt.Println("") fmt.Println("
") fmt.Println("") - fmt.Println("") - fmt.Println("") - if isAlive() { - fmt.Println("yes") - } else { - fmt.Println("no") + fmt.Println("
Sorry, looks like nobody's playing right now.
") } - fmt.Println("") - fmt.Println("Use the external server address h.woozle.org
, port 30919
to join in.