-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.erl
executable file
·43 lines (41 loc) · 1.66 KB
/
agent.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
-module(agent).
-define(timeout, 60000).
-export([start/0,process_requests/3, stop/0]).
start()->
register(agent, spawn(fun()->process_requests(random:uniform(1000),[],0) end)).
stop()->
unregister(agent).
process_requests( Bid, Clients, NumBids)->
receive
{client_join_req, From } ->
UpdatedClients = [ From | Clients],
io:format("[Agent] New client joined auction ~w ~n",[From]),
process_requests( Bid, UpdatedClients, NumBids);
{client_leave_req, From } ->
UpdatedClients = lists:delete(From,Clients),
process_requests(Bid, UpdatedClients, NumBids);
{bid, Name, Value}->
io:format("Bid,recvd"),
if
Value > Bid->
broadcast(Clients, {message, Value, Name}), %sending all the clients the proposed value by Name, which wins up to now
io:format("[Agent] New Bid Value ~s by ~s ~n", [Value, Name]),
process_requests( Value, Clients, 1);
true->
process_requests( Bid, Clients , 1)
end
after ?timeout->
if
NumBids > 0 ->
broadcast(Clients, {win, Bid}),
io:format("[Agent] Auction over: Won with value ~s ",[Bid]);
true->
NewBid = (Bid /2),
io:format("[Agent] A new auction is started with value ~s ~n",[NewBid]),
broadcast( Clients, {newbid, NewBid}),
process_requests(NewBid, Clients ,0)
end
end.
broadcast(PeerList, Message)->
SMTP = fun(Peer) -> Peer ! Message end,
lists:map(SMTP, PeerList).