From 1a79689a2d3ec57ea1f54ec00e4e0dbb12ee5551 Mon Sep 17 00:00:00 2001 From: Richard Liu Date: Tue, 3 Jul 2018 22:23:22 -0700 Subject: [PATCH] =?UTF-8?q?let=20commander=20accept=20config=20file.?= =?UTF-8?q?=E2=80=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../experiment/commander/main.go | 43 ++++++++++++++----- .../experiment/soldier/main.go | 3 +- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/aws-experiment-launch/experiment/commander/main.go b/aws-experiment-launch/experiment/commander/main.go index 764d1d5c4..7d34cc502 100644 --- a/aws-experiment-launch/experiment/commander/main.go +++ b/aws-experiment-launch/experiment/commander/main.go @@ -1,19 +1,21 @@ package main import ( + "bufio" + "flag" "log" "net" - "strconv" + "os" "strings" ) const ( message = "init http://localhost:8080/configuration.txt" + defaultPort = 6000 StopCharacter = "\r\n\r\n" ) -func SocketClient(ip string, port int) { - addr := strings.Join([]string{ip, strconv.Itoa(port)}, ":") +func SocketClient(addr string) { conn, err := net.Dial("tcp", addr) defer conn.Close() @@ -28,18 +30,39 @@ func SocketClient(ip string, port int) { buff := make([]byte, 1024) n, _ := conn.Read(buff) - log.Printf("Receive from %v: %s", port, buff[:n]) + log.Printf("Receive from %s: %s", addr, buff[:n]) } func main() { + configFile := flag.String("config_file", "config.txt", "file containing all ip addresses") + flag.Parse() - var ( - ip = "127.0.0.1" - portList = []int{3333, 4444} - ) + configs := readConfigFile(*configFile) - for _, port := range portList { - SocketClient(ip, port) + for _, config := range configs { + ip := config[0] + port := config[1] + addr := strings.Join([]string{ip, port}, ":") + SocketClient(addr) } +} + +func readConfigFile(configFile string) [][]string { + file, _ := os.Open(configFile) + fscanner := bufio.NewScanner(file) + + result := [][]string{} + for fscanner.Scan() { + p := strings.Split(fscanner.Text(), " ") + result = append(result, p) + } + return result +} +func Map(vs []string, f func(string) string) []string { + vsm := make([]string, len(vs)) + for i, v := range vs { + vsm[i] = f(v) + } + return vsm } diff --git a/aws-experiment-launch/experiment/soldier/main.go b/aws-experiment-launch/experiment/soldier/main.go index 1ab904b20..e0d559099 100644 --- a/aws-experiment-launch/experiment/soldier/main.go +++ b/aws-experiment-launch/experiment/soldier/main.go @@ -121,7 +121,8 @@ func handleInitCommand(args []string, w *bufio.Writer) { if err != nil { log.Fatal(err) } - log.Println("Successfully init-ed with config", content) + log.Println("Successfully init-ed with config") + log.Println(string(content)) w.Write([]byte("Successfully init-ed")) w.Flush()