gs.erl:
-module(gf).
-behavior(gen_server).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-export([start/0, allocate/0, deallocate/1, stop/0, client/2, test1/0, test2/0]).
-export([cast_report/0, call_report/0, add_frequencies/1]).
%%====== Server Internal Function Replacements ==========
init([]) ->
{ok, {get_frequencies(), []} }.
get_frequencies() -> [10,11,12,13,14,15].
handle_call({add_freqs, NewFreqs}, _Pid, {Free, Taken}=Freqs) ->
AllFreqs = lists:foldr( fun({Freq, _Ref}, Acc) -> [Freq|Acc] end, Free, Taken),
case length(AllFreqs -- NewFreqs) =:= length(AllFreqs) of
true -> {reply, added, {Free++NewFreqs, Taken}};
false -> {reply, {error, duplicates}, Freqs}
end;
handle_call(call_report, _Pid, Freqs) ->
{reply, Freqs, Freqs};
handle_call(allocate, Client, Freqs) ->
{NewFreqs, Response} = allocate(Freqs, Client),
io:format("handle_call(allocate):~n \tFreqs: ~lp~n \tNewFreqs: ~lp~n~n",
[Freqs, NewFreqs]),
{reply, {reply, Response}, NewFreqs}.
handle_cast(cast_report, {Free, Taken}=Freqs) ->
io:format("\nhandle_cast(cast_report):\n===REPORT===~nFree: ~lp~nTaken: ~lp~n",
[Free, Taken]),
io:format("----------~n"),
{noreply, Freqs};
handle_cast({deallocate, Freq}, Freqs) ->
NewFreqs = deallocate(Freqs, Freq),
io:format("handle_cast(deallocate):~n \tFreqs: ~lp~n \tNewFreqs: ~lp~n~n",
[Freqs, NewFreqs]),
{noreply, NewFreqs};
handle_cast(stop, Freqs) ->
{stop, normal, Freqs}.
terminate(Reason, State) ->
io:format("~n~n<====== My Gen Frequency Server Shutdown ======>~n"),
io:format("Reason: ~w~n", [Reason]),
io:format("Ending Freqs:~n \t~lp~n", [State]).
%%Template:
%handle_cast({reset,N}, _State) ->
% {noreply, N}.
handle_info(_Info, State) ->
{noreply, State}.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%% ----- No changes to server helper functions -----
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}.
%%==== Server External Function Replacements ===
start() ->
gen_server:start_link(
{local, ?MODULE},
?MODULE, [], []
).
allocate() ->
{reply, Response} = gen_server:call(?MODULE, allocate),
io:format("client: allocate(): Response: ~w~n", [Response]),
Response.
deallocate(Freq) ->
gen_server:cast(?MODULE, {deallocate, Freq}),
io:format("client: deallocate(): async deallocate(~w) request sent to gen_server~n",
[Freq]).
cast_report() ->
gen_server:cast(?MODULE, cast_report).
call_report() ->
{Free, Taken} = gen_server:call(?MODULE, call_report),
io:format("call_report():\n===REPORT===~nFree: ~w~nTaken: ~w~n",
[Free, Taken]),
io:format("----------~n").
add_frequencies(NewFreqs) ->
gen_server:call(?MODULE, {add_freqs, NewFreqs}).
stop() ->
gen_server:cast(?MODULE, stop),
io:format("stop(): ansync stop request sent to gen_server~n").
%%===== TESTS =======
test2() ->
start(),
_Client1 = spawn(?MODULE, client, [1, 1000]),
_Client2 = spawn(?MODULE, client, [2, 2000]),
_Client3 = spawn(?MODULE, client, [3, 3000]),
timer:sleep(3000), %%Let clients go at it for awhile
call_report(), %%Get report.
timer:sleep(2000), %%Let clients go at it for ahwile
cast_report(), %%Get report.
timer:sleep(2000), %%Let clients go at it for awhile
AddFreqs1 = [20,21,22],
Response1 = add_frequencies(AddFreqs1),
io:format("~n****test2: add_freqs(~w): ~w~n", [AddFreqs1, Response1]),
call_report(),
AddFreqs2 = [15,16,17], %%Should fail.
Response2 = add_frequencies(AddFreqs2),
io:format("~n****test2: add_freqs(~w): ~w~n", [AddFreqs2, Response2]),
call_report(),
AddFreqs3 = [18,19,20], %%Should also fail.
Response3 = add_frequencies(AddFreqs2),
io:format("~n****test2: add_freqs(~w): ~w~n", [AddFreqs3, Response3]),
call_report(),
timer:sleep(2000), %%Let clients go at it for awhile.
stop().
test1() ->
start(),
allocate_loop(8),
AddFreqs = [1,2,3,4,5],
Response = add_frequencies(AddFreqs),
io:format("~n****test2: add_freqs(~w): ~w~n", [AddFreqs, Response]),
call_report(),
allocate_loop(8),
stop().
allocate_loop(0) ->
ok;
allocate_loop(N) ->
allocate(),
allocate_loop(N-1).
%%======= CLIENT =========
client(Id, Sleep) ->
handle_allocate_response(allocate(), Sleep),
client(Id, Sleep).
handle_allocate_response({error, no_frequency}, _) ->
timer:sleep(500); %Sleep for a short time before trying again.
handle_allocate_response({ok,Freq}, Sleep) ->
timer:sleep(Sleep), %%Call duration
deallocate(Freq).
In the shell:
$ ./run.sh
Erlang/OTP 19 [erts-8.2] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
handle_call(allocate):
Freqs: {[10,11,12,13,14,15],[]}
NewFreqs: {[11,12,13,14,15],[{10,{<0.58.0>,#Ref<0.0.4.74>}}]}
handle_call(allocate):
Freqs: {[11,12,13,14,15],[{10,{<0.58.0>,#Ref<0.0.4.74>}}]}
NewFreqs: {[12,13,14,15],
[{11,{<0.59.0>,#Ref<0.0.4.75>}},
{10,{<0.58.0>,#Ref<0.0.4.74>}}]}
client: allocate(): Response: {ok,10}
handle_call(allocate):
Freqs: {[12,13,14,15],
[{11,{<0.59.0>,#Ref<0.0.4.75>}},
{10,{<0.58.0>,#Ref<0.0.4.74>}}]}
NewFreqs: {[13,14,15],
[{12,{<0.60.0>,#Ref<0.0.1.52>}},
{11,{<0.59.0>,#Ref<0.0.4.75>}},
{10,{<0.58.0>,#Ref<0.0.4.74>}}]}
client: allocate(): Response: {ok,12}
client: allocate(): Response: {ok,11}
Eshell V8.2 (abort with ^G)
1> client: deallocate(): async deallocate(10) request sent to gen_server
handle_cast(deallocate):
Freqs: {[13,14,15],
[{12,{<0.60.0>,#Ref<0.0.1.52>}},
{11,{<0.59.0>,#Ref<0.0.4.75>}},
{10,{<0.58.0>,#Ref<0.0.4.74>}}]}
NewFreqs: {[10,13,14,15],
[{12,{<0.60.0>,#Ref<0.0.1.52>}},
{11,{<0.59.0>,#Ref<0.0.4.75>}}]}
handle_call(allocate):
Freqs: {[10,13,14,15],
[{12,{<0.60.0>,#Ref<0.0.1.52>}},
{11,{<0.59.0>,#Ref<0.0.4.75>}}]}
NewFreqs: {[13,14,15],
[{10,{<0.58.0>,#Ref<0.0.1.61>}},
{12,{<0.60.0>,#Ref<0.0.1.52>}},
{11,{<0.59.0>,#Ref<0.0.4.75>}}]}
client: allocate(): Response: {ok,10}
client: deallocate(): async deallocate(11) request sent to gen_server
handle_cast(deallocate):
Freqs: {[13,14,15],
[{10,{<0.58.0>,#Ref<0.0.1.61>}},
{12,{<0.60.0>,#Ref<0.0.1.52>}},
{11,{<0.59.0>,#Ref<0.0.4.75>}}]}
NewFreqs: {[11,13,14,15],
[{10,{<0.58.0>,#Ref<0.0.1.61>}},
{12,{<0.60.0>,#Ref<0.0.1.52>}}]}
handle_call(allocate):
Freqs: {[11,13,14,15],
[{10,{<0.58.0>,#Ref<0.0.1.61>}},
{12,{<0.60.0>,#Ref<0.0.1.52>}}]}
NewFreqs: {[13,14,15],
[{11,{<0.59.0>,#Ref<0.0.1.66>}},
{10,{<0.58.0>,#Ref<0.0.1.61>}},
{12,{<0.60.0>,#Ref<0.0.1.52>}}]}
client: allocate(): Response: {ok,11}
client: deallocate(): async deallocate(10) request sent to gen_server
handle_cast(deallocate):
Freqs: {[13,14,15],
[{11,{<0.59.0>,#Ref<0.0.1.66>}},
{10,{<0.58.0>,#Ref<0.0.1.61>}},
{12,{<0.60.0>,#Ref<0.0.1.52>}}]}
NewFreqs: {[10,13,14,15],
[{11,{<0.59.0>,#Ref<0.0.1.66>}},
{12,{<0.60.0>,#Ref<0.0.1.52>}}]}
handle_call(allocate):
Freqs: {[10,13,14,15],
[{11,{<0.59.0>,#Ref<0.0.1.66>}},
{12,{<0.60.0>,#Ref<0.0.1.52>}}]}
NewFreqs: {[13,14,15],
[{10,{<0.58.0>,#Ref<0.0.1.71>}},
{11,{<0.59.0>,#Ref<0.0.1.66>}},
{12,{<0.60.0>,#Ref<0.0.1.52>}}]}
client: allocate(): Response: {ok,10}
call_report():
===REPORT===
Free: [13,14,15]
Taken: [{10,{<0.58.0>,#Ref<0.0.1.71>}},{11,{<0.59.0>,#Ref<0.0.1.66>}},{12,{<0.60.0>,#Ref<0.0.1.52>}}]
----------
client: deallocate(): async deallocate(12) request sent to gen_server
handle_cast(deallocate):
Freqs: {[13,14,15],
[{10,{<0.58.0>,#Ref<0.0.1.71>}},
{11,{<0.59.0>,#Ref<0.0.1.66>}},
{12,{<0.60.0>,#Ref<0.0.1.52>}}]}
NewFreqs: {[12,13,14,15],
[{10,{<0.58.0>,#Ref<0.0.1.71>}},
{11,{<0.59.0>,#Ref<0.0.1.66>}}]}
handle_call(allocate):
Freqs: {[12,13,14,15],
[{10,{<0.58.0>,#Ref<0.0.1.71>}},
{11,{<0.59.0>,#Ref<0.0.1.66>}}]}
NewFreqs: {[13,14,15],
[{12,{<0.60.0>,#Ref<0.0.4.84>}},
{10,{<0.58.0>,#Ref<0.0.1.71>}},
{11,{<0.59.0>,#Ref<0.0.1.66>}}]}
client: allocate(): Response: {ok,12}
client: deallocate(): async deallocate(10) request sent to gen_server
handle_cast(deallocate):
Freqs: {[13,14,15],
[{12,{<0.60.0>,#Ref<0.0.4.84>}},
{10,{<0.58.0>,#Ref<0.0.1.71>}},
{11,{<0.59.0>,#Ref<0.0.1.66>}}]}
NewFreqs: {[10,13,14,15],
[{12,{<0.60.0>,#Ref<0.0.4.84>}},
{11,{<0.59.0>,#Ref<0.0.1.66>}}]}
handle_call(allocate):
Freqs: {[10,13,14,15],
[{12,{<0.60.0>,#Ref<0.0.4.84>}},
{11,{<0.59.0>,#Ref<0.0.1.66>}}]}
NewFreqs: {[13,14,15],
[{10,{<0.58.0>,#Ref<0.0.1.81>}},
{12,{<0.60.0>,#Ref<0.0.4.84>}},
{11,{<0.59.0>,#Ref<0.0.1.66>}}]}
client: allocate(): Response: {ok,10}
client: deallocate(): async deallocate(11) request sent to gen_server
handle_cast(deallocate):
Freqs: {[13,14,15],
[{10,{<0.58.0>,#Ref<0.0.1.81>}},
{12,{<0.60.0>,#Ref<0.0.4.84>}},
{11,{<0.59.0>,#Ref<0.0.1.66>}}]}
NewFreqs: {[11,13,14,15],
[{10,{<0.58.0>,#Ref<0.0.1.81>}},
{12,{<0.60.0>,#Ref<0.0.4.84>}}]}
handle_call(allocate):
Freqs: {[11,13,14,15],
[{10,{<0.58.0>,#Ref<0.0.1.81>}},
{12,{<0.60.0>,#Ref<0.0.4.84>}}]}
NewFreqs: {[13,14,15],
[{11,{<0.59.0>,#Ref<0.0.1.86>}},
{10,{<0.58.0>,#Ref<0.0.1.81>}},
{12,{<0.60.0>,#Ref<0.0.4.84>}}]}
client: allocate(): Response: {ok,11}
client: deallocate(): async deallocate(10) request sent to gen_server
handle_cast(deallocate):
Freqs: {[13,14,15],
[{11,{<0.59.0>,#Ref<0.0.1.86>}},
{10,{<0.58.0>,#Ref<0.0.1.81>}},
{12,{<0.60.0>,#Ref<0.0.4.84>}}]}
NewFreqs: {[10,13,14,15],
[{11,{<0.59.0>,#Ref<0.0.1.86>}},
{12,{<0.60.0>,#Ref<0.0.4.84>}}]}
handle_call(allocate):
Freqs: {[10,13,14,15],
[{11,{<0.59.0>,#Ref<0.0.1.86>}},
{12,{<0.60.0>,#Ref<0.0.4.84>}}]}
NewFreqs: {[13,14,15],
[{10,{<0.58.0>,#Ref<0.0.1.91>}},
{11,{<0.59.0>,#Ref<0.0.1.86>}},
{12,{<0.60.0>,#Ref<0.0.4.84>}}]}
client: allocate(): Response: {ok,10}
handle_cast(cast_report):
===REPORT===
Free: [13,14,15]
Taken: [{10,{<0.58.0>,#Ref<0.0.1.91>}},
{11,{<0.59.0>,#Ref<0.0.1.86>}},
{12,{<0.60.0>,#Ref<0.0.4.84>}}]
----------
client: deallocate(): async deallocate(10) request sent to gen_server
handle_cast(deallocate):
Freqs: {[13,14,15],
[{10,{<0.58.0>,#Ref<0.0.1.91>}},
{11,{<0.59.0>,#Ref<0.0.1.86>}},
{12,{<0.60.0>,#Ref<0.0.4.84>}}]}
NewFreqs: {[10,13,14,15],
[{11,{<0.59.0>,#Ref<0.0.1.86>}},
{12,{<0.60.0>,#Ref<0.0.4.84>}}]}
handle_call(allocate):
Freqs: {[10,13,14,15],
[{11,{<0.59.0>,#Ref<0.0.1.86>}},
{12,{<0.60.0>,#Ref<0.0.4.84>}}]}
NewFreqs: {[13,14,15],
[{10,{<0.58.0>,#Ref<0.0.1.98>}},
{11,{<0.59.0>,#Ref<0.0.1.86>}},
{12,{<0.60.0>,#Ref<0.0.4.84>}}]}
client: allocate(): Response: {ok,10}
client: deallocate(): async deallocate(12) request sent to gen_server
handle_cast(deallocate):
Freqs: {[13,14,15],
[{10,{<0.58.0>,#Ref<0.0.1.98>}},
{11,{<0.59.0>,#Ref<0.0.1.86>}},
{12,{<0.60.0>,#Ref<0.0.4.84>}}]}
NewFreqs: {[12,13,14,15],
[{10,{<0.58.0>,#Ref<0.0.1.98>}},
{11,{<0.59.0>,#Ref<0.0.1.86>}}]}
handle_call(allocate):
Freqs: {[12,13,14,15],
[{10,{<0.58.0>,#Ref<0.0.1.98>}},
{11,{<0.59.0>,#Ref<0.0.1.86>}}]}
NewFreqs: {[13,14,15],
[{12,{<0.60.0>,#Ref<0.0.1.103>}},
{10,{<0.58.0>,#Ref<0.0.1.98>}},
{11,{<0.59.0>,#Ref<0.0.1.86>}}]}
client: allocate(): Response: {ok,12}
client: deallocate(): async deallocate(11) request sent to gen_server
handle_cast(deallocate):
Freqs: {[13,14,15],
[{12,{<0.60.0>,#Ref<0.0.1.103>}},
{10,{<0.58.0>,#Ref<0.0.1.98>}},
{11,{<0.59.0>,#Ref<0.0.1.86>}}]}
NewFreqs: {[11,13,14,15],
[{12,{<0.60.0>,#Ref<0.0.1.103>}},
{10,{<0.58.0>,#Ref<0.0.1.98>}}]}
handle_call(allocate):
Freqs: {[11,13,14,15],
[{12,{<0.60.0>,#Ref<0.0.1.103>}},
{10,{<0.58.0>,#Ref<0.0.1.98>}}]}
NewFreqs: {[13,14,15],
[{11,{<0.59.0>,#Ref<0.0.1.108>}},
{12,{<0.60.0>,#Ref<0.0.1.103>}},
{10,{<0.58.0>,#Ref<0.0.1.98>}}]}
client: allocate(): Response: {ok,11}
client: deallocate(): async deallocate(10) request sent to gen_server
handle_cast(deallocate):
Freqs: {[13,14,15],
[{11,{<0.59.0>,#Ref<0.0.1.108>}},
{12,{<0.60.0>,#Ref<0.0.1.103>}},
{10,{<0.58.0>,#Ref<0.0.1.98>}}]}
NewFreqs: {[10,13,14,15],
[{11,{<0.59.0>,#Ref<0.0.1.108>}},
{12,{<0.60.0>,#Ref<0.0.1.103>}}]}
handle_call(allocate):
Freqs: {[10,13,14,15],
[{11,{<0.59.0>,#Ref<0.0.1.108>}},
{12,{<0.60.0>,#Ref<0.0.1.103>}}]}
NewFreqs: {[13,14,15],
[{10,{<0.58.0>,#Ref<0.0.1.113>}},
{11,{<0.59.0>,#Ref<0.0.1.108>}},
{12,{<0.60.0>,#Ref<0.0.1.103>}}]}
client: allocate(): Response: {ok,10}
****test2: add_freqs([20,21,22]): added
call_report():
===REPORT===
Free: [13,14,15,20,21,22]
Taken: [{10,{<0.58.0>,#Ref<0.0.1.113>}},{11,{<0.59.0>,#Ref<0.0.1.108>}},{12,{<0.60.0>,#Ref<0.0.1.103>}}]
----------
****test2: add_freqs([15,16,17]): {error,duplicates}
call_report():
===REPORT===
Free: [13,14,15,20,21,22]
Taken: [{10,{<0.58.0>,#Ref<0.0.1.113>}},{11,{<0.59.0>,#Ref<0.0.1.108>}},{12,{<0.60.0>,#Ref<0.0.1.103>}}]
----------
****test2: add_freqs([18,19,20]): {error,duplicates}
call_report():
===REPORT===
Free: [13,14,15,20,21,22]
Taken: [{10,{<0.58.0>,#Ref<0.0.1.113>}},{11,{<0.59.0>,#Ref<0.0.1.108>}},{12,{<0.60.0>,#Ref<0.0.1.103>}}]
----------
client: deallocate(): async deallocate(10) request sent to gen_server
handle_cast(deallocate):
Freqs: {[13,14,15,20,21,22],
[{10,{<0.58.0>,#Ref<0.0.1.113>}},
{11,{<0.59.0>,#Ref<0.0.1.108>}},
{12,{<0.60.0>,#Ref<0.0.1.103>}}]}
NewFreqs: {[10,13,14,15,20,21,22],
[{11,{<0.59.0>,#Ref<0.0.1.108>}},
{12,{<0.60.0>,#Ref<0.0.1.103>}}]}
handle_call(allocate):
Freqs: {[10,13,14,15,20,21,22],
[{11,{<0.59.0>,#Ref<0.0.1.108>}},
{12,{<0.60.0>,#Ref<0.0.1.103>}}]}
NewFreqs: {[13,14,15,20,21,22],
[{10,{<0.58.0>,#Ref<0.0.1.133>}},
{11,{<0.59.0>,#Ref<0.0.1.108>}},
{12,{<0.60.0>,#Ref<0.0.1.103>}}]}
client: allocate(): Response: {ok,10}
client: deallocate(): async deallocate(11) request sent to gen_server
handle_cast(deallocate):
Freqs: {[13,14,15,20,21,22],
[{10,{<0.58.0>,#Ref<0.0.1.133>}},
{11,{<0.59.0>,#Ref<0.0.1.108>}},
{12,{<0.60.0>,#Ref<0.0.1.103>}}]}
NewFreqs: {[11,13,14,15,20,21,22],
[{10,{<0.58.0>,#Ref<0.0.1.133>}},
{12,{<0.60.0>,#Ref<0.0.1.103>}}]}
handle_call(allocate):
Freqs: {[11,13,14,15,20,21,22],
[{10,{<0.58.0>,#Ref<0.0.1.133>}},
{12,{<0.60.0>,#Ref<0.0.1.103>}}]}
NewFreqs: {[13,14,15,20,21,22],
[{11,{<0.59.0>,#Ref<0.0.1.138>}},
{10,{<0.58.0>,#Ref<0.0.1.133>}},
{12,{<0.60.0>,#Ref<0.0.1.103>}}]}
client: allocate(): Response: {ok,11}
client: deallocate(): async deallocate(10) request sent to gen_server
handle_cast(deallocate):
Freqs: {[13,14,15,20,21,22],
[{11,{<0.59.0>,#Ref<0.0.1.138>}},
{10,{<0.58.0>,#Ref<0.0.1.133>}},
{12,{<0.60.0>,#Ref<0.0.1.103>}}]}
NewFreqs: {[10,13,14,15,20,21,22],
[{11,{<0.59.0>,#Ref<0.0.1.138>}},
{12,{<0.60.0>,#Ref<0.0.1.103>}}]}
handle_call(allocate):
Freqs: {[10,13,14,15,20,21,22],
[{11,{<0.59.0>,#Ref<0.0.1.138>}},
{12,{<0.60.0>,#Ref<0.0.1.103>}}]}
NewFreqs: {[13,14,15,20,21,22],
[{10,{<0.58.0>,#Ref<0.0.1.143>}},
{11,{<0.59.0>,#Ref<0.0.1.138>}},
{12,{<0.60.0>,#Ref<0.0.1.103>}}]}
client: allocate(): Response: {ok,10}
stop(): ansync stop request sent to gen_server
<====== My Gen Frequency Server Shutdown ======>
Reason: normal
Ending Freqs:
{[13,14,15,20,21,22],
[{10,{<0.58.0>,#Ref<0.0.1.143>}},
{11,{<0.59.0>,#Ref<0.0.1.138>}},
{12,{<0.60.0>,#Ref<0.0.1.103>}}]}
client: deallocate(): async deallocate(12) request sent to gen_server
client: deallocate(): async deallocate(10) request sent to gen_server
client: deallocate(): async deallocate(11) request sent to gen_server