ExercisesThe 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: ClockThis 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.
Define a protocol for
transmitting a date containing the day, month, hour, minute, and
- 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.
- Write the client , which sets the
clock every hour.
Keep track of time differences when
requests are sent.
A Network Coffee MachineWe 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.
when it makes a connection, the client receives a
list of available drinks;
- it then sends to the server its beverage choice;
- the server returns the price of the beverage;
- the client sends the requested price, or some other sum;
- the server responds with the name of the chosen beverage
and shows the change tendered.
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
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.
Rewrite the function establish_server
using the primitives in ThreadUnix.
- 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.
- Write a class cmd_fifo
to manage pending commands. Each new command is assigned
a unique number. For this purpose, implement a class
- Write a class ready_table
for stocking the machine with drinks.
- 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.
- Write the service function waiter.
- 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