/// <summary>
 /// 
 /// </summary>
 /// <param name="problemType">
 ///     The problem type name as given by TaskSolver and Client
 /// </param>
 /// <param name="id">
 ///     The ID of the problem instance assigned by the server
 /// </param>
 /// <param name="commonData">
 ///     common data which was previously sent to all Computational Nodes (possibly
 ///     could be stored on server as TaskManagers could have changed)
 /// </param>
 /// <param name="solutions">
 ///     Solutions 
 /// </param>
 public SolutionsMessage(string problemType, ulong id, byte[] commonData, Solution[] solutions)
 {
     ProblemType = problemType;
     Id = id;
     CommonData = commonData;
     Solutions = solutions;
 }
        public void Parse_XMLString_SolutionMessage()
        {
            /*********** Actual message ***********/
            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"xml_samples\Solutions.xml");

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path);
            string xmlStr = xmlDoc.OuterXml;

            string name = Message.GetMessageName(xmlStr);
            SolutionsMessage actualMessage = null;

            if (name == SolutionsMessage.ELEMENT_NAME)
                actualMessage = SolutionsMessage.Construct(xmlStr);

            /*********** Expected message ***********/
            // construct fields for SolutionsMessage
            string problemType = "TSP";
            ulong id = 12;
            byte[] commonData = { 1, 23, 25, 2, 5, 5, 5, 5, 2, 26, 87 };

            // fields for Solution1
            ulong taskId1 = 123;
            bool timeoutOccured1 = false;
            SolutionsSolutionType typeField1 = SolutionsSolutionType.Final;
            ulong computationsTime1 = 12334;
            byte[] data1 = { 24, 252, 6, 43, 57, 88 };
            Solution solution1 = new Solution(taskId1, timeoutOccured1, typeField1, computationsTime1, data1);

            // fields for Solution2
            ulong taskId2 = 321;
            bool timeoutOccured2 = true;
            SolutionsSolutionType typeField2 = SolutionsSolutionType.Ongoing;
            ulong computationsTime2 = 43321;
            byte[] data2 = { 24, 25, 6, 3, 7, 8 };
            Solution solution2 = new Solution(taskId2, timeoutOccured2, typeField2, computationsTime2, data2);

            Solution[] solutions = { solution1, solution2 };

            SolutionsMessage expectedMessage = new SolutionsMessage(problemType, id, commonData, solutions);


            Assert.AreEqual(expectedMessage, actualMessage);
        }
        /// <summary>
        /// in this function solution message with solved partial solution is sent (it checks if all subtasks of task are solved.
        /// It returns true in case of completition of sending the message.
        /// </summary>
        /// <param name="numberOfTask"></param>
        /// <param name="node"></param>
        /// <param name="messagePackage"></param>
        /// <returns></returns>
        private bool isMergeSolutionSent(int numberOfTask, NetworkNode node, MessagePackage messagePackage)
        {
            if (taskTracker.Tasks[numberOfTask].Status != Cluster.TaskStatus.Merging && taskTracker.Tasks[numberOfTask].Status != Cluster.TaskStatus.Merged && taskTracker.Tasks[numberOfTask].subTasks.Count != 0)
            {
                for (int j = 0; j < taskTracker.Tasks[numberOfTask].subTasks.Count; j++)
                {
                    if (taskTracker.Tasks[numberOfTask].subTasks[j].Status != Cluster.TaskStatus.Solved)
                    {
                        return false;
                    }
                }
            }
            else
            {
                return false;
            }
            if (taskTracker.Tasks[numberOfTask].Type == node.SolvableProblems[0])
            {
                taskTracker.Tasks[numberOfTask].Status = Cluster.TaskStatus.Merging;
                Solution[] solutions = new Solution[taskTracker.Tasks[numberOfTask].subTasks.Count];
                for (int k = 0; k < solutions.Count(); k++)
                {
                    solutions[k] = new Solution(SolutionsSolutionType.Final);
                }
                for (int j = 0; j < taskTracker.Tasks[numberOfTask].subTasks.Count; j++)
                {
                    solutions[j].Data = taskTracker.Tasks[numberOfTask].subTasks[j].Solutions[0].Data;
                }
                SolutionsMessage solutionMessage = new SolutionsMessage(taskTracker.Tasks[numberOfTask].Type, (ulong)taskTracker.Tasks[numberOfTask].ID, taskTracker.Tasks[numberOfTask].CommonData, solutions);

                server.Send(messagePackage.Socket, solutionMessage);

               SmartConsole.PrintLine("Solution Message has been sent to Task Manager", SmartConsole.DebugLevel.Advanced);
               return true;        
            }
            return false;
        }
