Created
June 8, 2015 18:29
-
-
Save jfacorro/804d4d7cf40a36fbd5b8 to your computer and use it in GitHub Desktop.
Campeones - Erlang Dojo 2105
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(mapa). | |
-behaviour(gen_server). | |
-compile(export_all). | |
init (_) -> | |
process_flag(trap_exit, true), | |
{ok, []}. | |
terminate(R, S) -> | |
io:format("R = ~p, S = ~p~n", [R, S]), | |
ok. | |
handle_call(get, _F, S) -> | |
{reply, S, S}; | |
handle_call(T, F, S) -> | |
io:format("T = ~p, F = ~p~n", [T, F]), | |
{noreply, S ++ [T]}. | |
handle_cast(R, S) -> | |
io:format("R = ~p~n", [R]), | |
{noreply, S ++ [R]}. | |
handle_info(I, S) -> | |
io:format("I = ~p~n", [I]), | |
{noreply, S ++ [I]}. | |
code_change(_, S, _) -> {ok, S}. | |
do() -> net_kernel:connect_node('[email protected]'), | |
{ok, _MapaPid} = gen_server:start_link({local, mapa}, mapa, [], []), | |
P = spawn(fun larry/0), | |
erlang:register(larry, P). | |
mostrar() -> | |
receive X -> io:format("X = ~p~n", [X]), mostrar() | |
after 0 -> ok | |
end. | |
ask (Kathy, Token) -> | |
L = gen_fsm:sync_send_event(Kathy, #{question => "flower color", token => Token}), | |
io:format("Color: ~p~n", [L]). | |
larry() -> | |
process_flag(trap_exit, true), | |
M = receive X -> X end, | |
io:format("Recibido mapa: ~p~n", [M]), | |
Kathy = maps:get(kathy, M), | |
Token = maps:get(token, M), | |
Kathy ! flower, | |
mostrar(), | |
Kathy ! #{name => mapa, token => Token}, | |
timer:sleep(1000), | |
[L1, L2, L3] = gen_server:call(mapa, get), | |
Msg = lists:flatten(lists:map(fun erlang:tuple_to_list/1, lists:zip3(L2, L1 ++ " ", L3 ++ " "))), | |
io:format("Kathy dice: ~p~n", [Msg]), | |
{monitored_by, [Carlos]} = rpc:pinfo(Kathy, monitored_by), | |
exit(Carlos, kill), | |
mostrar(), | |
ask(Kathy, Token), | |
ask(Kathy, Token), | |
ask(Kathy, Token), | |
ask(Kathy, Token), | |
ask(Kathy, Token), | |
Kathy ! kathy, | |
mostrar(), | |
ok. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment