Skip to content

Commit

Permalink
Adding R14B02-style specs
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Smith committed Mar 18, 2011
1 parent d732113 commit ce8c342
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
1 change: 1 addition & 0 deletions rebar.config
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{erl_opts, [debug_info]}.
24 changes: 9 additions & 15 deletions src/gen_nb_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -63,38 +63,32 @@ behaviour_info(callbacks) ->
behaviour_info(_) ->
undefined.

%% @spec start_link(CallbackModule, InitParams) -> Result
%% CallbackModule = atom()
%% InitParams = [any()]
%% Result = {ok, pid()} | {error, any()}
%% @doc Start server listening on IpAddr:Port
-spec start_link(atom(), [any()]) -> ok | ignore | {error, any()}.
start_link(CallbackModule, InitParams) ->
gen_server:start_link(?MODULE, [CallbackModule, InitParams], [{fullsweep_after, 0}]).

%% @spec start_link(Name, CallbackModule, InitParams) -> Result
%% Name = {local, atom()} | {global, atom()}
%% CallbackModule = atom()
%% InitParams = [any()]
%% Result = {ok, pid()} | {error, any()}

%% @doc Start server listening on IpAddr:Port registered as Name
-spec start_link(atom(), atom(), [any()]) -> ok | ignore | {error, any()}.
start_link(Name, CallbackModule, InitParams) ->
gen_server:start_link(Name, ?MODULE, [CallbackModule, InitParams], [{fullsweep_after, 0}]).

%% @spec get_cb_state(#state{}) -> any()
%% @doc Extracts the callback module's state from the server's overall state
%% NOTE: Should only be called by the submodule
-spec get_cb_state(#state{}) -> any().
get_cb_state(#state{server_state=SState}) ->
SState.

%% @spec store_cb_state(any(), #state{}) -> #state{}
%% @doc Stores the callback module's state into the server's state
%% NOTE: Should only be called by the submodule
-spec store_cb_state(any(), #state{}) -> #state{}.
store_cb_state(CBState, State) when is_record(State, state) ->
State#state{server_state=CBState}.

%% @spec add_listen_socket({string(), integer()}, #state{}) -> {ok, #state{}} | {error, any()}
%% @doc Adds a new listener socket to be managed by gen_nb_server
%% NOTE: Should only be called by the submodule
-spec add_listen_socket({string(), integer()}, #state{}) -> {ok, #state{}} | {error, any()}.
add_listen_socket({IpAddr, Port}, #state{cb=Callback, addrs=Addrs, socks=Socks}=State) ->
Key = {IpAddr, Port},
case dict:find(Key, Socks) of
Expand All @@ -109,9 +103,10 @@ add_listen_socket({IpAddr, Port}, #state{cb=Callback, addrs=Addrs, socks=Socks}=
Error
end
end.
%% @spec remove_listen_socket({string(), integer()}, #state{}) -> {ok, #state{}} | {error, any()}

%% @doc Removes a new listener socket to be managed by gen_nb_server
%% NOTE: Should only be called by the submodule
-spec remove_listen_socket({string(), integer()}, #state{}) -> {error, not_listening} | {ok, #state{}}.
remove_listen_socket({IpAddr, Port}, #state{socks=Socks, addrs=Addrs}=State) ->
Key = {IpAddr, Port},
case dict:find(Key, Socks) of
Expand All @@ -123,9 +118,8 @@ remove_listen_socket({IpAddr, Port}, #state{socks=Socks, addrs=Addrs}=State) ->
addrs=dict:erase(Sock, Addrs)}}
end.

%% @spec init(#state{}) -> Result
%% Result = any()
%% @doc Returns the callback module's state
-spec init([atom()|any()]) -> {ok, #state{}} | {error, bad_init_state} | {error, any()}.
init([CallbackModule, InitParams]) ->
process_flag(trap_exit, true),
State = #state{cb=CallbackModule},
Expand Down

0 comments on commit ce8c342

Please sign in to comment.