Skip to content

Instantly share code, notes, and snippets.

@passingloop
Created October 6, 2011 05:57
Show Gist options
  • Save passingloop/1266629 to your computer and use it in GitHub Desktop.
Save passingloop/1266629 to your computer and use it in GitHub Desktop.
7l7w-erlang-day-3
[{sasl, [
{sasl_error_logger, {file, "/var/tmp/elog"}}
]}].
-module(immortal_doctor).
-export([loop/0]).
loop() ->
process_flag(trap_exit, true),
receive
new ->
io:format("Creating and monitoring process.~n"),
register(revolver, spawn_link(fun roulette:loop/0)),
loop();
{'EXIT', From, Reason} ->
case Reason of
poisoning ->
io:format("~p poisoned the immortal with reason ~p.", [From, Reason]),
io:format(" Restarting. ~n"),
exit(whereis(revolver), revolver),
Immortal = spawn(fun immortal:loop/0),
Immortal ! new;
_ ->
io:format("The shooter ~p died with reason ~p.", [From, Reason]),
io:format(" Restarting. ~n"),
self() ! new,
loop()
end
end.
-module(translate_server).
-behaviour(gen_server).
-export([translate/1, start_link/0]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
translate(Word) ->
gen_server:call(?MODULE, {trans, Word}, 1000).
init([]) ->
process_flag(trap_exit, true),
io:format("~p starting~n", [?MODULE]),
{ok, 0}.
handle_call({trans, Word}, _From, N) -> {reply, lookup(Word), N+1}.
handle_cast(_Msg, N) -> {noreply, N}.
handle_info(_Info, N) -> {noreply, N}.
terminate(_Reason, _N) ->
io:format("~p stopping~n", [?MODULE]),
ok.
code_change(_OldVsn, N, _Extra) -> {ok, N}.
lookup(Word) ->
case Word of
"casa" -> "white";
"blanca" -> "house"
end.
-module(translate_server_with_log).
-behaviour(gen_server).
-export([translate/1, start_link/0]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
translate(Word) ->
gen_server:call(?MODULE, {trans, Word}, 1000).
init([]) ->
process_flag(trap_exit, true),
error_logger:error_msg("~p starting~n", [?MODULE]),
{ok, 0}.
handle_call({trans, Word}, _From, N) -> {reply, lookup(Word), N+1}.
handle_cast(_Msg, N) -> {noreply, N}.
handle_info(_Info, N) -> {noreply, N}.
terminate(_Reason, _N) ->
error_logger:error_msg("~p stopping~n", [?MODULE]),
ok.
code_change(_OldVsn, N, _Extra) -> {ok, N}.
lookup(Word) ->
case Word of
"casa" -> "white";
"blanca" -> "house"
end.
-module(translate_supervisor).
-behavior(supervisor).
-export([start/0, init/1]).
start() ->
{ok, Pid} = supervisor:start_link({local, ?MODULE}, ?MODULE, _Arg = []),
unlink(Pid).
init([]) ->
{ok, {{one_for_one, 3, 10},
[{ts1,
{translate_server, start_link, []},
permanent,
100000,
worker,
[translate_server]}
]}}.
-module(translate_supervisor_with_log).
-behavior(supervisor).
-export([start/0, init/1]).
start() ->
{ok, Pid} = supervisor:start_link({local, ?MODULE}, ?MODULE, _Arg = []),
unlink(Pid).
init([]) ->
{ok, {{one_for_one, 3, 10},
[{ts1,
{translate_server_with_log, start_link, []},
permanent,
100000,
worker,
[translate_server_with_log]}
]}}.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment