From 61e7f6dff094c7d48b6d21eef8e30a683b05b504 Mon Sep 17 00:00:00 2001 From: Richard Liu Date: Tue, 3 Jul 2018 21:10:10 -0700 Subject: [PATCH] updated soldier's init command. --- .../experiment/commander/main.go | 5 +- .../experiment/soldier/main.go | 62 +++++++++++++------ 2 files changed, 44 insertions(+), 23 deletions(-) diff --git a/aws-experiment-launch/experiment/commander/main.go b/aws-experiment-launch/experiment/commander/main.go index ee7d22101..34d955026 100644 --- a/aws-experiment-launch/experiment/commander/main.go +++ b/aws-experiment-launch/experiment/commander/main.go @@ -8,7 +8,7 @@ import ( ) const ( - message = "Ping" + message = "init http://localhost:8080/configuration.txt" StopCharacter = "\r\n\r\n" ) @@ -23,13 +23,12 @@ func SocketClient(ip string, port int) { } conn.Write([]byte(message)) - conn.Write([]byte(StopCharacter)) + // conn.Write([]byte(StopCharacter)) log.Printf("Send: %s", message) buff := make([]byte, 1024) n, _ := conn.Read(buff) log.Printf("Receive: %s", buff[:n]) - } func main() { diff --git a/aws-experiment-launch/experiment/soldier/main.go b/aws-experiment-launch/experiment/soldier/main.go index 00d7a171c..1935b47d8 100644 --- a/aws-experiment-launch/experiment/soldier/main.go +++ b/aws-experiment-launch/experiment/soldier/main.go @@ -3,6 +3,7 @@ package main import ( "bufio" "io" + "io/ioutil" "log" "net" "net/http" @@ -57,11 +58,13 @@ ILOOP: break ILOOP case nil: log.Println("Receive:", data) - go handleCommand(data) if isTransportOver(data) { + log.Println("Tranport Over!") break ILOOP } + go handleCommand(data) + default: log.Fatalf("Receive data failed:%s", err) return @@ -75,34 +78,53 @@ ILOOP: } func handleCommand(command string) { - // assume this is init command - handleInitCommand(command) + args := strings.Split(command, " ") + + if len(args) <= 0 { + return + } + + switch command := args[0]; command { + case "init": + { + handleInitCommand(args[1:]) + } + case "close": + { + log.Println("close command") + } + } } -func handleInitCommand(command string) { - log.Println("Init command") - out, err := os.Create("config_copy.txt") +func handleInitCommand(args []string) { + log.Println("Init command", args) + // create local config file + localConfig := "node_config.txt" + out, err := os.Create(localConfig) if err != nil { - panic("Failed to create local file") + log.Fatal("Failed to create local file") } - log.Println("Created local file") defer out.Close() - resp, err := http.Get("http://localhost/config.txt") + + // get remote config file + configURL := args[0] + resp, err := http.Get(configURL) + if err != nil { + log.Fatal("Failed to read file content") + } + defer resp.Body.Close() + + // copy remote to local + _, err = io.Copy(out, resp.Body) if err != nil { - log.Println("Failed to read file content") - panic("Failed to read file content") + log.Fatal("Failed to copy file") } - log.Println("Read file content") - log.Println(resp) - log.Println(resp.Body) - n, err := io.Copy(out, resp.Body) + + content, err := ioutil.ReadFile(localConfig) if err != nil { - panic("Failed to copy file") + log.Fatal(err) } - log.Println("copy done") - log.Println(resp.Body) - defer resp.Body.Close() - log.Println(n) + log.Printf("File contents: %s", content) } func isTransportOver(data string) (over bool) {