Example #4
0
        public Task(int id, string type, byte[] data)
        {
            ID = id;
            BaseData = data;
            Type = type;

            Status = TaskStatus.New;
            Solutions = new Solution[1];
            Solutions[0] = new Solution(SolutionsSolutionType.Ongoing);
        }
        /*******************************************************************/
        /************************ PRIVATE METHODS **************************/
        /*******************************************************************/

        /*******************************************************************/
        /************************* PUBLIC METHODS **************************/
        /*******************************************************************/

        public void Start()
        {
            switch (currentTask.Status)
            {
                case TaskStatus.Dividing:

                    byte[][] dividedProblems =  taskSolver.DivideProblem(4);

                    PartialProblem[] partialProblems = new PartialProblem[dividedProblems.Count()];
                    for (int i = 0; i < dividedProblems.Count(); i++)
                    {
                        partialProblems[i] = new PartialProblem((ulong)currentTask.ID, dividedProblems[i], (ulong)NodeID);
                    }
                    SolvePartialProblemsMessage solvePartialProblemsMessage = new SolvePartialProblemsMessage(currentTask.Type, (ulong) currentTask.ID, currentTask.CommonData, (ulong)4, partialProblems);
                    Console.Write(">>Sending solve partial problems message. ");
                    messageProcessor.Communicate(solvePartialProblemsMessage);
                    this.statusThread.State = StatusThreadState.Idle;
                    this.currentTask = null;
                    
                        break;
                    
                case TaskStatus.Solving:

                    byte[] solvedPartialProblem = taskSolver.Solve(currentTask.BaseData, new TimeSpan(0, 0, 5));
                    Solution[] solutions = new Solution[1];
                    //TODO subTask id, timeout checking , computations time
                    solutions[0] = new Solution((ulong)currentTask.ID, false, SolutionsSolutionType.Partial, 4000, solvedPartialProblem);

                    SolutionsMessage solutionMessage = new SolutionsMessage(currentTask.Type, (ulong)currentTask.ID, currentTask.CommonData, solutions);
                    Console.WriteLine(">>Sending solution message. ");
                    messageProcessor.Communicate(solutionMessage);
                    statusThread.State = StatusThreadState.Idle;
                    this.currentTask = null;
                    break;

                case TaskStatus.Merging:
                    byte[][] partialSolutions = new byte[currentTask.subTasks.Count()][];
                    for(int i=0;i<currentTask.subTasks.Count();i++){
                        partialSolutions[i] = currentTask.subTasks[i].BaseData;
                    }
                    byte[] mergedSolution = taskSolver.MergeSolution(partialSolutions);

                    Solution[] solution = new Solution[1];
                    //TODO subTask id, timeout checking , computations time
                    solution[0] = new Solution((ulong)currentTask.ID, false, SolutionsSolutionType.Final, 4000, mergedSolution);
                    SolutionsMessage finalSolutionMessage = new SolutionsMessage(currentTask.Type, (ulong)currentTask.ID, currentTask.CommonData, solution);
                    messageProcessor.Communicate(finalSolutionMessage);
                    this.statusThread.State = StatusThreadState.Idle;
                    this.currentTask = null;
                    break;
                
            }
            // Implement in private methods:
            // Depending on what has to be computed
            // run a task solver method.

            // TODO collect result.
        }
        public static SolutionsMessage CreateSolutionsMessage()
        {
            string problemType = "TSP";
            ulong id = 12;
            byte[] commonData = { 1, 23, 25, 2, 5, 5, 5, 5, 2, 26, 87 };

            // fields for Solution1
            ulong taskId1 = 123;
            bool timeoutOccured1 = false;
            SolutionsSolutionType typeField1 = SolutionsSolutionType.Final;
            ulong computationsTime1 = 12334;
            byte[] data1 = { 24, 252, 6, 43, 57, 88 };
            Solution solution1 = new Solution(taskId1, timeoutOccured1, typeField1, computationsTime1, data1);

            // fields for Solution2
            ulong taskId2 = 321;
            bool timeoutOccured2 = true;
            SolutionsSolutionType typeField2 = SolutionsSolutionType.Ongoing;
            ulong computationsTime2 = 43321;
            byte[] data2 = { 24, 25, 6, 3, 7, 8 };
            Solution solution2 = new Solution(taskId2, timeoutOccured2, typeField2, computationsTime2, data2);

            Solution[] solutions = { solution1, solution2 };

            SolutionsMessage expectedMessage = new SolutionsMessage(problemType, id, commonData, solutions);
            return expectedMessage;
        }