CS 321 Spring 2013
Assignment 6
Assignment 6 is due at
5 p.m. Friday, April 19.
It is worth 25 points.
Procedures
E-mail
answers to the exercises below to
ggchappell@alaska.edu
,
using the subject
“SA6
”.
- Your answers should consist of two files:
kvserver.cpp
from Exercise A,
and the answers to Exercise B.
These files (or a single archive file containing them)
should be attached to your e-mail message.
- Be sure to include your name in your e-mail.
- I may not read your homework e-mail immediately.
If you wish to discuss the assignment (or anything else)
with me, send me a separate message with a different subject line.
Exercises (25 pts total)
Exercise A — Key-Value Server
Purpose
In this exercise, you will write a Multi-threaded TCP server
that makes a key-value store accessible via atomic operations.
Instructions
Write a complete C++ program
that serves a key-value store over TCP/IP as described below.
- Your source code should be in the file
kvserver.cpp
.
- The program maintains a key-value store:
with each possible key there is an associated value.
Keys and values are both strings.
If the value corresponding to a key has not been set,
then the value is the empty string
Legal keys and values are strings containing no space characters.
You may place an upper limit on the length of keys and values,
but this must be at least 1000 characters.
(""
).
- The program listens on port 8888 for TCP segments.
When a segment is received it should spawn a thread to handle it.
- A spawned thread should examine the body
of the TCP segment for a command.
A command consists of space-separated strings.
A set command looks like this:
S ID KEY VALUE
When a set command is received, the value corresponding to
KEY
should be set to VALUE
,
and the previous value corresponding to KEY
should be sent to the client in a value response
using the same ID
.
A value response looks like this:
V ID VALUE
A get command looks like this:
G ID KEY
When a get command is received,
the value corresponding to KEY should be sent
via a value response with the same ID
, as above.
- Legal values for an
ID
are the same
as those for keys and values.
- The operations performed by commands must be atomic,
with respect to other commands.
- Received TCP segments that do not contain legal commands
may be ignored.
- When a spawned thread has sent its response,
or decided to send no response,
it should exit.
- Your program should use the C++11 Standard Library
threads package for threading and locking.
Use Dr. Lawlor’s
socket
library
(available on the class webpage) for TCP.
- Threads and strings must be handled properly:
join or detach,
no memory leaks or buffer overflows, etc.
Example Run
- Client sends: “
S 1 abc def
”.
- Server responds: “
V 1
”.
- Client sends: “
S 38 abc qqq
”.
- Server responds: “
V 38 def
”.
- Client sends: “
G 9x 2
”.
- Server responds: “
V 9x
”.
- Client sends: “
G ZZZ abc
”.
- Server responds: “
V ZZZ qqq
”.
- Client sends: “
Howdy!
”.
- Server does not respond.
- Client sends: “
G ZZZ abc
”.
- Server responds: “
V ZZZ qqq
”.
Exercise B — Questions about Exercise A
Instructions
Answer the following questions about the program in Exercise A.
- The specification for the server
requires each command to contain an
ID
,
which is returned in the response.
Why is this a good idea?
- Assuming clients cooperate,
the server allows them to do
mutex-style locking on the key-value store,
or portions of the store.
How can this be done?
- If locking is done as in the previous question,
then a client might get a lock and then crash,
resulting in a permanent lock.
How can this problem be solved?
(A very simple change would be for the server
to preemptively release the lock after some period of time.
This would solve the above problem.
However, if no other change were made,
then this would introduce a new problem that is just as bad:
a client might think it has the lock
when it actually does not.)
Further Notes on the Assignment
- There is no test program;
test your code!
- Coding standards are as in
Assignment 2.