Inferno Contribution: converter

This example illustrates how to write a Limbo program to provide a service which accepts requests from multiple processes.

A previous contribution from Roger Peppe showed how to use file2chan to provide a service based on a single file, but it would get confused if it was used by more than one process at once.

The example here shows how to use the FID returned by file2chan to handle multiplexed requests from any number of processes.

There are two modules, converts.b (a generalized service module) and degrees.b (an example converter module).

converts.b

This module also implements a "conversion service", ie one which takes a string written to a single file, and returns a new string based on the old one. Because it can be used by multiple simultaneous processes, the file must be held open for reading and writing by the process making use of it.

The module is invoked with two arguments:

  1. The name of the file to be served, eg /chan/temp
  2. The converter module, eg degrees.dis
Eg
% converts /chan/temp degrees.dis
Whenever the served file is written to by a user, the converter module is called to find the reply, and this is stored (in the list replist). When the user reads back the file, the correct reply is located, returned to the user and is deleted from the stored list.

When the served file is closed by the user, any associated reply is discarded.

degrees.b

This is a very simple module which provides a temperature conversion facility. It will convert degrees Fahrenheit to degrees Centigrade and vice-versa.

This module could be replaced by any other module with the same signature (as defined in the template in converter_tmpl.m).

Installation and Testing using the shell

To try out this example, download the three files and compile the two Limbo modules:
% limbo converts.b degrees.b
Now run the convertor:
% converts /chan/temp degrees.dis
If you want to test this using the shell, it is slightly more complicated than just echoing a string to the file and reading it back again, because the file has to be held open. The way to do this is to use the <> operator to open the standard input for both reading and writing, echo a string to this file and read back the result with cat:
% { echo 10F >[1=0] ; cat } <> /chan/temp
This should produce the following reply:
10.00 deg F = -12.22 deg C
Dave Atkin
Vita Nuova
The following files are available for downloading:
converter_tmpl.m (111 bytes)
converts.b (2577 bytes)
degrees.b (845 bytes)