updated soldier's init command.

pull/23/head
Richard Liu 6 years ago
parent 15a2391ac0
commit 61e7f6dff0
  1. 5
      aws-experiment-launch/experiment/commander/main.go
  2. 62
      aws-experiment-launch/experiment/soldier/main.go

@ -8,7 +8,7 @@ import (
) )
const ( const (
message = "Ping" message = "init http://localhost:8080/configuration.txt"
StopCharacter = "\r\n\r\n" StopCharacter = "\r\n\r\n"
) )
@ -23,13 +23,12 @@ func SocketClient(ip string, port int) {
} }
conn.Write([]byte(message)) conn.Write([]byte(message))
conn.Write([]byte(StopCharacter)) // conn.Write([]byte(StopCharacter))
log.Printf("Send: %s", message) log.Printf("Send: %s", message)
buff := make([]byte, 1024) buff := make([]byte, 1024)
n, _ := conn.Read(buff) n, _ := conn.Read(buff)
log.Printf("Receive: %s", buff[:n]) log.Printf("Receive: %s", buff[:n])
} }
func main() { func main() {

@ -3,6 +3,7 @@ package main
import ( import (
"bufio" "bufio"
"io" "io"
"io/ioutil"
"log" "log"
"net" "net"
"net/http" "net/http"
@ -57,11 +58,13 @@ ILOOP:
break ILOOP break ILOOP
case nil: case nil:
log.Println("Receive:", data) log.Println("Receive:", data)
go handleCommand(data)
if isTransportOver(data) { if isTransportOver(data) {
log.Println("Tranport Over!")
break ILOOP break ILOOP
} }
go handleCommand(data)
default: default:
log.Fatalf("Receive data failed:%s", err) log.Fatalf("Receive data failed:%s", err)
return return
@ -75,34 +78,53 @@ ILOOP:
} }
func handleCommand(command string) { func handleCommand(command string) {
// assume this is init command args := strings.Split(command, " ")
handleInitCommand(command)
if len(args) <= 0 {
return
} }
func handleInitCommand(command string) { switch command := args[0]; command {
log.Println("Init command") case "init":
out, err := os.Create("config_copy.txt") {
handleInitCommand(args[1:])
}
case "close":
{
log.Println("close command")
}
}
}
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 { if err != nil {
panic("Failed to create local file") log.Fatal("Failed to create local file")
} }
log.Println("Created local file")
defer out.Close() 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 { if err != nil {
log.Println("Failed to read file content") log.Fatal("Failed to copy file")
panic("Failed to read file content")
} }
log.Println("Read file content")
log.Println(resp) content, err := ioutil.ReadFile(localConfig)
log.Println(resp.Body)
n, err := io.Copy(out, resp.Body)
if err != nil { if err != nil {
panic("Failed to copy file") log.Fatal(err)
} }
log.Println("copy done") log.Printf("File contents: %s", content)
log.Println(resp.Body)
defer resp.Body.Close()
log.Println(n)
} }
func isTransportOver(data string) (over bool) { func isTransportOver(data string) (over bool) {

Loading…
Cancel
Save