コード例 #1
0
        static void Main(string[] args)
        {
            Initializator init = new Initializator(args);
            Console.WriteLine(init.isMaven());
            Console.WriteLine(init.isWhite());
            Console.WriteLine(init.mavensPort());
            Console.WriteLine(init.nodesPort());
            Console.WriteLine(init.xmlName());

            Task t1 = Task.Factory.StartNew(async () =>
            {
                UDPSenderReceiver udp1 = new UDPSenderReceiver();

                UdpReceiveResult res = await udp1.receiveBytesAsync(init.nodesPort());
                string str = Encoding.UTF8.GetString(res.Buffer);
                Console.WriteLine(str);

                if (init.isMaven() == true)
                {
                    //UDP соединение с клиентом для отклика на Who is Maven?
                    Console.WriteLine("I am Maven!");
                    UDPSenderReceiver udp2 = new UDPSenderReceiver();
                    int mavensPort = init.nodesPort();
                    byte[] bytesSended = BitConverter.GetBytes(mavensPort);
                    await udp2.sendUnicastBytesAsync(bytesSended, 30000);

                    //Готовим данные, которые будут отправлены клиенту
                    string xmlName = init.xmlName();
                    int empNum = getEmpNum(xmlName);
                    EmployeeRepository empRepRes = new EmployeeRepository();
                    float avgSalaryVal = getSalaryList(xmlName);
                    int avgSalaryNum = 1;

                    //Заносим данные Maven'a
                    XMLWorker xmlWorker = new XMLWorker(xmlName);
                    EmployeeRepository empRep = xmlWorker.readXML();
                    empRepRes.employees.AddRange(empRep.employees);

                    //Получаем данные с узлов
                    int nodeNum = init.getNodeNumber();
                    Console.WriteLine();
                    Console.WriteLine("Received information:");
                    for (int i = 0; i < nodeNum; i++)
                    {
                        AvgSalary avgSal = new AvgSalary();
                        TCPServer connectorToNode = new TCPServer(IPAddress.Parse("127.0.0.1"), mavensPort + i + 1);
                        byte[] receivedBytes = connectorToNode.TCPReceive();
                        BinaryWorker bw = new BinaryWorker();
                        Message msg = bw.bytesToMessage(receivedBytes);
                        avgSal = msg.s;
                        avgSalaryVal += avgSal.getAvgSalary();
                        avgSalaryNum ++;
                        empRep = msg.empRep;
                        empRepRes.employees.AddRange(empRep.employees);

                        Console.WriteLine(avgSal.getAvgSalary());
                        for (int j = 0; j < empRep.employees.Count(); j++)
                        {
                            Console.WriteLine(empRep.employees[j]);
                        }
                    }

                    //Обрабатываем информацию для отправки клиенту
                    AvgSalary avgSalary = new AvgSalary(avgSalaryNum, avgSalaryVal);
                    Calculator calc = new Calculator(empRepRes);
                    List<EmployeeRepository> groupedList = calc.groupBy();
                    EmployeeRepository sortedList = calc.sortBy();
                    EmployeeRepository filteredList = calc.filterBy(avgSalary.getAvgSalary());

                    //Готовим данные для отправки
                    MessageCalc msgCalc = new MessageCalc(sortedList, filteredList, groupedList);
                    BinaryWorker bwForCalc = new BinaryWorker();
                    byte[] bytesForClient = bwForCalc.messageCalcToBytes(msgCalc);

                    //Отправляем клиенту
                    TCPClient connectorToClient = new TCPClient(IPAddress.Parse("127.0.0.1"), 30000);
                    connectorToClient.TCPSend(bytesForClient);
                }

                else if (init.isWhite() == true)
                {
                    string xmlName = init.xmlName();
                    float salarySum = getSalaryList(xmlName);
                    int empNum = getEmpNum(xmlName);

                    //Считываем данные с xml
                    XMLWorker xmlWorker = new XMLWorker(xmlName);
                    EmployeeRepository empRep = xmlWorker.readXML();

                    //Готовим данные для отправки Maven'у
                    Message msg = new Message(new AvgSalary(empNum, salarySum), empRep);
                    BinaryWorker bw = new BinaryWorker();
                    byte[] bytesToSend = bw.messageToBytes(msg);

                    //Отправляем
                    TCPClient senderToMaven = new TCPClient(IPAddress.Parse("127.0.0.1"), init.nodesPort());
                    senderToMaven.TCPSend(bytesToSend);
                }

                else if (init.isWhite() == false)
                {
                    string xmlName = init.xmlName();
                    float salarySum = getSalaryList(xmlName);
                    int empNum = getEmpNum(xmlName);

                    //Готовим данные для отправки
                    Message msg = new Message(new AvgSalary(empNum, salarySum), new EmployeeRepository());
                    BinaryWorker bw = new BinaryWorker();
                    byte[] bytes = bw.messageToBytes(msg);

                    //Отправляем
                    TCPClient senderToMaven = new TCPClient(IPAddress.Parse("127.0.0.1"), init.nodesPort());
                    senderToMaven.TCPSend(bytes);
                }
            });
            t1.Wait();

            Console.ReadLine();
        }
コード例 #2
0
        static void Main(string[] args)
        {
            // получаем количество узлов
            int node_number = 0;
            if (args.Count() > 0)
            {
                try { node_number = Int32.Parse(args[0]); }
                catch { node_number = 0; }
            }

            // ждем ответа от Maven
            Task t1 = Task.Factory.StartNew(async () =>
            {
                UDPSenderReceiver client = new UDPSenderReceiver();
                UdpReceiveResult res = await client.receiveBytesAsync(30000);
                int mavensPort = BitConverter.ToInt32(res.Buffer, 0);
                Console.WriteLine(mavensPort);

                //Соединение с Maven'ом
                TCPServer tcpServer = new TCPServer(IPAddress.Parse("127.0.0.1"), 30000);
                byte[] bytesFromMaven = tcpServer.TCPReceive();
                if (bytesFromMaven == null) Console.WriteLine("Byte's array is empty");

                BinaryWorker bw = new BinaryWorker();
                MessageCalc msgCalc = bw.bytesToMessageCalc(bytesFromMaven);
                if (msgCalc == null) Console.WriteLine("MsgCalc is empty");

                //Вывод полученной информации
                else
                {
                    EmployeeRepository filteredList = msgCalc.filteredEl;
                    EmployeeRepository sortedList = msgCalc.sortedEl;
                    List<EmployeeRepository> groupedList = msgCalc.groupedEl;
                    Console.WriteLine();
                    Console.WriteLine("Filtered list: ");
                    for (int i = 0; i < filteredList.employees.Count(); i++)
                    {
                        Console.WriteLine(filteredList.employees[i]);
                    }
                    Console.WriteLine();
                    Console.WriteLine("Sorted list: ");
                    for (int i = 0; i < sortedList.employees.Count(); i++)
                    {
                        Console.WriteLine(sortedList.employees[i]);
                    }
                    Console.WriteLine();
                    Console.WriteLine("Grouped list: ");
                    EmployeeRepository groupedEmpRep = new EmployeeRepository();
                    for (int i = 0; i < groupedList.Count(); i++)
                    {
                        List<Employee> empList = groupedList[i].employees;
                        for (int j = 0; j < empList.Count(); j++)
                        {
                            groupedEmpRep.Add(empList[j]);
                            Console.WriteLine(empList[j]);
                        }
                    }

                    //Записываем в xml
                    XMLWorker xmlWorker1 = new XMLWorker("filteredList.txt");
                    xmlWorker1.writeXML(filteredList);
                    XMLWorker xmlWorker2 = new XMLWorker("sortedList.txt");
                    xmlWorker2.writeXML(sortedList);
                    XMLWorker xmlWorker3 = new XMLWorker("groupedList.txt");
                    xmlWorker3.writeXML(groupedEmpRep);
                }

            });
            t1.Wait();

            // Шлем узлам запрос на Maven
            Task t2 = Task.Factory.StartNew(async () =>
            {
                UDPSenderReceiver client = new UDPSenderReceiver();

                Console.WriteLine("Client. Press enter key to send!");
                Console.ReadLine();
                byte[] bytes = Encoding.UTF8.GetBytes("Who is Maven?");

                for (int i = 0; i < node_number; i++)
                {
                    await client.sendBroadcastBytesAsync(bytes, 30000 + i + 1);
                }

            });
            t2.Wait();

            Console.ReadLine();
        }