private void updateTimer_Tick(object sender, EventArgs e) { if (server != null) { server.Update(); List <SimulationEvent> events = eventClient.GetEvents(); foreach (SimulationEvent ev in events) { switch (ev.eventType) { case "ServerState": HandleServerStateEvent(((StringValue)ev["MessageType"]).value, ((StringValue)ev["MessageText"]).value); break; case "PauseScenario": HandlePauseScenarioEvent(); break; case "ResumeScenario": HandleResumeScenarioEvent(); break; } } UpdateDMDisplay(); int time = server.CurrentTime; textBox1.Text = String.Format("{0:HH:mm:ss}", ExtractTime(time)); } }
public void StartTextChatServer() { try { List <SimulationEvent> incomingEvents = new List <SimulationEvent>(); server.Subscribe("TextChatRequest"); server.Subscribe("TimeTick"); server.Subscribe("CreateChatRoom"); server.Subscribe("AddToChatRoom"); server.Subscribe("CloseChatRoom"); isRunning = true; //This can be set false elsewhere, and end the loop. while (isRunning) { incomingEvents = server.GetEvents(); if (incomingEvents.Count != 0) { foreach (SimulationEvent se in incomingEvents) { switch (se.eventType) { case "TextChatRequest": TextChatRequest(se); break; case "TimeTick": currentTick = ((IntegerValue)se["Time"]).value; timeString = ((StringValue)se["SimulationTime"]).value; break; case "CreateChatRoom": CreateChatRoom(se); break; case "CloseChatRoom": CloseChatRoom(se); break; case "AddToChatRoom": AddToChatRoom(se); break; default: break; } } } Thread.Sleep(500); } } 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(); } }
public void StartHandshakeManager() { //started in a thread List <SimulationEvent> incomingEvents = new List <SimulationEvent>(); string eventType; //if (availableDMs.Count == 0) // return; //foreach (string s in availableDMs) lock (dmLock) { foreach (string s in allDMs.Keys) { dmToTerminalMap.Add(s, string.Empty); } } server.Subscribe("HandshakeGUIRegister"); server.Subscribe("HandshakeGUIRoleRequest"); server.Subscribe("HandshakeInitializeGUIDone"); server.Subscribe("ClientSideAssetTransferAllowed"); while (!stopLoop) { incomingEvents = server.GetEvents(); foreach (SimulationEvent e in incomingEvents) { eventType = e.eventType; switch (eventType) { case "HandshakeGUIRegister": GUIRegister(e); break; case "HandshakeGUIRoleRequest": GUIRoleRequest(e); break; case "HandshakeInitializeGUIDone": InitializeGUIDone(e); break; default: break; } } Thread.Sleep(200); } }
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(); } }
public void WriteToLog() { try { //SimulationEventDistributor dist = new SimulationEventDistributor(ref simModelInfo); //cc = new SimulationEventDistributorClient(); List <SimulationEvent> incomingEvents = new List <SimulationEvent>(); FileStream file = new FileStream(logPath, FileMode.Create); StreamWriter sr = new StreamWriter(file); sr.WriteLine("<Creator><Version>" + productVersion + "</Version><CompiledOn>" + compileDate + "</CompiledOn></Creator>"); actualTime = 0; //dist.RegisterClient(ref cc); foreach (string subString in simModelInfo.eventModel.events.Keys) { //cc.Subscribe(subString); //If the user wants the detailed log, or if the event is flagged with the shouldLog //bool value, it should subscribe to that event. Otherwise, don't subscribe. if ((logMode == "Detailed") || simModelInfo.eventModel.events[subString].shouldLog) { cc.Subscribe(subString); } } isRunning = true; while (isRunning) { //incomingEvents = cc.GetEvents(); incomingEvents = cc.GetEvents(); if (incomingEvents.Count != 0) { foreach (SimulationEvent se in incomingEvents) { if (se.eventType == "TimeTick") { actualTime = ((IntegerValue)se["Time"]).value; } ((IntegerValue)se["Time"]).value = actualTime; try { sr.WriteLine(SimulationEventFactory.XMLSerialize(se)); } catch (Exception exc) { ErrorLog.Write(String.Format("NONFATAL Serialize Error in ReplayLogger: {0}", se.eventType)); ErrorLog.Write(exc.ToString()); continue; } sr.Flush(); } incomingEvents.Clear(); } Thread.Sleep(100); } sr.Close(); file.Close(); //dist.RemoveClient(cc.id); //is this correct? //server.Disconnect(); } 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(); } }
public void Update() { if (scenarioState == ScenarioState.LOADING) { if (simEngine.ErrorLoadingScenario()) { SendServerStateEvent("SCENARIO_LOAD_FAILURE", "Error Loading Scenario."); this.ResetServer(); return; } if (simEngine.IsReady()) { scenarioState = ScenarioState.PAUSED; isReady = true; SendServerStateEvent("SCENARIO_LOAD_SUCCESS", ""); } } if (replayState == ScenarioState.LOADING) { if (simEngine.ErrorLoadingScenario()) { this.StopReplay(); SendServerStateEvent("REPLAY_LOAD_FAILURE", "Error Loading Replay."); return; } if (simEngine.IsReady()) { replayState = ScenarioState.PAUSED; //SendOutAvailablePlayers(); isReady = true; SendServerStateEvent("REPLAY_LOAD_SUCCESS", ""); } } currentTime = (simEngine.simCore.GetSimTime() / 1000); List <SimulationEvent> events = eventClient.GetEvents(); if (events.Count > 0) { foreach (SimulationEvent e in events) { switch (e.eventType) { case "PauseScenarioRequest": if (scenarioState == ScenarioState.RUNNING) { SimControlPauseScenario(); } break; case "ResumeScenarioRequest": if (scenarioState == ScenarioState.PAUSED) { SimControlPauseScenario(); } break; case "LoadScenarioRequest": if (scenarioState == ScenarioState.STOPPED) { SimControlLoadScenario(((StringValue)e["ScenarioPath"]).value, ((StringValue)e["GroupName"]).value, ((StringValue)e["OutputLogDir"]).value); } break; case "StopScenarioRequest": if (scenarioState != ScenarioState.STOPPED) { SimControlStopScenario(false); } break; case "SimCoreReady": SendOutAvailablePlayers(); break; case "GameSpeedRequest": double speed = 1.0; try { speed = ((DoubleValue)e["SpeedFactor"]).value; } catch (Exception ex) { continue; } this.SetReplaySpeed(speed); break; case "ForkReplayFinished": HandshakeManager.IsForkReplay = false; simEngine.SetGameSpeed(1); if (scenarioState == ScenarioState.RUNNING) { SimControlPauseScenario(); } break; case "ForkReplayStarted": HandshakeManager.IsForkReplay = true; simEngine.SetGameSpeed(ServerOptions.ForkReplaySpeed); break; } } } }
public void StartViewManagerLoop() { try { List <SimulationEvent> incomingEvents; foreach (string e in simModelInfo.eventModel.events.Keys) { server.Subscribe(e); } while (isRunning) { incomingEvents = server.GetEvents(); foreach (SimulationEvent e in incomingEvents) { //eventStreamViewer.AddEvent(e.eventType, ((IntegerValue)e.parameters["Time"]).value); switch (e.eventType) {//this should eventually just send handshake manager the event, and HM will determine //how to handle it. case "AuthenticationRequest": authenticationManager.AuthenticationRequest(e); break; case "HandshakeGUIRegister": handshakeManager.GUIRegister(e); break; case "HandshakeGUIRoleRequest": handshakeManager.GUIRoleRequest(e); break; case "HandshakeInitializeGUIDone": handshakeManager.InitializeGUIDone(e); authenticationManager.IncrementUsers(); break; case "Playfield": handshakeManager.SetPlayfieldInformation(e); break; //case "TextChat": // textChat.AddTextMessage(e); // break; case "DisconnectDecisionMaker": handshakeManager.DisconnectPlayer(e); if (((StringValue)e["DecisionMakerID"]).value != string.Empty) { authenticationManager.DecrementUsers(); //TODO: See if this is called as well as below } break; case "DisconnectTerminal": handshakeManager.DisconnectTerminal(e); //authenticationManager.DecrementUsers(); //TODO: See if this is called as well as above break; case "NewObject": if (((StringValue)e["ObjectType"]).value == "DecisionMaker") { handshakeManager.ReceiveDecisionMakerEvent(e); } break; case "CreateChatRoom": handshakeManager.CreateChatRoom(e); break; case "CloseChatRoom": handshakeManager.CloseChatRoom(e); break; case "CreateVoiceChannel": handshakeManager.CreateVoiceChannel(e); break; case "CloseVoiceChannel": handshakeManager.CloseVoiceChannel(e); break; case "CreateWhiteboardRoom": handshakeManager.CreateWhiteboardRoom(e); break; case "ClientSideAssetTransferAllowed": handshakeManager.SetAssetTransferFlag(e); break; case "StopScenario": authenticationManager.ResetUserCount(); break; default: break; } } Thread.Sleep(500); } } 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(); } }
public void SendEvents() { NetMessage m = new NetMessage(); List <SimulationEvent> events = null; int count = 0; while (true) { try { if (eventDistClient != null) { events = eventDistClient.GetEvents(); foreach (SimulationEvent e in events) { m.type = NetMessageType.EVENT; m.clientID = eventDistClient.id; try { m.msg = SimulationEventFactory.XMLSerialize(e); } catch (Exception exc) { ErrorLog.Write(String.Format("NONFATAL Serialize Error in NetworkServer: {0}", e.eventType)); ErrorLog.Write(exc.ToString()); continue; } m.Send(ref netStream, String.Empty); } Thread.Sleep(50); count += 1; if (count >= 100) { count = 0; m.type = NetMessageType.PING; m.clientID = eventDistClient.id; m.Send(ref netStream, String.Empty); } } } catch (System.IO.IOException exc) { ForceClose(); return; } catch (System.ObjectDisposedException) { return; } catch (ThreadAbortException) { return; } catch (Exception exc) { MessageBox.Show("An error has occured 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(); } } }