//======================================================================== public static SymCommand Parse(string dat) { char[] seperators = { '~' }; SymCommand cmd = new SymCommand(); cmd.data = dat; if ((dat.IndexOf("~") != -1) && (dat.IndexOf('\u00FD') == -1)) { Match match = cmdPattern.Match(dat); cmd.command = match.Groups[1].Value; string[] sep = dat.Substring(cmd.command.Length + 1).Split(seperators); for (int i = 0; i < sep.Length; i++) { int eqPos = sep[i].IndexOf("="); if (eqPos != -1) { cmd.SetParam(sep[i].Substring(0, eqPos), sep[i].Substring(eqPos + 1)); } else { cmd.SetParam(sep[i], ""); } } } else { cmd.command = dat; } return(cmd); }
public static SymCommand Parse(string message) { var symCommand = new SymCommand(); if (!message.Contains("~") || message.Contains("\u00FD")) { symCommand.Command = message; } else { string[] tokens = message.Split(Delimiter); symCommand.Command = tokens[0]; for (int i = 1; i < tokens.Length; i++) { List<string> pair = tokens[i].Split('=').ToList(); // Add default 'value' for parameter // if not specified if (pair.Count == 1) pair.Add(""); symCommand.Set(pair[0], pair[1]); } } return symCommand; }
public List <File> FileList(string pattern, FileType type) { var files = new List <File>(); ISymCommand cmd = new SymCommand("File"); cmd.Set("Type", Utilities.FileTypeString(type)); cmd.Set("Name", pattern); cmd.Set("Action", "List"); _socket.Write(cmd); while (true) { cmd = _socket.ReadCommand(); if (cmd.HasParameter("Status")) { break; } if (cmd.HasParameter("Name")) { files.Add(new File(_socket.Server, SymDirectory.ToString(), cmd.Get("Name"), type, cmd.Get("Date"), cmd.Get("Time"), int.Parse(cmd.Get("Size")))); } if (cmd.HasParameter("Done")) { break; } } return(files); }
//======================================================================== public static SymCommand Parse(string dat) { char[] seperators = {'~'}; SymCommand cmd = new SymCommand(); cmd.data = dat; if((dat.IndexOf("~") != -1) && (dat.IndexOf('\u00FD') == -1)) { Match match = cmdPattern.Match(dat); cmd.command = match.Groups[1].Value; string[] sep = dat.Substring(cmd.command.Length + 1).Split(seperators); for(int i=0; i<sep.Length; i++) { int eqPos = sep[i].IndexOf("="); if(eqPos != -1) cmd.SetParam(sep[i].Substring(0,eqPos), sep[i].Substring(eqPos+1)); else cmd.SetParam(sep[i],""); } } else cmd.command = dat; return cmd; }
public static SymCommand Parse(string message) { var symCommand = new SymCommand(); if (!message.Contains("~") || message.Contains("\u00FD")) { symCommand.Command = message; } else { string[] tokens = message.Split(Delimiter); symCommand.Command = tokens[0]; for (int i = 1; i < tokens.Length; i++) { List <string> pair = tokens[i].Split('=').ToList(); // Add default 'value' for parameter // if not specified if (pair.Count == 1) { pair.Add(""); } symCommand.Set(pair[0], pair[1]); } } return(symCommand); }
private List <int> GetPrintSequences(string searchTerm) { var seqs = new List <int>(); ISymCommand cmd; cmd = new SymCommand("File"); cmd.Set("Action", "List"); cmd.Set("MaxCount", "50"); cmd.Set("Query", "LAST 20 \"+" + searchTerm + "+\""); cmd.Set("Type", "Report"); _socket.Write(cmd); cmd = _socket.ReadCommand(); while (!cmd.HasParameter("Done")) { if (cmd.HasParameter("Sequence")) { seqs.Add(int.Parse(cmd.Get("Sequence"))); } cmd = _socket.ReadCommand(); } seqs.Sort(); seqs.Reverse(); return(seqs); }
public void FileRename(string oldName, string newName, FileType type) { ISymCommand cmd = new SymCommand("File"); cmd.Set("Action", "Rename"); cmd.Set("Type", Utilities.FileTypeString(type)); cmd.Set("Name", oldName); cmd.Set("NewName", newName); _socket.Write(cmd); cmd = _socket.ReadCommand(); if (cmd.HasParameter("Status")) { if (cmd.Get("Status").IndexOf("No such file or directory") != -1) { throw new FileNotFoundException(); } else { throw new Exception("Filename Too Long"); } } if (cmd.HasParameter("Done")) { return; } throw new Exception("Unknown Renaming Error"); }
public ISymCommand ReadCommand() { _data += Read(); if (!string.IsNullOrEmpty(_data)) { string commandStart = Encoding.ASCII.GetString(new byte[] { 0x1b, 0xfe }); string commandEnd = Encoding.ASCII.GetString(new byte[] { 0xfc }); int start = _data.IndexOf(commandStart, StringComparison.Ordinal); int end = _data.IndexOf(commandEnd, start + commandStart.Length, StringComparison.Ordinal); while (start >= 0 && end >= start + commandStart.Length) { int commandLength = end - start - commandStart.Length; string commandString = _data.Substring(start + commandStart.Length, commandLength); SymCommand newCommand = SymCommand.Parse(commandString); _data = _data.Substring(start + commandStart.Length + commandString.Length); int dataLength = _data.Length; if (newCommand.Command == "File") { int nextStart = _data.IndexOf(commandStart, StringComparison.Ordinal); if (nextStart > 2) { newCommand.Data = _data.Substring(0, nextStart - 2); _data = _data.Substring(nextStart); } } _commands.Add(newCommand); start = _data.IndexOf(commandStart, StringComparison.Ordinal); end = _data.IndexOf(commandEnd, start + commandStart.Length, StringComparison.Ordinal); } } if (_commandIndex + 1 == _commands.Count) { _commands.Clear(); _commandIndex = -1; } if (_commands.Count == 0) { return(SymCommand.Parse("")); } ISymCommand cmd = _commands[++_commandIndex]; if ((cmd.Command == "MsgDlg") && (cmd.HasParameter("Text"))) { if (cmd.Get("Text").IndexOf("From PID", StringComparison.Ordinal) != -1) { cmd = ReadCommand(); } } return(cmd); }
public string FileRead(string name, FileType type) { var content = new StringBuilder(); ISymCommand cmd = new SymCommand("File"); cmd.Set("Action", "Retrieve"); cmd.Set("Type", Utilities.FileTypeString(type)); cmd.Set("Name", name); _socket.Write(cmd); while (true) { cmd = _socket.ReadCommand(); if (cmd.HasParameter("Status")) { string status = cmd.Get("Status"); if (status.Contains("No such file or directory")) { throw new FileNotFoundException(); } if (status.Contains("Cannot view a blank report")) { return(""); } throw new Exception("Filename Too Long"); } string chunk = cmd.Data; if (!string.IsNullOrEmpty(chunk)) { content.Append(chunk); if (type == FileType.Report) { content.Append('\n'); } } if (cmd.HasParameter("Done")) { break; } } return(content.ToString()); }
private int GetOpenQueue(Dictionary <int, int> availableQueues) { int queue; ISymCommand cmd = new SymCommand(); if (availableQueues == null || availableQueues.Count == 0) { availableQueues = GetQueueList(cmd); } //get queue counts cmd = new SymCommand("Misc"); cmd.Set("InfoType", "BatchQueues"); _socket.Write(cmd); cmd = _socket.ReadCommand(); while (!cmd.HasParameter("Done")) { if ((cmd.Get("Action") == "QueueEntry") && (cmd.Get("Stat") == "Running")) { availableQueues[int.Parse(cmd.Get("Queue"))]++; } cmd = _socket.ReadCommand(); } // Get queue with lowest pending int lowestPending = 0; queue = 0; foreach (var Q in availableQueues) { if (Q.Value == 0) { return(Q.Key); } if (Q.Value < lowestPending) { lowestPending = Q.Value; queue = Q.Key; } } return(queue); }
public void FileDelete(string name, FileType type) { ISymCommand cmd = new SymCommand("File"); cmd.Set("Action", "Delete"); cmd.Set("Type", Utilities.FileTypeString(type)); cmd.Set("Name", name); _socket.Write(cmd); cmd = _socket.ReadCommand(); if (cmd.HasParameter("Status")) { if (cmd.Get("Status").IndexOf("No such file or directory") != -1) throw new FileNotFoundException(); else throw new Exception("Filename Too Long"); } if (cmd.HasParameter("Done")) return; throw new Exception("Unknown Deletion Error"); }
public bool IsFileRunning(int sequence) { if (sequence <= 0) { throw new ArgumentOutOfRangeException("sequence"); } ISymCommand cmd; bool running = false; cmd = new SymCommand("Misc"); cmd.Set("InfoType", "BatchQueues"); _socket.Write(cmd); cmd = _socket.ReadCommand(); while (!cmd.HasParameter("Done")) { if ((cmd.Get("Action") == "QueueEntry") && (int.Parse(cmd.Get("Seq")) == sequence)) { running = true; } else if (cmd.Get("Action") == "Close") { Reconnect(); cmd = new SymCommand("Misc"); cmd.Set("InfoType", "BatchQueues"); _socket.Write(cmd); } else if (cmd.Command == "") { cmd = new SymCommand("Misc"); cmd.Set("InfoType", "BatchQueues"); _socket.Write(cmd); } cmd = _socket.ReadCommand(); } return(running); }
public RepgenRunResult FileRun(File file, FileRunStatus callStatus, FileRunPrompt callPrompt, int queue, RunWorkerCompletedEventHandler Notify = null) { if (file.Type != FileType.RepGen) { throw new InvalidOperationException("Cannot run a " + file.FileTypeString() + " file"); } ISymCommand cmd; callStatus(RunState.Initializing, file); _socket.Write("mm0\u001B"); WaitForCommand("Input"); _socket.Write("1\r"); WaitForCommand("Input"); _socket.Write("11\r"); WaitForPrompt("Specification File"); _socket.Write(file.Name + "\r"); bool erroredOut = false; while (true) { cmd = _socket.ReadCommand(); if (cmd.Command == "Input") { if (cmd.Get("HelpCode") == "20301") { break; } callStatus(RunState.Prompts, file); string result = callPrompt(cmd.Get("Prompt")); if (result == null) //cancelled { _socket.Write("\u001B"); cmd = _socket.ReadCommand(); while (cmd.Command != "Input") { cmd = _socket.ReadCommand(); } callStatus(RunState.Cancelled, file); return(RepgenRunResult.Cancelled()); } _socket.Write(result.Trim() + '\r'); } else if (cmd.Command == "Bell") { callStatus(RunState.Prompts, "Invalid Prompt Input, Please Re-Enter"); } else if ((cmd.Command == "Batch") && (cmd.Get("Text") == "No such file or directory")) { cmd = _socket.ReadCommand(); while (cmd.Command != "Input") { cmd = _socket.ReadCommand(); } callStatus(RunState.Failed, "File not found"); return(RepgenRunResult.FileNotFound()); } else if (cmd.Command == "SpecfileErr") { erroredOut = true; } else if (erroredOut && (cmd.Command == "Batch") && (cmd.Get("Action") == "DisplayLine")) { string err = cmd.Get("Text"); cmd = _socket.ReadCommand(); while (cmd.Command != "Input") { cmd = _socket.ReadCommand(); } callStatus(RunState.Failed, err); return(RepgenRunResult.Error(err)); } else if ((cmd.Command == "Batch") && (cmd.Get("Action") == "DisplayLine")) { callStatus(RunState.Initializing, cmd.Get("Text")); } } while (cmd.Get("Prompt").Contains("Specification File")) { _socket.Write("\r"); cmd = _socket.ReadCommand(); } WaitForPrompt("Batch Options"); _socket.Write("0\r"); Dictionary <int, int> availableQueues = GetQueueList(cmd); if (queue < 0) { queue = GetOpenQueue(availableQueues); } WaitForPrompt("Batch Queue"); _socket.Write(queue + "\r"); WaitForCommand("Input"); _socket.Write("1\r"); cmd = _socket.ReadCommand(); while (cmd.Command != "Input") { cmd = _socket.ReadCommand(); } cmd = new SymCommand("Misc"); cmd.Set("InfoType", "BatchQueues"); _socket.Write(cmd); int newestTime = 0; int sequenceNo = -1; cmd = _socket.ReadCommand(); while (!cmd.HasParameter("Done")) { if (cmd.Get("Action") == "QueueEntry") { int currTime = Utilities.ConvertTime(cmd.Get("Time")); if (currTime >= newestTime) { newestTime = currTime; sequenceNo = int.Parse(cmd.Get("Seq")); } } cmd = _socket.ReadCommand(); } callStatus(RunState.Running, sequenceNo); if (Notify != null) { var worker = new BackgroundWorker(); worker.DoWork += (sender, eventArgs) => { Thread.Sleep(5000); // Wait 5 seconds before first check while (IsFileRunning(sequenceNo)) { Thread.Sleep(15000); } object[] result = new object[3]; result[0] = file.Name; result[1] = sequenceNo; result[2] = GetBatchOutputSequence(file.Name, newestTime); eventArgs.Result = result; }; worker.RunWorkerCompleted += Notify; worker.RunWorkerAsync(); } return(RepgenRunResult.Okay(sequenceNo, newestTime)); }
private void Write(SymCommand cmd, int timeout) { Write(cmd.Packet(), timeout); }
public ReportInfo FMRun(string reportName, FileMaintenanceType fmtype, FileRunStatus callStatus, int queue, RunWorkerCompletedEventHandler Notify = null) { callStatus(RunState.Initializing, reportName); ISymCommand cmd; string outTitle = "FM - " + new Random().Next(8388608).ToString("D7"); _socket.Write("mm0\u001B"); cmd = WaitForCommand("Input"); _socket.Write("1\r"); cmd = WaitForCommand("Input"); _socket.Write("24\r"); //Misc. Processing cmd = WaitForCommand("Input"); _socket.Write("5\r"); //Batch FM cmd = WaitForCommand("Input"); _socket.Write(((int) fmtype).ToString() + "\r"); //FM File Type cmd = WaitForCommand("Input"); _socket.Write("0\r"); //Undo a Posting? (NO) cmd = WaitForCommand("Input"); _socket.Write(reportName + "\r"); //Title of Batch Report Output to Use as FM Script cmd = WaitForCommand("Input"); _socket.Write("1\r"); //Number of Search Days? (1) cmd = WaitForCommand("Input"); if (fmtype == FileMaintenanceType.Account) { _socket.Write("1\r"); //Record FM History (YES) cmd = WaitForCommand("Input"); } _socket.Write(outTitle + "\r"); //Name of Posting (needed to lookup later) cmd = WaitForCommand("Input"); _socket.Write("1\r"); //Produce Empty Report If No Exceptions? (YES) cmd = WaitForCommand("Input"); _socket.Write("0\r"); //Batch Options? (NO) if (queue < 0) { queue = GetOpenQueue(null); } _socket.Write(queue.ToString() + "\r"); //write queue cmd = WaitForCommand("Input"); _socket.Write("1\r"); //Okay (to Proceed)? (YES) cmd = WaitForCommand("Input"); //get queues again callStatus(RunState.Initializing, "Finding FM Sequence"); cmd = new SymCommand("Misc"); cmd.Set("InfoType", "BatchQueues"); _socket.Write(cmd); int newestTime = 0; int sequenceNo = -1; cmd = _socket.ReadCommand(); while (!cmd.HasParameter("Done")) { if (cmd.Get("Action") == "QueueEntry") { int currTime = 0; string timeStr = cmd.Get("Time"); string[] tokens = timeStr.Split(':'); string seconds = tokens[2], minutes = tokens[1], hours = tokens[0]; currTime = int.Parse(seconds); currTime += 60*int.Parse(minutes); currTime += 3600*int.Parse(hours); if (currTime >= newestTime) { newestTime = currTime; sequenceNo = int.Parse(cmd.Get("Seq")); } } cmd = _socket.ReadCommand(); } callStatus(RunState.Running, sequenceNo); if (Notify != null) { var worker = new BackgroundWorker(); worker.DoWork += (sender, eventArgs) => { while (IsFileRunning(sequenceNo)) { Thread.Sleep(15000); } eventArgs.Result = GetFileMaintenanceSequence(reportName); }; worker.RunWorkerCompleted += Notify; worker.RunWorkerAsync(); } return new ReportInfo(sequenceNo, outTitle); }
public void FileWrite(string name, FileType type, string content) { int chunkMax = 1024; // 1 MB max file size by default ISymCommand cmd = new SymCommand("File"); cmd.Set("Action", "Store"); cmd.Set("Type", Utilities.FileTypeString(type)); cmd.Set("Name", name); _socket.WakeUp(); _socket.Write(cmd); cmd = _socket.ReadCommand(); int wtf_is_this = 0; while (!cmd.Data.Contains("BadCharList")) { cmd = _socket.ReadCommand(); wtf_is_this++; if (wtf_is_this > 5) throw new NullReferenceException(); } if (cmd.Data.Contains("MaxBuff")) chunkMax = int.Parse(cmd.Get("MaxBuff")); if (content.Length > (999*chunkMax)) throw new FileLoadException("File too large"); if (cmd.Get("Status").Contains("Filename is too long")) throw new Exception("Filename Too Long"); content = SanitizeFileContent(content, cmd.Get("BadCharList").Split(',')); int bytesSent = 0; int block = 0; string blockStr; byte[] response; while (bytesSent < content.Length) { int chunkSize = (content.Length - bytesSent); if (chunkSize > chunkMax) chunkSize = chunkMax; string chunk = content.Substring(bytesSent, chunkSize); string chunkStr = chunkSize.ToString("D5"); blockStr = block.ToString("D3"); response = new byte[] {0x4E, 0x4E, 0x4E, 0x4E, 0x4E, 0x4E, 0x4E, 0x4E, 0x4E, 0x4E, 0x4E}; while (response[7] == 0x4E) { _socket.Write("PROT" + blockStr + "DATA" + chunkStr); _socket.Write(chunk); response = Convert.FromBase64String(_socket.Read()); } block++; bytesSent += chunkSize; } blockStr = block.ToString("D3"); _socket.Write("PROT" + blockStr + "EOF\u0020\u0020\u0020\u0020\u0020\u0020"); _socket.Read(); _socket.ReadCommand(); _socket.WakeUp(); }
//------------------------------------------------------------------------ public FMRunNfo FMRun(string inpTitle, FMType fmtype, FileRun_Status callStatus, int queue) { callStatus(1,"Initializing..."); SymCommand cmd; string outTitle = "PwrIDE FM - " + new Random().Next(8388608).ToString("D7"); Write("mm0\u001B"); cmd=ReadCommand(); while(cmd.command!="Input") cmd=ReadCommand(); callStatus(2,"Writing Commands..."); Write("1\r"); cmd=ReadCommand(); while(cmd.command!="Input") cmd=ReadCommand(); Write("24\r"); //Misc. Processing cmd=ReadCommand(); while(cmd.command!="Input") cmd=ReadCommand(); Write("5\r"); //Batch FM cmd=ReadCommand(); while(cmd.command!="Input") cmd=ReadCommand(); Write(((int)fmtype).ToString()+"\r"); //FM File Type cmd=ReadCommand(); while(cmd.command!="Input") cmd=ReadCommand(); Write("0\r"); //Undo a Posting? (NO) cmd=ReadCommand(); while(cmd.command!="Input") cmd=ReadCommand(); Write(inpTitle+"\r"); //Title of Batch Report Output to Use as FM Script cmd=ReadCommand(); while(cmd.command!="Input") cmd=ReadCommand(); Write("1\r"); //Number of Search Days? (1) cmd=ReadCommand(); while(cmd.command!="Input") cmd=ReadCommand(); if(fmtype == FMType.Account) { Write("1\r"); //Record FM History (YES) cmd=ReadCommand(); while(cmd.command!="Input") cmd=ReadCommand(); } Write(outTitle+"\r"); //Name of Posting (needed to lookup later) cmd=ReadCommand(); while(cmd.command!="Input") cmd=ReadCommand(); Write("1\r"); //Produce Empty Report If No Exceptions? (YES) cmd=ReadCommand(); while(cmd.command!="Input") cmd=ReadCommand(); Write("0\r"); //Batch Options? (NO) //get queues callStatus(4, "Getting Queue List"); cmd = ReadCommand(); Dictionary<int,int> queAvailable = new Dictionary<int,int>(); while(cmd.command != "Input") { if((cmd.GetParam("Action") == "DisplayLine") && (cmd.GetParam("Text").Contains("Batch Queues Available:"))) { string line = cmd.GetParam("Text"); string[] strQueues = line.Substring(line.IndexOf(':')+1).Split(new char[]{','}); for(int i=0; i<strQueues.Length; i++) { strQueues[i] = strQueues[i].Trim(); if(strQueues[i].Contains("-")) { int pos = strQueues[i].IndexOf('-'); int start = int.Parse(strQueues[i].Substring(0,pos)); int end = int.Parse(strQueues[i].Substring(pos+1)); for(int c=start; c<=end; c++) queAvailable.Add(c,0); } else queAvailable.Add(int.Parse(strQueues[i]),0); } } cmd = ReadCommand(); } //get queue counts callStatus(5, "Getting Queue Counts"); cmd = new SymCommand("Misc"); cmd.SetParam("InfoType", "BatchQueues"); Write(cmd); cmd = ReadCommand(); while(!cmd.HasParam("Done")) { if((cmd.GetParam("Action") == "QueueEntry") && (cmd.GetParam("Stat") == "Running")) queAvailable[int.Parse(cmd.GetParam("Queue"))]++; cmd = ReadCommand(); } if(queue == -1) //auto select lowest pending queue, or last available Zero queue { queue = 0; foreach(KeyValuePair<int, int> Q in queAvailable) if(Q.Value <= queAvailable[queue]) queue = Q.Key; } callStatus(7, "Writing Final Commands"); Write(queue.ToString()+"\r"); //write queue cmd=ReadCommand(); while(cmd.command!="Input") cmd=ReadCommand(); Write("1\r"); //Okay (to Proceed)? (YES) cmd=ReadCommand(); while(cmd.command!="Input") cmd=ReadCommand(); //get queues again callStatus(8, "Finding FM Sequence"); cmd = new SymCommand("Misc"); cmd.SetParam("InfoType", "BatchQueues"); Write(cmd); int newestTime = 0; int sequenceNo = -1; cmd = ReadCommand(); while(!cmd.HasParam("Done")) { if(cmd.GetParam("Action") == "QueueEntry") { int currTime = 0; string timeStr = cmd.GetParam("Time"); currTime = int.Parse(timeStr.Substring(timeStr.LastIndexOf(':')+1)); currTime += 60 * int.Parse(timeStr.Substring(timeStr.IndexOf(':')+1, 2)); currTime += 3600 * int.Parse(timeStr.Substring(0, timeStr.IndexOf(':'))); if(currTime >= newestTime) { newestTime = currTime; sequenceNo = int.Parse(cmd.GetParam("Seq")); } } cmd = ReadCommand(); } callStatus(9, "Running.."); return new FMRunNfo(sequenceNo, outTitle); }
//------------------------------------------------------------------------ public bool IsFileRunning(int sequence) { SymCommand cmd; bool running = false; cmd = new SymCommand("Misc"); cmd.SetParam("InfoType", "BatchQueues"); Write(cmd); cmd = ReadCommand(); while(!cmd.HasParam("Done")) { if((cmd.GetParam("Action")=="QueueEntry") && (int.Parse(cmd.GetParam("Seq")) == sequence)) running = true; cmd = ReadCommand(); } return running; }
//------------------------------------------------------------------------ public RepRunErr FileRun(SymFile file, FileRun_Status callStatus, FileRun_Prompt callPrompt, int queue) { if (file.type != SymFile.Type.REPGEN) throw new Exception("Cannot Run a " + file.TypeString() + " File"); SymCommand cmd; callStatus(0,"Initializing..."); Write("mm0\u001B"); cmd = ReadCommand(); while(cmd.command != "Input") cmd = ReadCommand(); callStatus(1,"Writing Commands..."); Write("1\r"); cmd = ReadCommand(); while(cmd.command != "Input") cmd = ReadCommand(); callStatus(2,"Writing Commands..."); Write("11\r"); cmd = ReadCommand(); while(cmd.command != "Input") cmd = ReadCommand(); callStatus(3,"Writing Commands..."); Write(file.name + "\r"); bool erroredOut = false; while(true) { cmd = ReadCommand(); if((cmd.command == "Input") && (cmd.GetParam("HelpCode")=="20301")) break; if(cmd.command == "Input") { callStatus(4,"Please Enter Prompts"); string result = callPrompt(cmd.GetParam("Prompt")); if(result == null) //cancelled { Write("\u001B"); cmd = ReadCommand(); while(cmd.command != "Input") cmd = ReadCommand(); return RepRunErr.Cancelled(); } else Write(result.Trim()+'\r'); } else if(cmd.command == "Bell") callStatus(4, "Invalid Prompt Input, Please Re-Enter"); else if((cmd.command == "Batch") && (cmd.GetParam("Text")=="No such file or directory")) { cmd = ReadCommand(); while(cmd.command != "Input") cmd = ReadCommand(); return RepRunErr.NotFound(); } else if(cmd.command == "SpecfileErr") erroredOut = true; else if(erroredOut && (cmd.command == "Batch") && (cmd.GetParam("Action") == "DisplayLine")) { string err = cmd.GetParam("Text"); cmd = ReadCommand(); while (cmd.command != "Input") cmd = ReadCommand(); return RepRunErr.Errored(err); } else if((cmd.command == "Batch") && (cmd.GetParam("Action") == "DisplayLine")) callStatus(5, cmd.GetParam("Text")); } Write("\r"); cmd = ReadCommand(); while(cmd.command != "Input") cmd = ReadCommand(); callStatus(6, "Getting Queue List"); Write("0\r"); cmd = ReadCommand(); Dictionary<int,int> queAvailable = new Dictionary<int,int>(); while(cmd.command != "Input") { if((cmd.GetParam("Action") == "DisplayLine") && (cmd.GetParam("Text").Contains("Batch Queues Available:"))) { string line = cmd.GetParam("Text"); string[] strQueues = line.Substring(line.IndexOf(':')+1).Split(new char[]{','}); for(int i=0; i<strQueues.Length; i++) { strQueues[i] = strQueues[i].Trim(); if(strQueues[i].Contains("-")) { int pos = strQueues[i].IndexOf('-'); int start = int.Parse(strQueues[i].Substring(0,pos)); int end = int.Parse(strQueues[i].Substring(pos+1)); for(int c=start; c<=end; c++) queAvailable.Add(c,0); } else queAvailable.Add(int.Parse(strQueues[i]),0); } } cmd = ReadCommand(); } callStatus(7, "Getting Queue Counts"); cmd = new SymCommand("Misc"); cmd.SetParam("InfoType", "BatchQueues"); Write(cmd); cmd = ReadCommand(); while(!cmd.HasParam("Done")) { if((cmd.GetParam("Action") == "QueueEntry") && (cmd.GetParam("Stat") == "Running")) queAvailable[int.Parse(cmd.GetParam("Queue"))]++; cmd = ReadCommand(); } if(queue == -1) //auto select lowest pending queue, or last available Zero queue { queue = 0; foreach(KeyValuePair<int, int> Q in queAvailable) if(Q.Value <= queAvailable[queue]) queue = Q.Key; } Write(queue.ToString()+"\r"); cmd = ReadCommand(); while(cmd.command != "Input") cmd = ReadCommand(); callStatus(8, "Getting Sequence Numbers"); Write("1\r"); cmd = ReadCommand(); while(cmd.command != "Input") cmd = ReadCommand(); cmd = new SymCommand("Misc"); cmd.SetParam("InfoType", "BatchQueues"); Write(cmd); int newestTime = 0; int sequenceNo = -1; cmd = ReadCommand(); while(!cmd.HasParam("Done")) { if(cmd.GetParam("Action") == "QueueEntry") { int currTime = 0; string timeStr = cmd.GetParam("Time"); currTime = int.Parse(timeStr.Substring(timeStr.LastIndexOf(':')+1)); currTime += 60 * int.Parse(timeStr.Substring(timeStr.IndexOf(':')+1, 2)); currTime += 3600 * int.Parse(timeStr.Substring(0, timeStr.IndexOf(':'))); if(currTime >= newestTime) { newestTime = currTime; sequenceNo = int.Parse(cmd.GetParam("Seq")); } } cmd = ReadCommand(); } callStatus(9, "Running.."); return RepRunErr.Okay(sequenceNo, newestTime); }
//======================================================================== public void FileWrite(string name, SymFile.Type type, string content) { int chunkMax = 1024; SymCommand cmd = new SymCommand("File"); cmd.SetParam("Action", "Store"); cmd.SetParam("Type" , SymFile.TypeString(type)); cmd.SetParam("Name" , name); WakeUp(); Write(cmd); cmd = ReadCommand(); int wtf_is_this = 0; while(cmd.data.IndexOf("BadCharList") == -1) { cmd = ReadCommand(); wtf_is_this++; if(wtf_is_this > 5) throw new Exception("Null Pointer"); } if(cmd.data.IndexOf("MaxBuff") > -1) chunkMax = int.Parse(cmd.GetParam("MaxBuff")); if(content.Length > (999*chunkMax)) throw new Exception("File Too Large"); if(cmd.GetParam("Status").IndexOf("Filename is too long") != -1) throw new Exception("Filename Too Long"); string[] badChars = cmd.GetParam("BadCharList").Split(new char[] { ',' }); for(int i=0; i<badChars.Length; i++) content = content.Replace(((char)int.Parse(badChars[i]))+"", ""); int sent=0, block=0; string blockStr; byte[] resp; while(sent < content.Length) { int chunkSize = (content.Length - sent); if(chunkSize > chunkMax) chunkSize = chunkMax; string chunk = content.Substring(sent, chunkSize); string chunkStr = chunkSize.ToString("D5"); blockStr = block.ToString("D3"); resp = new byte[]{0x4E,0x4E,0x4E,0x4E,0x4E,0x4E,0x4E,0x4E,0x4E,0x4E,0x4E}; while(resp[7] == 0x4E) { Write("PROT"+blockStr+"DATA"+chunkStr); Write(chunk); resp = Read(16); } block++; sent += chunkSize; } blockStr = block.ToString("D3"); Write("PROT"+blockStr+"EOF\u0020\u0020\u0020\u0020\u0020\u0020"); resp = Read(16); cmd = ReadCommand(); WakeUp(); }
public string FileRead(string name, FileType type) { var content = new StringBuilder(); ISymCommand cmd = new SymCommand("File"); cmd.Set("Action", "Retrieve"); cmd.Set("Type", Utilities.FileTypeString(type)); cmd.Set("Name", name); _socket.Write(cmd); while (true) { cmd = _socket.ReadCommand(); if (cmd.HasParameter("Status")) { string status = cmd.Get("Status"); if (status.Contains("No such file or directory")) throw new FileNotFoundException(); if (status.Contains("Cannot view a blank report")) return ""; throw new Exception("Filename Too Long"); } string chunk = cmd.Data; if (!string.IsNullOrEmpty(chunk)) { content.Append(chunk); if (type == FileType.Report) content.Append('\n'); } if (cmd.HasParameter("Done")) break; } return content.ToString(); }
private int GetOpenQueue(Dictionary<int, int> availableQueues) { int queue; ISymCommand cmd = new SymCommand(); if (availableQueues == null || availableQueues.Count == 0) availableQueues = GetQueueList(cmd); //get queue counts cmd = new SymCommand("Misc"); cmd.Set("InfoType", "BatchQueues"); _socket.Write(cmd); cmd = _socket.ReadCommand(); while (!cmd.HasParameter("Done")) { if ((cmd.Get("Action") == "QueueEntry") && (cmd.Get("Stat") == "Running")) availableQueues[int.Parse(cmd.Get("Queue"))]++; cmd = _socket.ReadCommand(); } // Get queue with lowest pending int lowestPending = 0; queue = 0; foreach (var Q in availableQueues) { if (Q.Value == 0) return Q.Key; if (Q.Value < lowestPending) { lowestPending = Q.Value; queue = Q.Key; } } return queue; }
public ReportInfo FMRun(string reportName, FileMaintenanceType fmtype, FileRunStatus callStatus, int queue, RunWorkerCompletedEventHandler Notify = null) { callStatus(RunState.Initializing, reportName); ISymCommand cmd; string outTitle = "FM - " + new Random().Next(8388608).ToString("D7"); _socket.Write("mm0\u001B"); cmd = WaitForCommand("Input"); _socket.Write("1\r"); cmd = WaitForCommand("Input"); _socket.Write("24\r"); //Misc. Processing cmd = WaitForCommand("Input"); _socket.Write("5\r"); //Batch FM cmd = WaitForCommand("Input"); _socket.Write(((int)fmtype).ToString() + "\r"); //FM File Type cmd = WaitForCommand("Input"); _socket.Write("0\r"); //Undo a Posting? (NO) cmd = WaitForCommand("Input"); _socket.Write(reportName + "\r"); //Title of Batch Report Output to Use as FM Script cmd = WaitForCommand("Input"); _socket.Write("1\r"); //Number of Search Days? (1) cmd = WaitForCommand("Input"); if (fmtype == FileMaintenanceType.Account) { _socket.Write("1\r"); //Record FM History (YES) cmd = WaitForCommand("Input"); } _socket.Write(outTitle + "\r"); //Name of Posting (needed to lookup later) cmd = WaitForCommand("Input"); _socket.Write("1\r"); //Produce Empty Report If No Exceptions? (YES) cmd = WaitForCommand("Input"); _socket.Write("0\r"); //Batch Options? (NO) if (queue < 0) { queue = GetOpenQueue(null); } _socket.Write(queue.ToString() + "\r"); //write queue cmd = WaitForCommand("Input"); _socket.Write("1\r"); //Okay (to Proceed)? (YES) cmd = WaitForCommand("Input"); //get queues again callStatus(RunState.Initializing, "Finding FM Sequence"); cmd = new SymCommand("Misc"); cmd.Set("InfoType", "BatchQueues"); _socket.Write(cmd); int newestTime = 0; int sequenceNo = -1; cmd = _socket.ReadCommand(); while (!cmd.HasParameter("Done")) { if (cmd.Get("Action") == "QueueEntry") { int currTime = 0; string timeStr = cmd.Get("Time"); string[] tokens = timeStr.Split(':'); string seconds = tokens[2], minutes = tokens[1], hours = tokens[0]; currTime = int.Parse(seconds); currTime += 60 * int.Parse(minutes); currTime += 3600 * int.Parse(hours); if (currTime >= newestTime) { newestTime = currTime; sequenceNo = int.Parse(cmd.Get("Seq")); } } cmd = _socket.ReadCommand(); } callStatus(RunState.Running, sequenceNo); if (Notify != null) { var worker = new BackgroundWorker(); worker.DoWork += (sender, eventArgs) => { while (IsFileRunning(sequenceNo)) { Thread.Sleep(15000); } eventArgs.Result = GetFileMaintenanceSequence(reportName); }; worker.RunWorkerCompleted += Notify; worker.RunWorkerAsync(); } return(new ReportInfo(sequenceNo, outTitle)); }
private List<int> GetPrintSequences(string searchTerm) { var seqs = new List<int>(); ISymCommand cmd; cmd = new SymCommand("File"); cmd.Set("Action", "List"); cmd.Set("MaxCount", "50"); cmd.Set("Query", "LAST 20 \"+" + searchTerm + "+\""); cmd.Set("Type", "Report"); _socket.Write(cmd); cmd = _socket.ReadCommand(); while (!cmd.HasParameter("Done")) { if (cmd.HasParameter("Sequence")) seqs.Add(int.Parse(cmd.Get("Sequence"))); cmd = _socket.ReadCommand(); } seqs.Sort(); seqs.Reverse(); return seqs; }
//------------------------------------------------------------------------ private List<int> GetPrintSequences(string which) { List<int> seqs = new List<int>(); SymCommand cmd; cmd = new SymCommand("File"); cmd.SetParam("Action" , "List"); cmd.SetParam("MaxCount", "50"); cmd.SetParam("Query" , "LAST 20 \"+"+which+"+\""); cmd.SetParam("Type" , "Report"); Write(cmd); cmd = ReadCommand(); while(!cmd.HasParam("Done")) { if(cmd.HasParam("Sequence")) seqs.Add(int.Parse(cmd.GetParam("Sequence"))); cmd = ReadCommand(); } seqs.Sort(); seqs.Reverse(); return seqs; }
public bool IsFileRunning(int sequence) { if (sequence <= 0) throw new ArgumentOutOfRangeException("sequence"); ISymCommand cmd; bool running = false; cmd = new SymCommand("Misc"); cmd.Set("InfoType", "BatchQueues"); _socket.Write(cmd); cmd = _socket.ReadCommand(); while (!cmd.HasParameter("Done")) { if ((cmd.Get("Action") == "QueueEntry") && (int.Parse(cmd.Get("Seq")) == sequence)) running = true; else if (cmd.Get("Action") == "Close") { Reconnect(); cmd = new SymCommand("Misc"); cmd.Set("InfoType", "BatchQueues"); _socket.Write(cmd); } else if (cmd.Command == "") { cmd = new SymCommand("Misc"); cmd.Set("InfoType", "BatchQueues"); _socket.Write(cmd); } cmd = _socket.ReadCommand(); } return running; }
private void Write(SymCommand cmd) { Write(cmd.Packet(), defaultTimeout); }
public RepgenRunResult FileRun(File file, FileRunStatus callStatus, FileRunPrompt callPrompt, int queue, RunWorkerCompletedEventHandler Notify = null) { if (file.Type != FileType.RepGen) throw new InvalidOperationException("Cannot run a " + file.FileTypeString() + " file"); ISymCommand cmd; callStatus(RunState.Initializing, file); _socket.Write("mm0\u001B"); WaitForCommand("Input"); _socket.Write("1\r"); WaitForCommand("Input"); _socket.Write("11\r"); WaitForPrompt("Specification File"); _socket.Write(file.Name + "\r"); bool erroredOut = false; while (true) { cmd = _socket.ReadCommand(); if (cmd.Command == "Input") { if (cmd.Get("HelpCode") == "20301") break; callStatus(RunState.Prompts, file); string result = callPrompt(cmd.Get("Prompt")); if (result == null) //cancelled { _socket.Write("\u001B"); cmd = _socket.ReadCommand(); while (cmd.Command != "Input") cmd = _socket.ReadCommand(); callStatus(RunState.Cancelled, file); return RepgenRunResult.Cancelled(); } _socket.Write(result.Trim() + '\r'); } else if (cmd.Command == "Bell") callStatus(RunState.Prompts, "Invalid Prompt Input, Please Re-Enter"); else if ((cmd.Command == "Batch") && (cmd.Get("Text") == "No such file or directory")) { cmd = _socket.ReadCommand(); while (cmd.Command != "Input") cmd = _socket.ReadCommand(); callStatus(RunState.Failed, "File not found"); return RepgenRunResult.FileNotFound(); } else if (cmd.Command == "SpecfileErr") erroredOut = true; else if (erroredOut && (cmd.Command == "Batch") && (cmd.Get("Action") == "DisplayLine")) { string err = cmd.Get("Text"); cmd = _socket.ReadCommand(); while (cmd.Command != "Input") cmd = _socket.ReadCommand(); callStatus(RunState.Failed, err); return RepgenRunResult.Error(err); } else if ((cmd.Command == "Batch") && (cmd.Get("Action") == "DisplayLine")) callStatus(RunState.Initializing, cmd.Get("Text")); } while (cmd.Get("Prompt").Contains("Specification File")) { _socket.Write("\r"); cmd = _socket.ReadCommand(); } WaitForPrompt("Batch Options"); _socket.Write("0\r"); Dictionary<int, int> availableQueues = GetQueueList(cmd); if (queue < 0) queue = GetOpenQueue(availableQueues); WaitForPrompt("Batch Queue"); _socket.Write(queue + "\r"); WaitForCommand("Input"); _socket.Write("1\r"); cmd = _socket.ReadCommand(); while (cmd.Command != "Input") cmd = _socket.ReadCommand(); cmd = new SymCommand("Misc"); cmd.Set("InfoType", "BatchQueues"); _socket.Write(cmd); int newestTime = 0; int sequenceNo = -1; cmd = _socket.ReadCommand(); while (!cmd.HasParameter("Done")) { if (cmd.Get("Action") == "QueueEntry") { int currTime = Utilities.ConvertTime(cmd.Get("Time")); if (currTime >= newestTime) { newestTime = currTime; sequenceNo = int.Parse(cmd.Get("Seq")); } } cmd = _socket.ReadCommand(); } callStatus(RunState.Running, sequenceNo); if (Notify != null) { var worker = new BackgroundWorker(); worker.DoWork += (sender, eventArgs) => { Thread.Sleep(5000); // Wait 5 seconds before first check while (IsFileRunning(sequenceNo)) { Thread.Sleep(15000); } object[] result = new object[3]; result[0] = file.Name; result[1] = sequenceNo; result[2] = GetBatchOutputSequence(file.Name, newestTime); eventArgs.Result = result; }; worker.RunWorkerCompleted += Notify; worker.RunWorkerAsync(); } return RepgenRunResult.Okay(sequenceNo, newestTime); }
//======================================================================== public List<SymFile> FileList(string pattern, SymFile.Type type) { List<SymFile> files = new List<SymFile>(); SymCommand cmd = new SymCommand("File"); cmd.SetParam("Type" , SymFile.TypeDescriptor[(int)type]); cmd.SetParam("Name" , pattern); cmd.SetParam("Action", "List"); Write(cmd); while(true) { cmd = ReadCommand(2000); if(cmd.HasParam("Status")) break; if(cmd.HasParam("Name")) files.Add(new SymFile(server, sym, cmd.GetParam("Name"), cmd.GetParam("Date"), cmd.GetParam("Time"), int.Parse(cmd.GetParam("Size")), type)); if(cmd.HasParam("Done")) break; } return files; }
public void FileWrite(string name, FileType type, string content) { int chunkMax = 1024; // 1 MB max file size by default ISymCommand cmd = new SymCommand("File"); cmd.Set("Action", "Store"); cmd.Set("Type", Utilities.FileTypeString(type)); cmd.Set("Name", name); _socket.WakeUp(); _socket.Write(cmd); cmd = _socket.ReadCommand(); int wtf_is_this = 0; while (!cmd.Data.Contains("BadCharList")) { cmd = _socket.ReadCommand(); wtf_is_this++; if (wtf_is_this > 5) { throw new NullReferenceException(); } } if (cmd.Data.Contains("MaxBuff")) { chunkMax = int.Parse(cmd.Get("MaxBuff")); } if (content.Length > (999 * chunkMax)) { throw new FileLoadException("File too large"); } if (cmd.Get("Status").Contains("Filename is too long")) { throw new Exception("Filename Too Long"); } content = SanitizeFileContent(content, cmd.Get("BadCharList").Split(',')); int bytesSent = 0; int block = 0; string blockStr; byte[] response; while (bytesSent < content.Length) { int chunkSize = (content.Length - bytesSent); if (chunkSize > chunkMax) { chunkSize = chunkMax; } string chunk = content.Substring(bytesSent, chunkSize); string chunkStr = chunkSize.ToString("D5"); blockStr = block.ToString("D3"); response = new byte[] { 0x4E, 0x4E, 0x4E, 0x4E, 0x4E, 0x4E, 0x4E, 0x4E, 0x4E, 0x4E, 0x4E }; while (response[7] == 0x4E) { _socket.Write("PROT" + blockStr + "DATA" + chunkStr); _socket.Write(chunk); response = Convert.FromBase64String(_socket.Read()); } block++; bytesSent += chunkSize; } blockStr = block.ToString("D3"); _socket.Write("PROT" + blockStr + "EOF\u0020\u0020\u0020\u0020\u0020\u0020"); _socket.Read(); _socket.ReadCommand(); _socket.WakeUp(); }
//======================================================================== public string FileRead(string name, SymFile.Type type) { StringBuilder content = new StringBuilder(); SymCommand cmd = new SymCommand("File"); cmd.SetParam("Action", "Retrieve"); cmd.SetParam("Type" , SymFile.TypeString(type)); cmd.SetParam("Name" , name); Write(cmd); while(true) { cmd = ReadCommand(); if(cmd.HasParam("Status")) { if(cmd.GetParam("Status").IndexOf("No such file or directory") != -1) throw new FileNotFoundException("File \""+name+"\" Not Found"); else if(cmd.GetParam("Status").IndexOf("Cannot view a blank report") != -1) return ""; else throw new Exception("Filename Too Long"); } string chunk = cmd.GetFileData(); if((chunk.Length > 0) || (type == SymFile.Type.REPORT)) { content.Append(chunk); if(type==SymFile.Type.REPORT) content.Append('\n'); } if(cmd.HasParam("Done")) break; } return content.ToString(); }
//======================================================================== public void FileRename(string oldName, SymFile.Type type, string newName) { SymCommand cmd = new SymCommand("File"); cmd.SetParam("Action" , "Rename"); cmd.SetParam("Type" , SymFile.TypeString(type)); cmd.SetParam("Name" , oldName); cmd.SetParam("NewName", newName); Write(cmd); cmd = ReadCommand(2000); if(cmd.HasParam("Status")) { if(cmd.GetParam("Status").IndexOf("No such file or directory") != -1) throw new FileNotFoundException("File \""+oldName+"\" Not Found"); else throw new Exception("Filename Too Long"); } else if(cmd.HasParam("Done")) return; else throw new Exception("Unknown Renaming Error"); }
public List<File> FileList(string pattern, FileType type) { var files = new List<File>(); ISymCommand cmd = new SymCommand("File"); cmd.Set("Type", Utilities.FileTypeString(type)); cmd.Set("Name", pattern); cmd.Set("Action", "List"); _socket.Write(cmd); while (true) { cmd = _socket.ReadCommand(); if (cmd.HasParameter("Status")) break; if (cmd.HasParameter("Name")) { files.Add(new File(_socket.Server, SymDirectory.ToString(), cmd.Get("Name"), type, cmd.Get("Date"), cmd.Get("Time"), int.Parse(cmd.Get("Size")))); } if (cmd.HasParameter("Done")) break; } return files; }