0% found this document useful (0 votes)
26 views3 pages

Lab1 - Singleton

The document defines a load balancing system using a singleton pattern with a LoadBalancer class. It initializes 10 Server objects and configures random or balanced load balancing. It then starts 5 threads that each make 5 requests to randomly selected or least-used servers. After the threads finish, it prints a report on request processing times for each server.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views3 pages

Lab1 - Singleton

The document defines a load balancing system using a singleton pattern with a LoadBalancer class. It initializes 10 Server objects and configures random or balanced load balancing. It then starts 5 threads that each make 5 requests to randomly selected or least-used servers. After the threads finish, it prints a report on request processing times for each server.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

using System;

namespace Lab1_Singleton
{
class Program
{
static void Main(string[] args)
{
[Link] = false;
[Link]("Enter load balancing type: (true -> random, false ->
balanced)");
string randomAccesUserInput = [Link]();
if ([Link](randomAccesUserInput, out bool
randomAccesUserInputBool))
{
[Link] = randomAccesUserInputBool;
}
if ([Link] == true)
{
[Link]("Random balancing");
}
else
{
[Link]("Balanced loading");
}

Thread[] threads = new Thread[5];


for (int i = 0; i < 5; i++)
{
Thread t = new Thread([Link]);
threads[i] = t;
}
for (int i = 0; i < 5; i++)
{
threads[i].Start();
[Link](2000);
}

[Link]("Raport final cu Threads");


[Link]();
}

class Server
{
public string Name { get; private set; }
public string IpAdress { get; private set; }
public int NumOfRequest { get; private set; }
public int MinTime { get; private set; }
public int MaxTime { get; private set; }
public int TotalTime { get; private set; }

public Server(string name, string ipAdress)


{
[Link] = name;
IpAdress = ipAdress;

MinTime = [Link];
MaxTime = [Link];
NumOfRequest = 0;
TotalTime = 0;

}
public void ProcessRequest(int processingTime)
{
NumOfRequest++;
TotalTime += processingTime;
if (processingTime < MinTime)
{
MinTime = processingTime;
}
if (processingTime > MaxTime)
{
MaxTime = processingTime;
}
}
public override string ToString()
{
string stringDescription = "Server name " + Name;
stringDescription += " IpAdress " + IpAdress;
stringDescription += " MinTime " + MinTime;
stringDescription += " MaxTime " + MaxTime;
stringDescription += " AvgTime " + TotalTime / NumOfRequest;
stringDescription += " TotalTime " + TotalTime;
stringDescription += " NumOfRequest " + NumOfRequest;

return stringDescription;
}
}
class LoadBalancer
{
private static LoadBalancer instance;
private static readonly object syncRoot = new Object();
private List<Server> servers = new List<Server>();

public Boolean RandomAccess { get; set; }

private LoadBalancer()
{

for (int i = 0; i < 10; i++)


{
[Link](new Server("Server " + i, "Ip " + 10 + 10 * i + 100
* i));
}
}

public static LoadBalancer Instance


{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
{
instance = new LoadBalancer();
}

}
}
return instance;
}
}

public void ProcessRequest(int request)


{
if (RandomAccess)
{
Random rnd = new Random();
int idxServer = [Link](0, 9);
servers[idxServer].ProcessRequest(request);
}
else
{
int min = servers[0].NumOfRequest;
int idxChosenServer = 0;

for (int i = 0; i < [Link]; i++)


{
if (servers[i].NumOfRequest <= min)
{
min = servers[i].NumOfRequest;
idxChosenServer = i;
}
}
servers[idxChosenServer].ProcessRequest(request);
}
}
public void GenerateRapport()
{
foreach (Server s in servers)
{
[Link]([Link]());
}
}

}
class WorkerThread
{
public static void PostRequest()
{
Random rnd = new Random();
for (int i = 0; i < 5; i++)
{
int timeProcessing = [Link](500, 1000);
[Link](timeProcessing);
}
}
}

}
}

You might also like