diff --git a/commands.go b/commands.go index b1c1441..3bff937 100644 --- a/commands.go +++ b/commands.go @@ -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 diff --git a/system.go b/system.go index dfc17e0..8505e25 100644 --- a/system.go +++ b/system.go @@ -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) + }) +}