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
Show 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() {
fmt
.
Println
(
"Err: "
,
err
)
}
fmt
.
Println
(
msg
)
buf
:=
[]
byte
(
msg
)[
0
:
len
(
msg
)
-
1
]
buf
:=
[]
byte
(
msg
)[
0
:
len
(
msg
)
-
1
]
buf
=
append
(
buf
,
[]
byte
(
"
\r\n
"
)
...
)
fmt
.
Println
(
len
(
buf
))
conn
.
Write
(
buf
)
...
...
assignment1/server.go
View file @
7129b4dc
...
...
@@ -4,14 +4,14 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"os"
"strconv"
"strings"
"sync"
"strconv"
"time"
"log"
"os"
"io/ioutil"
)
const
(
...
...
@@ -22,12 +22,12 @@ const (
CAS
=
"cas"
DELETE
=
"delete"
NOREPLY
=
"noreply"
//
// //response
//
// //response
OK
=
"OK"
CRLF
=
"
\r\n
"
//
VALUE = "VALUE"
// DELETED = "DELETED"
VALUE
=
"VALUE"
// DELETED = "DELETED"
//errors
ERR_CMD_ERR
=
"ERR_CMD_ERR"
...
...
@@ -59,7 +59,7 @@ func startServer() {
}
//initialize key value store
table
:=
&
KeyValueStore
{
dictionary
:
make
(
map
[
string
]
*
Data
)}
table
:=
&
KeyValueStore
{
dictionary
:
make
(
map
[
string
]
*
Data
)}
//infinite loop
for
{
...
...
@@ -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
)
_
,
err
:=
conn
.
Read
(
buf
)
...
...
@@ -103,7 +103,7 @@ func write(conn net.Conn, msg string) {
func
handleClient
(
conn
net
.
Conn
,
table
*
KeyValueStore
)
{
defer
conn
.
Close
()
for
{
if
msg
,
ok
:=
read
(
conn
,
1024
);
ok
{
if
msg
,
ok
:=
read
(
conn
,
1024
);
ok
{
parseInput
(
conn
,
string
(
msg
),
table
)
}
else
{
break
...
...
@@ -111,7 +111,7 @@ 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
switch
cmd
{
case
SET
:
...
...
@@ -161,7 +161,8 @@ func isValid(cmd string, tokens []string, conn net.Conn) int{
}
switch
flag
{
case
1
:
write
(
conn
,
ERR_CMD_ERR
)
case
1
:
write
(
conn
,
ERR_CMD_ERR
)
}
return
flag
...
...
@@ -176,23 +177,41 @@ func parseInput(conn net.Conn, msg string, table *KeyValueStore) {
if
isValid
(
SET
,
tokens
,
conn
)
!=
0
{
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
)
if
r
{
buffer
.
Reset
()
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
:
if
isValid
(
GET
,
tokens
,
conn
)
!=
0
{
return
}
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"
)
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
]
e
,
_
:=
strconv
.
ParseUint
(
tokens
[
1
],
10
,
64
)
n
,
_
:=
strconv
.
ParseUint
(
tokens
[
2
],
10
,
64
)
...
...
@@ -208,7 +227,7 @@ func performSet(conn net.Conn, tokens []string, table *KeyValueStore) (uint64, b
v
,
ok
:=
read
(
conn
,
n
+
2
)
if
!
ok
{
//error here
return
0
,
false
return
0
,
false
,
false
}
table
.
Lock
()
...
...
@@ -217,7 +236,7 @@ func performSet(conn net.Conn, tokens []string, table *KeyValueStore) (uint64, b
var
val
*
Data
if
_
,
ok
:=
table
.
dictionary
[
k
];
ok
{
val
=
table
.
dictionary
[
k
]
}
else
{
}
else
{
val
=
new
(
Data
)
table
.
dictionary
[
k
]
=
val
}
...
...
@@ -230,12 +249,29 @@ func performSet(conn net.Conn, tokens []string, table *KeyValueStore) (uint64, b
table
.
Unlock
()
logger
.
Println
(
"Table unlocked"
)
debug
(
table
)
return
val
.
version
,
true
return
val
.
version
,
true
,
r
}
func
debug
(
table
*
KeyValueStore
){
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
)
{
logger
.
Println
(
"----start debug----"
)
for
key
,
val
:=
range
(
*
table
)
.
dictionary
{
for
key
,
val
:=
range
(
*
table
)
.
dictionary
{
logger
.
Println
(
key
,
val
)
}
logger
.
Println
(
"----end debug----"
)
...
...
@@ -245,10 +281,10 @@ func main() {
ver
=
1
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
()
logger
=
log
.
New
(
logf
,
"SERVER: "
,
log
.
Ltime
|
log
.
Lshortfile
)
}
else
{
}
else
{
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