コード例 #1
0
        /// <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();
            }
        }
コード例 #2
0
        /// <summary>
        /// Send a work to a client
        /// </summary>
        /// <param name="listClient">List of clients</param>
        /// <param name="listPacketSended">Packet already sended</param>
        /// <param name="ipAddress">Ip address of a client</param>
        /// <param name="port">Port of a client</param>
        /// <param name="nbrPackets">Number packet to send</param>
        /// <param name="issueNumber">Issu number</param>
        public void sendWork(ref SortedList <string, Client> listClient, ref SortedList <int, PacketInfo> listPacketSended, string ipAddress, int port, int nbrPackets, int issueNumber)
        {
            List <byte[]> listWorksToSend = new List <byte[]>();
            /*Connect to client*/
            Sender s = new Sender(ipAddress, Convert.ToInt32(port));
            SortedList <int, byte[]> tempListTasks = new SortedList <int, byte[]>(tasks);

            for (int i = 0; i < nbrPackets; i++)
            {
                if (tempListTasks.Count > 0)  //More tasks?
                {
                    int    firstKey = tempListTasks.Keys[0];
                    byte[] data     = tempListTasks[firstKey];
                    tempListTasks.Remove(firstKey); //remove temporary data, if the sender doesn't work
                    /*Add info of data*/
                    PacketInfo packetInfo = new PacketInfo(Command.Work, firstKey, "", data.Length, SecondSince1970.Get(), issueNumber);
                    if (!listPacketSended.ContainsKey(firstKey))
                    {
                        listPacketSended.Add(firstKey, packetInfo);
                    }
                    listWorksToSend.Add(packetInfo.ToByte());
                    /*Add data*/
                    listWorksToSend.Add(data);
                    listClient[ipAddress].currentWork.Add(firstKey); //Add to current client work
                }
                else
                {
                    break;
                }
            }

            /*Assembly the packets*/
            byte[] dataToSend = PacketAssembler.Assemble(listWorksToSend);
            /*Send data to client*/
            s.send(dataToSend);
            int nbrPaquetsToRemove = nbrPackets;

            if (nbrPackets > tasks.Count) //More packet to remove than number of task?
            {
                nbrPaquetsToRemove = tasks.Count;
            }
            for (int i = 0; i < nbrPaquetsToRemove; i++) //Remove real data
            {
                tasks.RemoveAt(0);
            }
        }