/// ======================================================================================================= public static Return UDP_LISTEN(string IP_address, string port, string duration_seconds) { List <CATMessage> msgs = new List <CATMessage>(); if (Listening || ListenerClient != null) { try { ListenerClient.Close(); } catch (Exception e) { CAT.FormAccess(CAT.FormDelegates.AddOverlay, new object[] { ErrorMsg(e), Status.FAIL }); msgs.Add(new CATMessage(ErrorMsg(e), Status.FAIL)); } } Listening = true; ListenerClient = new UdpClient(Convert.ToInt16(port)); ListenerEndPoint = new IPEndPoint(IPAddress.Any, Convert.ToInt16(port)); string receivedtext; byte[] receivebytearray; DateTime start = DateTime.Now; while ((DateTime.Now - start).TotalSeconds < Convert.ToInt32(duration_seconds) && Listening) { receivebytearray = ListenerClient.Receive(ref ListenerEndPoint); receivedtext = Encoding.ASCII.GetString(receivebytearray, 0, receivebytearray.Length); CAT.FormAccess(CAT.FormDelegates.AddOverlay, new object[] { receivedtext, Status.INFO }); msgs.Add(new CATMessage(receivedtext)); } ListenerClient.Close(); Listening = false; return(new Return(msgs)); }
// Utilities /// ======================================================================================================= public static bool GetInput(ref string upatedtext, string caption, List <string> options = null) { Form prompt = new Form() { Icon = (Icon)CAT.FormAccess(FormDelegates.GetIcon), Width = 610, Height = 100, BackColor = System.Drawing.Color.Black, FormBorderStyle = FormBorderStyle.FixedDialog, Text = caption, StartPosition = FormStartPosition.CenterParent, }; TextBox textBox = CAT.GetTextBox(upatedtext, left: 5, top: 5, width: 500); Button confirmation = CAT.GetButton("OK", left: 510, width: 80, top: 3); confirmation.Click += (sender, e) => { prompt.DialogResult = DialogResult.OK; prompt.Close(); }; if (options != null) { ComboBox cb = CAT.GetComboBox(upatedtext, options, left: 5, top: 30, width: 580); cb.SelectedValueChanged += new EventHandler((object o, EventArgs e) => { textBox.Text = cb.Text; }); prompt.Controls.Add(cb); } prompt.Controls.Add(textBox); prompt.Controls.Add(confirmation); prompt.AcceptButton = confirmation; DialogResult result = prompt.ShowDialog(); if (result == DialogResult.OK) { upatedtext = textBox.Text.Trim(); return(true); } return(false); }
/// ======================================================================================================= public void WaitForAuto() { bool complete = false; DateTime start = DateTime.Now; CAT.FormAccess(CAT.FormDelegates.AddNamedOverlay, new object[] { Alias, Alias, CAT.Status.BUSY }, Alias); while (!complete && !CAT.AbortAll) { complete = true; CAT.FormAccess(CAT.FormDelegates.UpdateNamedOverlay, new object[] { Alias, Alias + " (" + (int)(DateTime.Now - start).TotalMilliseconds + "ms)", CAT.Status.BUSY }, Alias); foreach (KeyValuePair <int, BatchReport> b in BatchReports) { if (!b.Value.BatchComplete) { complete = false; } foreach (KeyValuePair <Map, CommandReport> c in b.Value.CommandReports) { if (!c.Value.CommandComplete) { complete = false; } } Thread.Sleep(10); } } AutoComplete(); }
/// ======================================================================================================= public static Return UDP_SEND(string IP_address, string port, string text_to_send) { List <CATMessage> msgs = new List <CATMessage>(); Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); socket.SendTo(Encoding.ASCII.GetBytes(text_to_send), new IPEndPoint(IPAddress.Parse(IP_address), Convert.ToInt16(port))); socket.Close(); CAT.FormAccess(CAT.FormDelegates.AddOverlay, new object[] { text_to_send, Status.INFO }); return(new Return(msgs)); }
/// ======================================================================================================= private static void RunAuto(AutoReport auto_report, string auto_alias, string binary_name, out List <Control> interactive_controls) { List <Control> interactivecontrols = new List <Control>(); DataGridView autodata = Data.BlankDgv((int)CAT.AutoFields.NumberOfColumns); DataGridView batchdata = Data.BlankDgv((int)CAT.BatchFields.NumberOfColumns); string binary_path = DISK.AutoAddBaseDirectory(auto_alias + @"\" + binary_name); Data.LoadDataGrid(binary_path, ref autodata); FormAccess(FormDelegates.LoadAutoData, null, auto_alias); FormAccess(FormDelegates.ClearAutoStatuses, new object[] { }, auto_alias); foreach (DataGridViewRow autorow in autodata.Rows) { if (autorow.Index < autodata.RowCount - 1) { if (!AbortAll) { Data.LoadDataGrid(new FileInfo(binary_path).Directory.FullName + @"\batch" + autorow.Index + ".bin", ref batchdata); Action runbatch = new Action(() => { List <Control> ctrls; RunBatch(auto_report, auto_alias, autorow.Index, autodata, batchdata, out ctrls); if (ctrls != null) { foreach (Control ctrl in ctrls) { interactivecontrols.Add(ctrl); } } }); if (Regex.IsMatch(Data.GetCell(autodata, (int)CAT.AutoFields.Mode, autorow.Index), "-t")) { Task t = new Task(() => { runbatch(); }); AutoTasks.Add(t); t.Start(); } else { runbatch(); } } else { FormAccess(FormDelegates.SetBatchStatus, new object[] { Status.ABORT, autorow.Index }, auto_alias); } } } CAT.FormAccess(CAT.FormDelegates.SaveAuto, new object[] { }, auto_alias); interactive_controls = interactivecontrols; }
/// ======================================================================================================= public static void InteractiveShowReport(Func <Return> returndatafunction, double y_scale) { CAT.FormAccess(CAT.FormDelegates.FormClose, null, "Report"); AutoReport autoreport = new AutoReport(CAT.InteractiveControlsFormName); Map map = new Map(CAT.InteractiveControlsFormName, 0, 0); try { DateTime start = DateTime.Now; bool complete = false; InteractiveAddNamedOverlay("InteractiveProgress", "Started", Status.BUSY); new Thread(() => { while (!complete) { InteractiveUpdateNamedOverlay("InteractiveProgress", "Processing " + (DateTime.Now - start).TotalMilliseconds + "ms", Status.BUSY); Thread.Sleep(10); } InteractiveUpdateNamedOverlay("InteractiveProgress", "Complete " + (DateTime.Now - start).TotalMilliseconds + "ms", Status.PASS); }).Start(); autoreport.AddBatch(new BatchReport(0, "Batch")); autoreport.AddCommand(new CommandReport(map, "Command")); Return returndata = returndatafunction(); if (returndata.Charts != null) { autoreport.AddChartList(map, returndata.Charts); } if (returndata.Messages != null) { foreach (CATMessage msg in returndata.Messages) { autoreport.AddOverlayedMessage(map, msg.Text, msg.Status); } } autoreport.CommandComplete(map, CAT.Status.PASS); autoreport.BatchComplete(0); autoreport.AutoComplete(); CAT.ShowReport(autoreport, y_scale); complete = true; } catch (Exception ex) { InteractiveUpdateNamedOverlay("InteractiveProgress", "Failed control", Status.PASS); autoreport.AddOverlayedMessage(map, CAT.ErrorMsg(ex), CAT.Status.FAIL); try { CAT.ShowReport(autoreport, y_scale); } catch { } } }
// FEATURES /// ======================================================================================================= public static void RUN_AUTO(string auto_alias) { Running = false; // TODO hack to get passed recursion foreach (Form o in Application.OpenForms) { if (o.Name.Contains("CAT")) { ConstructorInfo ci = o.GetType().GetConstructor(new[] { typeof(string) }); object instance = ci.Invoke(new object[] { auto_alias }); Thread thread = new Thread(() => Application.Run((Form)instance)); thread.Start(); Thread.Sleep(2000); //AutoReport = new AutoReport(auto_alias); CAT.FormAccess(CAT.FormDelegates.RunAuto, null, auto_alias); } } }
/// ======================================================================================================= public void AutoComplete() { EndTime = DateTime.Now; if (BatchReports.Count > 0) { string reportfolder = DISK.AutoAddBaseDirectory(Alias) + @"\Reports\"; DISK.CreateFolder(reportfolder); string reportpath = reportfolder + "AutoReport.bin"; string timestampedreportpath = reportfolder + "AutoReport" + Timer.TimeStamp() + ".bin"; Save(reportpath); Save(timestampedreportpath); CAT.FormAccess(CAT.FormDelegates.UpdateNamedOverlay, new object[] { Alias, Alias + " complete (" + AutoDuration() + ")", CalculatedAutoResult() }, Alias); } else { CAT.FormAccess(CAT.FormDelegates.UpdateNamedOverlay, new object[] { Alias, "No report for" + Alias, CAT.Status.ABORT }, Alias); Enabled = false; } }
/// ======================================================================================================= public void UpdateOverlay(string overlayname, string message, CAT.Status status) { CAT.FormAccess(CAT.FormDelegates.UpdateNamedOverlay, new object[] { overlayname, message, status }, Alias); }
/// ======================================================================================================= public void AddOverlay(string message, CAT.Status status) { CAT.FormAccess(CAT.FormDelegates.AddNamedOverlay, new object[] { "autoReportOverlay" + overlaynumber++, message, status }, Alias); }
/// ======================================================================================================= public void BatchComplete(int b, CAT.Status status) { CAT.FormAccess(CAT.FormDelegates.UpdateNamedOverlay, new object[] { BatchAlias(b), BatchDescription(b) + " complete", status }, Alias); BatchReports[b].Complete(status); }
/// ======================================================================================================= public void CommandComplete(Map map, CAT.Status status) { CAT.FormAccess(CAT.FormDelegates.UpdateNamedOverlay, new object[] { CommandAlias(map), CommandDescription(map) + " complete", status }, Alias); BatchReports[map.B].CommandReports[map].Complete(status); }
/// ======================================================================================================= public void UpdateCommandProgress(Map map) { CAT.FormAccess(CAT.FormDelegates.UpdateNamedOverlay, new object[] { CommandAlias(map), CommandDescription(map) + " " + BatchReports[map.B].CommandReports[map].Duration(), CAT.Status.BUSY }, Alias); }
/// ======================================================================================================= public void BatchComplete(int batch, CAT.Status?status = null) { CAT.Status result = status == null ? BatchReports[batch].CalculatedResult() : (CAT.Status)status; CAT.FormAccess(CAT.FormDelegates.UpdateNamedOverlay, new object[] { BatchAlias(batch), BatchDescription(batch) + " complete", result }, Alias); BatchReports[batch].Complete(result); }
/// ======================================================================================================= private static void RunCommand(AutoReport auto_report, Map map, DataGridView batch_data, out List <Control> interactive_controls, List <Var> vars = null) { List <Control> interactivecontrols = null; int rep = 1; if (Regex.IsMatch(Data.GetCell(batch_data, (int)CAT.BatchFields.Mode, map.C), COMMAND_MODE_REPEAT)) { rep = Convert.ToInt16(Regex.Match(Data.GetCell(batch_data, (int)CAT.BatchFields.Mode, map.C), COMMAND_MODE_REPEAT).Value.Substring(1)); } if (Regex.IsMatch(Data.GetCell(batch_data, (int)CAT.BatchFields.Mode, map.C), COMMAND_MODE_THREAD_CATCH)) { auto_report.WaitForPreviousBatches(map); } Action runcommand = new Action(() => { for (int i = 1; i <= rep; i++) { DataGridViewRow cmd = batch_data.Rows[map.C]; string workdir = Data.GetCell(cmd, (int)CAT.BatchFields.Path); string command = Data.GetCell(cmd, (int)CAT.BatchFields.Command); Map newmap = new Map(map.A, map.B, map.C, rep > 1 ? i : 0); auto_report.AddCommand(new CommandReport(newmap, workdir + ">" + command)); Status status = Status.START; if (Data.IsCommandEnabled(cmd)) { if (vars != null) { string initialcommand = command; string initialworkdir = workdir; foreach (Var vartoinsert in vars) { command = command.Replace("{" + vartoinsert.Name + "}", vartoinsert.Value); workdir = workdir.Replace("{" + vartoinsert.Name + "}", vartoinsert.Value); } command = BasicArithmetic(command); if (initialcommand != command || initialworkdir != workdir) { auto_report.AddOverlayedMessage(newmap, "Variable updates (Workdir>Command): " + workdir + ">" + command, Status.WARN); } } if (string.IsNullOrWhiteSpace(Data.GetCell(cmd, (int)CAT.BatchFields.Command))) { auto_report.AddOverlayedMessage(newmap, "Command is null", Status.FAIL); status = Status.FAIL; } else if (string.IsNullOrWhiteSpace(Data.GetCell(cmd, (int)CAT.BatchFields.Timeout))) { auto_report.AddOverlayedMessage(newmap, "Timeout is null", Status.FAIL); status = Status.FAIL; } else { CAT.FormAccess(CAT.FormDelegates.SetCommandStatus, new object[] { Status.BUSY, newmap.C }, newmap.A); if (Regex.IsMatch(Data.GetCell(cmd, (int)CAT.BatchFields.Path), @".*\.dll")) { status = RunDll(newmap, workdir, command, Data.GetCmdTimeout(cmd), out interactivecontrols, auto_report); } else { status = RunCmd(auto_report, newmap, "cmd.exe", workdir, command, Data.GetCmdTimeout(cmd), "/C"); } } } else { status = Status.SKIP; } auto_report.CommandComplete(newmap, status); CAT.FormAccess(CAT.FormDelegates.SetCommandStatus, new object[] { status, newmap.C }, newmap.A); } }); if (Regex.IsMatch(Data.GetCell(batch_data, (int)CAT.BatchFields.Mode, map.C), "-t")) { Task t = new Task(() => { runcommand(); }); CommandTasks.Add(t); t.Start(); } else { runcommand(); } interactive_controls = interactivecontrols; }
/// ======================================================================================================= private static void RunBatch(AutoReport auto_report, string auto_alias, int batch_number, DataGridView auto_data, DataGridView batch_data, out List <Control> interactive_controls, int?command_number = null) { List <Control> interactivecontrols = new List <Control>(); int rep = 1; if (Regex.IsMatch(Data.GetCell(auto_data, (int)CAT.AutoFields.Mode, batch_number), COMMAND_MODE_REPEAT)) { rep = Convert.ToInt16(Regex.Match(Data.GetCell(auto_data, (int)CAT.AutoFields.Mode, batch_number), COMMAND_MODE_REPEAT).Value.Substring(1)); } for (int i = 0; i < rep; i++) { Action runbatch = new Action(() => { FormAccess(FormDelegates.SelectAutoRow, new object[] { batch_number }, auto_alias); FormAccess(FormDelegates.LoadBatchData, new object[] { batch_number }, auto_alias); FormAccess(FormDelegates.ClearBatchStatuses, new object[] { }, auto_alias); auto_report.AddBatch(new BatchReport(batch_number, Data.GetCell(auto_data, (int)CAT.AutoFields.Name, batch_number))); Status?status = null; if (Data.IsBatchEnabled(auto_data, batch_number) || command_number != null) { CAT.FormAccess(CAT.FormDelegates.SetBatchStatus, new object[] { Status.BUSY, batch_number }, auto_alias); foreach (DataGridViewRow commandrow in batch_data.Rows) { if (!AbortAll && ((command_number != null && command_number == commandrow.Index) || command_number == null) && commandrow.Index < batch_data.RowCount - 1) { FormAccess(FormDelegates.SelectBatchRow, new object[] { commandrow.Index }, auto_alias); List <Control> ctrls; RunCommand(auto_report, new Map(auto_alias, batch_number, commandrow.Index), batch_data, out ctrls, GetAutoVars(auto_data, batch_number, auto_alias)); if (ctrls != null) { foreach (Control ctrl in ctrls) { interactivecontrols.Add(ctrl); } } } } status = auto_report.CalculatedBatchResult(batch_number); } else { status = Status.SKIP; } CAT.FormAccess(CAT.FormDelegates.SetBatchStatus, new object[] { status, batch_number }, auto_alias); auto_report.BatchComplete(batch_number, status); }); if (Regex.IsMatch(Data.GetCell(auto_data, (int)CAT.AutoFields.Mode, batch_number), "-t")) { Task t = new Task(() => { runbatch(); }); BatchTasks.Add(t); t.Start(); } else { runbatch(); CAT.FormAccess(CAT.FormDelegates.SaveSelectedBatch, new object[] { }, auto_alias); } } interactive_controls = interactivecontrols; }
// Basic Abstractions /// ======================================================================================================= public static void AddOverlay(string auto_alias, string msg, Status status = Status.FAIL) { CAT.FormAccess(CAT.FormDelegates.AddOverlay, new object[] { msg, status }, auto_alias); }
/// ======================================================================================================= public void UpdateBatchProgress(int b) { CAT.FormAccess(CAT.FormDelegates.UpdateNamedOverlay, new object[] { BatchAlias(b), BatchDescription(b) + " " + BatchReports[b].Duration(), CAT.Status.BUSY }, Alias); }