gen_server通用模板
1 %%%------------------------------------------------------------------- 2 %%% File : gen_server_template.full 3 %%% Author : my name <yourname@localhost.localdomain> 4 %%% Description : 5 %%% 6 %%% Created : 2 Mar 2007 by my name <yourname@localhost.localdomain> 7 %%%------------------------------------------------------------------- 8 -module(template). 9 10 -behaviour(gen_server). 11 12 %% API 13 -export([start_link/0]). 14 15 %% gen_server callbacks 16 -export([init/1, handle_call/3, handle_cast/2, handle_info/2, 17 terminate/2, code_change/3]). 18 19 -record(state, {}). 20 21 %%==================================================================== 22 %% API 23 %%==================================================================== 24 %%-------------------------------------------------------------------- 25 %% Function: start_link() -> {ok,Pid} | ignore | {error,Error} 26 %% Description: Starts the server 27 %%-------------------------------------------------------------------- 28 start_link() -> 29 gen_server:start_link({local, ?SERVER}, ?MODULE, [], []). 30 31 %%==================================================================== 32 %% gen_server callbacks 33 %%==================================================================== 34 35 %%-------------------------------------------------------------------- 36 %% Function: init(Args) -> {ok, State} | 37 %% {ok, State, Timeout} | 38 %% ignore | 39 %% {stop, Reason} 40 %% Description: Initiates the server 41 %%-------------------------------------------------------------------- 42 init([]) -> 43 {ok, #state{}}. 44 45 %%-------------------------------------------------------------------- 46 %% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} | 47 %% {reply, Reply, State, Timeout} | 48 %% {noreply, State} | 49 %% {noreply, State, Timeout} | 50 %% {stop, Reason, Reply, State} | 51 %% {stop, Reason, State} 52 %% Description: Handling call messages 53 %%-------------------------------------------------------------------- 54 handle_call(_Request, _From, State) -> 55 Reply = ok, 56 {reply, Reply, State}. 57 58 %%-------------------------------------------------------------------- 59 %% Function: handle_cast(Msg, State) -> {noreply, State} | 60 %% {noreply, State, Timeout} | 61 %% {stop, Reason, State} 62 %% Description: Handling cast messages 63 %%-------------------------------------------------------------------- 64 handle_cast(_Msg, State) -> 65 {noreply, State}. 66 67 %%-------------------------------------------------------------------- 68 %% Function: handle_info(Info, State) -> {noreply, State} | 69 %% {noreply, State, Timeout} | 70 %% {stop, Reason, State} 71 %% Description: Handling all non call/cast messages 72 %%-------------------------------------------------------------------- 73 handle_info(_Info, State) -> 74 {noreply, State}. 75 76 %%-------------------------------------------------------------------- 77 %% Function: terminate(Reason, State) -> void() 78 %% Description: This function is called by a gen_server when it is about to 79 %% terminate. It should be the opposite of Module:init/1 and do any necessary 80 %% cleaning up. When it returns, the gen_server terminates with Reason. 81 %% The return value is ignored. 82 %%-------------------------------------------------------------------- 83 terminate(_Reason, _State) -> 84 ok. 85 86 %%-------------------------------------------------------------------- 87 %% Func: code_change(OldVsn, State, Extra) -> {ok, NewState} 88 %% Description: Convert process state when code is changed 89 %%-------------------------------------------------------------------- 90 code_change(_OldVsn, State, _Extra) -> 91 {ok, State}. 92 93 %%-------------------------------------------------------------------- 94 %%% Internal functions 95 %%--------------------------------------------------------------------