Пример #1
0
 public SimCore()
 {
     simCoreLock     = new object();
     simTimeLock     = new object();
     state           = SimCoreState.UNINITIALIZED;
     eventLoopThread = null;
 }
Пример #2
0
 public void Stop()
 {
     lock (simCoreLock)
     {
         if (server != null)
         {
             server.Shutdown();
             state = SimCoreState.STOPPING;
         }
     }
 }
Пример #3
0
        public void Start(string simModelFile, int serverPort)
        {
            lock (simCoreLock)
            {
                InitializeDynamicData();
                InitializeSimulationModel(simModelFile);
                InitializeDistributor();
                InitializeBlackboard();
                InitializeServer(serverPort);
                InitializeSimulators();

                state = SimCoreState.RUNNING;
                server.ListenForConnections();
            }

            eventLoopThread = new Thread(new ThreadStart(EventLoop));
            eventLoopThread.Start();
        }
Пример #4
0
        public void Reset()
        {
            lock (simCoreLock)
            {
                state = SimCoreState.STOPPING;
            }
            while (state != SimCoreState.UNINITIALIZED)
            {
                Thread.Sleep(20);
            }
            lock (simCoreLock)
            {
                InitializeDynamicData();
                InitializeBlackboard();
                InitializeSimulators();
                state = SimCoreState.RUNNING;
            }

            eventLoopThread = new Thread(new ThreadStart(EventLoop));
            eventLoopThread.Start();
        }
Пример #5
0
 public void InitializeDynamicData()
 {
     simulationTime = 0;
     state          = SimCoreState.UNINITIALIZED;
 }
Пример #6
0
        private void EventLoop()
        {
            try
            {
                List <SimulationEvent> events = null;
                DateTime nowTimer             = new DateTime();
                DateTime tickTimer            = DateTime.Now;
                DateTime startTime            = new DateTime();
                int      eventCounter         = 0;

                long start;
                long end;
                Dictionary <string, long> simTimes = new Dictionary <string, long>();

                foreach (SimulatorExecutionInfo sei in simModelInfo.simulationExecutionModel.simulators)
                {
                    simTimes[sei.simulatorName] = 0;
                }

                while (true)
                {
                    nowTimer = DateTime.Now;
                    lock (simCoreLock)
                    {
                        switch (state)
                        {
                        case SimCoreState.UNINITIALIZED:
                            throw new Exception("SimCore: shouldn't be in event loop if uninitialized");

                        case SimCoreState.RUNNING:
                            //distributor.StopIncoming();
                            events       = distClient.GetEvents();
                            eventCounter = events.Count;

                            foreach (SimulationEvent e in events)
                            {
                                switch (e.eventType)
                                {
                                case "TimeTick":
                                    lock (simTimeLock)
                                    {
                                        simulationTime = (int)((IntegerValue)e["Time"]).value;

                                        if (ServerOptions.UsePerformanceLog)
                                        {
                                            StringBuilder b = new StringBuilder();
                                            b.AppendFormat("SimCore Metric: SimTime: {0}; Processing Time: {1}.", simulationTime / 1000, DateTime.Now - tickTimer);
                                            PerformanceLog.Write(b.ToString());
                                        }
                                        tickTimer = DateTime.Now;
                                        if (simulationTime == 1000)
                                        {
                                            startTime = DateTime.Now;
                                        }
                                    }
                                    break;

                                case "ExternalApp_SimStop":
                                    //Console.Out.WriteLine("SimCore Metric: Exiting SimCore loop.  Total run time: {0}; Total ticks: {1}.", DateTime.Now - startTime, (simulationTime / 1000) - 1);//-1 because it is starting at time 1
                                    isReady = false;
                                    break;

                                //case "PauseScenario":
                                //    if (simulators.ContainsKey("ViewPro"))
                                //    {
                                //        ((ViewProSim)simulators["ViewPro"]).PauseScenario();
                                //    }
                                //    break;
                                //case "ResumeScenario":
                                //    if (simulators.ContainsKey("ViewPro"))
                                //    {
                                //        ((ViewProSim)simulators["ViewPro"]).ResumeScenario();
                                //    }
                                //    break;
                                case "ResetSimulation":
                                    lock (simTimeLock)
                                    {
                                        simulationTime = 0;
                                        isReady        = false;
                                    }
                                    break;

                                default:
                                    break;
                                }

                                //foreach (SimulatorExecutionInfo sei in simModelInfo.simulationExecutionModel.simulators)
                                foreach (KeyValuePair <String, ISimulator> sim in simulators)
                                {
                                    start = DateTime.Now.Ticks;
                                    //simulators[sei.simulatorName].ProcessEvent(e);
                                    sim.Value.ProcessEvent(e);
                                    end = DateTime.Now.Ticks;

                                    //simTimes[sei.simulatorName] += (end - start);
                                    simTimes[sim.Key] += (end - start);
                                }

                                if (e.eventType == "StartupComplete")
                                {
                                    SimulationEvent ev = SimulationEventFactory.BuildEvent(ref simModelInfo, "SimCoreReady");
                                    isReady = true;
                                    distClient.PutEvent(ev);
                                    if (ServerOptions.UsePerformanceLog)
                                    {
                                        foreach (SimulatorExecutionInfo sei in simModelInfo.simulationExecutionModel.simulators)
                                        {
                                            if (!simTimes.ContainsKey(sei.simulatorName))
                                            {
                                                continue;
                                            }
                                            StringBuilder b = new StringBuilder();
                                            b.AppendFormat("SimCore Metric: Initialization time in: {0} was {1} seconds.", sei.simulatorName, simTimes[sei.simulatorName] / 10000000.0);
                                            PerformanceLog.Write(b.ToString());
                                            simTimes[sei.simulatorName] = 0;
                                        }
                                    }
                                }
                            }
                            if (eventCounter > 0)
                            {
                                //Console.Out.WriteLine("SimCore Metric: Events Processed: {0}; processing time: {1}.", eventCounter, DateTime.Now - nowTimer);
                                //distributor.ResumeIncoming();
                            }
                            break;

                        case SimCoreState.STOPPING:

                            long total = 0;
                            if (ServerOptions.UsePerformanceLog)
                            {
                                foreach (SimulatorExecutionInfo sei in simModelInfo.simulationExecutionModel.simulators)
                                {
                                    if (!simTimes.ContainsKey(sei.simulatorName))
                                    {
                                        continue;
                                    }
                                    total += simTimes[sei.simulatorName];
                                }
                                foreach (SimulatorExecutionInfo sei in simModelInfo.simulationExecutionModel.simulators)
                                {
                                    if (!simTimes.ContainsKey(sei.simulatorName))
                                    {
                                        continue;
                                    }
                                    StringBuilder b = new StringBuilder();
                                    b.AppendFormat("SimCore Metric: Total time in: {0} was {1} seconds which is {2}% of total simulator time.", sei.simulatorName, simTimes[sei.simulatorName] / 10000000.0, (((double)simTimes[sei.simulatorName]) / total) * 100.0);
                                    PerformanceLog.Write(b.ToString());
                                }
                            }
                            state = SimCoreState.UNINITIALIZED;
                            return;
                        }
                    }
                    Thread.Sleep(updateFrequency / 10);
                }
            }
            catch (ThreadAbortException) { }
            catch (Exception exc)
            {
                MessageBox.Show("An error '" + exc.Message + "' has occurred in the Simulation Server.\nPlease email the C:\\DDDErrorLog.txt file to Aptima customer support with a description of what you were doing at the time of the error.");
                ErrorLog.Write(exc.ToString() + "\n");
                throw new Exception();
            }
        }