void componentsInfo_ComponentTimedOut(object sender, EventArgs e) { var cInfo = sender as ComponentsInfo; EventLogger.GetLog().WarnFormat("{0}({1}) przekroczył timeout", cInfo.Type, cInfo.Id); TerminateComponent(cInfo); }
protected override void ProcessSolveRequestResponseMessage(SolveRequestResponseMessage message) { _taskId = message.Id; _waitingForSolution = true; _solutionRequestApoloniuszTimer = new Timer(SolutionCheckTime); _solutionRequestApoloniuszTimer.Elapsed += CheckForSolution; //_solutionRequestApoloniuszTimer.Start(); EventLogger.GetLog().InfoFormat("Zaczęto rozwiązywanie problemu. Przydzielone id:{0}",_taskId); }
private void ProcessPartialSolutionsMessage(SolutionsMessage solutionsMessage) { ComponentsInfo taskManager = GetTaskManagerByProblemId(solutionsMessage.Id); if (taskManager == null) { EventLogger.GetLog().ErrorFormat("Brak task managera dla problemu {0}", solutionsMessage.Id); return; } SendMessage(solutionsMessage.Serialize(), taskManager.Client); }
protected virtual void RemoveClient(TcpClient client) { var clientThread = _clients[client]; _clients.Remove(client); if (clientThread != null && clientThread.ThreadState == ThreadState.Running) { clientThread.Abort(); } EventLogger.GetLog().InfoFormat("Klient rozłączony: {0}", client.Client.RemoteEndPoint); }
private void AddClient(TcpClient client) { //create a thread to handle communication //with connected client client.NoDelay = true; client.Client.NoDelay = true; var clientThread = new Thread(ListenForClientMessages); _clients.Add(client, clientThread); clientThread.Start(client); EventLogger.GetLog().InfoFormat("Klient podłączony: {0}", client.Client.RemoteEndPoint); }
/// <summary> /// sends a message to server, yes /// </summary> /// <param name="msg"></param> public void SendMessage(byte[] msg) { //_tcpClient.GetStream().Write(msg, 0, msg.Length); try { _tcpClient.Client.Send(msg); //Thread.Sleep(500); //EventLogger.GetLog().InfoFormat("Wiadomość wysłana:\n{0}", Encoding.UTF8.GetString(msg)); } catch (Exception e) { EventLogger.GetLog().ErrorFormat("Błąd podczas próby wysłania wiadomości: {0}", e.Message); } }
private void ProcessFinalSolution(SolutionsMessage message, Solution solution) { EventLogger.GetLog().InfoFormat("Odebrano rozwiazanie dla problemu typu: {0} o id: {1}",message.ProblemType,message.Id); switch (message.ProblemType) { case "DVRP": var dvrpSolution =DllProject.DvrpSolutionData.Deserialize(solution.Data); EventLogger.GetLog().InfoFormat(dvrpSolution.ToString()); break; case "MultiplyProblem": var multiplySolution = MultiplySolutionData.Deserialize(solution.Data); EventLogger.GetLog().InfoFormat("Wynik: {0}",multiplySolution.result); break; } }
/// <summary> /// Sends a message to a client /// </summary> /// <param name="msg">byte array with message</param> /// <param name="client">Connected tcp client</param> public void SendMessage(byte[] msg, TcpClient client) { try { var clientStream = client.GetStream(); if (clientStream.CanWrite) { clientStream.Write(msg, 0, msg.Length); clientStream.FlushAsync(); } //EventLogger.GetLog().InfoFormat("Wysłano wiadomość do {0}:\n{1} ", client.Client.RemoteEndPoint,Encoding.UTF8.GetString(msg) ); } catch (Exception) { EventLogger.GetLog().ErrorFormat("Błąd podczas wysyłania wiadomosci do: {0}", client.Client.RemoteEndPoint); } }
protected override void ProcessDivideProblemMessage(DivideProblemMessage message) { if (SolvableProblems.FirstOrDefault(x => x == message.ProblemType) == null) { EventLogger.GetLog().ErrorFormat("Nie obsługiwany problem: {0}", message.ProblemType); return; } TaskSolver taskSolver = null; switch (message.ProblemType) { case ("MultiplyProblem"): taskSolver = new MultiplyTaskSolver(message.Data); break; case ("DVRP"): taskSolver = new DllProject.DvrpTaskSolver(message.Data); break; } var problemSolvingHelper = new ProblemSolvingHelper() { ProblemId = message.Id, ProblemType = message.ProblemType, TaskSolver = taskSolver, }; _problems.Add(message.Id, problemSolvingHelper); taskSolver.ProblemDividingFinished += taskSolver_ProblemDividingFinished; taskSolver.SolutionsMergingFinished += taskSolver_SolutionsMergingFinished; var thread = new IOThread { ProblemType = message.ProblemType, State = EState.Busy, RealThread = new Thread(() => DivideProblem(message, taskSolver)), ProblemInstanceId = message.Id }; _runningThreads.Add(thread); thread.RealThread.Start(); }
private void ListenForClients() { _tcpListener.Start(); while (true) { //blocks until a client has connected to the server try { EventLogger.GetLog().Info("Czekanie na nowego klienta"); var client = _tcpListener.AcceptTcpClient(); AddClient(client); } catch (Exception) { EventLogger.GetLog().ErrorFormat("Błąd podczas podłączania klienta"); break; } } }
private void ListenForClientMessages(object client) { var tcpClient = (TcpClient)client; var clientStream = tcpClient.GetStream(); while (true) { var messageStream = new MemoryStream(); var inBuffer = new byte[MessageSize]; try { if (clientStream.CanRead) { do { var bytes = clientStream.Read(inBuffer, 0, 4096); messageStream.Write(inBuffer, 0, bytes); } while (clientStream.DataAvailable); } } catch (Exception ex) { //TcpConnectionErrorEvent.Invoke(this,new TcpConnectionErrorEventArgs(tcpClient,ETcpAction.Receive, "Błąd odbierania wiadomości")); EventLogger.GetLog().ErrorFormat("Błąd podczas odbierania wiadomosci od: {0}", tcpClient.Client.RemoteEndPoint); //a socket error has occured break; } if (messageStream.Length == 0) { //the client has disconnected from the server break; } messageStream.Position = 0; byte[] completeMessage = messageStream.ToArray(); //message has successfully been received //EventLogger.GetLog().InfoFormat("Odebrano wiadomość od {0}:\n{1}", tcpClient.Client.RemoteEndPoint,Encoding.UTF8.GetString(completeMessage)); ProcessMessage(completeMessage, tcpClient); } RemoveClient(tcpClient); }
/// <summary> /// Started in new thread. Waits for new incoming server messages. /// </summary> private void ListenForMessages() { var serverStream = _tcpClient.GetStream(); while (true) { var messageStream = new MemoryStream(); var inBuffer = new byte[MessageSize]; try { if (serverStream.CanRead) { do { var bytes = serverStream.Read(inBuffer, 0, 4096); messageStream.Write(inBuffer, 0, bytes); } while (serverStream.DataAvailable); } } catch (Exception e) { EventLogger.GetLog().ErrorFormat("Błąd podczas próby odebrania wiadomości: {0}", e.Message); //a socket error has occured break; } if (messageStream.Length == 0) { //the client has disconnected from the server break; } messageStream.Position = 0; byte[] completeMessage = messageStream.ToArray(); //EventLogger.GetLog().InfoFormat("Odebrano wiadomość:\n{0}", Encoding.UTF8.GetString(completeMessage)); ProcessMessage(completeMessage); } _tcpClient.Close(); }
private void ProcessSolvePartialProblemsMessage(SolvePartialProblemsMessage partialProblemsMsg) { var availableNodes = GetComputationalNodesByProblemType(partialProblemsMsg.ProblemType); var availableNodesCount = availableNodes.Count; if (availableNodesCount == 0) { EventLogger.GetLog().ErrorFormat("Brak node'ów dla problemu {0} - id:{1}", partialProblemsMsg.ProblemType, partialProblemsMsg.Id); return; } var subProblemDivision = new List <List <PartialProblem> >(availableNodesCount); //Lista podproblemow dla kazdego node'a for (int i = 0; i < availableNodesCount; i++) { subProblemDivision.Add(new List <PartialProblem>()); } var partialProblems = partialProblemsMsg.PartialProblems; for (int i = 0; i < partialProblems.Count; i++) //przydziel podproblemy { subProblemDivision[i % availableNodesCount].Add(partialProblems[i]); } for (int i = 0; i < subProblemDivision.Count; i++) //Roześlij { var node = availableNodes[i]; var message = new SolvePartialProblemsMessage() { CommonData = partialProblemsMsg.CommonData, Id = partialProblemsMsg.Id, PartialProblems = subProblemDivision[i], ProblemType = partialProblemsMsg.ProblemType, SolvingTimeout = partialProblemsMsg.SolvingTimeout }; SendMessage(message.Serialize(), node.Client); //EventLogger.GetLog().InfoFormat("Wysłano {0} partial problemy typu {1} do Computational Noda o id {2}", subProblemDivision[i].Count, partialProblemsMsg.ProblemType, node.Id); } }
private void ProcessSolveRequestMessage(SolveRequestMessage msg, TcpClient client) { var data = msg.Data; var problemType = msg.ProblemType; var timeout = msg.SolvingTimeout; var taskManagerInfo = GetTaskManagerByProblemType(problemType); if (taskManagerInfo == null) { EventLogger.GetLog().ErrorFormat("Brak task managera dla problemu {0}", msg.ProblemType); return; } var problemInstance = new ProblemInstanceInfo(++_maxProblemId, msg.ProblemType) { Data = data, TaskManager = taskManagerInfo, ComputationalClient = GetComponentsInfoByTcpClient(client), SolvingTimeout = timeout, ProblemType = msg.ProblemType }; _problemInstances.Add(problemInstance); //Wyslij odpowiedz var responseMsg = new SolveRequestResponseMessage(problemInstance.Id); SendMessage(responseMsg.Serialize(), client); EventLogger.GetLog().InfoFormat("Otrzymano żądanie rozwiązania problemu {0} o id {1}", problemInstance.ProblemType, problemInstance.Id); //Wyslij prosbe o podzielenie var availableComputationalNodes = _computationalNodes.Count(c => c.SolvableProblems.Contains(problemInstance.ProblemType)); var divideProblemMsg = new DivideProblemMessage(problemInstance.ProblemType, problemInstance.Id, problemInstance.Data, (ulong)availableComputationalNodes); SendMessage(divideProblemMsg.Serialize(), taskManagerInfo.Client); EventLogger.GetLog().InfoFormat("Wysłano żądanie podzielenia problemu {0} o id {1} do TM {2}", problemInstance.ProblemType, problemInstance.Id, taskManagerInfo.Id); }
/// <summary> /// generates id for the component, adds it to an adequate list, and sends returning message /// </summary> /// <param name="client"></param> /// <param name="type"></param> /// <param name="parallelThreads"></param> /// <param name="solvableProblems"></param> public void RegisterComponent(TcpClient client, EComponentType type, int parallelThreads = 1, List <string> solvableProblems = null) { var componentsInfo = new ComponentsInfo(type, ++_maxComponentId, client, _timeout, parallelThreads, solvableProblems); componentsInfo.ComponentTimedOut += componentsInfo_ComponentTimedOut; switch (type.ToString()) { case ("TaskManager"): _taskManagers.Add(componentsInfo); break; case ("ComputationalNode"): _computationalNodes.Add(componentsInfo); break; case ("ComputationalClient"): _computationalClients.Add(componentsInfo); break; } var responseMsg = new RegisterResponseMessage(_maxComponentId, DateTime.Now); SendMessage(responseMsg.Serialize(), client); EventLogger.GetLog().InfoFormat("Zarejestrowano {0} z Id:{1}", componentsInfo.Type, componentsInfo.Id); }
void taskSolver_SolutionsMergingFinished(EventArgs eventArgs, TaskSolver sender) { EventLogger.GetLog().InfoFormat("TaskSolver {0} skonczyl mergowanie problemu", sender.Name); }
void taskSolver_ProblemDividingFinished(EventArgs eventArgs, TaskSolver sender) { EventLogger.GetLog().InfoFormat("TaskSolver {0} skonczyl dzielenie problemu", sender.Name); }
public void TerminateComponent(ComponentsInfo cInfo) { EventLogger.GetLog().WarnFormat("{0}({1}) Odłączony", cInfo.Type, cInfo.Id); RemoveClient(cInfo.Client); }
/// <summary> /// Procesess a byte array message received from a Component (byte to xml conversion, message logic etc.) /// </summary> /// <param name="msg"></param> /// <param name="client"></param> public override void ProcessMessage(byte[] msg, TcpClient client) { Type messageType; ASCIIEncoding encoder = new ASCIIEncoding(); string messagesString = encoder.GetString(msg); _messageParser.Value.ParseMessage(messagesString); var messagesToProcess = _messageParser.Value.GetMessagesToProcess(); foreach (var messageString in messagesToProcess) { try { messageType = Message.GetMessageType(messageString); } catch (Exception exception) { EventLogger.GetLog().ErrorFormat("{0} - Problem z deserializacją wiadomości - {1}\n{2}", client.Client.RemoteEndPoint, exception.Message, messageString); return; } try { switch (messageType.Name) { case ("RegisterMessage"): var regMsg = Message.Deserialize <RegisterMessage>(messageString); RegisterComponent(client, regMsg.ComponentType, regMsg.ParallelThreads, regMsg.SolvableProblems); break; case ("StatusMessage"): var statusMsg = Message.Deserialize <StatusMessage>(messageString); GetComponentsInfoById(statusMsg.Id).ResetTimeoutTimer(); break; case ("SolutionRequestMessage"): { break; } case ("SolutionsMessage"): { var solutionMsg = Message.Deserialize <SolutionsMessage>(messageString); ProcessSolutionsMessage(solutionMsg); break; } case ("SolvePartialProblemsMessage"): { var solvePartialProblemsMsg = Message.Deserialize <SolvePartialProblemsMessage>(messageString); ProcessSolvePartialProblemsMessage(solvePartialProblemsMsg); break; } case ("SolveRequestResponseMessage"): { break; } case ("SolveRequestMessage"): { var solveRequestMsg = Message.Deserialize <SolveRequestMessage>(messageString); ProcessSolveRequestMessage(solveRequestMsg, client); break; } case ("DivideProblemMessage"): { break; } } } catch (Exception e) { EventLogger.GetLog().ErrorFormat("{0} - Błąd przy przetwarzaniu wiadomości - {1}", client.Client.RemoteEndPoint, e.Message); } } }