Esempio n. 1
0
        public void PartialProblemConstructorTest()
        {
            //Arrange
            byte[] data = new byte[0];

            //Act
            PartialProblem partialProblem = new PartialProblem(0, data);

            //Assert
            Assert.IsNotNull(partialProblem);
        }
Esempio n. 2
0
        /// <summary>
        /// metoda na potzreby symulacji to TEST-weeku
        /// dlatego korzystamy w sumie tylko z liczby CN
        /// </summary>
        /// <param name="msg">DivideProblem message object</param>
        public void DivideProblemSimulation(DivideProblem msg, ComputationalThread thread)
        {
            if (msg == null)
            {
                Console.WriteLine("TM: DivideProblemSimulation failure - msg is null");
                return;
            }
            ulong? timeout_in_miliseconds = timeout != null ? (ulong?)timeout.Millisecond : null;
            ulong computational_nodes = msg.ComputationalNodes;

            var asm = Assembly.Load(AssemblyName.GetAssemblyName(Path.GetFullPath("DVRP.dll")));
            Type t = asm.GetType("DVRP.DVRP");

            var methodInfo = t.GetMethod("DivideProblem");
            object[] constructor_params = new object[1];
            constructor_params[0] = msg.Data;
            var o = Activator.CreateInstance(t, constructor_params);

            object[] param = new object[1];
            param[0] = (int)msg.ComputationalNodes;
            byte[][] result = (byte[][])methodInfo.Invoke(o, param);

            // start_time = DateTime.Now;

            List<PartialProblem> divided_problems = new List<PartialProblem>();
            //tworzymy tyle podproblemow ile dostepnych nodów

            for (int i = 0; i < (int)computational_nodes; i++)
            {
                Console.WriteLine("adding partial problem to divided problems");
                PartialProblem pp = new PartialProblem((ulong)i, result[i]);
                divided_problems.Add(pp);
            }

            if (divided_problems.Count == 0)
            {
                Console.WriteLine("TM: Cannot send msg with no partial problems set");
                return;
            }

            SolverRegisteredProblem srp = new SolverRegisteredProblem(msg.Id, divided_problems);
            ongoing_problems.Add(srp);
            SolvePartialProblems solve_partial_problems_msg = new SolvePartialProblems(msg.ProblemType, msg.Id, msg.Data, timeout_in_miliseconds, divided_problems);

            byte[] response = client.Work(solve_partial_problems_msg.GetXmlData());
            //in this case it's expected value
            if (response == null)
            {
                Console.WriteLine("TM: DivideProblems message sent successfully");
            }
            SetComputationalThreadIdle((ulong)thread.ProblemInstanceId, (ulong)thread.TaskId);
        }
