Merge pull request #23 from simple-rules/ric-command

updated soldier's init command.
pull/27/head
7z7 6 years ago committed by GitHub
commit 0a96eb7aba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      aws-experiment-launch/experiment/commander/main.go
  2. 62
      aws-experiment-launch/experiment/soldier/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() {

@ -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) {

Loading…
Cancel
Save