Commit 2539813d authored by Sushant Mahajan's avatar Sushant Mahajan

additional error handling for set command

parent 57bd661b
...@@ -7,16 +7,25 @@ import ( ...@@ -7,16 +7,25 @@ import (
"os" "os"
) )
var die bool
func doRead(conn net.Conn) { func doRead(conn net.Conn) {
for { for {
buf := make([]byte, 1024) buf := make([]byte, 1024)
conn.Read(buf) _, err := conn.Read(buf)
if err != nil {
fmt.Println("Server closed connection")
die = true
break
}
fmt.Println(string(buf)) fmt.Println(string(buf))
} }
} }
func main() { func main() {
conn, err := net.Dial("tcp", "localhost:5000") conn, err := net.Dial("tcp", "localhost:5000")
die = false
go doRead(conn) go doRead(conn)
if err != nil { if err != nil {
...@@ -25,6 +34,9 @@ func main() { ...@@ -25,6 +34,9 @@ func main() {
reader := bufio.NewReader(os.Stdin) reader := bufio.NewReader(os.Stdin)
for { for {
if die {
break
}
msg, err := reader.ReadString('\n') msg, err := reader.ReadString('\n')
if err != nil { if err != nil {
fmt.Println("Err: ", err) fmt.Println("Err: ", err)
......
...@@ -94,8 +94,9 @@ func read(conn net.Conn, toRead uint64) ([]byte, bool){ ...@@ -94,8 +94,9 @@ func read(conn net.Conn, toRead uint64) ([]byte, bool){
} }
func write(conn net.Conn, msg string) { func write(conn net.Conn, msg string) {
buf := []byte(msg)[0:len(msg)-1] buf := []byte(msg)
buf = append(buf, []byte(CRLF)) buf = append(buf, []byte(CRLF)...)
logger.Println(buf, len(buf))
conn.Write(buf) conn.Write(buf)
} }
...@@ -116,6 +117,23 @@ func isValid(cmd string, tokens []string, conn net.Conn) int{ ...@@ -116,6 +117,23 @@ func isValid(cmd string, tokens []string, conn net.Conn) int{
case SET: case SET:
if len(tokens) > 5 || len(tokens) < 4 { if len(tokens) > 5 || len(tokens) < 4 {
flag = 1 flag = 1
logger.Println(cmd, ":Invalid no. of tokens")
}
if len([]byte(tokens[1])) > 250 {
flag = 1
logger.Println(cmd, ":Invalid size of key")
}
if len(tokens) == 5 && tokens[4] != NOREPLY {
logger.Println(cmd, ":optional arg incorrect")
flag = 1
}
if _, err := strconv.ParseUint(tokens[2], 10, 64); err != nil {
logger.Println(cmd, ":expiry time invalid")
flag = 1
}
if _, err := strconv.ParseUint(tokens[3], 10, 64); err != nil {
logger.Println(cmd, ":numBytes invalid")
flag = 1
} }
//other validations //other validations
case GET: case GET:
...@@ -159,9 +177,14 @@ func parseInput(conn net.Conn, msg string, table *KeyValueStore) { ...@@ -159,9 +177,14 @@ func parseInput(conn net.Conn, msg string, table *KeyValueStore) {
return return
} }
if ver, ok := performSet(conn, tokens[1:len(tokens)], table); ok { if ver, ok := performSet(conn, tokens[1:len(tokens)], table); ok {
logger.Println(ver)
buffer.WriteString(OK)
buffer.WriteString(" ")
buffer.WriteString(strconv.FormatUint(ver, 10))
logger.Println(buffer.String())
write(conn, buffer.String())
} }
//case GET: performGet(tokens[1:len(tokens)]) case GET: performGet(tokens[1:len(tokens)])
//case GETM: performGetm(tokens[1:len(tokens)]) //case GETM: performGetm(tokens[1:len(tokens)])
//case CAS: performCas(tokens[1:len(tokens)]) //case CAS: performCas(tokens[1:len(tokens)])
//case DELETE: performDelete(tokens[1:len(tokens)]) //case DELETE: performDelete(tokens[1:len(tokens)])
...@@ -185,7 +208,7 @@ func performSet(conn net.Conn, tokens []string, table *KeyValueStore) (uint64, b ...@@ -185,7 +208,7 @@ func performSet(conn net.Conn, tokens []string, table *KeyValueStore) (uint64, b
v, ok := read(conn, n+2) v, ok := read(conn, n+2)
if !ok { if !ok {
//error here //error here
return return 0, false
} }
table.Lock() table.Lock()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment