added back in teh broadcast funciton

pull/5/head
Jordan Orelli 10 years ago
parent d85872b0a2
commit 8d0353d03f

@ -3,6 +3,7 @@ package main
import (
"fmt"
"sort"
"strings"
)
var commandRegistry map[string]*Command
@ -92,6 +93,26 @@ var scanCommand = &Command{
},
}
var broadcastCommand = &Command{
name: "broadcast",
help: "broadcast a message for all systems to hear",
handler: func(conn *Connection, args ...string) {
msg := strings.Join(args, " ")
system := conn.System()
log_info("broadcast sent from %s: %v\n", system.name, msg)
for id, _ := range index {
if id == system.id {
continue
}
delay := system.TimeTo(index[id])
id2 := id
After(delay, func() {
deliverMessage(id2, system.id, msg)
})
}
},
}
func isCommand(name string) bool {
_, ok := commandRegistry[name]
return ok

@ -194,3 +194,11 @@ func deliverReply(id int, echo int, results *scanResults) {
fmt.Fprintf(conn, "scan results from %s (%v away): %v\n", source.name, delay, results)
})
}
func deliverMessage(to_id, from_id int, msg string) {
to := index[to_id]
from := index[from_id]
to.EachConn(func(conn *Connection) {
fmt.Fprintf(conn, "Message from %s: %s", from.name, msg)
})
}

Loading…
Cancel
Save