September 2013
Intermediate to advanced
548 pages
12h 25m
English
Once we remove the idea that individual services need to have their different message formats, we can use uniform messaging to solve a range of problems. Here, for example, is a “multiserver”:
| multi_server.erl | |
| Line 1 | -module(multi_server). |
| - | -export([start/0]). |
| - | |
| - | start() -> spawn(fun() -> multi_server() end). |
| 5 | |
| - | multi_server() -> |
| - | receive |
| - | {_Pid, {email, _From, _Subject, _Text} = Email} -> |
| - | {ok, S} = file:open("mbox", [write,append]), |
| 10 | io:format(S, "~p.~n", [Email]), |
| - | file:close(S); |
| - | {_Pid, {im, From, Text}} -> |
| - | io:format("Msg (~s): ~s~n",[From, Text]); |
| - | {Pid, {get, File}} -> |
| 15 | Pid ! {self(), file:read_file(File)}; |
| - | Any -> |
| - | io:format("multi server got:~p~n",[Any]) ... |
Read now
Unlock full access