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) }
}) 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) Expect(err).To(BeNil()) Expect(result.Id).To(Equal("12345")) Expect(result.Action).To(Equal(msg.ActionSet)) }) It("can handle a get command", func() { cmd := msg.NewCommand("12345", msg.NewGetCommand("doodle")) 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.ActionGet)) subRes := result.SubResult.(msg.GetResult) Expect(subRes.Key).To(Equal("doodle")) Expect(subRes.Value).To(Equal("poodle")) }) }) Context("With TLS enabled", func() {
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") } } }