forked from torbiak/gopl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clockwall.go
56 lines (52 loc) · 1.02 KB
/
clockwall.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// clockwall listens to multiple clock servers concurrently.
package main
import (
"bufio"
"fmt"
"io"
"log"
"net"
"os"
"strings"
"time"
)
type clock struct {
name, host string
}
func (c *clock) watch(w io.Writer, r io.Reader) {
s := bufio.NewScanner(r)
for s.Scan() {
fmt.Fprintf(w, "%s: %s\n", c.name, s.Text())
}
fmt.Println(c.name, "done")
if s.Err() != nil {
log.Printf("can't read from %s: %s", c.name, s.Err())
}
}
func main() {
if len(os.Args) == 1 {
fmt.Fprintln(os.Stderr, "usage: clockwall NAME=HOST ...")
os.Exit(1)
}
clocks := make([]*clock, 0)
for _, a := range os.Args[1:] {
fields := strings.Split(a, "=")
if len(fields) != 2 {
fmt.Fprintf(os.Stderr, "bad arg: %s\n", a)
os.Exit(1)
}
clocks = append(clocks, &clock{fields[0], fields[1]})
}
for _, c := range clocks {
conn, err := net.Dial("tcp", c.host)
if err != nil {
log.Fatal(err)
}
defer conn.Close()
go c.watch(os.Stdout, conn)
}
// Sleep while other goroutines do the work.
for {
time.Sleep(time.Minute)
}
}