From d74b7e1415e4af1c5497e5e360a3d3990a1087c1 Mon Sep 17 00:00:00 2001
From: Neale Pickett
Date: Sat, 27 Dec 2014 23:52:34 +0000
Subject: [PATCH 1/3] Better minecraft pinger
---
minepig.cgi.go | 55 ++++++++++++++++++++++++++++++++----------
papers/longbets.mdwn | 57 +++++++++++++++++++++++++++++++++++++++++++-
2 files changed, 99 insertions(+), 13 deletions(-)
diff --git a/minepig.cgi.go b/minepig.cgi.go
index faf4007..836d82d 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,54 @@ 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("Is Ginnie playing Minecraft PE?")
+ fmt.Println("")
+ fmt.Println("Minecraft PE ping")
fmt.Println("")
- fmt.Println("Is Ginnie playing Minecraft PE?
")
- fmt.Println("")
- if isAlive() {
- fmt.Println("yes")
- } else {
- fmt.Println("no")
+ fmt.Println("
Are The Picketts playing Minecraft PE?
")
+ fmt.Println("")
+ for msg := range results {
+ fmt.Printf("- %s
\n", msg)
}
- fmt.Println("
")
- fmt.Println("Use the external server address h.woozle.org
, port 30919
to join in.
")
+ fmt.Println("")
fmt.Println("