added experiment related code

pull/18/head
Richard Liu 6 years ago
parent 1b5f6d10f4
commit 7079476c01
  1. 44
      aws-code/experiment/commander/main.go
  2. 119
      aws-code/experiment/soldier/main.go

@ -0,0 +1,44 @@
package main
import (
"log"
"net"
"strconv"
"strings"
)
const (
message = "Ping"
StopCharacter = "\r\n\r\n"
)
func SocketClient(ip string, port int) {
addr := strings.Join([]string{ip, strconv.Itoa(port)}, ":")
conn, err := net.Dial("tcp", addr)
defer conn.Close()
if err != nil {
log.Fatalln(err)
}
conn.Write([]byte(message))
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() {
var (
ip = "127.0.0.1"
port = 3333
)
SocketClient(ip, port)
}

@ -0,0 +1,119 @@
package main
import (
"bufio"
"io"
"log"
"net"
"net/http"
"os"
"strconv"
"strings"
)
const (
Message = "Pong"
StopCharacter = "\r\n\r\n"
)
func SocketServer(port int) {
listen, err := net.Listen("tcp4", ":"+strconv.Itoa(port))
defer listen.Close()
if err != nil {
log.Fatalf("Socket listen port %d failed,%s", port, err)
os.Exit(1)
}
log.Printf("Begin listen for command on port: %d", port)
for {
conn, err := listen.Accept()
if err != nil {
log.Fatalln(err)
continue
}
go handler(conn)
}
}
func handler(conn net.Conn) {
defer conn.Close()
var (
buf = make([]byte, 1024)
r = bufio.NewReader(conn)
w = bufio.NewWriter(conn)
)
ILOOP:
for {
n, err := r.Read(buf)
data := string(buf[:n])
switch err {
case io.EOF:
break ILOOP
case nil:
log.Println("Receive:", data)
go handleCommand(data)
if isTransportOver(data) {
break ILOOP
}
default:
log.Fatalf("Receive data failed:%s", err)
return
}
}
w.Write([]byte(Message))
w.Flush()
log.Printf("Send: %s", Message)
}
func handleCommand(command string) {
// assume this is init command
handleInitCommand(command)
}
func handleInitCommand(command string) {
log.Println("Init command")
out, err := os.Create("config_copy.txt")
if err != nil {
panic("Failed to create local file")
}
log.Println("Created local file")
defer out.Close()
resp, err := http.Get("http://localhost/config.txt")
if err != nil {
log.Println("Failed to read file content")
panic("Failed to read file content")
}
log.Println("Read file content")
log.Println(resp)
log.Println(resp.Body)
n, err := io.Copy(out, resp.Body)
if err != nil {
panic("Failed to copy file")
}
log.Println("copy done")
log.Println(resp.Body)
defer resp.Body.Close()
log.Println(n)
}
func isTransportOver(data string) (over bool) {
over = strings.HasSuffix(data, "\r\n\r\n")
return
}
func main() {
port := 3333
SocketServer(port)
}
Loading…
Cancel
Save