Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 2.30 KB | None | 0 0
  1. -module(frequency).
  2. -export([start/0, stop/0, allocate/0, deallocate/1, is_running/0]).
  3. -export([init/0]).
  4. -compile([export_all]).
  5.  
  6. %% START FUNCTIONS
  7.  
  8. start() ->
  9.   register(frequency_serv, spawn(frequency, init, [])).
  10.  
  11. init() -> process_flag(trap_exit, true),
  12.   Frequencies = {get_frequencies(), []},
  13.   loop(Frequencies).
  14.  
  15. %% HARDCODED FUNCTIONS
  16.  
  17. get_frequencies() -> [10, 11].
  18.  
  19. %% CLIENT FUNCTIONS
  20.  
  21. stop() -> call(stop).
  22.  
  23. allocate() -> call(allocate).
  24.  
  25. deallocate(Freq) -> call({deallocate, Freq}).
  26.  
  27. is_running() ->
  28.   WhereIs = whereis(frequency_serv),
  29.   if
  30.     WhereIs /= undefined -> running;
  31.     true -> stopped
  32.   end.
  33.  
  34. call(Message) -> frequency_serv ! {request, self(), Message},
  35.   receive
  36.     {reply, Reply} -> Reply
  37.   end.
  38.  
  39. reply(Pid, Message) -> Pid ! {reply, Message}.
  40.  
  41. reply_to_gui(Message, Pid, Freq) ->
  42.   frequency_serv_gui ! {Message, Pid, Freq}.
  43.  
  44. loop(Freq) ->
  45.   receive
  46.     {request, Pid, allocate} -> {NewF, Reply} = allocate(Freq, Pid),
  47.       reply(Pid, Reply), reply_to_gui(allocated, Pid, Reply), loop(NewF);
  48.     {request, Pid, {deallocate, F}} -> {NewF, Reply} = deallocate(Freq, F, Pid),
  49.       reply(Pid, Reply), loop(NewF);
  50.     {'EXIT', Pid, _Reason} -> NewF = exited(Freq, Pid), loop(NewF);
  51.     {request, Pid, stop} -> reply(Pid, ok);
  52.     allocated_frequencies ->
  53.       io:format("~p~n", [Freq]),
  54.       loop(Freq)
  55.   end.
  56.  
  57. allocate({[], Allocated}, _Pid) -> {{[], Allocated}, {error, no_frequencies}};
  58.  
  59. allocate({[Freq|Frequencies], Allocated}, Pid) -> link(Pid),
  60.   {{Frequencies, [{Freq, Pid}|Allocated]}, {ok, Freq}}.
  61.  
  62. deallocate({Free, Allocated}, Freq, Sender_pid) ->
  63.   Object = lists:keysearch(Freq, 1, Allocated),
  64.   case Object of
  65.     {value, {Freq, Pid}} ->
  66.       if
  67.         Pid == Sender_pid ->
  68.           unlink(Pid),
  69.           NewAllocated = lists:keydelete(Freq, 1, Allocated),
  70.           {{[Freq|Free], NewAllocated}, released};
  71.         true ->
  72.           {{Free, Allocated}, wrong_frequency}
  73.       end;
  74.     false ->
  75.        {{Free, Allocated}, frequency_not_found}
  76.   end.
  77.  
  78. exited({Free, Allocated}, Pid) ->
  79.   unregister(frequency_serv),
  80.   case lists:keysearch(Pid, 2, Allocated) of
  81.     {value, {Freq, Pid}} -> NewAllocated = lists:keydelete(Freq, 1, Allocated),
  82.       {[Freq | Free], NewAllocated};
  83.     false -> {Free, Allocated}
  84.   end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement