Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %% ---------------------------------------------------------------------
- %% File: mathserver.erl
- %%
- %% This is a simple implementation of a gen_server callback module.
- -module(mathserver).
- -behaviour(gen_server).
- -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
- terminate/2, code_change/3]).
- -export([
- start_link/0,
- stop/0,
- multiply/2
- ]).
- -define(SERVER, ?MODULE).
- -record(state, { }).
- %%%===================================================================
- %%% API
- %%%===================================================================
- %%--------------------------------------------------------------------
- %% @doc Starts the server.
- %%
- %% @spec start_link() -> {ok, Pid}
- %% where
- %% Pid = pid()
- %% @end
- %%--------------------------------------------------------------------
- start_link() ->
- gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
- %%--------------------------------------------------------------------
- %% @doc Stops the server.
- %% @spec stop() -> ok
- %% @end
- %%--------------------------------------------------------------------
- stop() ->
- gen_server:cast(?SERVER, stop).
- %%--------------------------------------------------------------------
- %% @doc multiplies two integers.
- %% @spec multiply(X::integer(), Y::integer()) -> {ok, Product}
- %% where
- %% Product = integer()
- %% @end
- %%--------------------------------------------------------------------
- multiply(X, Y) ->
- gen_server:call(?SERVER, {multiply, {X, Y}}).
- %%%===================================================================
- %%% gen_server callbacks
- %%%===================================================================
- init([]) ->
- {ok, #state{}}.
- handle_call({multiply, {X, Y}}, _From, State) ->
- Reply = {ok, multiply_impl(X,Y)},
- {reply, Reply, State};
- handle_call(_Request, _From, State) ->
- Reply = ok,
- {reply, Reply, State}.
- handle_cast(_Msg, State) ->
- {noreply, State}.
- handle_info(_Info, State) ->
- {noreply, State}.
- terminate(_Reason, _State) ->
- ok.
- code_change(_OldVsn, State, _Extra) ->
- {ok, State}.
- %%%===================================================================
- %%% API internals
- %%%===================================================================
- multiply_impl(First, Second) ->
- First * Second.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement