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; 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 string xmlNameFiltered = "filteredList.txt"; string xmlNameSorted = "sortedList.txt"; string xmlNameGrouped = "groupedList.txt"; XMLWorker xmlWorker1 = new XMLWorker(xmlNameFiltered); xmlWorker1.writeXML(filteredList); XMLWorker xmlWorker2 = new XMLWorker(xmlNameSorted); xmlWorker2.writeXML(sortedList); XMLWorker xmlWorker3 = new XMLWorker(xmlNameGrouped); xmlWorker3.writeXML(groupedEmpRep); //Подготавливаем документы для валидации XDocument xmlFiltered = XDocument.Load(xmlNameFiltered); XDocument xmlSorted = XDocument.Load(xmlNameSorted); XDocument xmlGrouped = XDocument.Load(xmlNameGrouped); XDocument xsd = XDocument.Load("xsd.txt"); //Если документ прошел валидацию, выводим инфу if (Validator.Validate(xmlFiltered, xsd) == true) { Console.WriteLine(); Console.WriteLine("Filtered list: "); for (int i = 0; i < filteredList.employees.Count(); i++) { Console.WriteLine(filteredList.employees[i]); } } else Console.WriteLine("Filtered XML didn't pass validation!"); if (Validator.Validate(xmlSorted, xsd) == true) { Console.WriteLine(); Console.WriteLine("Sorted list: "); for (int i = 0; i < sortedList.employees.Count(); i++) { Console.WriteLine(sortedList.employees[i]); } } else Console.WriteLine("Sorted XML didn't pass validation!"); if (Validator.Validate(xmlSorted, xsd) == true) { Console.WriteLine(); Console.WriteLine("Grouped list: "); for (int i = 0; i < groupedEmpRep.employees.Count(); i++) { Console.WriteLine(groupedEmpRep.employees[i]); } } else Console.WriteLine("Grouped XML didn't pass validation!"); } }); 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(); }
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.txtName()); 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 txtName = init.txtName(); int empNum = getEmpNum(txtName); EmployeeRepository empRepRes = new EmployeeRepository(); AvgSalary avgSal = new AvgSalary(getEmpNum(txtName), getSalaryList(txtName)); float avgSalaryVal = avgSal.getAvgSalary(); int avgSalaryNum = 1; //Заносим данные Maven'a JsonWorker jsWorker = new JsonWorker(txtName); EmployeeRepository empRep = jsWorker.jsonRead(); empRepRes.employees.AddRange(empRep.employees); //Получаем данные с узлов int nodeNum = init.getNodeNumber(); Console.WriteLine(); Console.WriteLine("Received information:"); for (int i = 0; i < nodeNum; i++) { 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 jsName = init.txtName(); float salarySum = getSalaryList(jsName); int empNum = getEmpNum(jsName); //Считываем данные с xml //XMLWorker xmlWorker = new XMLWorker(xmlName); //EmployeeRepository empRep = xmlWorker.readXML(); //Считываем данные с JSON JsonWorker jsWorker = new JsonWorker(jsName); EmployeeRepository empRep = jsWorker.jsonRead(); //Готовим данные для отправки 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 jsName = init.txtName(); float salarySum = getSalaryList(jsName); int empNum = getEmpNum(jsName); //Готовим данные для отправки 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(); }