Getting into Erlang, Playing Telephone
by Sean Cribbs
Over the last couple of months, while I haven’t been writing on this blog, I’ve been getting more acquainted with Erlang. In the process, I wrote a twitter API library, a webapp that consumes the Twitter API, and became good friends with Kevin Smith, host of the Pragmatic Programmers “Erlang in Practice” screencast series, and the Erlang guru behind vertebra.
Kevin hosts the local RDU-CH Erlang meetup but was at the Codemash conference last week, so we decided to do a group coding session while he was gone. Since Erlang was originally designed for telephone switches, we thought it would be fun to play a game of “telephone” in Erlang.
The first step was to make sure everyone had Erlang properly installed and could start it up. In order send messages across the network, however, two steps are needed. First, you have to decide on a “cookie” (a secret token, really) and a name for your node, and startup the erlang shell like so:
Once the shell is started, you need to connect to a peer node who will be in the ring, and you do that with
net_adm:ping(Nodename). For example, if I wanted to connect to Mark, I would do:
If you can connect, it should return
pong. To check who is in the cluster of nodes, you can call
nodes(). Once everyone is hooked up, we startup the telephone server, telling it who to pass incoming messages to, and put its name in the global registry. Then it’s just a matter of sending messages around the circle.
A few things we discovered in the process:
- Token-ring network configurations are not very robust. Take one node down and the token (in our case, a message), gets stuck in the node previous to the downed node.
- You have to be careful when defining default matches (i.e. underscores). One of our participants was able to easily create an infinite loop in the ring because our
check_circlefunction didn’t care if the message tuple had three elements.
We’re planning to continue our telephone game next time with a different network configuration, perhaps a client-server (star) formation.