private void processStream(Stream stream) { MessageStream msgStream = new MessageStream(new StandardStream(stream)); Message msg = msgStream.readMessage(); if (msg == null) { return; } EMessageType mType = (EMessageType)msg.getType(); if (mType == EMessageType.eNumber) { // End a session MessageNumber response = new MessageNumber(msg); int sid = response.getNumber(); m_agent.signalToStopSession(sid); } else if (mType == EMessageType.ePidAndCompileRequest) { MessagePidAndCompileRequest request = new MessagePidAndCompileRequest(msg); TCompileResult cr = m_agent.compile(request.getPid(), request.getCmd(), request.getWorkingDir()); // The output file was already saved to local disk by the agent, now respond the result only msg = MessageCompileResponse.createMessage(cr.wasExec, cr.exitCode, cr.outputText, null, 0); msgStream.writeMessage(msg); } else { // WTF??? } }
/// <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 void handleMessageCompile(Message msg) { MessageCompileRequest msgCompile = null; try { msgCompile = new MessageCompileRequest(msg); } catch (Exception) { // Ignore return; } string cmd = msgCompile.getCmd(); ICmdParser parser = CmdParserFactory.createCmdParser(cmd); string outputFileName; cmd = parser.makeServerCmd(m_sessionFolderPath, out outputFileName, m_debugPrefixMap); string alias = parser.getAlias(); Message respondMessage = null; bool ok = false; if (alias != null && outputFileName != null) { // Compile CConsole.writeInfoLine(string.Format("{0} Recv a compile request: {1}", cur(), alias)); string outputFilePath = m_sessionFolderPath + outputFileName; string workingDir = parser.getLocalSpecificWorkingDir(); if (workingDir == null) { workingDir = m_sessionFolderPath; } TProcessResult pr = m_processCompile.execute(cmd, workingDir, outputFilePath); if (!pr.wasExec || pr.exitCode != 0) { CConsole.writeInfoLine( string.Format("{0} Compile error: wasExec=[{1}], exitCode=[{2}], cmd=[{3}], outputText=[{4}]", cur(), pr.wasExec, pr.exitCode, cmd, pr.outputText) ); respondMessage = MessageCompileResponse.createMessage(pr.wasExec, pr.exitCode, pr.outputText, null, 0); } else { ok = true; // Read the output file from disk byte[] buffer = IOUtils.readFile_Bytes(outputFilePath); int fileSize = buffer == null ? 0 : buffer.Length; respondMessage = MessageCompileResponse.createMessage( pr.wasExec, pr.exitCode, pr.outputText, buffer, fileSize); } } else { CConsole.writeInfoLine(string.Format("{0} Receive a compile request but it is a invalid command", cur())); respondMessage = MessageCompileResponse.createMessage(false, 0, "error: Invalid compile command!", null, 0); } CConsole.writeInfoLine(string.Format("{0} Send compile result: {1}, success = {2}", cur(), alias != null ? alias : "[no file]", ok)); m_messageStream.writeMessage(respondMessage); }
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); }