天天看點

gen_server的模闆

-module(first_gen_server).

-behaviour(gen_server).

-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).

%% ====================================================================

%% API functions

-export([]).

%% Behavioural functions

-record(state, {}).

%% init/1

%% @doc <a href="http://www.erlang.org/doc/man/gen_server.html#Module:init-1">gen_server:init/1</a>

-spec init(Args :: term()) -> Result when

    Result :: {ok, State}

            | {ok, State, Timeout}

            | {ok, State, hibernate}

            | {stop, Reason :: term()}

            | ignore,

    State :: term(),

    Timeout :: non_neg_integer() | infinity.

init([]) ->

    {ok, #state{}}.

%% handle_call/3

%% @doc <a href="http://www.erlang.org/doc/man/gen_server.html#Module:handle_call-3">gen_server:handle_call/3</a>

-spec handle_call(Request :: term(), From :: {pid(), Tag :: term()}, State :: term()) -> Result when

    Result :: {reply, Reply, NewState}

            | {reply, Reply, NewState, Timeout}

            | {reply, Reply, NewState, hibernate}

            | {noreply, NewState}

            | {noreply, NewState, Timeout}

            | {noreply, NewState, hibernate}

            | {stop, Reason, Reply, NewState}

            | {stop, Reason, NewState},

    Reply :: term(),

    NewState :: term(),

    Timeout :: non_neg_integer() | infinity,

    Reason :: term().

handle_call(Request, From, State) ->

    Reply = ok,

    {reply, Reply, State}.

%% handle_cast/2

%% @doc <a href="http://www.erlang.org/doc/man/gen_server.html#Module:handle_cast-2">gen_server:handle_cast/2</a>

-spec handle_cast(Request :: term(), State :: term()) -> Result when

    Result :: {noreply, NewState}

            | {stop, Reason :: term(), NewState},

handle_cast(Msg, State) ->

    {noreply, State}.

%% handle_info/2

%% @doc <a href="http://www.erlang.org/doc/man/gen_server.html#Module:handle_info-2">gen_server:handle_info/2</a>

-spec handle_info(Info :: timeout | term(), State :: term()) -> Result when

handle_info(Info, State) ->

%% terminate/2

%% @doc <a href="http://www.erlang.org/doc/man/gen_server.html#Module:terminate-2">gen_server:terminate/2</a>

-spec terminate(Reason, State :: term()) -> Any :: term() when

    Reason :: normal

            | shutdown

            | {shutdown, term()}

            | term().

terminate(Reason, State) ->

    ok.

%% code_change/3

%% @doc <a href="http://www.erlang.org/doc/man/gen_server.html#Module:code_change-3">gen_server:code_change/3</a>

-spec code_change(OldVsn, State :: term(), Extra :: term()) -> Result when

    Result :: {ok, NewState :: term()} | {error, Reason :: term()},

    OldVsn :: Vsn | {down, Vsn},

    Vsn :: term().

code_change(OldVsn, State, Extra) ->

    {ok, State}.

%% Internal functions