func sendGetCommand(conn net.Conn, s parser.Statement) { msgId, _ := newUUID() cmd := msg.NewCommand(msgId, msg.NewGetCommand(s.Key)) if err := cmd.Encode(conn); err != nil { fmt.Printf("Failed to send command to server: %v\n", err.Error()) } result, err := msg.DecodeResult(conn) if err != nil { fmt.Printf("Failed to read result from server: %v\n", err.Error()) } sr := result.SubResult.(msg.GetResult) fmt.Printf("%v: %v\n", sr.Key, sr.Value) }
func main() { fmt.Println("Connecting to localhost:4567") conn, err := net.Dial("tcp", "localhost:4567") if err != nil { fmt.Fprintln(os.Stderr, err) } msgId, _ := newUUID() cmd := msg.NewCommand(msgId, msg.NewHandshakeCommand("mescal-repl version 0.0.0")) if err := cmd.Encode(conn); err != nil { fmt.Fprintln(os.Stderr, err) } r, err := msg.DecodeResult(conn) if err != nil { fmt.Fprintln(os.Stderr, err) } if r.Status != msg.StatusSuccess { fmt.Fprintln(os.Stderr, "Failed to complete handshake with server") } for { reader := bufio.NewReader(os.Stdin) fmt.Print("> ") command, _ := reader.ReadString('\n') p := parser.NewParser(command) s, err := p.Parse() if err != nil { fmt.Printf("Unable to parse command: %s\n", err.Error()) continue } switch s.Action { case parser.GET: sendGetCommand(conn, s) case parser.SET: sendSetCommand(conn, s) default: fmt.Println("Unknown command") } } }
func sendSetCommand(conn net.Conn, s parser.Statement) { msgId, _ := newUUID() cmd := msg.NewCommand(msgId, msg.NewSetCommand(s.Key, s.Value, s.Expiry)) if err := cmd.Encode(conn); err != nil { fmt.Printf("Failed to send command to server: %v\n", err.Error()) } result, err := msg.DecodeResult(conn) if err != nil { fmt.Printf("Failed to read result from server: %v\n", err.Error()) } if result.Status == msg.StatusSuccess { fmt.Println("OK") } else { fmt.Println("Failed") } }
time.Sleep(time.Millisecond * 1000) var err error client, err = net.Dial("tcp", "127.0.0.1:"+cfg.Port) if err != nil { panic(err) } }) It("can handle a client handshake", func() { cmd := msg.NewCommand("12345", msg.NewHandshakeCommand("Go v0.1.1")) err := cmd.Encode(client) Expect(err).To(BeNil()) result, err := msg.DecodeResult(client) Expect(err).To(BeNil()) Expect(result.Id).To(Equal("12345")) Expect(result.Action).To(Equal(msg.ActionHandshake)) subRes := result.SubResult.(msg.HandshakeResult) Expect(subRes.ClientId).To(BeAssignableToTypeOf("string")) }) Context("With a client that has already performed handshake", func() { It("can handle a set command", func() { cmd := msg.NewCommand("12345", msg.NewSetCommand("doodle", "poodle", time.Now().Unix())) err := cmd.Encode(client) Expect(err).To(BeNil()) result, err := msg.DecodeResult(client)
func main() { certPool := x509.NewCertPool() caFile, err := ioutil.ReadFile("../test/ca.pem") block, _ := pem.Decode(caFile) caCert, err := x509.ParseCertificate(block.Bytes) certPool.AddCert(caCert) clientCert, err := tls.LoadX509KeyPair("../test/cert.pem", "../test/key.pem") if err != nil { panic(err) } tlsCfg := tls.Config{ Certificates: []tls.Certificate{clientCert}, RootCAs: certPool, ServerName: "localhost", } conn, err := tls.Dial("tcp", "127.0.0.1:4333", &tlsCfg) // conn, err := net.Dial("tcp", "localhost:4333") if err != nil { fmt.Println("Failed to open connection:", err) } cmd := msg.NewCommand("1234", msg.NewHandshakeCommand("mescal go version 1.2.3")) if err = cmd.Encode(conn); err != nil { fmt.Println(err) return } res, err := msg.DecodeResult(conn) if err != nil { panic(err) } fmt.Println(res.Id) fmt.Printf("%+v", res.SubResult.(msg.HandshakeResult)) cmd = msg.NewCommand("1233", msg.NewSetCommand("ascii", "characters", time.Now().Unix())) if err = cmd.Encode(conn); err != nil { fmt.Println(err) return } res, err = msg.DecodeResult(conn) if err != nil { panic(err) } fmt.Println(res.Action) for { fmt.Println("\n\n") time.Sleep(1 * time.Second) cmd = msg.NewCommand("1234", msg.NewGetCommand("ascii")) if err = cmd.Encode(conn); err != nil { fmt.Println(err) return } res, err = msg.DecodeResult(conn) if err != nil { panic(err) } fmt.Println(res.Action) switch sb := res.SubResult.(type) { case msg.GetResult: fmt.Println("SubResult is GetResult") fmt.Println("Key: ", sb.Key) fmt.Println("Value:", sb.Value) default: fmt.Println("Unknown SubCommand") } } }