-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
140 lines (119 loc) · 5.38 KB
/
main.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# importing libraries
import tkinter as tk
import random
import time
import threading
class Server:
def __init__(self,serverId,capacity) -> None:
self.serverId = serverId
self.capacity = capacity
self.currLoad = 0
# Generate a random color for the server
self.color = "#{:06x}".format(random.randint(0, 0xFFFFFF))
class Client:
def __init__(self,clientId) -> None:
self.clientId = clientId
class LoadBalancer:
pass
class LoadBalancerVisualizer:
def __init__(self,master,serverQuantity,servers,geometry) -> None:
self.master = master
self.servers = servers
self.clients = []
self.clientsItems = {}
# print(geometry)
self.frame = self.master
# self.frame = tk.Frame(self.master)
# self.frame.grid(row=0,column=0)
# self.frame.config(width=geometry[0],height=geometry[1])
# self.canvas = tk.Canvas(master,width=geometry[0],height=geometry[1])
self.canvas = tk.Canvas(self.frame)
self.scrollbar = tk.Scrollbar(self.frame,orient='horizontal',command=self.canvas.xview)
self.genReqButton = tk.Button(self.frame,text="GENERATE REQ.", command=self.genReq)
self.genReqButton.grid(row=2,column=0)
self.canvas.config(xscrollcommand=self.scrollbar.set)
# self.canvas.grid(row=0,column=0,columnspan=serverQuantity,padx=10,pady=10)
self.canvas.grid(row=0,column=0,padx=10,pady=10,sticky='nsew')
self.scrollbar.grid(row=1,column=0,sticky='ew')
# Bind canvas to frame
self.frame.grid_rowconfigure(0, weight=1)
self.frame.grid_columnconfigure(0, weight=1)
self.canvas.bind("<Configure>", self.onCanvasConfigure)
self.drawServers()
# master.bind("<Configure>", self.onWindowResize)
# def onWindowResize(self, event):
# Adjust canvas size only if the user resizes the Tkinter window
# if event.widget == self.master:
# self.canvas.config(width=event.width)
def onCanvasConfigure(self, event):
self.canvas.configure(scrollregion=self.canvas.bbox("all"))
def drawServers(self):
for i,server in enumerate(self.servers):
x = 100 + i*200
y = 100
serverText = f'{server.serverId}\nLoad: {server.currLoad}/{server.capacity}'
server_rect = self.canvas.create_rectangle(x-40,y-40,x+40,y+40,fill='blue')
server_text = self.canvas.create_text(x,y,text=serverText,justify=tk.CENTER,fill='white',tags=server.serverId)
self.canvas.itemconfigure(server_text,tags=(server.serverId,))
def handleRequest(self,client,server):
processingTime = random.randint(1,7)
# processingTime = 7
time.sleep(processingTime)
output = f'Output for client {client.clientId} from server {server.serverId}'
print(output)
server.currLoad -= 1
self.canvas.itemconfigure(server.serverId,text=f'{server.serverId}\nLoad: {server.currLoad}/{server.capacity}')
# self.canvas.delete(self.clientsItems[client])
# del self.clientsItems[client]
clientItems = self.clientsItems.pop(client, None)
if clientItems:
for item in clientItems:
self.canvas.delete(item)
def genReq(self):
clientId = len(self.clients)+1
newClient = Client(clientId=clientId)
self.clients.append(newClient)
# Choose a server with available capacity
available_servers = [server for server in self.servers if server.currLoad < server.capacity]
if not available_servers:
print("All servers are fully loaded")
return
server = random.choice(available_servers)
# Update server load
server.currLoad += 1
# Draw line from client to server
client_x = random.randint(50, self.canvas.winfo_width()-50)
client_y = random.randint(200, self.canvas.winfo_height()-50)
server_x = 100 + self.servers.index(server) * 200
server_y = 100
# color = "#{:06x}".format(random.randint(0, 0xFFFFFF)) # Random color for the line
line = self.canvas.create_line(client_x, client_y, server_x, server_y, fill=server.color,width=3)
# Draw client circle with its ID
clientCircle = self.canvas.create_oval(client_x-20, client_y-20, client_x+20, client_y+20, fill='green')
clientText = self.canvas.create_text(client_x, client_y, text=str(clientId), fill='white')
self.clientsItems[newClient] = (clientCircle, clientText, line)
# Update server load text
serverText = f'{server.serverId}\nLoad: {server.currLoad}/{server.capacity}'
print(serverText)
print()
self.canvas.itemconfigure(server.serverId, text=serverText)
# print(self.canvas.itemconfigure(server.serverId, text="text"))
thread = threading.Thread(target=self.handleRequest, args=(newClient,server))
thread.start()
def main():
root = tk.Tk()
root.title("Load Balancer Visualizer")
width = 600
height = 500
serverFarm = [
Server("S1",1),
Server("S2",2),
Server("S3",3),
# Server("S4",2),
# Server("S5",2),
]
root.geometry(f'{width}x{height}')
lbv = LoadBalancerVisualizer(root,serverQuantity=len(serverFarm),servers=serverFarm,geometry=(width,height))
root.mainloop()
if __name__=="__main__":
main()