Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
cs733
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Sushant Mahajan
cs733
Commits
7129b4dc
Commit
7129b4dc
authored
Jan 21, 2015
by
Sushant Mahajan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
formatted code
parent
2539813d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
127 additions
and
91 deletions
+127
-91
assignment1/client.go
assignment1/client.go
+1
-1
assignment1/server.go
assignment1/server.go
+126
-90
No files found.
assignment1/client.go
View file @
7129b4dc
...
@@ -42,7 +42,7 @@ func main() {
...
@@ -42,7 +42,7 @@ func main() {
fmt
.
Println
(
"Err: "
,
err
)
fmt
.
Println
(
"Err: "
,
err
)
}
}
fmt
.
Println
(
msg
)
fmt
.
Println
(
msg
)
buf
:=
[]
byte
(
msg
)[
0
:
len
(
msg
)
-
1
]
buf
:=
[]
byte
(
msg
)[
0
:
len
(
msg
)
-
1
]
buf
=
append
(
buf
,
[]
byte
(
"
\r\n
"
)
...
)
buf
=
append
(
buf
,
[]
byte
(
"
\r\n
"
)
...
)
fmt
.
Println
(
len
(
buf
))
fmt
.
Println
(
len
(
buf
))
conn
.
Write
(
buf
)
conn
.
Write
(
buf
)
...
...
assignment1/server.go
View file @
7129b4dc
...
@@ -4,43 +4,43 @@ import (
...
@@ -4,43 +4,43 @@ import (
"bytes"
"bytes"
"fmt"
"fmt"
"io"
"io"
"io/ioutil"
"log"
"net"
"net"
"os"
"strconv"
"strings"
"strings"
"sync"
"sync"
"strconv"
"time"
"time"
"log"
"os"
"io/ioutil"
)
)
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"
CRLF
=
"
\r\n
"
CRLF
=
"
\r\n
"
//
VALUE = "VALUE"
VALUE
=
"VALUE"
// DELETED = "DELETED"
// DELETED = "DELETED"
//errors
//errors
ERR_CMD_ERR
=
"ERR_CMD_ERR"
ERR_CMD_ERR
=
"ERR_CMD_ERR"
//logging
//logging
LOG
=
true
LOG
=
true
)
)
type
Data
struct
{
type
Data
struct
{
numBytes
uint64
numBytes
uint64
version
uint64
version
uint64
expTime
uint64
expTime
uint64
value
[]
byte
value
[]
byte
}
}
type
KeyValueStore
struct
{
type
KeyValueStore
struct
{
...
@@ -59,7 +59,7 @@ func startServer() {
...
@@ -59,7 +59,7 @@ func startServer() {
}
}
//initialize key value store
//initialize key value store
table
:=
&
KeyValueStore
{
dictionary
:
make
(
map
[
string
]
*
Data
)}
table
:=
&
KeyValueStore
{
dictionary
:
make
(
map
[
string
]
*
Data
)}
//infinite loop
//infinite loop
for
{
for
{
...
@@ -73,7 +73,7 @@ func startServer() {
...
@@ -73,7 +73,7 @@ func startServer() {
}
}
}
}
func
read
(
conn
net
.
Conn
,
toRead
uint64
)
([]
byte
,
bool
){
func
read
(
conn
net
.
Conn
,
toRead
uint64
)
([]
byte
,
bool
)
{
buf
:=
make
([]
byte
,
toRead
)
buf
:=
make
([]
byte
,
toRead
)
_
,
err
:=
conn
.
Read
(
buf
)
_
,
err
:=
conn
.
Read
(
buf
)
...
@@ -103,7 +103,7 @@ func write(conn net.Conn, msg string) {
...
@@ -103,7 +103,7 @@ func write(conn net.Conn, msg string) {
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
,
1024
);
ok
{
if
msg
,
ok
:=
read
(
conn
,
1024
);
ok
{
parseInput
(
conn
,
string
(
msg
),
table
)
parseInput
(
conn
,
string
(
msg
),
table
)
}
else
{
}
else
{
break
break
...
@@ -111,57 +111,58 @@ func handleClient(conn net.Conn, table *KeyValueStore) {
...
@@ -111,57 +111,58 @@ func handleClient(conn net.Conn, table *KeyValueStore) {
}
}
}
}
func
isValid
(
cmd
string
,
tokens
[]
string
,
conn
net
.
Conn
)
int
{
func
isValid
(
cmd
string
,
tokens
[]
string
,
conn
net
.
Conn
)
int
{
var
flag
int
var
flag
int
switch
cmd
{
switch
cmd
{
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"
)
logger
.
Println
(
cmd
,
":Invalid no. of tokens"
)
}
}
if
len
([]
byte
(
tokens
[
1
]))
>
250
{
if
len
([]
byte
(
tokens
[
1
]))
>
250
{
flag
=
1
flag
=
1
logger
.
Println
(
cmd
,
":Invalid size of key"
)
logger
.
Println
(
cmd
,
":Invalid size of key"
)
}
}
if
len
(
tokens
)
==
5
&&
tokens
[
4
]
!=
NOREPLY
{
if
len
(
tokens
)
==
5
&&
tokens
[
4
]
!=
NOREPLY
{
logger
.
Println
(
cmd
,
":optional arg incorrect"
)
logger
.
Println
(
cmd
,
":optional arg incorrect"
)
flag
=
1
flag
=
1
}
}
if
_
,
err
:=
strconv
.
ParseUint
(
tokens
[
2
],
10
,
64
);
err
!=
nil
{
if
_
,
err
:=
strconv
.
ParseUint
(
tokens
[
2
],
10
,
64
);
err
!=
nil
{
logger
.
Println
(
cmd
,
":expiry time invalid"
)
logger
.
Println
(
cmd
,
":expiry time invalid"
)
flag
=
1
flag
=
1
}
}
if
_
,
err
:=
strconv
.
ParseUint
(
tokens
[
3
],
10
,
64
);
err
!=
nil
{
if
_
,
err
:=
strconv
.
ParseUint
(
tokens
[
3
],
10
,
64
);
err
!=
nil
{
logger
.
Println
(
cmd
,
":numBytes invalid"
)
logger
.
Println
(
cmd
,
":numBytes invalid"
)
flag
=
1
flag
=
1
}
}
//other validations
//other validations
case
GET
:
case
GET
:
if
len
(
tokens
)
!=
2
{
if
len
(
tokens
)
!=
2
{
flag
=
1
flag
=
1
}
}
//other validations
//other validations
case
GETM
:
case
GETM
:
if
len
(
tokens
)
!=
2
{
if
len
(
tokens
)
!=
2
{
flag
=
1
flag
=
1
}
}
//other validations
//other validations
case
CAS
:
case
CAS
:
if
len
(
tokens
)
>
6
||
len
(
tokens
)
<
5
{
if
len
(
tokens
)
>
6
||
len
(
tokens
)
<
5
{
flag
=
1
flag
=
1
}
}
//other validations
//other validations
case
DELETE
:
case
DELETE
:
if
len
(
tokens
)
!=
2
{
if
len
(
tokens
)
!=
2
{
flag
=
1
flag
=
1
}
}
//other validations
//other validations
default
:
default
:
return
0
return
0
}
}
switch
flag
{
switch
flag
{
case
1
:
write
(
conn
,
ERR_CMD_ERR
)
case
1
:
write
(
conn
,
ERR_CMD_ERR
)
}
}
return
flag
return
flag
...
@@ -172,32 +173,50 @@ func parseInput(conn net.Conn, msg string, table *KeyValueStore) {
...
@@ -172,32 +173,50 @@ func parseInput(conn net.Conn, msg string, table *KeyValueStore) {
var
buffer
bytes
.
Buffer
var
buffer
bytes
.
Buffer
//logger.Println(tokens)
//logger.Println(tokens)
switch
tokens
[
0
]
{
switch
tokens
[
0
]
{
case
SET
:
case
SET
:
if
isValid
(
SET
,
tokens
,
conn
)
!=
0
{
if
isValid
(
SET
,
tokens
,
conn
)
!=
0
{
return
return
}
}
if
ver
,
ok
:=
performSet
(
conn
,
tokens
[
1
:
len
(
tokens
)],
table
);
ok
{
if
ver
,
ok
,
r
:=
performSet
(
conn
,
tokens
[
1
:
len
(
tokens
)],
table
);
ok
{
logger
.
Println
(
ver
)
logger
.
Println
(
ver
)
if
r
{
buffer
.
Reset
()
buffer
.
WriteString
(
OK
)
buffer
.
WriteString
(
OK
)
buffer
.
WriteString
(
" "
)
buffer
.
WriteString
(
" "
)
buffer
.
WriteString
(
strconv
.
FormatUint
(
ver
,
10
))
buffer
.
WriteString
(
strconv
.
FormatUint
(
ver
,
10
))
logger
.
Println
(
buffer
.
String
())
logger
.
Println
(
buffer
.
String
())
write
(
conn
,
buffer
.
String
())
write
(
conn
,
buffer
.
String
())
}
}
case
GET
:
performGet
(
tokens
[
1
:
len
(
tokens
)])
}
//case GETM: performGetm(tokens[1:len(tokens)])
case
GET
:
//case CAS: performCas(tokens[1:len(tokens)])
if
isValid
(
GET
,
tokens
,
conn
)
!=
0
{
//case DELETE: performDelete(tokens[1:len(tokens)])
return
default
:
logger
.
Println
(
"Command not found"
)
}
if
data
,
ok
:=
performGet
(
conn
,
tokens
[
1
:
len
(
tokens
)],
table
);
ok
{
logger
.
Println
(
"sending"
,
tokens
[
1
],
"data"
)
buffer
.
Reset
()
buffer
.
WriteString
(
VALUE
)
buffer
.
WriteString
(
" "
)
buffer
.
WriteString
(
strconv
.
FormatUint
(
data
.
numBytes
,
10
))
write
(
conn
,
buffer
.
String
())
buffer
.
Reset
()
buffer
.
Write
(
data
.
value
)
write
(
conn
,
buffer
.
String
())
}
//case GETM: performGetm(tokens[1:len(tokens)])
//case CAS: performCas(tokens[1:len(tokens)])
//case DELETE: performDelete(tokens[1:len(tokens)])
default
:
logger
.
Println
(
"Command not found"
)
}
}
}
}
func
performSet
(
conn
net
.
Conn
,
tokens
[]
string
,
table
*
KeyValueStore
)
(
uint64
,
bool
)
{
func
performSet
(
conn
net
.
Conn
,
tokens
[]
string
,
table
*
KeyValueStore
)
(
uint64
,
bool
,
bool
)
{
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
)
r
:=
true
r
:=
true
if
len
(
tokens
)
==
4
&&
tokens
[
3
]
==
NOREPLY
{
if
len
(
tokens
)
==
4
&&
tokens
[
3
]
==
NOREPLY
{
r
=
false
r
=
false
}
}
...
@@ -205,10 +224,10 @@ func performSet(conn net.Conn, tokens []string, table *KeyValueStore) (uint64, b
...
@@ -205,10 +224,10 @@ func performSet(conn net.Conn, tokens []string, table *KeyValueStore) (uint64, b
logger
.
Println
(
r
)
logger
.
Println
(
r
)
//read value
//read value
v
,
ok
:=
read
(
conn
,
n
+
2
)
v
,
ok
:=
read
(
conn
,
n
+
2
)
if
!
ok
{
if
!
ok
{
//error here
//error here
return
0
,
false
return
0
,
false
,
false
}
}
table
.
Lock
()
table
.
Lock
()
...
@@ -217,7 +236,7 @@ func performSet(conn net.Conn, tokens []string, table *KeyValueStore) (uint64, b
...
@@ -217,7 +236,7 @@ func performSet(conn net.Conn, tokens []string, table *KeyValueStore) (uint64, b
var
val
*
Data
var
val
*
Data
if
_
,
ok
:=
table
.
dictionary
[
k
];
ok
{
if
_
,
ok
:=
table
.
dictionary
[
k
];
ok
{
val
=
table
.
dictionary
[
k
]
val
=
table
.
dictionary
[
k
]
}
else
{
}
else
{
val
=
new
(
Data
)
val
=
new
(
Data
)
table
.
dictionary
[
k
]
=
val
table
.
dictionary
[
k
]
=
val
}
}
...
@@ -230,12 +249,29 @@ func performSet(conn net.Conn, tokens []string, table *KeyValueStore) (uint64, b
...
@@ -230,12 +249,29 @@ func performSet(conn net.Conn, tokens []string, table *KeyValueStore) (uint64, b
table
.
Unlock
()
table
.
Unlock
()
logger
.
Println
(
"Table unlocked"
)
logger
.
Println
(
"Table unlocked"
)
debug
(
table
)
debug
(
table
)
return
val
.
version
,
true
return
val
.
version
,
true
,
r
}
func
performGet
(
conn
net
.
Conn
,
tokens
[]
string
,
table
*
KeyValueStore
)
(
*
Data
,
bool
)
{
k
:=
tokens
[
0
]
defer
table
.
RUnlock
()
table
.
RLock
()
//critical section begin
if
v
,
ok
:=
table
.
dictionary
[
k
];
ok
{
data
:=
new
(
Data
)
data
.
numBytes
=
v
.
numBytes
logger
.
Println
(
"Value:"
,
v
.
value
)
data
.
value
=
v
.
value
[
:
]
logger
.
Println
(
"Value:"
,
data
.
value
)
return
data
,
true
}
else
{
return
nil
,
false
}
}
}
func
debug
(
table
*
KeyValueStore
){
func
debug
(
table
*
KeyValueStore
)
{
logger
.
Println
(
"----start debug----"
)
logger
.
Println
(
"----start debug----"
)
for
key
,
val
:=
range
(
*
table
)
.
dictionary
{
for
key
,
val
:=
range
(
*
table
)
.
dictionary
{
logger
.
Println
(
key
,
val
)
logger
.
Println
(
key
,
val
)
}
}
logger
.
Println
(
"----end debug----"
)
logger
.
Println
(
"----end debug----"
)
...
@@ -245,10 +281,10 @@ func main() {
...
@@ -245,10 +281,10 @@ func main() {
ver
=
1
ver
=
1
if
LOG
{
if
LOG
{
logf
,
_
:=
os
.
OpenFile
(
"serverlog.log"
,
os
.
O_RDWR
|
os
.
O_CREATE
|
os
.
O_TRUNC
,
0666
)
logf
,
_
:=
os
.
OpenFile
(
"serverlog.log"
,
os
.
O_RDWR
|
os
.
O_CREATE
|
os
.
O_TRUNC
,
0666
)
defer
logf
.
Close
()
defer
logf
.
Close
()
logger
=
log
.
New
(
logf
,
"SERVER: "
,
log
.
Ltime
|
log
.
Lshortfile
)
logger
=
log
.
New
(
logf
,
"SERVER: "
,
log
.
Ltime
|
log
.
Lshortfile
)
}
else
{
}
else
{
logger
=
log
.
New
(
ioutil
.
Discard
,
"SERVER: "
,
log
.
Ldate
)
logger
=
log
.
New
(
ioutil
.
Discard
,
"SERVER: "
,
log
.
Ldate
)
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment