-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrontEnd.py
More file actions
64 lines (49 loc) · 2.31 KB
/
FrontEnd.py
File metadata and controls
64 lines (49 loc) · 2.31 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import Pyro4
serverList = []
print("POPULATING SERVER LIST")
with Pyro4.locateNS() as ns:#initialises list of servers using name server
servers = ns.list(prefix = "SERVER")
for server in servers.keys():
serverList.append(Pyro4.Proxy(servers.get(server)))
print("SERVERS FOUND AND CONNECTED")
@Pyro4.expose
class FrontEnd:
#initialising timestamp and update ID
ts = [0,0,0]
uID = 0
#this function takes a user query action and attempts to execute it on one of the servers
def userQuery(self,action,movie,userID):
for server in serverList:#attempts each server in the list to see if one is online
if server.getStatus() == "online":
return server.query(FrontEnd.ts,action,movie,userID)
#if all servers are offline or overloaded it tells the client
return "All servers are currently busy, please try again later"
#this function takes a user update and attempts to send it to a server
def userUpdate(self,action,movie,userID,rating):
for server in serverList:
if server.getStatus() == "online":
#sends update to online server and gets a timestamp back
newTS = server.update([action,movie,userID,rating],FrontEnd.ts,FrontEnd.uID)
if newTS != 1:#if the server already did the update then don't worry
FrontEnd.uID += 1
FrontEnd.ts = FrontEnd.merge(FrontEnd.ts,newTS)
return "Your update has been sent"
return "All servers are currently busy, please try again later"
def merge(ts1,ts2):#merges two timestamps
outputTS = []
for i in range(len(ts1)):
outputTS.append(max(ts1[i],ts2[i]))
return outputTS
def changeStatus(self,serverNum):#changes server status
return serverList[serverNum].changeStatus()
#creating frontend server and adding it to the name server
print("REGISTERING FRONT END SERVER")
daemon = Pyro4.Daemon()
uri = daemon.register(FrontEnd)
#making sure all servers know eachother before being ready for client connection
for server in serverList:
server.getOtherServers()
with Pyro4.locateNS() as ns:
ns.register("FRONTEND",uri)
print("CLIENT CAN NOW CONNECT")
daemon.requestLoop()