/// <summary> /// Make sure isCompiling() == true as setCompilingTrue() was called /// Make sure all parameters are not null /// Make sure this method is thread-safety: it cannot be called at the same time /// </summary> /// <param name="cmd"></param> /// <param name="workingDir"></param> /// <returns></returns> public TCompileResult compile(string cmd, string workingDir) { int oldTick = SystemUtils.getCurrentTimeMs(); ICmdParser parser = CmdParserFactory.createCmdParser(cmd); List <string> inputFilePaths = null; string outputFilePath = null; TCompileResult result = null; if (!isAllowCompiling()) { result = new TCompileResult(false, 1, "error: Compiling is stopped!", 0, null); goto my_end; } try { parser.getInOutFilePath(workingDir, out inputFilePaths, out outputFilePath); } catch (Exception ex) { // Error result = new TCompileResult(false, 1, "error: " + ex.Message, 0, null); goto my_end; } if (inputFilePaths == null || inputFilePaths.Count == 0 || outputFilePath == null) { // Error result = new TCompileResult(false, 1, "error: Invalid command, input file is mandatory!", 0, null); goto my_end; } outputFilePath = PathUtils.combine(workingDir, outputFilePath); if (!isConnected()) { // Local goto my_end; } #region Distribute // Send the input files to server foreach (string inputFilePath in inputFilePaths) { string f = PathUtils.combine(workingDir, inputFilePath); string fNormal = PathUtils.normalizePath(f); if (!m_sentFiles.Contains(fNormal)) { if (sendFile(f)) { m_sentFiles.Add(fNormal); } else { // Local goto my_end; } } } // Send compile request to server string cmdToSend = parser.makeNetworkCmd(); Message msg = MessageCompileRequest.createMessage(cmdToSend); if (!m_messageStream.writeMessage(msg)) { goto my_end; } if (!isAllowCompiling()) { result = new TCompileResult(false, 1, "error: Compiling is stopped!", 0, null); goto my_end; } // Receive the compile result msg = m_messageStream.readMessage(); if (msg == null) { goto my_end; } if (!isAllowCompiling()) { result = new TCompileResult(false, 1, "error: Compiling is stopped!", 0, null); goto my_end; } // Parse and check the compile result MessageCompileResponse msgCompileRes = new MessageCompileResponse(msg); if (msgCompileRes.getWasExec() && msgCompileRes.getExitCode() != 0) { // Compile error // Nghia: TODO rem this for the hot fix //result = new TCompileResult(true, msgCompileRes.getExitCode(), msgCompileRes.getOutputText(), 0, m_host); goto my_end; } if (!msgCompileRes.getWasExec()) { goto my_end; } // Compile OK by server // Save the file to disk if (!IOUtils.writeFile_Bytes(outputFilePath, msgCompileRes.getOFileData(), msgCompileRes.getOFileOffset(), msgCompileRes.getOFileSize())) { // No need to compile local as this is a serious error result = new TCompileResult(true, 1, "error: Could not save the output file to disk", 0, m_host); goto my_end; } // Everything is OK result = new TCompileResult(true, 0, "Remotely compiled from " + m_host + "\n" + msgCompileRes.getOutputText(), 0, m_host); #endregion my_end: if (result == null) { // Local string specOutFilePath = parser.getLocalSpecificOutputFilePath(); string specCmd = parser.getLocalSpecificCommand(); string specWorkingDir = parser.getLocalSpecificWorkingDir(); if (specOutFilePath == null) { specOutFilePath = outputFilePath; } if (specCmd == null) { specCmd = cmd; } if (specWorkingDir == null) { specWorkingDir = workingDir; } specOutFilePath = PathUtils.combine(specWorkingDir, specOutFilePath); TProcessResult res = null; if (parser.needToLockLocal()) { lock (s_lock) { doLocal(specCmd, specWorkingDir, specOutFilePath, ref oldTick, ref res); } } else { doLocal(specCmd, specWorkingDir, specOutFilePath, ref oldTick, ref res); } result = new TCompileResult(res.wasExec, res.exitCode, "Locally compiled\n" + res.outputText, 0, null); if (result.wasExec && result.exitCode == 0 && specOutFilePath != outputFilePath) { File.Move(specOutFilePath, outputFilePath); } } result.spentTimeMs = SystemUtils.getCurrentTimeMs() - oldTick; setCompiling(false); return(result); }
private bool compile(int sid, string cmd, string workingDir) { if (!isAgentRunning()) { string processPath = SystemUtils.getCurrentProcessPath(); if (!ProcessHelper.justExecuteStatic(processPath + " --agent", SystemUtils.getWorkingDir())) { CConsole.writeError("error: could not start the mongcc agent.\n"); return(false); } Thread.Sleep(s_kWaitingForAgentStartTime); // note: is it enough? } // Check again to make sure the agent is running if (!isAgentRunning()) { CConsole.writeError("error: could not start the mongcc agent.\n"); return(false); } // Connect to the agent and send data NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", Config.s_kAgentName); try { pipeClient.Connect(s_kPipeConnectTimeout); } catch (Exception) { CConsole.writeError("error: could not connect to the mongcc agent.\n"); return(false); } if (!pipeClient.IsConnected) { CConsole.writeError("error: could not connect to the mongcc agent.\n"); return(false); } bool success = false; m_sid = sid == 0 ? SystemUtils.getParentProcessId() : sid; MessageStream stream = new MessageStream(new StandardStream(pipeClient)); Message msg = MessagePidAndCompileRequest.createMessage(m_sid, cmd, workingDir); if (!stream.writeMessage(msg)) { CConsole.writeError("error: could not send data to the mongcc agent.\n"); goto my_end; } setCompiling(true); msg = stream.readMessage(); //============= take long time here if (msg == null) { CConsole.writeError("error: could not receive data from the mongcc agent.\n"); goto my_end; } MessageCompileResponse res = new MessageCompileResponse(msg); success = res.getWasExec() && res.getExitCode() == 0; if (success) { CConsole.writeInfo(res.getOutputText() + "\n"); } else { CConsole.writeError(res.getOutputText() + "\n"); } my_end: pipeClient.Close(); #region temp //Console.WriteLine("OK"); //Console.ReadLine(); #endregion return(success); }