-
Notifications
You must be signed in to change notification settings - Fork 1
/
waxmoth.go
65 lines (53 loc) · 1.22 KB
/
waxmoth.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
57
58
59
60
61
62
63
64
65
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"net"
"os"
"strings"
"github.com/butlermatt/waxmoth/msg"
)
type RemoteClient struct {
address string
data chan<- *msg.Message
}
type Message struct {
raw []byte
origin string
}
func main() {
a := flag.String("a", "localhost:30005", "Comma separated list of addresses and ports to connect to for input.")
flag.Parse()
addresses := strings.Split(*a, ",")
fmt.Println("Received addresses: ", addresses)
data := make(chan *msg.Message)
for _, addr := range addresses {
go readRemote(&RemoteClient{addr, data})
}
for m := range data {
//fmt.Printf("%s: %x %v\n", m.Station, m.Icao, m.GenDate)
msg.AddMessage(m)
}
}
func readRemote(rc *RemoteClient) {
conn, err := net.Dial("tcp", rc.address)
if err != nil {
fmt.Fprintf(os.Stderr, "unable to connect to %q: %v\n", rc.address, err)
return
}
defer conn.Close()
parser := make(chan *msg.Raw)
go msg.ParseChannel(parser, rc.data)
buf := bufio.NewReader(conn)
for {
data, err := buf.ReadBytes('\n')
if err != nil {
fmt.Fprintf(os.Stderr, "failed to read bytes on %q: %v\n", rc.address, err)
continue
}
rm := &msg.Raw{Origin: rc.address, Data: bytes.TrimSpace(data)}
parser <- rm
}
}