public void Parse_XMLString_SolutionRequestMessage() { /*********** Actual message ***********/ string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"xml_samples\SolutionRequest.xml"); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(path); string xmlStr = xmlDoc.OuterXml; string name = Message.GetMessageName(xmlStr); SolutionRequestMessage actualMessage = null; if (name == SolutionRequestMessage.ELEMENT_NAME) { actualMessage = SolutionRequestMessage.Construct(xmlStr); } /*********** Expected message ***********/ ulong id = 9; SolutionRequestMessage expectedMessage = new SolutionRequestMessage(id); Assert.AreEqual(expectedMessage, actualMessage); }
/// <summary> /// Sends request for solution of the problem. /// </summary> /// <param name="id">The ID of the problem instance assigned by the server.</param> /// <returns>Solutions message(s) or null if server is not responding.</returns> public List <SolutionsMessage> SendSolutionRequest(ulong id) { var solutionRequestMessage = new SolutionRequestMessage { ProblemInstanceId = id }; var receivedMessages = SendMessage(solutionRequestMessage); if (receivedMessages == null) { return(null); } var solutionsMessages = new List <SolutionsMessage>(); foreach (var receivedMessage in receivedMessages) { SolutionsMessage solutionsMessage; if ((solutionsMessage = receivedMessage as SolutionsMessage) != null) { solutionsMessages.Add(solutionsMessage); } else { RaiseEvent(MessageHandlingException, receivedMessage, new InvalidOperationException("SolutionsMessage expected.")); } } return(solutionsMessages); }
public static SolutionRequestMessage CreateSolutionRequestMessage() { ulong id = 9; SolutionRequestMessage expectedMessage = new SolutionRequestMessage(id); return(expectedMessage); }
/*******************************************************************/ /************************* PUBLIC METHODS **************************/ /*******************************************************************/ public void Start(ulong id) { solutionRequestMessage = new SolutionRequestMessage(id); this.timer = new System.Timers.Timer((systemTracker.Node.Timeout * 1000) / KEEP_ALIVE_SCALLAR); this.timer.Elapsed += keepAliveSolution; this.timer.Enabled = true; }
public void SolutionRequestMessageXmlIsProperlySerializedAndDeserialized() { var message = new SolutionRequestMessage { ProblemInstanceId = 5 }; var serializedMessage = _serializer.Serialize(message); var deserializedMessage = _serializer.Deserialize(serializedMessage); Assert.IsInstanceOfType(deserializedMessage, typeof(SolutionRequestMessage)); }
public void SolutionRequestMessageSerialization() { string testData = System.IO.File.ReadAllText(@"XMLTestData\SolutionRequestMessage.xml"); var serializer = new ComputationSerializer <SolutionRequestMessage>(); var solutionRequestMessage = new SolutionRequestMessage() { Id = 1 }; var result = serializer.Serialize(solutionRequestMessage); Assert.AreEqual(result, testData); }
/// <summary> /// Constructs message by given xml string /// </summary> /// <param name="xmlString"> /// xml in string /// </param> /// <returns> /// Message constructed by the xml /// </returns> public static Message Construct(string xmlString) { xmlString = concCompabilityIssuesStringWojtekIsGay(xmlString); if (GetMessageName(xmlString) == RegisterMessage.ELEMENT_NAME) { return(RegisterMessage.Construct(xmlString)); } if (GetMessageName(xmlString) == DivideProblemMessage.ELEMENT_NAME) { return(DivideProblemMessage.Construct(xmlString)); } if (GetMessageName(xmlString) == NoOperationMessage.ELEMENT_NAME) { return(NoOperationMessage.Construct(xmlString)); } if (GetMessageName(xmlString) == RegisterResponseMessage.ELEMENT_NAME) { return(RegisterResponseMessage.Construct(xmlString)); } if (GetMessageName(xmlString) == SolutionRequestMessage.ELEMENT_NAME) { return(SolutionRequestMessage.Construct(xmlString)); } if (GetMessageName(xmlString) == SolutionsMessage.ELEMENT_NAME) { return(SolutionsMessage.Construct(xmlString)); } if (GetMessageName(xmlString) == SolvePartialProblemsMessage.ELEMENT_NAME) { try { //return SolvePartialProblemsMessage.Construct(xmlString); } catch (InvalidOperationException e) { return(null); } Console.WriteLine("******** SOLVE PARTIAL PROBLEM MESSAGE **************"); return(SolvePartialProblemsMessage.Construct(xmlString)); } if (GetMessageName(xmlString) == SolveRequestMessage.ELEMENT_NAME) { return(SolveRequestMessage.Construct(xmlString)); } if (GetMessageName(xmlString) == SolveRequestResponseMessage.ELEMENT_NAME) { return(SolveRequestResponseMessage.Construct(xmlString)); } if (GetMessageName(xmlString) == StatusMessage.ELEMENT_NAME) { return(StatusMessage.Construct(xmlString)); } return(null); }
/******************************************************************/ /************************** CONSTRUCTORS **************************/ /******************************************************************/ /// <summary> /// Creates KeepAliveTimer /// </summary> /// <param name="messageProcessor"></param> /// /// <param name="systemTracker"></param> public ComputationalClientCheckTimer(MessageProcessor messageProcessor, ClientSystemTracker systemTracker, ulong id) { solutionRequestMessage = new SolutionRequestMessage(id); this.messageProcessor = messageProcessor; this.systemTracker = systemTracker; // TODO Magic numbers this.timer = new System.Timers.Timer((systemTracker.Node.Timeout * 1000) / 2); this.timer.Elapsed += keepAlive; Active = false; }
/// <summary> /// SolutionRequest is sent by Computational Client /// </summary> /// <param name="messagePackage"></param> private void handleSolutionRequestMessage(MessagePackage messagePackage) { SolutionRequestMessage message = (SolutionRequestMessage)messagePackage.Message; Task task = taskTracker.GetTask((int)message.Id); SolutionsMessage solutionMessage = new SolutionsMessage(task.Type, (ulong)task.ID, task.CommonData, task.Solutions); server.Send(messagePackage.Socket, solutionMessage); if (task.Solutions[0].Type != SolutionsSolutionType.Ongoing) { taskTracker.Tasks.Remove(task); } }
public void Parse_SolutionRequestMessage_XMLString() { string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"xml_samples\SolutionRequest.xml"); ulong id = 9; SolutionRequestMessage message = new SolutionRequestMessage(id); //message.ToXmlFile(path); string actualXmlStr = message.ToXmlString(); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(path); string expectedXmlStr = xmlDoc.OuterXml; Assert.AreEqual(expectedXmlStr, actualXmlStr); }
public void SolutionRequestMessageSerializationTest() { var solutionRequestMessage = new SolutionRequestMessage(123L); var result = solutionRequestMessage.SerializeToXml(); Assert.IsNotNull(result); Assert.AreNotEqual(0, result.Length); var xmlValidator = new XmlValidator(); var xsdSchemaFile = "SolutionRequestMessage.xsd"; var xsdSchemaPath = Path.Combine(_xsdSchemasPath, xsdSchemaFile); var validationResult = xmlValidator.Validate(result, xsdSchemaPath, true); var errorsCount = validationResult.Errors.Count + validationResult.Warnings.Count; Assert.AreEqual(0, errorsCount); #region ExampleResult //<?xml version="1.0" encoding="utf-16"?> //<SolutionRequest xmlns="http://www.mini.pw.edu.pl/ucc/"> // <Id>123</Id> //</SolutionRequest> #endregion }
public string SendSolutionRequest(SolutionRequestMessage solutionRequestMessage) { try { clientSocket = communicationModule.SetupClient(); communicationModule.Connect(clientSocket); var serializer = new ComputationSerializer <SolutionRequestMessage>(); var message = serializer.Serialize(solutionRequestMessage); communicationModule.SendData(message, clientSocket); var response = communicationModule.ReceiveData(clientSocket); communicationModule.CloseSocket(clientSocket); return(response); } catch (Exception ex) { Trace.WriteLine(ex.ToString()); //TODO logowanie } return(String.Empty); }
private void CheckForSolution(object sender, ElapsedEventArgs e) { var solutionRequestMsg = new SolutionRequestMessage(_taskId); SendMessage(solutionRequestMsg.Serialize()); }
private void ThreadBackgroundWork() { SolutionRequestMessage srm = new SolutionRequestMessage() { Id = this.problemId }; SolutionsMessage sm = null; String message = String.Empty; double len = 0; string ttt; while (sm == null) { //this.potwierdzenie.Text += "\n\nAsking for Final solution... "; string str = "\nAsking for Final solution... "; this.potwierdzenie.Dispatcher.Invoke(new UpdateTextCallback(this.UpdateText), new object[] { str }); message = this.computationalClient.SendSolutionRequest(srm); if (message != String.Empty) { switch (bn.GetMessageName(message)) { case "Solutions": var serializer = new ComputationSerializer <SolutionsMessage>(); sm = serializer.Deserialize(message); foreach (var solution in sm.Solutions) { if (solution.Type == SolutionType.Final) { //str = "\n\nFinal solution: " + System.Text.Encoding.UTF8.GetString(solution.Data); //len = DynamicVehicleRoutingProblem.DVRPPartialSolution.Parse2FinalSolLenght(System.Text.Encoding.UTF8.GetString(solution.Data)); DVRP.Solutions.FinalSolution finalSolution = (DVRP.Solutions.Solution.ToSolution(solution.Data)).ToFinalSolution(); ttt = finalSolution.ToString(); this.potwierdzenie.Dispatcher.Invoke(new UpdateTextCallback(this.UpdateText), new object[] { "\n\n Path lenght: " + ttt }); this.potwierdzenie.Dispatcher.Invoke(new UpdateTextCallback(this.UpdateText), new object[] { "\n SOLVED!!! \n SOLVED!!!" }); break; } } sm = null; break; case "Other...?": break; default: break; } if (len != 0) { break; } } else { //str = "\n\n Message empty"; //this.potwierdzenie.Dispatcher.Invoke(new UpdateTextCallback(this.UpdateText), new object[] { str }); } str = " Computing..."; this.potwierdzenie.Dispatcher.Invoke(new UpdateTextCallback(this.UpdateText), new object[] { str }); Thread.Sleep(10000); } }
protected virtual void ProcessSolutionRequestMessage(SolutionRequestMessage message) { }
/// <summary> /// Handle solution request message. /// </summary> /// <param name="msg">Solution request message.</param> /// <param name="metadata">Information about data and the TCP connection it came from.</param> /// <returns>List of response messages.</returns> private List <Message> HandleMessage(SolutionRequestMessage msg, TcpDataProviderMetadata metadata) { var solution = _workManager.GetSolution(msg.ProblemInstanceId); var problem = _workManager.GetProblem(msg.ProblemInstanceId); Message response; if (solution != null) { problem = solution.Problem; var msgSolution = new SolutionsMessage.Solution { ComputationsTime = solution.ComputationsTime, Data = solution.Data, TimeoutOccured = solution.TimeoutOccured, Type = SolutionsMessage.SolutionType.Final }; response = new SolutionsMessage { CommonData = problem.CommonData, ProblemInstanceId = problem.Id, ProblemType = problem.Type, Solutions = new List <SolutionsMessage.Solution> { msgSolution } }; } else if (problem != null) { var msgSolution = new SolutionsMessage.Solution { ComputationsTime = _workManager.GetComputationsTime(problem.Id), TimeoutOccured = false, Type = SolutionsMessage.SolutionType.Ongoing }; response = new SolutionsMessage { CommonData = problem.CommonData, ProblemInstanceId = problem.Id, ProblemType = problem.Type, Solutions = new List <SolutionsMessage.Solution> { msgSolution } }; } else { response = new ErrorMessage { ErrorType = ErrorType.ExceptionOccured, ErrorText = "The specified problem id is unknown to the system." }; } return(new List <Message> { response }); }
static void Main(string[] args) { EventLogger.AddAppender(new Log4NetAppender()); var ipAddress = "127.0.0.1"; var port = 8123; IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(ipAddress), port); Console.WriteLine("Choose component: \n" + "1 ComputationalClient\n" + "2 TaskManager\n" + "3 ComputationalNode\n"); Console.WriteLine("Select: "); int componentType; int.TryParse(Console.ReadLine(), out componentType); Component component = new ComputationalClient(); switch (componentType) { case 1: component = new ComputationalClient(); break; case 2: component = new TaskManager(); break; case 3: component = new ComputationalNode(); break; } component.SolvableProblems = new List <string>() { "MultiplyProblem", "DVRP" }; component.Register(endPoint); var filePath = @"DvrpData\okulD.vrp"; var problem = new DvrpProblem(new DvrpProblemData(filePath)); //var problem = new MultiplyProblem(10, 3, 1000000); while (true) { Common.Abstractions.Message msg = null; int result; Console.WriteLine("RegisterMessage was sent\n" + "Choose another message: \n" + "1 RegisterMessage\n" + "2 RegisterResponseMessage\n" + "3 StatusMessage\n" + "4 SolveRequestMessage\n" + "5 SolveRequestResponseMessage\n" + "6 DivideProblemMessage\n" + "7 SolutionRequestMessage\n" + "8 PartialProblemsMessage\n" + "9 Solutions message\n" + "10 Exit" ); Console.WriteLine("Choose message to send: "); if (int.TryParse(Console.ReadLine(), out result) == false || result < 1 || result > 10) { Console.WriteLine("\nWrong input\n\n"); continue; } switch (result) { case 1: msg = new RegisterMessage(component.Type, 0, component.SolvableProblems); break; case 2: msg = new RegisterResponseMessage(123L, DateTime.Now); break; case 3: msg = new StatusMessage(123L, null); break; case 4: msg = new SolveRequestMessage(problem.ProblemType, problem.SolvingTimeout, problem.Data); break; case 5: msg = new SolveRequestResponseMessage(123L); break; case 6: msg = new DivideProblemMessage("Problem type", 123L, Encoding.UTF8.GetBytes("test1"), 321L); break; case 7: msg = new SolutionRequestMessage(123L); break; case 8: msg = new SolvePartialProblemsMessage("problem type", 123L, Encoding.UTF8.GetBytes("test1"), 333L, null); break; case 9: msg = new SolutionsMessage("problemy type", 123L, Encoding.UTF8.GetBytes("test1"), null); break; case 10: Environment.Exit(0); break; } component.SendMessage(msg.Serialize()); } //component.SendMessage(Encoding.UTF8.GetBytes("dupa")); }