My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
%% -*- erlang-indent-level: 2 -*-
%%% Created : 17 Oct 2008 by Mats Cronqvist <masse@kreditor.se>

%% @doc
%% @end

-module(gen_serv).
-author('Mats Cronqvist').

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% the API

-export([start/1
, start/2
, stop/1
, shutdown/1
, print_state/1
, get_state/1
, get_state/2
, unlink/0]).


%% the gen_server boilerplate
-behaviour(gen_server).
-export([handle_call/3
, handle_cast/2
, handle_info/2
, init/1
, terminate/2
, code_change/3]).

-include("log.hrl").
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% the API

start(Mod) ->
start(Mod,[]).

start(Mod,Args) ->
gen_server:start_link({local, Mod}, ?MODULE, {Mod,Args}, []).

stop(Mod) ->
cast(Mod,stop).

shutdown(Mod) ->
cast(Mod,shutdown).

get_state(Mod,Field) ->
case proplists:is_defined(Field,State = get_state(Mod)) of
true -> proplists:get_value(Field,State);
false-> exit({no_such_field,Field,State})
end.

get_state(Mod) ->
case whereis(Mod) of
undefined -> exit({not_started,Mod});
_ -> gen_server:call(Mod,get_state)
end.

print_state(Mod) ->
print_term(get_state(Mod)).

unlink() ->
{links,Links} = process_info(self(),links),
lists:foreach(fun unlink/1,Links).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% gen_server boiler plate

-record(ld,{mod,args,cld}).

init({Mod,Args}) ->
safer(#ld{mod=Mod,args=Args},init,[Args]).

terminate(Reason,LD) ->
safer(LD,terminate,[Reason,LD#ld.cld]).

code_change(OldVsn,LD,Info) ->
safer(LD,code_change,[OldVsn,LD#ld.cld,Info]).

handle_cast(Msg,LD) ->
safer(LD,handle_cast,[Msg,LD#ld.cld]).

handle_call(Msg,From,LD) ->
safer(LD,handle_call,[Msg,From,LD#ld.cld]).

handle_info(Msg,LD) ->
safer(LD,handle_info,[Msg,LD#ld.cld]).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% implementation

safer(LD,_,[stop|_]) ->
{stop,normal,LD};
safer(LD,_,[shutdown|_]) ->
{stop,shutdown,LD};
safer(LD,F,[get_state|As]) ->
CLD = last(As),
safe_reply(F,LD,{expand_recs(LD#ld.mod,CLD),CLD});
safer(LD,F,As) ->
case erlang:function_exported(LD#ld.mod,F,length(As)) of
false-> safe_default(F,LD);
true ->
try safe_reply(F,LD,apply(LD#ld.mod,F,As))
catch
error:R ->
?log([R,{mfa,{LD#ld.mod,F,As}},erlang:get_stacktrace()]),
safe_default(F,LD);
throw:R ->
{stop,shutdown,R}
end
end.

safe_reply(_,LD,{stop,R,CLD}) -> {stop,R,LD#ld{cld=CLD}};
safe_reply(handle_call,LD,{Reply,CLD}) -> {reply,Reply,LD#ld{cld=CLD}};
safe_reply(handle_call,LD,CLD) -> {noreply,LD#ld{cld=CLD}};
safe_reply(handle_cast,LD,CLD) -> {noreply,LD#ld{cld=CLD}};
safe_reply(handle_info,LD,CLD) -> {noreply,LD#ld{cld=CLD}};
safe_reply(_ ,LD,CLD) -> {ok,LD#ld{cld=CLD}}.

safe_default(init,LD) -> {ok,LD#ld{cld={}}};
safe_default(handle_call,LD) -> {reply,ok,LD};
safe_default(handle_cast,LD) -> {noreply,LD};
safe_default(handle_info,LD) -> {noreply,LD};
safe_default(_ ,LD) -> {ok,LD}.

last(L) -> hd(lists:reverse(L)).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% utilities

print_term(Term) ->
print_term(group_leader(),Term).

print_term(FD,Term) ->
case node(FD) == node() of
true -> error_logger:info_report(Term);
false-> io:fwrite(FD," ~p~n",[Term])
end.

expand_recs(M,List) when is_list(List) ->
[expand_recs(M,L)||L<-List];
expand_recs(M,Tup) when is_tuple(Tup) ->
case tuple_size(Tup) of
L when L < 1 -> Tup;
L ->
try Fields = M:rec_info(element(1,Tup)),
L = length(Fields)+1,
lists:zip(Fields,expand_recs(M,tl(tuple_to_list(Tup))))
catch _:_ ->
list_to_tuple(expand_recs(M,tuple_to_list(Tup)))
end
end;
expand_recs(_,Term) ->
Term.

cast(Mod,What) ->
try gen_server:cast(Mod,What)
catch exit:{noproc,_} -> ok
end.

Change log

r227 by mats.cronqvist on Jan 18, 2010   Diff
  better start/stop handling
Go to: 
Project members, sign in to write a code review

Older revisions

r212 by mats.cronqvist on Sep 4, 2009   Diff
  bug in print state function
r209 by mats.cronqvist on Jul 16, 2009   Diff
  honors {stop, ..., ...} return value
from callbacks
r203 by mats.cronqvist on Jun 15, 2009   Diff
  better shutdown
All revisions of this file

File info

Size: 4115 bytes, 159 lines
Powered by Google Project Hosting