Advertisement
Guest User

Untitled

a guest
May 27th, 2015
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. %% ---------------------------------------------------------------------
  2. %% File: mathserver.erl
  3. %%
  4. %% This is a simple implementation of a gen_server callback module.
  5.  
  6. -module(mathserver).
  7.  
  8. -behaviour(gen_server).
  9.  
  10. -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
  11. terminate/2, code_change/3]).
  12.  
  13. -export([
  14. start_link/0,
  15. stop/0,
  16. multiply/2
  17. ]).
  18.  
  19. -define(SERVER, ?MODULE).
  20.  
  21. -record(state, { }).
  22. %%%===================================================================
  23. %%% API
  24. %%%===================================================================
  25.  
  26.  
  27. %%--------------------------------------------------------------------
  28. %% @doc Starts the server.
  29. %%
  30. %% @spec start_link() -> {ok, Pid}
  31. %% where
  32. %% Pid = pid()
  33. %% @end
  34. %%--------------------------------------------------------------------
  35. start_link() ->
  36. gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
  37.  
  38. %%--------------------------------------------------------------------
  39. %% @doc Stops the server.
  40. %% @spec stop() -> ok
  41. %% @end
  42. %%--------------------------------------------------------------------
  43. stop() ->
  44. gen_server:cast(?SERVER, stop).
  45.  
  46. %%--------------------------------------------------------------------
  47. %% @doc multiplies two integers.
  48. %% @spec multiply(X::integer(), Y::integer()) -> {ok, Product}
  49. %% where
  50. %% Product = integer()
  51. %% @end
  52. %%--------------------------------------------------------------------
  53. multiply(X, Y) ->
  54. gen_server:call(?SERVER, {multiply, {X, Y}}).
  55.  
  56.  
  57. %%%===================================================================
  58. %%% gen_server callbacks
  59. %%%===================================================================
  60.  
  61.  
  62. init([]) ->
  63. {ok, #state{}}.
  64.  
  65. handle_call({multiply, {X, Y}}, _From, State) ->
  66. Reply = {ok, multiply_impl(X,Y)},
  67. {reply, Reply, State};
  68. handle_call(_Request, _From, State) ->
  69. Reply = ok,
  70. {reply, Reply, State}.
  71.  
  72. handle_cast(_Msg, State) ->
  73. {noreply, State}.
  74.  
  75. handle_info(_Info, State) ->
  76. {noreply, State}.
  77.  
  78. terminate(_Reason, _State) ->
  79. ok.
  80.  
  81. code_change(_OldVsn, State, _Extra) ->
  82. {ok, State}.
  83.  
  84. %%%===================================================================
  85. %%% API internals
  86. %%%===================================================================
  87.  
  88. multiply_impl(First, Second) ->
  89. First * Second.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement