mirror of
https://github.com/EchoVault/SugarDB.git
synced 2025-10-15 12:30:38 +08:00
Return PONG response from server
This commit is contained in:
@@ -3,30 +3,31 @@ package serialization
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
|
|
||||||
"github.com/tidwall/resp"
|
"github.com/tidwall/resp"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Decode(raw string) {
|
func Decode(raw string) ([]string, error) {
|
||||||
rd := resp.NewReader(bytes.NewBufferString(raw))
|
rd := resp.NewReader(bytes.NewBufferString(raw))
|
||||||
|
|
||||||
for {
|
|
||||||
v, _, err := rd.ReadValue()
|
v, _, err := rd.ReadValue()
|
||||||
|
|
||||||
if err == io.EOF {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
res := []string{}
|
||||||
|
|
||||||
|
if v.Type().String() == "SimpleString" {
|
||||||
|
return []string{v.String()}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(v)
|
|
||||||
if v.Type().String() == "Array" {
|
if v.Type().String() == "Array" {
|
||||||
for _, elem := range v.Array() {
|
for _, elem := range v.Array() {
|
||||||
fmt.Printf("%s: %v\n", elem.Type().String(), elem)
|
res = append(res, elem.String())
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Println(res)
|
||||||
|
return res, nil
|
||||||
}
|
}
|
||||||
|
@@ -24,6 +24,10 @@ func tokenize(comm string) ([]string, error) {
|
|||||||
return r.Read()
|
return r.Read()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func EncodeSimpleString(wr *resp.Writer, token string) error {
|
||||||
|
return wr.WriteSimpleString(token)
|
||||||
|
}
|
||||||
|
|
||||||
func encodePingPong(wr *resp.Writer, tokens []string) error {
|
func encodePingPong(wr *resp.Writer, tokens []string) error {
|
||||||
switch len(tokens) {
|
switch len(tokens) {
|
||||||
default:
|
default:
|
||||||
|
@@ -12,15 +12,13 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/kelvinmwinuka/memstore/serialization"
|
"github.com/kelvinmwinuka/memstore/serialization"
|
||||||
|
"github.com/tidwall/resp"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Listener interface {
|
|
||||||
Accept() (net.Conn, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
TLS bool `json:"tls" yaml:"tls"`
|
TLS bool `json:"tls" yaml:"tls"`
|
||||||
Key string `json:"key" yaml:"key"`
|
Key string `json:"key" yaml:"key"`
|
||||||
@@ -29,18 +27,24 @@ type Config struct {
|
|||||||
Port uint16 `json:"port" yaml:"port"`
|
Port uint16 `json:"port" yaml:"port"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Data struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
data map[string]interface{}
|
||||||
|
}
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
config Config
|
config Config
|
||||||
|
data Data
|
||||||
}
|
}
|
||||||
|
|
||||||
func (server *Server) hanndleConnection(conn net.Conn) {
|
func (server *Server) hanndleConnection(conn net.Conn) {
|
||||||
rw := bufio.NewReadWriter(bufio.NewReader(conn), bufio.NewWriter(conn))
|
connRW := bufio.NewReadWriter(bufio.NewReader(conn), bufio.NewWriter(conn))
|
||||||
// sw := bufio.NewWriter(os.Stdout)
|
respWriter := resp.NewWriter(connRW)
|
||||||
|
|
||||||
var line [][]byte
|
var line [][]byte
|
||||||
|
|
||||||
for {
|
for {
|
||||||
b, _, err := rw.ReadLine()
|
b, _, err := connRW.ReadLine()
|
||||||
|
|
||||||
if err != nil && err == io.EOF {
|
if err != nil && err == io.EOF {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
@@ -54,7 +58,24 @@ func (server *Server) hanndleConnection(conn net.Conn) {
|
|||||||
// sw.Write(bytes.Join(line, []byte("\\r\\n")))
|
// sw.Write(bytes.Join(line, []byte("\\r\\n")))
|
||||||
// sw.Flush()
|
// sw.Flush()
|
||||||
|
|
||||||
serialization.Decode(string(bytes.Join(line, []byte("\r\n"))))
|
if cmd, err := serialization.Decode(string(bytes.Join(line, []byte("\r\n")))); err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
// Return error to client
|
||||||
|
continue
|
||||||
|
} else {
|
||||||
|
// Return encoded message to client
|
||||||
|
|
||||||
|
if len(cmd) == 1 && cmd[0] == "PING" {
|
||||||
|
serialization.EncodeSimpleString(respWriter, "PONG")
|
||||||
|
connRW.Flush()
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(cmd) == 2 && cmd[0] == "PING" {
|
||||||
|
fmt.Println(cmd)
|
||||||
|
serialization.EncodeSimpleString(respWriter, cmd[1])
|
||||||
|
connRW.Flush()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
line = [][]byte{}
|
line = [][]byte{}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user