Commit d8b7f7a4 authored by Sushant Mahajan's avatar Sushant Mahajan

set command complete

parent 9e48ea33
...@@ -7,8 +7,17 @@ import ( ...@@ -7,8 +7,17 @@ import (
"os" "os"
) )
func doRead(conn net.Conn) {
for {
buf := make([]byte, 1024)
conn.Read(buf)
fmt.Println(string(buf))
}
}
func main() { func main() {
conn, err := net.Dial("tcp", "localhost:5000") conn, err := net.Dial("tcp", "localhost:5000")
go doRead(conn)
if err != nil { if err != nil {
fmt.Println("Err:", err) fmt.Println("Err:", err)
......
...@@ -14,16 +14,19 @@ import ( ...@@ -14,16 +14,19 @@ import (
const ( const (
//request //request
SET = "set" SET = "set"
// GET = "get" GET = "get"
// GETM = "getm" GETM = "getm"
// CAS = "cas" CAS = "cas"
// DELETE = "delete" DELETE = "delete"
NOREPLY = "[noreply]" NOREPLY = "[noreply]"
// //
// //response // //response
// OK = "OK" // OK = "OK"
// VALUE = "VALUE" // VALUE = "VALUE"
// DELETED = "DELETED" // DELETED = "DELETED"
//errors
ERR_CMD_ERR = "ERR_CMD_ERR"
) )
type Data struct { type Data struct {
...@@ -62,44 +65,83 @@ func startServer() { ...@@ -62,44 +65,83 @@ func startServer() {
} }
} }
func read(conn *net.Conn) (msg string, success bool){ func read(conn *net.Conn, toRead uint64) ([]byte, bool){
buf := make([]byte, 1024) buf := make([]byte, toRead)
_, err := (*conn).Read(buf) _, err := (*conn).Read(buf)
if err != nil { if err != nil {
if err == io.EOF { if err == io.EOF {
fmt.Println("Client disconnected!") fmt.Println("Client disconnected!")
return "", false return []byte{0}, false
} }
} }
n := bytes.Index(buf, []byte{0}) n := bytes.Index(buf, []byte{0})
if n != 0 { if n != 0 {
msg := string(buf[:n-1]) fmt.Println("Received: ", string(buf[:n-1]))
fmt.Println("Received: ", msg) return buf[:n-1], true
return msg, true
} }
return "", false return []byte{0}, false
} }
func handleClient(conn *net.Conn, table *KeyValueStore) { func handleClient(conn *net.Conn, table *KeyValueStore) {
defer (*conn).Close() defer (*conn).Close()
for { for {
if msg, ok := read(conn); ok{ if msg, ok := read(conn, 1024); ok{
parseInput(&msg, table) parseInput(conn, string(msg), table)
} }
} }
} }
func parseInput(msg *string, table *KeyValueStore) { func isValid(cmd string, tokens []string, conn *net.Conn) int{
tokens := strings.Fields(*msg) var flag int
fmt.Println(tokens) switch cmd {
case SET:
if len(tokens) > 5 || len(tokens) < 4 {
flag = 1
}
//other validations
case GET:
if len(tokens) != 2 {
flag = 1
}
//other validations
case GETM:
if len(tokens) != 2 {
flag = 1
}
//other validations
case CAS:
if len(tokens) > 6 || len(tokens) < 5 {
flag = 1
}
//other validations
case DELETE:
if len(tokens) != 2 {
flag = 1
}
//other validations
default:
return 0
}
switch flag {
case 1: (*conn).Write([]byte(ERR_CMD_ERR))
}
return flag
}
func parseInput(conn *net.Conn, msg string, table *KeyValueStore) {
tokens := strings.Fields(msg)
//fmt.Println(tokens)
switch tokens[0] { switch tokens[0] {
case SET: case SET:
if v, ok := read(conn); ok{ if isValid(SET, tokens, conn) != 0 {
performSet(tokens[1:len(tokens)], v, table) return
} }
performSet(conn, tokens[1:len(tokens)], table)
//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)])
...@@ -108,7 +150,7 @@ func parseInput(msg *string, table *KeyValueStore) { ...@@ -108,7 +150,7 @@ func parseInput(msg *string, table *KeyValueStore) {
} }
} }
func performSet(tokens []string, val []byte, table *KeyValueStore){ func performSet(conn *net.Conn, tokens []string, table *KeyValueStore){
k := tokens[0] k := tokens[0]
e, _ := strconv.ParseUint(tokens[1], 10, 64) e, _ := strconv.ParseUint(tokens[1], 10, 64)
n, _ := strconv.ParseUint(tokens[2], 10, 64) n, _ := strconv.ParseUint(tokens[2], 10, 64)
...@@ -120,21 +162,28 @@ func performSet(tokens []string, val []byte, table *KeyValueStore){ ...@@ -120,21 +162,28 @@ func performSet(tokens []string, val []byte, table *KeyValueStore){
fmt.Println(r) fmt.Println(r)
//read value //read value
if v, ok := read(conn) v, ok := read(conn, n+2)
if !ok {
//error here
return
}
(*table).Lock() (*table).Lock()
//critical section start //critical section start
if ele, ok := (*table).dictionary[k]; ok { var val *Data
fmt.Println(ele) if _, ok := (*table).dictionary[k]; ok {
val = (*table).dictionary[k]
} else{ } else{
ver++ val = new(Data)
val := new(Data)
(*val).numBytes = n
(*val).version = ver
(*val).expTime = e + uint64(time.Now().Unix())
(*val).value = []byte{0}
(*table).dictionary[k] = val (*table).dictionary[k] = val
} }
ver++
(*val).numBytes = n
(*val).version = ver
(*val).expTime = e + uint64(time.Now().Unix())
(*val).value = v
(*table).Unlock() (*table).Unlock()
debug(table) debug(table)
} }
......
package main
import (
"fmt"
)
func main() {
var n int
fmt.Scanf("%d", &n)
buf := make([]byte, n)
buf[0] = 1
}
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