//using PleoStreamSimulatorEditor.Properties; using PleoLab.Properties; using System; using System.Diagnostics; using System.Drawing; using System.Threading.Tasks; using System.Windows.Forms; namespace PleoLab { public partial class StartSimulationForm : Form { private string m_pathToSimulator = JD.Core.DataRoot.Value.Remove(JD.Core.DataRoot.Value.LastIndexOf("\\")) + "\\engine\\Tools\\PleoStreamSimulator\\PleoStreamSimulator.exe"; private bool m_openReportAfterSimu = false; private Process m_simulator; private Stopwatch m_watch; private const string START_BUTTON_NAME = "Start Simulation"; private const string CANCEL_BUTTON_NAME = "Cancel Simulation"; public StartSimulationForm(Point _parentWindowPosition) { InitializeComponent(); this.Location = new Point(_parentWindowPosition.X - Width / 2, _parentWindowPosition.Y - Height / 2); simuArgsTextBox.Text = "Simulation arguments line"; m_openReportAfterSimu = openReportAfterSimuCheckBox.Checked; videoFormatComboBox.SelectedIndex = 0; } private void StartSimulationInfos_Load(object sender, EventArgs e) { textBoxMapsListPath.Text = Settings.Default.MapsFilePath; textBoxMapName.Text = Settings.Default.MapName; textBoxBandwidthSpeedMax.Text = Settings.Default.BandwidthMaxSpeed; textBoxBandwidthCurvesPath.Text = Settings.Default.BandwidthCurvesFilePath; textBoxSimulationReportFilePath.Text = Settings.Default.SimulationReportFilePath; nbOfBandwidthVariations.Value = Settings.Default.NbOfBandwidthVariations; openReportAfterSimuCheckBox.Checked = Settings.Default.OpenReportAfterSimu; videoFormatComboBox.SelectedIndex = Settings.Default.VideoFormat; } private void StartSimulation() { SaveArguments(); startSimulationButton.Text = CANCEL_BUTTON_NAME; CommandArguments arguments = new CommandArguments(); arguments.Set("MapListPath", textBoxMapsListPath.Text); arguments.Set("MapName", textBoxMapName.Text); arguments.Set("NetworkBandwidthSpeed", textBoxBandwidthSpeedMax.Text); arguments.Set("BandwidthCurvesFilePath", textBoxBandwidthCurvesPath.Text); arguments.Set("SimulationReportFilePath", textBoxSimulationReportFilePath.Text); arguments.Set("NbOfBandwidthVariation", nbOfBandwidthVariations.Value); arguments.Set("VideoFormat", videoFormatComboBox.SelectedIndex); simuArgsTextBox.Text = arguments.ToString(); richTextBoxConsole.Text = ""; m_simulator = new Process { StartInfo = new ProcessStartInfo { FileName = m_pathToSimulator, Arguments = arguments.ToString(), UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = true } }; m_simulator.EnableRaisingEvents = true; m_simulator.Exited += new EventHandler((object s, System.EventArgs args) => { if (IsHandleCreated) { this.Invoke(new Action(() => { m_watch.Stop(); startSimulationButton.Text = START_BUTTON_NAME; MessageBox.Show(this, "Simulation ended.", "", MessageBoxButtons.OK); })); if (m_openReportAfterSimu) { this.Invoke(new Action(() => { OpenFileInTool(); })); m_openReportAfterSimu = openReportAfterSimuCheckBox.Checked; } } }); try { m_watch = Stopwatch.StartNew(); m_simulator.Start(); } catch(Exception e) { MessageBoxButtons buttons = MessageBoxButtons.OK; DialogResult msgBoxResult = MessageBox.Show("An error occured when launching PleoStreamSimulator.exe :\n" + e.Message + "\n\nIs the path to the exe correct ? " + m_pathToSimulator + "\n", "Error !", buttons); m_simulator = null; m_watch.Stop(); startSimulationButton.Text = START_BUTTON_NAME; return; } // Update simulation duration value Task.Factory.StartNew(() => { while (m_watch.IsRunning) { this.Invoke(new Action(() => { TimeSpan ts = m_watch.Elapsed; string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); labelSimulationDuration.Text = "Simulation duration : " + elapsedTime; })); } }); // Reroute simulation standard output to this window's textbox widget Task.Factory.StartNew(() => { while (!m_simulator.StandardOutput.EndOfStream) { string line = m_simulator.StandardOutput.ReadLine(); this.Invoke(new Action(() => { richTextBoxConsole.AppendText("\n" + line); })); } }); // Reroute simulation error output to this window's textbox widget Task.Factory.StartNew(() => { while (!m_simulator.StandardError.EndOfStream) { string line = m_simulator.StandardError.ReadLine(); this.Invoke(new Action(() => { richTextBoxConsole.AppendText("\n" + line); })); } }); } private void CancelSimulation() { if (m_simulator != null && !m_simulator.HasExited) { MessageBoxButtons buttons = MessageBoxButtons.YesNo; DialogResult msgBoxResult = MessageBox.Show("Cancel the simulation ?", "Warning !", buttons); if (msgBoxResult == DialogResult.Yes) { m_openReportAfterSimu = false; m_simulator.Kill(); } } } private void startSimulationButton_Click(object sender, EventArgs e) { if (startSimulationButton.Text == START_BUTTON_NAME) { StartSimulation(); } else if (startSimulationButton.Text == CANCEL_BUTTON_NAME) { CancelSimulation(); } } protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { if (keyData == Keys.Escape) { this.Close(); return true; } return base.ProcessCmdKey(ref msg, keyData); } private void StartSimulationInfos_FormClosing(object sender, FormClosingEventArgs e) { if (m_simulator != null && !m_simulator.HasExited) { MessageBoxButtons buttons = MessageBoxButtons.YesNo; DialogResult msgBoxResult = MessageBox.Show("Closing this window will cancel the simulation. Are you sure you want to proceed ?", "Warning !", buttons); if (msgBoxResult == DialogResult.Yes) { m_openReportAfterSimu = false; m_simulator.Kill(); } else if (msgBoxResult == DialogResult.No) { e.Cancel = true; } } SaveArguments(); } private void SaveArguments() { Settings.Default.MapsFilePath = textBoxMapsListPath.Text; Settings.Default.MapName = textBoxMapName.Text; Settings.Default.BandwidthMaxSpeed = textBoxBandwidthSpeedMax.Text; Settings.Default.BandwidthCurvesFilePath = textBoxBandwidthCurvesPath.Text; Settings.Default.SimulationReportFilePath = textBoxSimulationReportFilePath.Text; Settings.Default.NbOfBandwidthVariations = (uint)nbOfBandwidthVariations.Value; Settings.Default.OpenReportAfterSimu = openReportAfterSimuCheckBox.Checked; Settings.Default.VideoFormat = videoFormatComboBox.SelectedIndex; Settings.Default.Save(); } private void OpenFileInTool() { try { PleoLabForm tool = this.Owner as PleoLabForm; tool.SimulationReadFile(textBoxSimulationReportFilePath.Text); } catch (Exception exc) { MessageBoxButtons buttons = MessageBoxButtons.OK; MessageBox.Show("Something went wrong :\n" + exc.Message, "Error while parsing the file", buttons); } } private void openReportAfterSimuCheckBox_CheckedChanged(object sender, EventArgs e) { m_openReportAfterSimu = openReportAfterSimuCheckBox.Checked; } } }