Lab 8: Server Roulette

In this lab you'll modify your database server to be part of a networked group of servers, then you will randomly connect to one of your classmates and try to find out whose it is.

When you're done with this lab you will have done the following things:

  1. Update your server to load a database when it starts
  2. Update your server to register with a central database "tracker"
  3. Modify your client to get a random database server
  4. Update your client to perform a search on a remote server

Pull your Git repository on your Linux. Do all your work for this lab in the lab8 directory in your repository. When you're finished, check in your code.

Testing: the automated tests will not test the work you do in this lab. For this lab, you'll have to verify your server and client work using your partner's computer, the instructor's Tracker, and by working with other teams.

Part 0: Make sure your Linux can talk to each other

  1. On your VM, launch a server using port 20000 (the -l is a lowercase L for 'listen')

    nc -l -p 20000
    
  2. Have your partner try to connect to your computer

    nc <your_ip> 20000
    

    Replace <your_ip> here with your VM's IP (use hostname -I to find your IP address).

Before starting to code, change the TRACKER_ADDR at the beginning of both server.c and client.c to the IP given by your instructor (it has the form 137.112.___.___). The TRACKER_PORT should be 22222.

Part 1: Importing on startup

As your first step, make your server import a file when it starts up. Right now, when you start the server it has an empty database. You will now specify a file to import when you run the server:

$ ./server data.txt
  1. Add code to server.c so when it starts, it imports a file.
  2. Create a file that can be imported. Practice your creativity to generate a unique file that people may identify you (I love ASCII art). For example, I created a file with this content:
    bob => notme
    alice => notme
    csse => rocks
    

Part 2: Registering your Server with the Tracker

Your instructor will provide you with the IP address for the tracker. The tracker responds to the command from the server: The communication protocol between the tracker and the server is described below:
Server <-----"Hi, this is TRACKER, what can I do for you?\n"---- Tracker
Server --------------------"REGISTER"--------------------------> Tracker
Server <--------------"HELLO <IP address>\n"-------------------- Tracker
(disconnect)
Note that the double quotes are *not* a part of the messages.

Implement sendTrackerCommand

Finish the function `sendTrackerCommand` in `server.c` to:
  1. Connect to the tracker (HINT: use TRACKER_ADDR and TRACKER_PORT; make sure you are using the TRACKER_ADDR given by your instructor)
  2. Send to the tracker the command string in the argument
  3. Print out any response from the tracker to stdout
  4. Disconnect from the tracker

See the comments in server.c for more details. You'll use this function for the remainder of the lab.

Register with the tracker

  1. Update the main function to register with the tracker before runServer.
  2. Be sure to test your registration. Ask your instructor if you want to see if the registration succeeded.
  3. Launch your server and then print out the instructor verification sheet. Show your instructor and have them verify that registration worked.

Part 3: Connecting your Client to a Random Server

Keep your server running for the rest of this lab. If it crashes or is killed, re-launch it. Other students may try to connect to it!

Now it's time to update your client to pick a random server for connecting. The tracker responds to the command from the client:

The communication protocol between the tracker and the server is described below:

Client <----"Hi, this is TRACKER, what can I do for you?\n"----- Tracker
Client --------------------"RANDOM"----------------------------> Tracker
Client <------------"TALKTO <IP address>\n"--------------------- Tracker
(disconnect)

Note that the double quotes are not part of the messages.

To implement this, make the following changes:

  1. Finish getRandomServer. This function connects to the tracker, gets a random server from it, and then stores the IP address in randomIP. See comments in client.c for details. This is very similar to sendTrackerCommand in the server.
  2. Add the 'r' command to the client's runloop. It is almost exactly like 'c', but will get the IP address from the tracker instead of the user input.
  3. Verify that you can connect to a remote server! Try it out. Identify whose server it is by looking at the server's contents.
  4. Get your instructor's signature on your instructor verification sheet!

Finishing the Lab

  1. Scan and upload the signed instructor verification sheet to Gradescope.