Distributed Systems

Distributed Systems (W4995-2) -- Fall 2012

Purpose

This is a warm-up exercise before the actual yfs series labs, which requires basic UNIX network programming knowledge. Note that you need to use C/C++ and pure socket API to finish this lab - other languages and external libraries are not accepted. If you are not comfortable with this lab, you will probably reconsider taking this course.

Introduction

You will need to implement a client-server application. The client connects to server using TCP and sends it the name of a file, and the server then looks up the file in the predefined directory. If the file exists, it returns the content of the file to the client side, and the client side saves it as a local file. Moreover, you will need to implement a cache. That is to say, if the file being requested by a client is previously requested, it will read the file directly from cache instead of reading it from disk. The cache can only use no more than 60 MB memory.

More specifically, the logic of the program looks like this:

  1. The server side creates a socket, binds it, and listens to a predefined port. After that, it blocks until a client connects.
  2. Once a client is connected, the server will parse the request and figures out which file the client is asking for. It prints a line "Client xxx.xxx.xxx.xxx is requesting file X", where xxx.xxx.xxx.xxx is the client IP address and X is the file name being requested.
  3. If the file is in the cache, it returns the file directly and outputs "Cache hit. File X sent to the client." If not, it will find the file in the predefined directory and save it in the cache, and then return it to the client and output "Cache miss. File X sent to the client", where X is the file name.
  4. If the file does not exist at all, it will return an error message. It prints a line "File X does not exist", where X is the file name. Note that the server only looks up the file in the given directory, not other directories.
  5. The client side creates a socket, binds it, and sends the request to the server and waits for its response. When the response comes, the client saves it as a local file. It prints a line "File X saved", where X is the local filename. It will output an error message if the file does not exist on the server. In such case, it prints a line "File X does not exist in the server".

You do NOT need to implement a multi-threaded server - a single thread version will be fine.

You are required to handle error gracefully. You will need to check the return value of each function, such as bind and receive. Should there be any error, you will print out the corresponding error message.

Your Job

The server side takes 2 parameters - the port to listen on and the directory to find the file. It looks like this:

% tcp_server port_to_listen_on file_directory
e.g.
% ./tcp_server 9089 /home/dist/lab0
The above argument will make the server listen on port 9089 and look up file in /home/dist/lab0.

The client side takes 4 parameters - the server name, server port, the file to request, and the local directory to save the file in. It looks like this:

% tcp_client server_host server_port file_name directory 
e.g.
% ./tcp_client 59.78.58.28 9089 lab0.html . 
The above argument will make the client connect server 59.78.58.28:9089 and request file lab0.html and save it in the current directory.

A typical output on the server side looks like this:

% ./tcp_server 9089 /home/dist/lab0 
Client 59.78.55.65 is requesting file lab0.html
Cache miss. lab0.html sent to the client
Client 59.78.55.66 is requesting file lab0.html
Cache hit. lab0.html sent to the client
Client 59.78.55.66 is requesting file lab1.html
Cache miss. lab0.html sent to the client
Client 59.78.55.65 is requesting file lab0.html
Cache hit. lab0.html sent to the client

A typical output on the client side looks like this:

% /tcp_client 59.78.58.28 9089 lab0.html . 
lab0.html saved

In addition to the source code, you will also need to write a Readme file containing the following information:

  1. An brief overview of your program - the structure of your program, cache implementation, etc.
  2. The test cases you used to test your program. You should cover as many scenarios as you can. Some examples are as follows:
    • The client requests a non-existent file.
    • The client is connecting to a dead server.
    • The command-line argument is invalid.

Hints

Handin procedure

Prepare a tar file containing all your source code and the readme file, including the Makefile. Upload the tar file onto Courseworks.

Grading

We will test your code in CLIC boxes. Failure to compile in such environment will lead to a ZERO!

Your program will be graded on the basis of (1) how well you handle errors, how good your coding style is (see Policies), and how thorough your Readme file is. The test cases under which we will test your program will explicitly try to test for error conditions. Unless your program fails gracefully with meaningful error message under all test cases, we will deduct points off your grade for each assignment. Hence, you will want to test your system very thoroughly before submitting it, and document your test cases in the Readme. An incomplete Readme will result in minus points, so be as thorough as possible!

We will also severely deduct points if your code is messy. Please consider reading (parts of) a style guide, such as Google's style guide for C++.

Make sure you read the collaboration policies before you begin. You must write all of your code yourself! We will check portions of your code online automatically, so do not attempt to use online code for this assignment. A late assignment will get a ZERO, so please submit before the deadline!

Why all this thoroughness for such a simple assignment?

We thoroughly believe that a good programmer writes every line of code as carefully as if it were the most critical line of code he's ever written! You need to learn (or rehearse) being a great programmer, and this assignment, however simple, is yet another opportunity to do so. You should use the grade and our comments as an opportunity for you to learn how to improve.