public static void Coordinate( string scenarioFile, string schemaFile, ref SimulationEventDistributor distributor, //string hostname, //string portString, string simModelName, string contextControl,//*NETWORK" string updateIncr, string lowerLevel, //GUI List<string> logTypes, string debugFile ) { /// <summary> debugLogger = new DebugLogger(); //DateTime dt = DateTime.Now; if (!Directory.Exists(debugFile.Remove(debugFile.LastIndexOf("\\")))) { Directory.CreateDirectory(debugFile.Remove(debugFile.LastIndexOf("\\"))); } DebugLogger.SetDebugStyleFile(debugFile); //DebugLogger.SetDebugStyle(DebugLogger.DebugStyleValues.FileReporting); DebugLogger.SetLoggingType("general", true); foreach (string s in logTypes) { DebugLogger.SetLoggingType(s, true); debugLogger.Writeline("Coordinator", s + " is being recorded.", "general"); } // DebugLogger.SetLoggingType("all", false); debugLogger.Writeline("Coordinator", "Hello", "general"); //int port = int.Parse(portString); SimulationModelReader smr = new SimulationModelReader(); SimulationModelInfo simModelInfo = smr.readModel(simModelName); if (distributor == null) { distributor = new SimulationEventDistributor(ref simModelInfo); } //SimulationEventDistributor dist = new SimulationEventDistributor(ref simModelInfo); //SimulationEventDistributorClient cc = new SimulationEventDistributorClient(); updateIncrement = simModelInfo.GetUpdateFrequency(); int enteredIncrement = Int32.Parse(updateIncr); if (enteredIncrement > 0) { updateIncrement = enteredIncrement; } tickController.UpdateIncrement = updateIncrement; try { new ScenarioToQueues(scenarioFile, schemaFile); new ForkReplayToQueues(replayFile,simModelInfo); } catch (System.Exception f) { if (f.Message.StartsWith("User Cancelled")) {//This means a missing map or icon library, and the user wanted to stop the server. Do not write to error log, just stop the server. throw f; } throw new ApplicationException("Failure in ScenarioToQueues: " + f.Message); } //NetworkClient c = new NetworkClient(); SimulationEventDistributorClient distClient = new SimulationEventDistributorClient(); distributor.RegisterClient(ref distClient); try { //c.Connect(hostname, port); EventCommunicator eventCommunicator = new EventCommunicator(ref distClient, simModelName); eCommReceiver = new Thread(new ThreadStart(eventCommunicator.WaitForEvents)); eCommReceiver.CurrentCulture = new System.Globalization.CultureInfo("en-US", true); eCommReceiver.Start(); } catch (System.Exception e) { //Coordinator.debugLogger.WriteLine("Unable to connect"); Coordinator.debugLogger.Writeline("Coordinator", "Error in startup: System message: "+e.Message, "general"); MessageBox.Show("Startup error: " + e.Message); Application.Exit(); } EventCommunicator.SendSimStartEvent(); //dist.RegisterClient(ref cc); /* * Temporary event watcher to allow for insertion of events from below */ if (lowerLevel != "GUI") { //NetworkClient client = new NetworkClient(); //client.Connect(hostname, port); /* // Tickwatcher is used only to simulate events from lower levels TickWatcher sink = new TickWatcher(client, simModelName); ThreadStart stub = new ThreadStart(sink.TickEventGetter); Thread stubThread = new Thread(stub); stubThread.Start(); */ } TimerQueueClass.SendImmediates(); if ("NETWORK" == contextControl) { while (!readyToTick) { Thread.Sleep(1000); } tickController.SetCallback(new Metronome.SendTimeTick(SendTimeTick)); tickController.Start(); while (true) { while (pause) { Thread.Sleep(1000); } timeSlice = (timeSlice + 1) % TimeSliceIncrement; //next tick will have 100ms between calls, will not handle sending time ticks, just recent incoming events. //a callback will handle sending time ticks TimerTicker.NextTimeSlice(); Thread.Sleep((int)((updateIncrement / TimeSliceIncrement) / speedFactor)); } } Coordinator.debugLogger.Writeline("Coordinator", "The End", "general"); }
public static void Coordinate( string scenarioFile, string schemaFile, ref SimulationEventDistributor distributor, //string hostname, //string portString, string simModelName, string contextControl, //*NETWORK" string updateIncr, string lowerLevel, //GUI List <string> logTypes, string debugFile ) { /// <summary> debugLogger = new DebugLogger(); //DateTime dt = DateTime.Now; if (!Directory.Exists(debugFile.Remove(debugFile.LastIndexOf("\\")))) { Directory.CreateDirectory(debugFile.Remove(debugFile.LastIndexOf("\\"))); } DebugLogger.SetDebugStyleFile(debugFile); //DebugLogger.SetDebugStyle(DebugLogger.DebugStyleValues.FileReporting); DebugLogger.SetLoggingType("general", true); foreach (string s in logTypes) { DebugLogger.SetLoggingType(s, true); debugLogger.Writeline("Coordinator", s + " is being recorded.", "general"); } // DebugLogger.SetLoggingType("all", false); debugLogger.Writeline("Coordinator", "Hello", "general"); //int port = int.Parse(portString); SimulationModelReader smr = new SimulationModelReader(); SimulationModelInfo simModelInfo = smr.readModel(simModelName); if (distributor == null) { distributor = new SimulationEventDistributor(ref simModelInfo); } //SimulationEventDistributor dist = new SimulationEventDistributor(ref simModelInfo); //SimulationEventDistributorClient cc = new SimulationEventDistributorClient(); updateIncrement = simModelInfo.GetUpdateFrequency(); int enteredIncrement = Int32.Parse(updateIncr); if (enteredIncrement > 0) { updateIncrement = enteredIncrement; } tickController.UpdateIncrement = updateIncrement; try { new ScenarioToQueues(scenarioFile, schemaFile); new ForkReplayToQueues(replayFile, simModelInfo); } catch (System.Exception f) { if (f.Message.StartsWith("User Cancelled")) {//This means a missing map or icon library, and the user wanted to stop the server. Do not write to error log, just stop the server. throw f; } throw new ApplicationException("Failure in ScenarioToQueues: " + f.Message); } //NetworkClient c = new NetworkClient(); SimulationEventDistributorClient distClient = new SimulationEventDistributorClient(); distributor.RegisterClient(ref distClient); try { //c.Connect(hostname, port); EventCommunicator eventCommunicator = new EventCommunicator(ref distClient, simModelName); eCommReceiver = new Thread(new ThreadStart(eventCommunicator.WaitForEvents)); eCommReceiver.CurrentCulture = new System.Globalization.CultureInfo("en-US", true); eCommReceiver.Start(); } catch (System.Exception e) { //Coordinator.debugLogger.WriteLine("Unable to connect"); Coordinator.debugLogger.Writeline("Coordinator", "Error in startup: System message: " + e.Message, "general"); MessageBox.Show("Startup error: " + e.Message); Application.Exit(); } EventCommunicator.SendSimStartEvent(); //dist.RegisterClient(ref cc); /* * Temporary event watcher to allow for insertion of events from below */ if (lowerLevel != "GUI") { //NetworkClient client = new NetworkClient(); //client.Connect(hostname, port); /* * // Tickwatcher is used only to simulate events from lower levels * TickWatcher sink = new TickWatcher(client, simModelName); * ThreadStart stub = new ThreadStart(sink.TickEventGetter); * Thread stubThread = new Thread(stub); * stubThread.Start(); */ } TimerQueueClass.SendImmediates(); if ("NETWORK" == contextControl) { while (!readyToTick) { Thread.Sleep(1000); } tickController.SetCallback(new Metronome.SendTimeTick(SendTimeTick)); tickController.Start(); while (true) { while (pause) { Thread.Sleep(1000); } timeSlice = (timeSlice + 1) % TimeSliceIncrement; //next tick will have 100ms between calls, will not handle sending time ticks, just recent incoming events. //a callback will handle sending time ticks TimerTicker.NextTimeSlice(); Thread.Sleep((int)((updateIncrement / TimeSliceIncrement) / speedFactor)); } } Coordinator.debugLogger.Writeline("Coordinator", "The End", "general"); }