Department of Information Technology

Project: More on Distributed Erlang

Erlang Nodes

The central concept in distributed Erlang is the node. A node is a self contained Erlang system containing a virtual machine with its own address space and own set of processes.
Access to a single node or set of nodes is secured by a cookie system. Each node has a single cookie, and this cookie must be the same as the cookies of any nodes to which the node talks. In order to enforce this, all the nodes in a distributed Erlang system must have been started with the same magic cookie.

Starting a Node

In order to start an Erlang node, open a terminal and enter the following command:

erl -name a_name -setcookie a_string

The argument -name a_name means "start an Erlang node with name a_name on the localhost". Note how the Erlang shell prints the name of the node before the command prompt.

(a_name@localhost)1>

In order to close the node, type q(). (don't forget the dot!)

Registered Processes

If you want to send a message to a process, then we need to know its PID. This is often inconvenient since the PID has to be known to all processes willing to communicate with that process.
Erlang has a method for publishing a process identifier so that any process in the system can communicate with this process. Such a process is said to be registered.
In order to register a process, we make use of the following command.

register(AnAtom,Pid).

This means: register the process Pid with the name AnAtom. The registration fails if AnAtom has already been used to register a process.

One can send messages to locally registered process in a set of Distributed Erlang nodes using the following syntax:

{RegName, Node} ! Msg

This sends the message Msg to the registered process RegName on the node Node.

Example

In order to illustrate, we give an example where we make use of the different concepts listed previously.

Client Side

Download the following client.erl module and have a look at it.
Can you tell what the function start(Param_1,Param_2) does?

start(ServerName,Node) ->
    {ServerName,Node} ! {connect, self()},
    receive
	{ok,ServerPid} -> connected(ServerPid)
    after 5000 ->
	    io:format("Server busy or unavailable.~n",[]),
	    exit(normal)
    end.

Server Side

Download the following server.erl module and try to understand what it does.
Do you recognize any of the commands used in the start() function?

start() ->
    ServerPid = spawn(fun() -> server_loop([]) end),
    register(my_registered_server, ServerPid).

Exercise (not compulsory)
  • Compile the given modules.
  • Start at least two Erlang nodes on two different terminals (client_1 and server). You are free to start any number of nodes.

Don't forget to use the same cookie!!

  • Start the server on one of them

server:start().

  • Start several clients on the remaining nodes. What are the parameters needed in order to start a client? Can you tell what they are in your case?
  • See what happens.
  • Feel free to edit and experiment more.

Updated  2008-03-19 15:52:25 by Noomene Ben Henda.