Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 4th, 2012  |  syntax: None  |  size: 2.48 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. -module(fridge2).
  2. -compile([export_all]).
  3.  
  4. start_fridge() ->
  5.   spawn(?MODULE, restarter, []).
  6.  
  7. stop_fridge() ->
  8.   compressor ! terminate,
  9.   fridge ! terminate.
  10.  
  11. restarter() ->
  12.   process_flag(trap_exit, true),
  13.   Pid = spawn_link(?MODULE, fridge, [[{bacon, 45}, {eggs, 60}]]),
  14.   register(fridge, Pid),
  15.   plugin(),
  16.   receive
  17.     {'EXIT', Pid, normal} -> % not a crash
  18.       ok;
  19.     {'EXIT', Pid, shutdown} -> % manual termination, not a crash
  20.       ok;
  21.     {'EXIT', Pid, _} ->
  22.       restarter()
  23.   end.
  24.  
  25. plugin() ->
  26.   spawn(?MODULE, restart_compressor, []).
  27.  
  28. unplug() ->
  29.   compressor ! terminate.
  30.  
  31. restart_compressor() ->
  32.   process_flag(trap_exit, true),
  33.   Pid = spawn_link(?MODULE, compressor, [5000]),
  34.   register(compressor, Pid),
  35.   receive
  36.     {'EXIT', Pid, normal} -> % not a crash
  37.       io:format("The compressor has been unplugged!~n");
  38.     {'EXIT', Pid, shutdown} -> % manual termination, not a crash
  39.       ok;
  40.     {'EXIT', Pid, _} ->
  41.       restart_compressor()
  42.   end.
  43.  
  44. compressor(Time) ->
  45.   Pid = whereis(fridge),
  46.   receive
  47.     {Pid, {cooled, FoodList}} ->
  48.       io:format("The contents are now:~n~p~n", [FoodList]),
  49.       compressor(Time);
  50.     terminate ->
  51.       ok
  52.   after Time ->
  53.       fridge ! {self(), {cool, 1}},
  54.       compressor(Time)
  55.   end.
  56.  
  57. take(Food) ->
  58.   fridge ! {self(), {take, Food}},
  59.   Pid = whereis(fridge),
  60.   receive
  61.     {Pid, {ok, Food}} -> Food
  62.   after 2000 ->
  63.       timeout
  64.   end.
  65.  
  66. list() ->
  67.   fridge ! {self(), list},
  68.   Pid = whereis(fridge),
  69.   receive
  70.  
  71.     {Pid, Food} -> Food
  72.   after 2000 ->
  73.       timeout
  74.   end.
  75.  
  76. store(Food, Temp) ->
  77.   fridge ! {self(), {store, {Food, Temp}}},
  78.   Pid = whereis(fridge),
  79.   receive
  80.     {Pid, {ok}} -> stored
  81.   after 2000 ->
  82.       timeout
  83.   end.
  84.  
  85. fridge(FoodList) ->
  86.   receive
  87.     {From, {store, {Food, Temp}}} ->
  88.       From ! {self(), {ok}},
  89.       fridge([{Food, Temp}|FoodList]);
  90.     {From, {take, Food}} ->
  91.       case lists:keyfind(Food, 1, FoodList) of
  92.         {Food,Temp} ->
  93.           From ! {self(), {ok, Food}},
  94.           fridge(lists:delete({Food,Temp}, FoodList));
  95.         false ->
  96.           From ! {self(), not_found},
  97.           fridge(FoodList)
  98.       end;
  99.     {From, {cool, Degrees}} ->
  100.       Cooled = [{Item, Temp - Degrees} || {Item, Temp} <- FoodList, Temp > 43],
  101.       Cold = [{Item, Temp} || {Item, Temp} <- FoodList, Temp =< 43],
  102.       NewContents = lists:flatten([Cold|Cooled]),
  103.       From ! {self(), {cooled, NewContents}},
  104.       fridge(NewContents);
  105.     {From, list} ->
  106.       From ! {self(), FoodList},
  107.       fridge(FoodList);
  108.     terminate ->
  109.       ok
  110.   end.