The Application Servers
Our application has two servers: a prime number server and an area server.
The Prime Number Server
Here’s the prime number server. It has been written using
the gen_server
behavior (see Getting Started with gen_server). Note how it includes the alarm
handling procedures we developed in the previous section.
prime_server.erl | |
| -module(prime_server). |
| |
| -behaviour(gen_server). |
| |
| -export([new_prime/1, start_link/0]). |
| |
| %% gen_server callbacks |
| -export([init/1, handle_call/3, handle_cast/2, handle_info/2, |
| terminate/2, code_change/3]). |
| |
| start_link() -> |
| gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). |
| |
| new_prime(N) -> |
| %% 20000 is a timeout (ms) |
| gen_server:call(?MODULE, {prime, N}, 20000). ... |
Get Programming Erlang, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.