soldier on multiple ports.

pull/24/head
Richard Liu 6 years ago
parent 61e7f6dff0
commit 5877e8c486
  1. 6
      aws-experiment-launch/experiment/commander/main.go
  2. 35
      aws-experiment-launch/experiment/soldier/main.go

@ -28,16 +28,18 @@ func SocketClient(ip string, port int) {
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 from %v: %s", port, buff[:n])
} }
func main() { func main() {
var ( var (
ip = "127.0.0.1" ip = "127.0.0.1"
port = 3333 portList = []int{3333, 4444}
) )
for _, port := range portList {
SocketClient(ip, port) SocketClient(ip, port)
}
} }

@ -2,6 +2,7 @@ package main
import ( import (
"bufio" "bufio"
"flag"
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
@ -13,12 +14,14 @@ import (
) )
const ( const (
Message = "Pong"
StopCharacter = "\r\n\r\n" StopCharacter = "\r\n\r\n"
) )
func SocketServer(port int) { var (
port *int
)
func SocketServer(port int) {
listen, err := net.Listen("tcp4", ":"+strconv.Itoa(port)) listen, err := net.Listen("tcp4", ":"+strconv.Itoa(port))
defer listen.Close() defer listen.Close()
if err != nil { if err != nil {
@ -35,11 +38,9 @@ func SocketServer(port int) {
} }
go handler(conn) go handler(conn)
} }
} }
func handler(conn net.Conn) { func handler(conn net.Conn) {
defer conn.Close() defer conn.Close()
var ( var (
@ -63,7 +64,7 @@ ILOOP:
break ILOOP break ILOOP
} }
go handleCommand(data) go handleCommand(data, w)
default: default:
log.Fatalf("Receive data failed:%s", err) log.Fatalf("Receive data failed:%s", err)
@ -71,13 +72,9 @@ ILOOP:
} }
} }
w.Write([]byte(Message))
w.Flush()
log.Printf("Send: %s", Message)
} }
func handleCommand(command string) { func handleCommand(command string, w *bufio.Writer) {
args := strings.Split(command, " ") args := strings.Split(command, " ")
if len(args) <= 0 { if len(args) <= 0 {
@ -87,7 +84,7 @@ func handleCommand(command string) {
switch command := args[0]; command { switch command := args[0]; command {
case "init": case "init":
{ {
handleInitCommand(args[1:]) handleInitCommand(args[1:], w)
} }
case "close": case "close":
{ {
@ -96,10 +93,10 @@ func handleCommand(command string) {
} }
} }
func handleInitCommand(args []string) { func handleInitCommand(args []string, w *bufio.Writer) {
log.Println("Init command", args) log.Println("Init command", args)
// create local config file // create local config file
localConfig := "node_config.txt" localConfig := "node_config_" + strconv.Itoa(*port) + ".txt"
out, err := os.Create(localConfig) out, err := os.Create(localConfig)
if err != nil { if err != nil {
log.Fatal("Failed to create local file") log.Fatal("Failed to create local file")
@ -124,7 +121,10 @@ func handleInitCommand(args []string) {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
log.Printf("File contents: %s", content) log.Println("Successfully init-ed with config", content)
w.Write([]byte("Successfully init-ed"))
w.Flush()
} }
func isTransportOver(data string) (over bool) { func isTransportOver(data string) (over bool) {
@ -133,9 +133,8 @@ func isTransportOver(data string) (over bool) {
} }
func main() { func main() {
port = flag.Int("port", 3333, "port of the node.")
flag.Parse()
port := 3333 SocketServer(*port)
SocketServer(port)
} }

Loading…
Cancel
Save