Created
April 9, 2017 04:35
-
-
Save abhishekc-sharma/da527c3cad5e8eadf870458e5aaa31f3 to your computer and use it in GitHub Desktop.
Frequency server that handles timeout and calls clear appropriately
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
%% Based on code from | |
%% Erlang Programming | |
%% Francecso Cesarini and Simon Thompson | |
%% O'Reilly, 2008 | |
%% http://oreilly.com/catalog/9780596518189/ | |
%% http://www.erlangprogramming.org/ | |
%% (c) Francesco Cesarini and Simon Thompson | |
-module(frequency). | |
-export([start/0,allocate/0,deallocate/1,stop/0]). | |
-export([init/0]). | |
%% These are the start functions used to create and | |
%% initialize the server. | |
start() -> | |
register(frequency, | |
spawn(frequency, init, [])). | |
init() -> | |
Frequencies = {get_frequencies(), []}, | |
loop(Frequencies). | |
% Hard Coded | |
get_frequencies() -> [10,11,12,13,14,15]. | |
%% The Main Loop | |
loop(Frequencies) -> | |
timer:sleep(5000), % Artifically induced load on server | |
%% Introduce a new message type called clear that clears all the messages in the mailbox | |
%% Always prioritize the clear message before any other message | |
receive | |
{request, _Pid, clear} -> | |
io:format("Clearing server messages ~n"), | |
clear(), | |
loop(Frequencies) | |
after 0 -> | |
receive | |
{request, Pid, allocate} -> | |
io:format("Got Allocate ~n"), | |
{NewFrequencies, Reply} = allocate(Frequencies, Pid), | |
Pid ! {reply, Reply}, | |
loop(NewFrequencies); | |
{request, Pid , {deallocate, Freq}} -> | |
io:format("Got Deallocate ~n"), | |
NewFrequencies = deallocate(Frequencies, Freq), | |
Pid ! {reply, ok}, | |
loop(NewFrequencies); | |
{request, Pid, stop} -> | |
io:format("Got Stop ~n"), | |
Pid ! {reply, stopped} | |
end | |
end. | |
%% Functional interface | |
%% Every time a timeout occurs, send the clear message so that the | |
%% unrocessed request is removed and server maintains a consistant state | |
allocate() -> | |
frequency ! {request, self(), allocate}, | |
receive | |
{reply, Reply} -> Reply | |
after 2000 -> | |
frequency ! {request, self(), clear}, | |
{timeout} | |
end. | |
deallocate(Freq) -> | |
frequency ! {request, self(), {deallocate, Freq}}, | |
receive | |
{reply, Reply} -> Reply | |
after 2000 -> | |
frequency ! {request, self(), clear}, | |
{timeout} | |
end. | |
stop() -> | |
frequency ! {request, self(), stop}, | |
receive | |
{reply, Reply} -> Reply | |
after 2000 -> | |
frequency ! {request, self(), clear}, | |
{timeout} | |
end. | |
%% Modified version of clear that prints the messages that it removes from the mailbox | |
clear() -> | |
receive | |
Msg -> | |
io:format("Clearing message ~p~n", [Msg]), | |
clear() | |
after 0 -> | |
ok | |
end. | |
%% The Internal Help Functions used to allocate and | |
%% deallocate frequencies. | |
allocate({[], Allocated}, _Pid) -> | |
{{[], Allocated}, {error, no_frequency}}; | |
allocate({[Freq|Free], Allocated}, Pid) -> | |
{{Free, [{Freq, Pid}|Allocated]}, {ok, Freq}}. | |
deallocate({Free, Allocated}, Freq) -> | |
NewAllocated=lists:keydelete(Freq, 1, Allocated), | |
{[Freq|Free], NewAllocated}. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment