|
|
@ -2,21 +2,22 @@ package main |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"bufio" |
|
|
|
"bufio" |
|
|
|
"fmt" |
|
|
|
"flag" |
|
|
|
"log" |
|
|
|
"log" |
|
|
|
"net" |
|
|
|
"net" |
|
|
|
"os" |
|
|
|
"os" |
|
|
|
"strings" |
|
|
|
"strings" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
type CommanderSetting struct { |
|
|
|
type commanderSetting struct { |
|
|
|
addr string |
|
|
|
ip string |
|
|
|
|
|
|
|
port string |
|
|
|
configFile string |
|
|
|
configFile string |
|
|
|
configs [][]string |
|
|
|
configs [][]string |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var ( |
|
|
|
var ( |
|
|
|
setting CommanderSetting |
|
|
|
setting commanderSetting |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
func socketClient(addr string, handler func(net.Conn, string)) { |
|
|
|
func socketClient(addr string, handler func(net.Conn, string)) { |
|
|
@ -24,7 +25,11 @@ func socketClient(addr string, handler func(net.Conn, string)) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func readConfigFile() [][]string { |
|
|
|
func readConfigFile() [][]string { |
|
|
|
file, _ := os.Open(setting.configFile) |
|
|
|
file, err := os.Open(setting.configFile) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
log.Println("Failed to read config file") |
|
|
|
|
|
|
|
return nil |
|
|
|
|
|
|
|
} |
|
|
|
fscanner := bufio.NewScanner(file) |
|
|
|
fscanner := bufio.NewScanner(file) |
|
|
|
|
|
|
|
|
|
|
|
result := [][]string{} |
|
|
|
result := [][]string{} |
|
|
@ -37,19 +42,15 @@ func readConfigFile() [][]string { |
|
|
|
|
|
|
|
|
|
|
|
func handleCommand(command string) { |
|
|
|
func handleCommand(command string) { |
|
|
|
args := strings.Split(command, " ") |
|
|
|
args := strings.Split(command, " ") |
|
|
|
|
|
|
|
log.Println(args) |
|
|
|
if len(args) <= 0 { |
|
|
|
if len(args) <= 0 { |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
switch cmd := args[0]; cmd { |
|
|
|
switch cmd := args[0]; cmd { |
|
|
|
case "config": |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
handleConfigCommand(args[1], args[2]) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
case "init": |
|
|
|
case "init": |
|
|
|
{ |
|
|
|
{ |
|
|
|
dictateNodes("init" + setting.addr + setting.configFile) |
|
|
|
dictateNodes("init http://" + setting.ip + ":" + setting.port + "/" + setting.configFile) |
|
|
|
} |
|
|
|
} |
|
|
|
default: |
|
|
|
default: |
|
|
|
{ |
|
|
|
{ |
|
|
@ -58,10 +59,12 @@ func handleCommand(command string) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func handleConfigCommand(addr string, configFile string) { |
|
|
|
func config(ip string, port string, configFile string) { |
|
|
|
setting.addr = addr |
|
|
|
setting.ip = ip |
|
|
|
|
|
|
|
setting.port = port |
|
|
|
setting.configFile = configFile |
|
|
|
setting.configFile = configFile |
|
|
|
setting.configs = readConfigFile() |
|
|
|
setting.configs = readConfigFile() |
|
|
|
|
|
|
|
log.Println("Config-ed", setting.configs) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func dictateNodes(command string) { |
|
|
|
func dictateNodes(command string) { |
|
|
@ -94,10 +97,18 @@ func dictateNodes(command string) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func main() { |
|
|
|
func main() { |
|
|
|
for { |
|
|
|
ip := flag.String("ip", "127.0.0.1", "ip of commander") |
|
|
|
reader := bufio.NewReader(os.Stdin) |
|
|
|
port := flag.String("port", "8080", "port of config file") |
|
|
|
fmt.Print("Listening to Your Command:") |
|
|
|
configFile := flag.String("config_file", "test.txt", "file name of config file") |
|
|
|
command, _ := reader.ReadString('\n') |
|
|
|
|
|
|
|
handleCommand(command) |
|
|
|
config(*ip, *port, *configFile) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(os.Stdin) |
|
|
|
|
|
|
|
for true { |
|
|
|
|
|
|
|
log.Printf("Listening to Your Command:") |
|
|
|
|
|
|
|
if !scanner.Scan() { |
|
|
|
|
|
|
|
break |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
handleCommand(scanner.Text()) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|