1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
%%%----------------------------------------------------------------------
%%% File : edit_genserv.erl
%%% Author : Luke Gorrie <luke@bluetail.com>
%%% Purpose : Generic utility functions for servers.
%%% Created : 28 Apr 2001 by Luke Gorrie <luke@bluetail.com>
%%%----------------------------------------------------------------------
%% This is a library for writing servers. The idea is to have a less
%% abstract gen_server, in which the protocols are exposed and the
%% servers still use 'receive' to get requests with pattern matching.
-module(edit_genserv).
-author('luke@bluetail.com').
-export([start_link/4, call/2, call/3, reply/2, cast/2]).
%% Start a named process, if it's not already running.
%%
%% Returns: {ok, Pid} | {error, Reason}
start_link(Name, M, F, A) ->
case whereis(Name) of
Pid when pid(Pid) ->
{error, {already_started, Pid}};
undefined ->
Pid = spawn_link(M, F, A),
register(Name, Pid),
{ok, Pid}
end.
%% Protocol for calls (from 'gen'):
%% -> {call, {SenderPid, Ref}, Req}
%% <- {Ref, Reply}
%% Synchronous call.
call(Pid, Req) ->
call(Pid, Req, 10000).
call(Pid, Req, Timeout) ->
{ok, Reply} = gen:call(Pid, call, Req, Timeout),
Reply.
reply(To, Reply) ->
gen:reply(To, Reply).
%% Protocol for casts:
%% -> {cast, Req}
cast(Pid, Req) ->
Pid ! {cast, Req}.
|