-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.erl
More file actions
39 lines (30 loc) · 1.72 KB
/
server.erl
File metadata and controls
39 lines (30 loc) · 1.72 KB
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
-module(server).
-import(string,[str/2]).
%exports method which starts the server.
-export([server/0]).
%method which initialises all values sent to the server and then runs an infinite loop updating the lists based on message values.
server() ->
global:register_name("Server", self()), %registers itself on the distributed registry.
Customers = [],
Auctions = [],
server(Customers,Auctions).
server(Customers, Auctions)->
receive
Val -> Val
end,
{Type,User,MessageID,Interest} = Val, %splits up the message.
if
User =:= "Customer" andalso Type =/= -1 -> Customers2 = [{MessageID}|Customers], lists:foreach(fun(N)->updateAuctions(MessageID,N)end,Auctions), server(Customers2, Auctions); %adds a customer to the list and sends each auction a message telling them this.
User =:= "Auction" andalso Type =/= -1 -> Auctions2 = [{MessageID,Interest}|Auctions], lists:foreach(fun(N)->updateCustomers({MessageID,Interest},N)end,Customers), server(Customers, Auctions2); %adds an auction to the list and sends each customer a message saying this.
User =:= "Customer" andalso Type =:= -1 -> Customers2 = Customers -- [{MessageID}], server(Customers2, Auctions); %removes customer from list.
User =:= "Auction" andalso Type =:= -1 -> Auctions2 = Auctions -- [{MessageID,Interest}], server(Customers, Auctions2) %removes auction from the list.
end.
%updates new customer on what auctions are available.
updateCustomers(Auction,Customer)->
{CustomerID} = Customer,
{AuctionID,Interest} = Auction,
global:send(CustomerID, {1,-1,AuctionID,Interest,-1}).
%updates customer of new auction.
updateAuctions(CustomerID,Auction)->
{AuctionID,Interest} = Auction,
global:send(CustomerID, {1,-1,AuctionID,Interest,-1}).