private void doWork() { CuttingTask cuttingTask = new CuttingTask(); ClassCalculator c = new ClassCalculator(); Fractal f = new Fractal(); byte[][] data = cuttingTask.CutTask(nbrPacket); for (int i = 0; i < data.Length; i++) { byte[] result = c.Calculate(data[i]); displayFractale1Computer(f.createFractale(i + 1, result, nbrPacket)); } }
/// <summary> /// Event when as sended a data /// </summary> /// <param name="clientIP">Client IP address</param> /// <param name="data">Data received</param> private void r_OnReceived(string clientIP, byte[] data) { lock (lockThis) { /*Disassemble data*/ List <DataReceived> listPackets = PacketDisassembler.Disassemble(data); /*Read each packets*/ for (int i = 0; i < listPackets.Count; i++) { DataReceived packet = listPackets[i]; Command cmd = (Command)BitConverter.ToInt32(data, 0); switch (cmd) { case Command.Init: Init dataInit = new Init(packet.data); Client client = new Client(clientIP, dataInit.nbrCore, dataInit.memoryUsed, dataInit.listMethod, false, new List <int>(), 7000); //Create the client with her sended data /*Init client*/ InitClient.initClient(client, sendPort, File.ReadAllText(calculatorClass)); if (!listClient.ContainsKey(clientIP)) //Client exist? { listClient.Add(client.ip, client); if (OnUpdateClient != null) { OnUpdateClient(client); //Display client } } else //Client already exist and not detected deconnexion { List <int> tmpCurrentWork = new List <int>(listClient[clientIP].currentWork); foreach (int value in tmpCurrentWork) { if (!dataWork.tasks.ContainsKey(value)) //Load packet non calculated { dataWork.tasks.Add(value, cuttedTask[value - 1]); listClient[clientIP].currentWork.Remove(value); } } } break; case Command.Result: if (issueNumber == packet.issueNumber) //Working? Old sended packet? { if (!listResults.Contains(packet.noPacket)) //Task already calculated? { listResults.Add(packet.noPacket); } if (listResults.Count == nbrPacket) { timer.Stop(); } listClient[clientIP].currentWork.Remove(packet.noPacket); listClient[clientIP].interval = 7000; int secondToCalculate = SecondSince1970.Get() - listPacketSended[packet.noPacket].secondSince1970; if (OnUpdateWorkInfo != null) { OnUpdateWorkInfo(string.Format("{0} send result no {1}", clientIP, packet.noPacket)); } OnUpdateResult((int)(((float)listResults.Count / cuttedTask.Length) * 100)); //Display numbre results //Next works if (listClient[clientIP].currentWork.Count == 0) //All the work has been done? { if (dataWork != null && dataWork.tasks.Count != 0) //No more work? { avgCalculationTime = avgTime.calculateNewTime(SecondSince1970.Get() - listPacketSended[packet.noPacket].secondSince1970); //Calculate average time from current time and sended packet timeRemaining = avgCalculationTime * (nbrPacket - listPacketSended.Count); //Calculate the time to remaining calculation dataWork.sendWork(ref listClient, ref listPacketSended, clientIP, sendPort, nbrPacketToSend, issueNumber); } } if (savePacketsToFile) //Save to file? or display fractale? { string path = string.Format("{0}\\tmp\\result", Environment.CurrentDirectory); //Path of file data for this noPacket SaveToFile.save(path, FILE_EXTENTION, packet.noPacket.ToString(), packet.data); } else { if (OnDisplayFractal != null) { OnDisplayFractal(fractale.createFractale(packet.noPacket, packet.data, nbrPacket)); } } } break; case Command.State: break; case Command.Initialized: listClient[clientIP].initialised = true; if (dataWork != null && dataWork.tasks.Count != 0) //No more work? { dataWork.sendWork(ref listClient, ref listPacketSended, clientIP, sendPort, nbrPacketToSend, issueNumber); } break; case Command.CorrectIP: if (clientIP != "127.0.0.1") //No local host { serverIPs += clientIP.ToString() + ";"; } break; //case Command.AliveClient: //if (listClient != null && listClient.ContainsKey(clientIP)) // listClient[clientIP].interval = packet.secondSince1970 * 7; //break; } } GC.Collect(); } }