From 3b6279ddf21ca58b8cf9469c04a249d69ea449cb Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Wed, 18 Apr 2018 16:28:37 -0400 Subject: add comments, fiddle with function publicity --- main.go | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index a89dc46..9c86758 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,8 @@ import ( "time" ) +// Given a host/node name and a port number, format a string suitable +// for net.Dial. func fmtAddress(node, port string) string { if isIPv6(node) { return fmt.Sprintf("[%s]:%s", node, port) @@ -27,33 +29,33 @@ func isIPv6(node string) bool { var jwg JobWaitGroup -func Emit(pt Point) { +func emit(pt Point) { if pt == nil { return } fmt.Println(pt) } -func DoHostfile(fname string) { +func doHostfile(fname string) { hostname := filepath.Base(fname) - cfg, err := readConfigFile(fname) + cfg, err := ReadConfigFile(fname) if err != nil { - Emit(NewPoint("public", map[string]string{"host": hostname}, map[string]interface{}{"error": err.Error()})) + emit(NewPoint("public", map[string]string{"host": hostname}, map[string]interface{}{"error": err.Error()})) return } addresses := make(map[string]struct{}) - for _, address := range getAddresses(cfg) { + for _, address := range GetAddresses(cfg) { addresses[fmtAddress(address.Node, address.Port)] = struct{}{} } for address := range addresses { - jwg.Do(hostname+"/"+address+"/tcp4", func() { Emit(DoAddress(hostname, "tcp4", address)) }) - jwg.Do(hostname+"/"+address+"/tcp6", func() { Emit(DoAddress(hostname, "tcp6", address)) }) + jwg.Do(hostname+"/"+address+"/tcp4", func() { emit(doAddress(hostname, "tcp4", address)) }) + jwg.Do(hostname+"/"+address+"/tcp6", func() { emit(doAddress(hostname, "tcp6", address)) }) } } -func DoAddress(host, network, address string) Point { +func doAddress(host, network, address string) Point { tags := map[string]string{ "host": host, "network": network, @@ -75,7 +77,7 @@ func DoAddress(host, network, address string) Point { var result_error error go func() { defer _wg.Done() - result_name, result_version, result_error = Hello(addr) + result_name, result_version, result_error = hello(addr) }() var result_ping float64 go func() { @@ -102,7 +104,9 @@ var dialer = net.Dialer{ Timeout: 10 * time.Second, } -func Hello(addr *net.TCPAddr) (name, version string, err error) { +// hello opens a TCP connection and waits for a Tinc server to say +// hello. +func hello(addr *net.TCPAddr) (name, version string, err error) { conn, err := dialer.Dial(addr.Network(), addr.String()) if err != nil { return "", "", err @@ -121,7 +125,7 @@ func Hello(addr *net.TCPAddr) (name, version string, err error) { func main() { for _, fname := range os.Args[1:] { - DoHostfile(fname) + doHostfile(fname) } watch(time.Second) } -- cgit v1.2.3