Created
November 25, 2011 21:50
-
-
Save japaz/1394490 to your computer and use it in GitHub Desktop.
7L7W Erlang - Day3
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
% Monitor the translate_service and restart it should it die. | |
-module(doctor_translate). | |
-export([loop/0]). | |
loop() -> | |
process_flag(trap_exit, true), | |
receive | |
new -> | |
io:format("Creating and monitoring process.~n"), | |
register(translator, spawn_link(fun translate_service:loop/0)), | |
loop(); | |
{'EXIT', From, Reason} -> | |
io:format("The translate service ~p died with reason ~p.", [From, Reason]), | |
io:format(" Restarting. ~n"), | |
self() ! new, | |
loop() | |
end. |
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
% Make the Doctor process restart itself if it should die. | |
-module(doctor_translate2). | |
-export([loop/0]). | |
loop() -> | |
process_flag(trap_exit, true),· | |
receive | |
new -> | |
io:format("Creating and monitoring process.~n"), | |
register(translator, spawn_link(fun translate_service:loop/0)), | |
loop(); | |
{'EXIT', From, Reason} ->· | |
case Reason of | |
doctor -> | |
io:format("The doctor service ~p died with reason ~p.", [From, Reason]), | |
io:format(" Restarting. ~n"), | |
exit(whereis(translator), translator), | |
Doctor = spawn(fun doctor_translate2:loop/0), | |
Doctor ! new; | |
_Else -> | |
io:format("The translate service ~p died with reason ~p.", [From, Reason]), | |
io:format(" Restarting. ~n"), | |
self() ! new,· | |
loop() | |
end···· | |
end. |
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
% An OTP service that will restart a process if it dies | |
% http://learnyousomeerlang.com/supervisors | |
% Documentation for building a simple OTP server | |
% http://learnyousomeerlang.com/what-is-otp | |
% http://learnyousomeerlang.com/ |
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
1> c(doctor_translate2). | |
{ok,doctor_translate2} | |
2> c(translate_service). | |
{ok,translate_service} | |
3> Doctor = spawn(fun doctor_translate2:loop/0). | |
<0.43.0> | |
4> Doctor ! new. | |
Creating and monitoring process. | |
new | |
5> translate_service:translate(translator, "casa"). | |
"house" | |
6> exit(Doctor, doctor). | |
The doctor service <0.31.0> died with reason doctor.true | |
Restarting. | |
Creating and monitoring process. | |
7> translate_service:translate(translator, "blanca"). | |
"white" | |
8> exit(whereis(translator), translator). | |
The translate service <0.50.0> died with reason translator.true | |
Restarting. | |
Creating and monitoring process. | |
9> translate_service:translate(translator, "blan"). | |
The translate service <0.53.0> died with reason {translate,service,die,at, | |
{23,5,57}}."I don't understand." | |
Restarting. | |
Creating and monitoring process. | |
10> translate_service:translate(translator, "blanca"). | |
"white" |
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
% Monitor the translate_service and restart it should it die. | |
-module(translate_service). | |
-export([loop/0, translate/2]). | |
loop() -> | |
receive | |
{From, "casa"} -> | |
From ! "house", | |
loop(); | |
{From, "blanca"} -> | |
From ! "white", | |
loop(); | |
{From, _} -> | |
From ! "I don't understand.", | |
exit({translate,service,die,at,erlang:time()}) | |
end. | |
translate(To, Word) -> | |
To ! {self(), Word}, | |
receive | |
Translation -> Translation | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you write "money denomination" code in erlang Language ?