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