caparol6991

phone erlang

Nov 25th, 2019
100
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -module(phone).
  2. -export([start/0]).
  3. -export([idle/0]).
  4. -export([call/2]).
  5. -export([off_hook/1]).
  6. -export([off_hook/2]).
  7. -export([on_hook/1]).
  8. -export([billing/1]).
  9. -export([connected/1]).
  10. -export([ringing/1]).
  11. -export([start_tone/0]).
  12. -export([start_ringing/0]).
  13. -export([stop_ringing/0]).
  14. -export([billingCreate/0]).
  15. -export([billingList/0]).
  16. -export([billingAdd/2]).
  17.  
  18.  
  19. call(Pid, Number) ->
  20.     Pid ! {Number, incoming}.
  21.  
  22. off_hook(Pid) ->
  23.     Pid ! off_hook.
  24.  
  25. off_hook(Pid, Number) ->
  26.     Pid ! {Number, off_hook}.
  27.    
  28. on_hook(Pid) ->
  29.     Pid ! on_hook.
  30.  
  31. billing(Pid) ->
  32.     Pid ! billing.
  33.  
  34. start() ->
  35.    billingCreate(),
  36.    spawn(phone, idle, []).
  37.  
  38. idle() ->
  39.     io:fwrite("Telefon czeka na polaczenie!~n", []),
  40.     receive
  41.         {Number, incoming} ->
  42.             start_ringing(),
  43.             ringing(Number);
  44.         off_hook ->
  45.             start_tone(),
  46.             dial();
  47.         billing ->
  48.             billingList(),
  49.             idle()
  50.     end.
  51.  
  52.  
  53. ringing(Number) ->
  54.     io:fwrite("Oczekuje na odebranie!~n", []),
  55.     receive
  56.         {Number, other_on_hook} ->
  57.             stop_ringing(),
  58.             idle();
  59.         {Number, off_hook} ->
  60.             stop_ringing(),
  61.             connected(Number)
  62.     end.
  63.  
  64. connected(Number) ->
  65.     billingAdd(Number,"Rozpoczecie rozmowy"),
  66.     io:fwrite("Polaczono ~n", []),
  67.    
  68.     receive
  69.         on_hook->
  70.             billingAdd(Number,"Zakonczenie rozmowy"),
  71.             idle();
  72.         _ ->
  73.             connected(Number)
  74.     end.
  75.  
  76. dial() ->
  77.     io:format("Tryb laczenia...~n"),
  78.     receive
  79.         on_hook ->
  80.             idle();
  81.         {Number, off_hook} ->
  82.             connected(Number)
  83.     end.
  84.  
  85. start_tone() ->
  86.         io:fwrite("Laczenie...~n", []).
  87.  
  88. start_ringing() ->
  89.         io:fwrite("Telefon dzwoni!~n", []).
  90.  
  91. stop_ringing() ->
  92.         io:fwrite("Telefon przestal dzownic!~n", []).
  93.        
  94.        
  95.        
  96. billingCreate() ->
  97.     ets:new(billing, [public,duplicate_bag,named_table]),
  98.     ets:insert(billing, {"Phone","Podlaczenie telefonu"}).
  99.  
  100. billingList() ->
  101.     erlang:display(ets:lookup(billing, "Phone")).
  102.  
  103. billingAdd(Number,Action) ->
  104.     ets:insert(billing, {"Phone",Number,Action,erlang:localtime()}).
RAW Paste Data