Skip to content

Instantly share code, notes, and snippets.

@archaelus
Created December 3, 2008 22:56
Show Gist options
  • Save archaelus/31742 to your computer and use it in GitHub Desktop.
Save archaelus/31742 to your computer and use it in GitHub Desktop.
%%%-------------------------------------------------------------------
%% @copyright Geoff Cant
%% @author Geoff Cant <nem@erlang.geek.nz>
%% @version {@vsn}, {@date} {@time}
%% @doc Test process group server
%% @end
%%%-------------------------------------------------------------------
-module(groupsrv).
-behaviour(gen_server).
%% API
-export([start_link/1, test/1, test_all/1]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-record(state, {name}).
-define(SERVER, ?MODULE).
-define(INFO(Format, Args),
error_logger:info_msg("(~p ~p:~p) " ++ Format,
[self(), ?MODULE, ?LINE | Args])).
-define(WARN(Format, Args),
error_logger:warning_msg("(~p ~p:~p) " ++ Format,
[self(), ?MODULE, ?LINE | Args])).
-define(ERR(Format, Args),
error_logger:error_msg("(~p ~p:~p) " ++ Format,
[self(), ?MODULE, ?LINE | Args])).
%%====================================================================
%% API
%%====================================================================
%%--------------------------------------------------------------------
%% @spec start_link() -> {ok,Pid} | ignore | {error,Error}
%% @doc Starts the server
%% @end
%%--------------------------------------------------------------------
start_link(Name) ->
gen_server:start_link(?MODULE, [Name], []).
test(Name) ->
case pg2:get_closest_pid(Name) of
Pid when is_pid(Pid) ->
io:format("~s", [gen_server:call(Pid, test)]);
Else -> Else
end.
test_all(Name) ->
case pg2:get_members(Name) of
List when is_list(List) ->
io:format("~s", [[gen_server:call(Pid, test) || Pid <- List,
is_pid(Pid) ]]);
Else -> Else
end.
%%====================================================================
%% gen_server callbacks
%%====================================================================
%%--------------------------------------------------------------------
%% @private
%% @spec init(Args) -> {ok, State} |
%% {ok, State, Timeout} |
%% ignore |
%% {stop, Reason}
%% @doc Initialises the server's state
%% @end
%%--------------------------------------------------------------------
init([Name]) ->
pg2:create(Name),
case pg2:join(Name, self()) of
ok ->
{ok, #state{name=Name}};
Else ->
{stop, Else}
end.
%%--------------------------------------------------------------------
%% @private
%% @spec
%% handle_call(Request, From, State) -> {reply, Reply, State} |
%% {reply, Reply, State, Timeout} |
%% {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, Reply, State} |
%% {stop, Reason, State}
%% @doc Call message handler callbacks
%% @end
%%--------------------------------------------------------------------
handle_call(test, _From, State) ->
{reply, io_lib:format("~p says hello.~n", [self()]), State}.
%%--------------------------------------------------------------------
%% @private
%% @spec
%% handle_cast(Msg, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% @doc Cast message handler callbacks
%% @end
%%--------------------------------------------------------------------
handle_cast(Msg, State) ->
?WARN("Unexpected cast ~p", [Msg]),
{noreply, State}.
%%--------------------------------------------------------------------
%% @private
%% @spec
%% handle_info(Info, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% @doc Non gen-server message handler callbacks
%% @end
%%--------------------------------------------------------------------
handle_info(Info, State) ->
?WARN("Unexpected info ~p", [Info]),
{noreply, State}.
%%--------------------------------------------------------------------
%% @private
%% @spec terminate(Reason, State) -> void()
%% @doc This function is called by a gen_server when it is about to
%% terminate. It should be the opposite of Module:init/1 and do any necessary
%% cleaning up. When it returns, the gen_server terminates with Reason.
%% The return value is ignored.
%% @end
%%--------------------------------------------------------------------
terminate(_Reason, _State) ->
ok.
%%--------------------------------------------------------------------
%% @private
%% @spec code_change(OldVsn, State, Extra) -> {ok, NewState}
%% @doc Convert process state when code is changed
%% @end
%%--------------------------------------------------------------------
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%%--------------------------------------------------------------------
%%% Internal functions
%%--------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment