public SubServer(int _serverNo) { serverNo = _serverNo; Console.WriteLine("SubServer " + serverNo + " started."); ServerLoads.sAmount[serverNo - 1] = 0; while (true) { Thread.Sleep(300); //sleep 300ms //Generate random(1-50) number in every 500ms. RandomGenerator rand1 = new RandomGenerator(); if (ServerLoads.sAmount[serverNo - 1] < capacity) { if (ServerLoads.sAmount[serverNo - 1] > 0) { double number1 = rand1.number; ServerLoads.sAmount[serverNo - 1] = ServerLoads.sAmount[serverNo - 1] - number1; } if (ServerLoads.sAmount[serverNo - 1] < 0) { ServerLoads.sAmount[serverNo - 1] = 0; } } Thread.Sleep(200); //sleep 300+200=500ms //Take random(1-50) number in every 500ms from mainServer. RandomGenerator rand2 = new RandomGenerator(); if (ServerLoads.mainAmount > 0) { RequestTaker requestTaker = new RequestTaker(rand2.number, serverNo); ServerLoads.sAmount[serverNo - 1] = ServerLoads.sAmount[serverNo - 1] + rand2.number; } } }
double capacity = 500; //I set to 500 instead of 1000 public SubServer(int _serverNo) { serverNo = _serverNo; Console.WriteLine("SubServer " + serverNo + " started."); ServerLoads.sAmount[serverNo - 1] = -1; while (true) { Thread.Sleep(300); //sleep 300ms //Generate random(1-50) number in every 500ms. RandomGenerator rand1 = new RandomGenerator(); if (ServerLoads.sAmount[serverNo - 1] < capacity) { if (ServerLoads.sAmount[serverNo - 1] > 0) { double number1 = rand1.number; ServerLoads.sAmount[serverNo - 1] = ServerLoads.sAmount[serverNo - 1] - number1; //server random amount of load } if (ServerLoads.sAmount[serverNo - 1] < 0) { ServerLoads.sAmount[serverNo - 1] = 0; //servers cannot take negative load } } Thread.Sleep(200); //sleep 300+200=500ms //Take random(1-50) number in every 500ms from mainServer. RandomGenerator rand2 = new RandomGenerator(); if (ServerLoads.mainAmount > 0) { RequestTaker requestTaker = new RequestTaker(rand2.number, serverNo); //takes randoom load from main server ServerLoads.sAmount[serverNo - 1] = ServerLoads.sAmount[serverNo - 1] + rand2.number; //then adds that random load to subserver } } }
public void Servers() { RequestTaker requestTaker = new RequestTaker(0, 0); //server1 takes 0 load from main server (we called this for to start thread) MainServer mainServer = new MainServer(); //starts main server constructor }
public void Servers() { RequestTaker requestTaker = new RequestTaker(0, 0); MainServer mainServer = new MainServer(); //starts main server constructor }
public void SubServerMethod(int _serverNo, int _otherServerNo) { serverNo = (_serverNo); ServerLoads.sAmount[serverNo - 1] = Math.Ceiling(ServerLoads.sAmount[_otherServerNo - 1] / 2); //half of the load goes to this subserver Console.WriteLine("SubServer " + serverNo + " started with " + ServerLoads.sAmount[serverNo - 1]); ServerLoads.sAmount[_otherServerNo - 1] = Math.Floor(ServerLoads.sAmount[_otherServerNo - 1] / 2); //another half stays on other subserver Console.WriteLine("SubServer " + _otherServerNo + " = " + ServerLoads.sAmount[_otherServerNo - 1]); while (SubServerIsRunning.isRunning[serverNo - 1]) //subserver takes random load periodicaly from main server and responds them randomly { Thread.Sleep(300); //sleep 300ms //Generate random(1-50) number in every 500ms. RandomGenerator rand1 = new RandomGenerator(); if (ServerLoads.sAmount[serverNo - 1] < capacity) { if (ServerLoads.sAmount[serverNo - 1] > 0) { double number1 = rand1.number; if (number1 >= ServerLoads.sAmount[serverNo - 1]) { ServerLoads.sAmount[serverNo - 1] = ServerLoads.sAmount[serverNo - 1] - number1; //server random amount of load } else { ServerLoads.sAmount[serverNo - 1] = 0; } } if (ServerLoads.sAmount[serverNo - 1] < 0) { ServerLoads.sAmount[serverNo - 1] = 0; //server load cannot be negative } } Thread.Sleep(200); //sleep 300+200=500ms //Take random(1-50) number in every 500ms from mainServer. RandomGenerator rand2 = new RandomGenerator(); if (ServerLoads.mainAmount > 0) { RequestTaker requestTaker = new RequestTaker(rand2.number, serverNo); //takes random load from mainserver ServerLoads.sAmount[serverNo - 1] = ServerLoads.sAmount[serverNo - 1] + rand2.number; //then responds them randomly } } ServerLoads.sAmount[serverNo - 1] = -1; Console.WriteLine("! " + serverNo + " STOPPED."); SubServerCount.serverThreadCount--; bool key = true; while (key) { Thread.Sleep(50); if (SubServerIsRunning.isRunning[serverNo - 1]) { key = false; SubServerMethod(serverNo, _otherServerNo); } } }