Previous Contents Next

Exercises

The suggested exercises allow you to try different types of distributed applications. The first offers a new network service for setting the time on client machines. The second exercise shows how to use resources on different machines to distribute a calculation.

Service: Clock

This exercise consists of implementing a ``clock'' service that gives the time to any client. The idea is to have a reference machine to set the time for different machines on a network.

  1. Define a protocol for transmitting a date containing the day, month, hour, minute, and second.

  2. Write the function or the class for the service reusing one of the generic servers presented in the Chapter. The service sends date information over each accepted connection, then closes the socket.

  3. Write the client , which sets the clock every hour.

  4. Keep track of time differences when requests are sent.

A Network Coffee Machine

We can build a little service that simulates a beverage vending machine. A summary description of the protocol between the client and service is as follows: The server may also respond with an error message if it has not understood a request, does not have enough change, etc. A client request always contains just one piece of information.

The exchanges between client and server are in the form of strings of characters. The different components of a message are separated by two periods and all strings end in :$\n.

The service function communicates with the coffee machine by using a file to pass commands and a hash table for recovering drinks and change.

This exercise will make use of sockets, lightweight processes with a little concurrency, and objects.

  1. Rewrite the function establish_server using the primitives in ThreadUnix.

  2. Write two functions, get_request and send_answer. The first function reads and encodes a request and the second formats and sends a response beginning with a list of strings of characters.

  3. Write a class cmd_fifo to manage pending commands. Each new command is assigned a unique number. For this purpose, implement a class num_cmd_gen.

  4. Write a class ready_table for stocking the machine with drinks.

  5. Write the class machine that models the coffee machine. The class contains a method run that loops through the sequence: wait for a command, then execute it, as long as there remain drinks available. Define a type drink_descr indicating, for each drink: its name, the quantity in stock, the quantity that will remain after satisying pending commands, and its price. We can use an auxiliary function array_index which returns the index of the first element in a table satisfying a criterion passed as a parameter.

  6. Write the service function waiter.

  7. Write the principal function main that obtains a port number for the service from the command line and performs a number of initialization tasks. In particular, the coffee machine executes in a process.

Previous Contents Next