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
d8b7f7a4
Commit
d8b7f7a4
authored
Jan 21, 2015
by
Sushant Mahajan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
set command complete
parent
9e48ea33
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
99 additions
and
29 deletions
+99
-29
assignment1/client.go
assignment1/client.go
+9
-0
assignment1/server.go
assignment1/server.go
+78
-29
assignment1/test.go
assignment1/test.go
+12
-0
No files found.
assignment1/client.go
View file @
d8b7f7a4
...
...
@@ -7,8 +7,17 @@ import (
"os"
)
func
doRead
(
conn
net
.
Conn
)
{
for
{
buf
:=
make
([]
byte
,
1024
)
conn
.
Read
(
buf
)
fmt
.
Println
(
string
(
buf
))
}
}
func
main
()
{
conn
,
err
:=
net
.
Dial
(
"tcp"
,
"localhost:5000"
)
go
doRead
(
conn
)
if
err
!=
nil
{
fmt
.
Println
(
"Err:"
,
err
)
...
...
assignment1/server.go
View file @
d8b7f7a4
...
...
@@ -14,16 +14,19 @@ import (
const
(
//request
SET
=
"set"
//
GET = "get"
//
GETM = "getm"
//
CAS = "cas"
//
DELETE = "delete"
GET
=
"get"
GETM
=
"getm"
CAS
=
"cas"
DELETE
=
"delete"
NOREPLY
=
"[noreply]"
//
// //response
// OK = "OK"
// VALUE = "VALUE"
// DELETED = "DELETED"
//errors
ERR_CMD_ERR
=
"ERR_CMD_ERR"
)
type
Data
struct
{
...
...
@@ -62,44 +65,83 @@ func startServer() {
}
}
func
read
(
conn
*
net
.
Conn
)
(
msg
string
,
success
bool
){
buf
:=
make
([]
byte
,
1024
)
func
read
(
conn
*
net
.
Conn
,
toRead
uint64
)
([]
byte
,
bool
){
buf
:=
make
([]
byte
,
toRead
)
_
,
err
:=
(
*
conn
)
.
Read
(
buf
)
if
err
!=
nil
{
if
err
==
io
.
EOF
{
fmt
.
Println
(
"Client disconnected!"
)
return
""
,
false
return
[]
byte
{
0
}
,
false
}
}
n
:=
bytes
.
Index
(
buf
,
[]
byte
{
0
})
if
n
!=
0
{
msg
:=
string
(
buf
[
:
n
-
1
])
fmt
.
Println
(
"Received: "
,
msg
)
return
msg
,
true
fmt
.
Println
(
"Received: "
,
string
(
buf
[
:
n
-
1
]))
return
buf
[
:
n
-
1
],
true
}
return
""
,
false
return
[]
byte
{
0
}
,
false
}
func
handleClient
(
conn
*
net
.
Conn
,
table
*
KeyValueStore
)
{
defer
(
*
conn
)
.
Close
()
for
{
if
msg
,
ok
:=
read
(
conn
);
ok
{
parseInput
(
&
msg
,
table
)
if
msg
,
ok
:=
read
(
conn
,
1024
);
ok
{
parseInput
(
conn
,
string
(
msg
)
,
table
)
}
}
}
func
parseInput
(
msg
*
string
,
table
*
KeyValueStore
)
{
tokens
:=
strings
.
Fields
(
*
msg
)
fmt
.
Println
(
tokens
)
func
isValid
(
cmd
string
,
tokens
[]
string
,
conn
*
net
.
Conn
)
int
{
var
flag
int
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
]
{
case
SET
:
if
v
,
ok
:=
read
(
conn
);
ok
{
performSet
(
tokens
[
1
:
len
(
tokens
)],
v
,
table
)
if
isValid
(
SET
,
tokens
,
conn
)
!=
0
{
return
}
performSet
(
conn
,
tokens
[
1
:
len
(
tokens
)],
table
)
//case GET: performGet(tokens[1:len(tokens)])
//case GETM: performGetm(tokens[1:len(tokens)])
//case CAS: performCas(tokens[1:len(tokens)])
...
...
@@ -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
]
e
,
_
:=
strconv
.
ParseUint
(
tokens
[
1
],
10
,
64
)
n
,
_
:=
strconv
.
ParseUint
(
tokens
[
2
],
10
,
64
)
...
...
@@ -120,21 +162,28 @@ func performSet(tokens []string, val []byte, table *KeyValueStore){
fmt
.
Println
(
r
)
//read value
if
v
,
ok
:=
read
(
conn
)
//read value
v
,
ok
:=
read
(
conn
,
n
+
2
)
if
!
ok
{
//error here
return
}
(
*
table
)
.
Lock
()
//critical section start
if
ele
,
ok
:=
(
*
table
)
.
dictionary
[
k
];
ok
{
fmt
.
Println
(
ele
)
var
val
*
Data
if
_
,
ok
:=
(
*
table
)
.
dictionary
[
k
];
ok
{
val
=
(
*
table
)
.
dictionary
[
k
]
}
else
{
ver
++
val
:=
new
(
Data
)
(
*
val
)
.
numBytes
=
n
(
*
val
)
.
version
=
ver
(
*
val
)
.
expTime
=
e
+
uint64
(
time
.
Now
()
.
Unix
())
(
*
val
)
.
value
=
[]
byte
{
0
}
val
=
new
(
Data
)
(
*
table
)
.
dictionary
[
k
]
=
val
}
ver
++
(
*
val
)
.
numBytes
=
n
(
*
val
)
.
version
=
ver
(
*
val
)
.
expTime
=
e
+
uint64
(
time
.
Now
()
.
Unix
())
(
*
val
)
.
value
=
v
(
*
table
)
.
Unlock
()
debug
(
table
)
}
...
...
assignment1/test.go
0 → 100644
View file @
d8b7f7a4
package
main
import
(
"fmt"
)
func
main
()
{
var
n
int
fmt
.
Scanf
(
"%d"
,
&
n
)
buf
:=
make
([]
byte
,
n
)
buf
[
0
]
=
1
}
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