diff --git a/LoadBalancer/ResponseProcessorLB.py b/LoadBalancer/ResponseProcessorLB.py index fc98afb..4c77a82 100644 --- a/LoadBalancer/ResponseProcessorLB.py +++ b/LoadBalancer/ResponseProcessorLB.py @@ -4,6 +4,7 @@ serverInstancePath = "..\ServerInstance\\" serverPath = "servers.txt" +clientsPerServer = 5 #should be set to atleast 2 def sendData(data, module): data = data.encode() @@ -27,15 +28,20 @@ def commandRouter(self, dataEnc, module): def stateStatus(self, dataDec, module): if dataDec.upper() == "STYPE": - StorageLB.setServerConnected(StorageLB.getServerConnected(serverPath) + 1, serverPath) + print("NEXT SERVER: ", self.server) - if StorageLB.getServerConnected(serverPath) == 6: - StorageLB.setServerPort(StorageLB.getServerPort(serverPath) + 1, serverPath) - StorageLB.setServerConnected(1, serverPath) - if StorageLB.getServerConnected(serverPath) == 1 or StorageLB.getServerConnected(serverPath) == 6: + if StorageLB.getServerConnected(serverPath) == 0: StorageLB.createCMD(StorageLB.getServerIP(serverPath), StorageLB.getServerPort(serverPath), serverInstancePath) StorageLB.runCMD(serverInstancePath) + # if last server is almost full, create a new one, so that it'll be read by the time the next client connects + if StorageLB.getServerConnected(serverPath) == clientsPerServer -1: + StorageLB.createCMD(StorageLB.getServerIP(serverPath), StorageLB.getServerPort(serverPath) + 1, serverInstancePath) + StorageLB.runCMD(serverInstancePath) + if StorageLB.getServerConnected(serverPath) == clientsPerServer: + StorageLB.setServerPort(StorageLB.getServerPort(serverPath) + 1, serverPath) + StorageLB.setServerConnected(0, serverPath) time.sleep(1) + StorageLB.setServerConnected(StorageLB.getServerConnected(serverPath) + 1, serverPath) sendData("LB " + StorageLB.getServerIP(serverPath) + " " + str(StorageLB.getServerPort(serverPath)), module) else: sendData("Commands: STYPE", module)