Esempio n. 3
0
        public void DivideThreadFunc(DivideProblem msg, ComputationalThread thread)
        {
            if (msg == null)
            {
                Console.WriteLine("TM: DivideProblemSimulation failure - msg is null");
                return;
            }
            ulong? timeout_in_miliseconds = timeout != null ? (ulong?)timeout.Millisecond : null;
            ulong computational_nodes = msg.ComputationalNodes;

            var asm = Assembly.Load(AssemblyName.GetAssemblyName(Path.GetFullPath(msg.ProblemType + ".dll")));
            if (asm == null)
                throw new ArgumentException("Nie mozna znalezc modulu " + msg.ProblemType + ".dll");
            //Type t = asm.GetType("DVRP.DVRP");
            Type t = asm.GetTypes().Where(x => x.IsSubclassOf(typeof(UCCTaskSolver.TaskSolver))).FirstOrDefault();
            if (t == null)
                throw new ArgumentException("Brak odpowiedniej klasy w module.");

            var methodInfo = t.GetMethod("DivideProblem");
            object[] constructor_params = new object[1];
            constructor_params[0] = msg.Data;
            var o = Activator.CreateInstance(t, constructor_params);
            /*********event handler*/

            //var eventInfo = t.GetEvent("ProblemDividingFinished");
            //Type tDelegate = eventInfo.EventHandlerType;

            //MethodInfo addHandler = eventInfo.GetAddMethod();

            //Type returnType = GetDelegateReturnType(tDelegate);
            //Console.WriteLine(returnType.ToString());

            //DynamicMethod handler = new DynamicMethod("", null,
            //                      GetDelegateParameterTypes(tDelegate), t);

            //ILGenerator ilgen = handler.GetILGenerator();

            //Type[] showParameters = { typeof(String) };
            //MethodInfo simpleShow = typeof(CNNode).GetMethod("SetComputationalThreadIdle");
            //Console.WriteLine(simpleShow.ToString());

            //ilgen.Emit(OpCodes.Ldstr, "string");//ct.ProblemInstanceId.Value);//Ldstr,"This event handler was constructed at run time.");
            //ilgen.Emit(OpCodes.Call, simpleShow);
            ////   ilgen.Emit(OpCodes.Pop);
            //ilgen.Emit(OpCodes.Ret);

            //// Complete the dynamic method by calling its CreateDelegate
            //// method. Use the "add" accessor to add the delegate to
            //// the invocation list for the event.
            ////
            //Delegate dEmitted = handler.CreateDelegate(tDelegate);
            //addHandler.Invoke(o, new Object[] { dEmitted });

            object[] param = new object[1];
            param[0] = (int)msg.ComputationalNodes;
            byte[][] result = null;

            try
            {
                result = (byte[][])methodInfo.Invoke(o, param);
            }
            catch (Exception e)
            {
                MessageBox.Show("Moduł '" + msg.ProblemType + ".dll' zakończył działanie z błędem:\n\n" + e.InnerException.Message, "Błąd modułu", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
            }

            //start_time = DateTime.Now;

            List<PartialProblem> divided_problems = new List<PartialProblem>();
            //tworzymy tyle podproblemow ile dostepnych nodów

            for (int i = 0; i < (int)computational_nodes; i++)
            {
                Console.WriteLine("adding partial problem to divided problems");
                PartialProblem pp = new PartialProblem((ulong)i, result[i]);
                divided_problems.Add(pp);
            }

            if (divided_problems.Count == 0)
            {
                Console.WriteLine("TM: Cannot send msg with no partial problems set");
                return;
            }

            SolverRegisteredProblem srp = new SolverRegisteredProblem(msg.Id, divided_problems);
            ongoing_problems.Add(srp);
            SolvePartialProblems solve_partial_problems_msg = new SolvePartialProblems(msg.ProblemType, msg.Id, msg.Data, timeout_in_miliseconds, divided_problems);

            byte[] response = client.Work(solve_partial_problems_msg.GetXmlData());
            //in this case it's expected value
            if (response == null)
            {
                Console.WriteLine("TM: DivideProblems message sent successfully");
            }
            SetComputationalThreadIdle((ulong)thread.ProblemInstanceId, (ulong)thread.TaskId);
        }
Esempio n. 4
0
        public void SolvePartialProblemsConstructorTest()
        {
            //Arrange
            byte[] data = new byte[0];
            PartialProblem[] partialProblems = new PartialProblem[]{ new PartialProblem(0, (byte[])data.Clone()),
                new PartialProblem(1, (byte[])data.Clone()) };

            //Act
            SolvePartialProblems solvePartialProblems = new SolvePartialProblems("name",
                0, data, null, partialProblems);

            //Assert
            Assert.IsNotNull(solvePartialProblems);
        }
Esempio n. 5
0
        public void NodeThreadFunc(/*object o, MethodInfo methodInfo,*/ SolvePartialProblems msg, PartialProblem pp, ComputationalThread ct)
        {
            DateTime start_time = DateTime.Now;

            var asm = Assembly.Load(AssemblyName.GetAssemblyName(Path.GetFullPath(msg.ProblemType + ".dll")));
            //Type t = asm.GetType("DVRP.DVRP");
            Type t = asm.GetTypes().Where(x => x.IsSubclassOf(typeof(UCCTaskSolver.TaskSolver))).FirstOrDefault();

            var methodInfo = t.GetMethod("Solve");
            object[] constructor_params = new object[1];
            constructor_params[0] = msg.CommonData;
            var o = Activator.CreateInstance(t, constructor_params);
            /*********event handler*/

            var eventInfo = t.GetEvent("SolutionsMergingFinished");
            Type tDelegate = eventInfo.EventHandlerType;

            MethodInfo addHandler = eventInfo.GetAddMethod();

            Type returnType = GetDelegateReturnType(tDelegate);
            Console.WriteLine(returnType.ToString());

            DynamicMethod handler = new DynamicMethod("", null,
                                  GetDelegateParameterTypes(tDelegate), t);

            ILGenerator ilgen = handler.GetILGenerator();

            Type[] showParameters = { typeof(String) };
            MethodInfo simpleShow = typeof(CNNode).GetMethod("SetComputationalThreadIdle");
            Console.WriteLine(simpleShow.ToString());

            ilgen.Emit(OpCodes.Ldstr, "string");//ct.ProblemInstanceId.Value);//Ldstr,"This event handler was constructed at run time.");
            ilgen.Emit(OpCodes.Call, simpleShow);
            //   ilgen.Emit(OpCodes.Pop);
            ilgen.Emit(OpCodes.Ret);

            // Complete the dynamic method by calling its CreateDelegate
            // method. Use the "add" accessor to add the delegate to
            // the invocation list for the event.
            //
            Delegate dEmitted = handler.CreateDelegate(tDelegate);
            addHandler.Invoke(o, new Object[] { dEmitted });

            if (methodInfo != null)
            {
                object[] param = new object[2];

                param[0] = pp.Data;
                if (msg.SolvingTimeout == null)
                    param[1] = null;
                else param[1] = new TimeSpan((long)msg.SolvingTimeout * 10000000);

                byte[] result = null;

                try
                {
                    result = (byte[])methodInfo.Invoke(o, param);
                }
                catch (Exception e)
                {
                    MessageBox.Show("Moduł '" + msg.ProblemType + ".dll' zakończył działanie z błędem:\n\n" + e.InnerException.Message, "Błąd modułu", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    Application.Exit();
                }

                TimeSpan ts = DateTime.Now - start_time;
                Solution s = new Solution(pp.TaskId, false, SolutionType.Partial, (ulong)ts.TotalSeconds, result);

                solution.Add(s);
                Console.WriteLine("sending partial solutions");
                Solutions solutions = new Solutions(msg.ProblemType, msg.Id, msg.CommonData, solution);

                client.Work(solutions.GetXmlData());

                SetComputationalThreadIdle(msg.Id, pp.TaskId);
            }
            else Console.WriteLine("Method equal to null");
        }