Ejemplo n.º 1
0
    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.");
    }