private void WorkLoop() { FmriCommon.LogToFile("MatlabRunner - worker thread started."); try { do { int index = WaitHandle.WaitAny(m_waitables); if (m_waitables[index] == m_stopEvent) { break; } while (HasRequests() && !m_stopEvent.WaitOne(0)) { handleRequest(DequeueRequest()); } } while (true); } catch (ThreadAbortException e) { FmriCommon.LogToFile("MatlabRunner - worker thread aborted!"); } }
public void RunMatlabAndLog(string cmd) { m_matlab.Evaluate(cmd); string res = m_matlab.LastResult; if (res.Trim() != "") { FmriCommon.LogToFile("MATLAB: " + res.Trim()); } }
protected void Page_Load(object sender, EventArgs e) { MatlabRunner m = FmriCommon.getMatlabRunner(Application, Server); List <FmriRequest> reqList; reqList = m.GetQueueList(); reqList.Reverse(); foreach (FmriRequest req in reqList) { TableRow r = new TableRow(); r.Height = 40; TableCell c; c = new TableCell(); c.Text = "Queue"; c.BackColor = Color.Orange; r.Cells.Add(c); AddCells(r.Cells, req); tblReq.Rows.Add(r); } reqList.Reverse(); FmriRequest currReq = m.CurrentRequest; if (currReq != null) { TableRow r = new TableRow(); r.Height = 40; TableCell c; c = new TableCell(); c.Text = "In Progress"; c.BackColor = Color.DarkGreen; c.ForeColor = Color.White; r.Cells.Add(c); AddCells(r.Cells, currReq); c = new TableCell(); c.Text = Convert.ToString(currReq.TimeExecuted); r.Cells.Add(c); tblReq.Rows.Add(r); } handleDone(m.GetDoneList(), tblReq, "Finished", Color.DarkSlateBlue, Color.White); handleDone((List <FmriRequest>)Application["ReqHist"], tblReq, "History", Color.LightGray, Color.Black); }
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { DirectoryInfo di = new DirectoryInfo(FmriCommon.getSrcImageDir(Server)); FileInfo[] lstFI = di.GetFiles("*.mat"); foreach (FileInfo f in lstFI) { lstImageName.Items.Add(f.Name); } } }
protected string isFilenameOK(string filename) { if (!filename.EndsWith(".mat")) { return(" Filename must end with '.mat' suffix! "); } if (System.IO.File.Exists(FmriCommon.getSrcImageDir(Server) + filename)) { return(" Filename already exists on the server! "); } return(null); }
public void writeToHistory() { try { FileStream fstream = new FileStream(m_server.MapPath("App_Data\\history.bin"), FileMode.Append); IFormatter formatter = new BinaryFormatter(); foreach (FmriRequest req in GetDoneList()) { formatter.Serialize(fstream, req); } fstream.Close(); } catch (Exception e) { FmriCommon.LogToFile("Exception in writeToHistory {0} [{1}]", e.Message, e.StackTrace); } }
protected void btnSubmit_Click(object sender, EventArgs e) { lblSFNValidationMessage.Visible = true; string errMsg = isFilenameOK(txtServerFileName.Text); if (errMsg != null) { lblSFNValidationMessage.Text = errMsg; return; } lblSFNValidationMessage.Visible = false; debugString("Got " + uploadCtrl.FileBytes.Length + " bytes..."); string fname = FmriCommon.getSrcImageDir(Server) + txtServerFileName.Text; try { FileStream fs = new FileStream( fname, FileMode.CreateNew); fs.Write(uploadCtrl.FileBytes, 0, uploadCtrl.FileBytes.Length); fs.Close(); debugString("Written successfully into " + fname + "."); lblResult.Text = "File was successfully uploaded."; lblResult.ForeColor = System.Drawing.Color.Green; pnlResult.Visible = true; } catch (IOException ioe) { lblResult.Text = ioe.Message; lblResult.ForeColor = System.Drawing.Color.Red; pnlResult.Visible = true; } }
private void execute(string imageName, int x1, int x2, int y1, int y2, int z1, int z2, double threshold, int t1, int t2, int cs1, int cs2) { debugString("Running on image: " + imageName); debugString("X=[" + x1 + "," + x2 + "]"); debugString("Y=[" + y1 + "," + y2 + "]"); debugString("Z=[" + z1 + "," + z2 + "]"); debugString("Threshold: " + threshold); FmriRequest req = new FmriRequest(imageName, x1, x2, y1, y2, z1, z2, threshold, t1, t2, cs1, cs2, Request.UserHostAddress); refStr.Value = req.AreaStringWithThresholdMD5; refNoThreshold.Value = req.AreaStringMD5; debugString("AreaString: " + req.AreaString + " (" + req.AreaStringMD5 + ")"); debugString("AreaStringWithThreshold: " + req.AreaStringWithThreshold + " (" + req.AreaStringWithThresholdMD5 + ")"); if (FmriCommon.isOutImageExists(req.AreaStringWithThresholdMD5, Server)) { debugString("Filename: \"" + FmriCommon.getOutImageDir(Server) + req.AreaStringWithThresholdMD5 + ".png" + "\" exists. good."); lblRef.ForeColor = System.Drawing.Color.Green; lblRef.Text = "Your request is completed. See results page!"; } else { MatlabRunner m = FmriCommon.getMatlabRunner(Application, Server); m.EnqueueRequest(req); debugString("Posted to queue."); lblRef.ForeColor = System.Drawing.Color.Blue; lblRef.Text = "Your request was posted to the queue. Try the results page later."; } }
public List <FmriRequest> readFromHistory() { List <FmriRequest> ret = new List <FmriRequest>(); try { FileStream fstream = new FileStream(m_server.MapPath("App_Data\\history.bin"), FileMode.Open); IFormatter formatter = new BinaryFormatter(); while (fstream.Position + 4 < fstream.Length) { object o = null; try { o = formatter.Deserialize(fstream); ret.Add((FmriRequest)o); } catch (InvalidCastException e) { FmriCommon.LogToFile("Exception in readFromHistory ({0}): {1} [{2}]", o.ToString(), e.Message, e.StackTrace); } } fstream.Close(); } catch (System.IO.FileNotFoundException e) { } catch (Exception e) { FmriCommon.LogToFile("Exception in readFromHistory: {0} [{1}]", e.Message, e.StackTrace); } return(ret); }
public static bool isOutImageExists(string md5, HttpServerUtility Server) { return(System.IO.File.Exists(FmriCommon.getOutImageDir(Server) + md5 + ".png")); }
public void handleRequest(FmriRequest req) { FmriCommon.LogToFile("MatlabRunner - handling request: {0}", req.AreaStringWithThreshold); m_currentRequest = req; string mtx_filename = FmriCommon.getMatrixDir(m_server) + req.AreaStringMD5 + ".mat"; string xls_filename = FmriCommon.getExcelDir(m_server) + req.AreaStringMD5 + ".csv"; string zip_filename = FmriCommon.getExcelDir(m_server) + req.AreaStringMD5 + ".zip"; try { if (!System.IO.File.Exists(mtx_filename)) { RunMatlabAndLog("should_calc_corr_matrix = 1;"); RunMatlabAndLog("src_image_filename = '" + FmriCommon.getSrcImageDir(m_server) + req.ImageName + "';"); object[] range = { req.X1, req.X2, req.Y1, req.Y2, req.Z1, req.Z2, req.T1, req.T2 }; RunMatlabAndLog(String.Format("x1={0};x2={1};y1={2};y2={3};z1={4};z2={5};t1={6};t2={7};", range)); RunMatlabAndLog("corr_matrix_out_filename = '" + mtx_filename + "';"); } else { RunMatlabAndLog("should_calc_corr_matrix = 0;"); RunMatlabAndLog("corr_matrix_in_filename = '" + mtx_filename + "';"); } if (!System.IO.File.Exists(xls_filename)) { RunMatlabAndLog("should_write_xls = 1;"); RunMatlabAndLog("xls_out_filename = '" + xls_filename + "';"); RunMatlabAndLog("zip_out_filename = '" + zip_filename + "';"); } else { RunMatlabAndLog("should_write_xls = 0;"); } RunMatlabAndLog("threshold = " + Convert.ToString(req.Threshold) + ";"); string out_image_filename = FmriCommon.getOutImageDir(m_server) + req.AreaStringWithThresholdMD5 + ".png"; RunMatlabAndLog("corr_image_out_filename = '" + out_image_filename + "';"); // Finished initializing variables. Run the MATLAB script now! req.executedNow(); RunMatlabAndLog("analyze;"); req.Result = m_matlab.LastResult; } catch (Exception e) { req.Result = e.Message; FmriCommon.LogToFile("MatlabRunner - exception caught: {0} [{1}]", e.Message, e.StackTrace); } finally { m_currentRequest = null; } try { string clique_filename = FmriCommon.getCliquesDir(m_server) + req.AreaStringWithThresholdMD5 + ".txt"; string jar_filename = FmriCommon.getJarFilename(m_server); object[] commandline = { jar_filename, xls_filename, req.Threshold, req.CS1, req.CS2, clique_filename }; System.Diagnostics.ProcessStartInfo psinfo = new System.Diagnostics.ProcessStartInfo( "java", String.Format("-jar {0} {1} {2} {3} {4} {5}", commandline)); psinfo.RedirectStandardError = true; psinfo.RedirectStandardOutput = true; psinfo.UseShellExecute = false; System.Diagnostics.Process java = System.Diagnostics.Process.Start(psinfo); FmriCommon.LogToFile("JAVA: started, PID=" + java.Id); java.WaitForExit(); FmriCommon.LogToFile("JAVA: finished, ExitCode=" + java.ExitCode); string stdout = java.StandardOutput.ReadToEnd().Trim(); string stderr = java.StandardError.ReadToEnd().Trim(); if (stdout != "" && stderr != "") { req.CliquesResult = stdout + ";\n" + stderr; } else if (stdout != "") { req.CliquesResult = stdout; } else { req.CliquesResult = stderr; } FmriCommon.LogToFile("JAVA: " + req.CliquesResult); } catch (Exception e) { req.CliquesResult = e.Message; FmriCommon.LogToFile("MatlabRunner - exception caught (cliques): {0} [{1}]", e.Message, e.StackTrace); } lock (m_doneList) { m_doneList.Add(req); } FmriCommon.LogToFile("MatlabRunner - request finished."); }
protected void findImage(string id, string id2) { if (!string.IsNullOrEmpty(id)) { pnlImage.Visible = false; if (FmriCommon.isOutImageExists(id, Server)) { imgResult.ImageUrl = "Results/" + id + ".png"; pnlImage.Visible = true; } else { lblMsg.Text = "File does not exist yet. Please try again later.<br />"; lblMsg.ForeColor = System.Drawing.Color.DarkRed; } string clique_filename = FmriCommon.getCliquesDir(Server) + id + ".txt"; if (System.IO.File.Exists(clique_filename)) { lnkCliques.NavigateUrl = "Cliques/" + id + ".txt"; pnlImage.Visible = true; } } else { pnlImage.Visible = false; } if (!string.IsNullOrEmpty(id2)) { string xls_filename = FmriCommon.getExcelDir(Server) + id2 + ".csv"; if (System.IO.File.Exists(xls_filename)) { lnkExcelFile.NavigateUrl = "Excel/" + id2 + ".csv"; lnkZipFile.NavigateUrl = "Excel/" + id2 + ".zip"; pnlExcelFile.Visible = true; } else { lblMsg.Text = "File does not exist yet. Please try again later.<br />"; lblMsg.ForeColor = System.Drawing.Color.DarkRed; pnlExcelFile.Visible = false; } } else { pnlExcelFile.Visible = false; } if (pnlExcelFile.Visible == false || pnlImage.Visible == false) { pnlMessage.Visible = true; } else { pnlMessage.Visible = false; } }