/doc/template.gen_server

https://code.google.com/p/zotonic/ · Unknown · 100 lines · 81 code · 19 blank · 0 comment · 0 complexity · fa0c0f8a33bd00e9d233de3fe526f857 MD5 · raw file

  1. %% @author Marc Worrell <marc@worrell.nl>
  2. %% @copyright 2009 Marc Worrell
  3. %%
  4. %% @doc
  5. %% Copyright 2009 Marc Worrell
  6. %%
  7. %% Licensed under the Apache License, Version 2.0 (the "License");
  8. %% you may not use this file except in compliance with the License.
  9. %% You may obtain a copy of the License at
  10. %%
  11. %% http://www.apache.org/licenses/LICENSE-2.0
  12. %%
  13. %% Unless required by applicable law or agreed to in writing, software
  14. %% distributed under the License is distributed on an "AS IS" BASIS,
  15. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. %% See the License for the specific language governing permissions and
  17. %% limitations under the License.
  18. -module(module_name).
  19. -author("Marc Worrell <marc@worrell.nl>").
  20. -behaviour(gen_server).
  21. %% gen_server exports
  22. -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
  23. -export([start_link/0, start_link/1]).
  24. %% interface functions
  25. -export([
  26. ]).
  27. %%====================================================================
  28. %% API
  29. %%====================================================================
  30. %% @spec start_link() -> {ok,Pid} | ignore | {error,Error}
  31. %% @doc Starts the server
  32. start_link() ->
  33. start_link([]).
  34. start_link(Args) when is_list(Args) ->
  35. gen_server:start_link({local, ?MODULE}, ?MODULE, Args, []).
  36. %%====================================================================
  37. %% gen_server callbacks
  38. %%====================================================================
  39. %% @spec init(Args) -> {ok, State} |
  40. %% {ok, State, Timeout} |
  41. %% ignore |
  42. %% {stop, Reason}
  43. %% @doc Initiates the server.
  44. init(_Args) ->
  45. {ok, []}.
  46. %% @spec 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. %% @doc Trap unknown calls
  54. handle_call(Message, _From, State) ->
  55. {stop, {unknown_call, Message}, State}.
  56. %% @spec handle_cast(Msg, State) -> {noreply, State} |
  57. %% {noreply, State, Timeout} |
  58. %% {stop, Reason, State}
  59. %% @doc Trap unknown casts
  60. handle_cast(Message, State) ->
  61. {stop, {unknown_cast, Message}, State}.
  62. %% @spec handle_info(Info, State) -> {noreply, State} |
  63. %% {noreply, State, Timeout} |
  64. %% {stop, Reason, State}
  65. %% @doc Handling all non call/cast messages
  66. handle_info(_Info, State) ->
  67. {noreply, State}.
  68. %% @spec terminate(Reason, State) -> void()
  69. %% @doc This function is called by a gen_server when it is about to
  70. %% terminate. It should be the opposite of Module:init/1 and do any necessary
  71. %% cleaning up. When it returns, the gen_server terminates with Reason.
  72. %% The return value is ignored.
  73. terminate(_Reason, _State) ->
  74. ok.
  75. %% @spec code_change(OldVsn, State, Extra) -> {ok, NewState}
  76. %% @doc Convert process state when code is changed
  77. code_change(_OldVsn, State, _Extra) ->
  78. {ok, State}.
  79. %%====================================================================
  80. %% support functions
  81. %%====================================================================