First of all thank you for looking at my question. Create a server that manages a list of available frequencies(say PROCESS [1,2,3,4,5,6,7,8,9,10]. The client process should be able to request any available frequency and release it. When the client releases the frequency, the server would calculate the amount (5 Rs/Sec) based upon the time the frequency was used by the client and would send the bill to the client.
I really do appreciate your help even if you can’t solve. I have been trying this for 1 whole month and I’m honestly so down and hopeless. Thank you so much.
-module(b).
-compile([export_all, nowarn_export_all]).
run() ->
file:delete("example.dets"),
case dets:info(example, owner) of
undefined ->
dets:open_file(example, [{file, "example.dets"}]);
_ ->
skip
end,
spawn(fun() -> init() end),
timer:sleep(100),
Client = self(),
example_server ! {allocate, Client, aaa, 5},
example_server ! {allocate, Client, bbb, 5},
example_server ! {allocate, Client, aaa, 5},
timer:sleep(1000),
example_server ! {release, Client, aaa, 5},
example_server ! {release, Client, aaa, 5},
timer:sleep(100),
io:format("~p~n", [receive_server_msg([])]),
example_server ! {allocate, Client, aaa, 5},
timer:sleep(100),
exit(whereis(example_server), kill),
spawn(fun() -> init() end),
timer:sleep(100),
example_server ! {allocate, Client, bbb, 5},
example_server ! {allocate, Client, aaa, 5},
timer:sleep(1000),
example_server ! {release, Client, aaa, 5},
example_server ! {release, Client, aaa, 5},
timer:sleep(100),
io:format("~p~n", [receive_server_msg([])]),
example_server ! {stop},
dets:close(example).
receive_server_msg(List) ->
receive
Reply ->
receive_server_msg([Reply|List])
after 0 ->
lists:reverse(List)
end.
init() ->
erlang:register(example_server, self()),
State = other_data_you_need,
manager(State).
manager(State) ->
receive
{stop} -> ok;
{allocate, Requestor, Name, Frequency} ->
case check_allocate_request_and_modify_state(Name, Frequency, State) of
{ok, State1} ->
Requestor ! {grant, Frequency},
manager(State1);
{error, Reason} ->
Requestor ! {error, Reason},
manager(State)
end;
{release, Requestor, Name, Frequency} ->
case check_release_request_and_modify_state(Name, Frequency, State) of
{ok, Cost, State1} ->
Requestor ! {bill, Cost},
manager(State1);
{error, Reason} ->
Requestor ! {error, Reason},
manager(State)
end;
UnknownMsg ->
logger:error("unknown message: ~p", [UnknownMsg]),
manager(State)
end.
check_allocate_request_and_modify_state(Name, Frequency, State) ->
case lists:member(Frequency, [1,2,3,4,5,6,7,8,9,10]) of
true ->
case dets:lookup(example, Frequency) of
[] ->
dets:insert(example, {Frequency, Name, erlang:system_time(second)}),
{ok, State};
[{_, Name, _}] ->
{error, using};
_ ->
{error, other_using}
end;
_ ->
{error, bad_request}
end.
check_release_request_and_modify_state(Name, Frequency, State) ->
case dets:lookup(example, Frequency) of
[{_, Name, StartTime}] ->
dets:delete(example, Frequency),
Cost = (erlang:system_time(second) - StartTime),% * Price,
{ok, Cost, State};
_ ->
{error, not_using}
end.
If possible could you help me correct this and help me with the right format or right answer.