public Server(Logger log, Eter eter, int id) { this.address = IPAddress.Any; this.port = 2048; this.server = new TcpListener(address, port); this.log = log; this.eter = eter; this.id = id; }
public Client(int id, int waitInterval, Logger log, Eter eter) { this.client = new TcpClient(); this.knownServers = new List <Server>(); cts = new CancellationTokenSource(); this.id = id; this.waitInterval = waitInterval; this.log = log; this.eter = eter; }
static void Main(string[] args) { Eter eter = new Eter(); Logger log = new Logger(true); string filename = "logi.txt"; //Logger log = new Logger(false, filename); //wipe filename.txt file File.WriteAllText(filename, ""); Server s = new Server(log, eter, 1); int howManyClients = 3; Client[] clients = new Client[howManyClients]; clients[0] = new Client(1, 500, log, eter); clients[1] = new Client(2, 1000, log, eter); clients[2] = new Client(3, 1200, log, eter); eter.Add(s); foreach (Client c in clients) { eter.Add(c); } foreach (Client c in clients) { if (c.TWH(s)) { log.writeLog(c.Id + " managed to connect to someone.", ConsoleColor.DarkYellow); } else { log.writeLog(c.Id + " failed to connect to someone.", ConsoleColor.DarkYellow); } } Console.WriteLine("Clients have tried discovering. \nProceed? (If clients failed, it's dangerous to proceed!)"); log.writeLog("DRUGA CZESC ZADANIA - KOMUNIKACJA NA WATKACH", ConsoleColor.White); Console.ReadKey(); foreach (Client c in clients) { c.Communicate(PACKET_TYPE.ACK); } s.Start(); foreach (Client c in clients) { c.Connect(); } clients[0].CancelAfter(2000); clients[1].CancelAfter(3500); clients[2].CancelAfter(6000); List <Task> taski = new List <Task>(); foreach (Client c in clients) { taski.Add(c.keepPinging(c.Id + " pings.")); } Task.WaitAll(taski.ToArray()); log.writeLog("Main is done waiting.", ConsoleColor.Yellow); foreach (Client c in clients) { c.ByeAll(); } s.Stop(); Console.WriteLine("DONE, press any key to quit.\n{0}, {1}, {2}", ((Task <int>)taski[0]).Result, ((Task <int>)taski[1]).Result, ((Task <int>)taski[2]).Result); Console.ReadKey(); }