📄 stafperlservice.cpp
字号:
STAFString perlInterpreterStartString = perlInterpreterOptions + " " + perlServiceExec + " " + perlServiceName; // Create the connection provider for the PerlInterpreter STAFString ipcName = perlServiceName; //CHANGED BY TMG STAFStringConst_t optionData[] = { sIPCName.getImpl(), ipcName.getImpl() }; STAFConnectionProviderConstructInfoLevel1 constructInfo = { kSTAFConnectionProviderOutbound, 1, optionData, &optionData[1] }; perlInterpreterData.fConnProv = STAFConnectionProvider::createRefPtr(ipcName, "STAFLIPC", &constructInfo, 1); // We need to shutdown any PerlInterpreter that might happen to be // leftover from a "bad" exit from a previous STAFProc STAFDoShutdownPerlInterpreter(perlInterpreterData.fConnProv); // We need to capture stdout/stderr for diagnostic purposes // Create directory for PerlInterpreter log file if doesn't // already exist STAFFSPath logPath; logPath.setRoot(pInfo->writeLocation); logPath.addDir("lang"); logPath.addDir("perl"); logPath.addDir("perlInterpreter"); logPath.addDir(perlServiceName); if (!logPath.exists()) { try { // Don't want exceptions here STAFFSEntryPtr dir = logPath.createDirectory(kSTAFFSCreatePath); } catch (...) { /* Do Nothing */ } if (!logPath.exists()) { STAFString errmsg( "Error constructing the Perl Interpreter using " "Perl Service Name: " + perlServiceName + ", PerlServiceExec " + perlServiceExec + ", Perl Interpreter: " + perlInterpreterExec + ", Perl Interpreter Options: " + perlInterpreterOptions + ", Error creating PerlInterpreterLog directory: " + logPath.asString()); *pErrorBuffer = errmsg.adoptImpl(); return kSTAFServiceConfigurationError; } } // Name the current PerlInterpreter log file - PerlInterpreterLog.1 logPath.setName("PerlInterpreterLog"); logPath.setExtension("1"); STAFString logName = logPath.asString(); // Instead of replacing the PerlInterpreter log file each time a // PerlInterpreter is created, use the following PerlInterpreter // OPTIONs to determine when to create a new PerlInterpreter log // file and how many PerlInterpreter log files to save: // - MaxLogs : Maximum number of PerlInterpreter log files to keep // - MaxLogSize: Maximum size of a PerlInterpreter log file in bytes // Open the PerlInterpreter log file fstream outfile(logName.toCurrentCodePage()->buffer(), ios::out | ios::app); if (!outfile) { STAFString errmsg( "Error constructing the Perl Interpreter using " "Perl Service Name: " + perlServiceName + ", PerlServiceExec " + perlServiceExec + ", Perl Interpreter: " + perlInterpreterExec + ", Perl Interpreter Options: " + perlInterpreterOptions + ", RC: " + STAFString(kSTAFFileOpenError) + ", Error opening file " + logName); *pErrorBuffer = errmsg.adoptImpl(); return kSTAFServiceConfigurationError; } // Figure out how big the PerlInterpreter log file is outfile.seekp(0, ios::end); unsigned int fileLength = (unsigned int)outfile.tellp(); if (fileLength > maxLogSize) { // Roll any existing log files (e.g. Rename // PerlInterpreterLog.2.out to PerlInterpreterLog.3, // PerlInterpreterLog.1 to PerlInterpreterLog.2, etc) and // create a new PerlInterpreterLog.1 file. If the # of // existing logs > MAXLOGS, don't save the oldest log. outfile.close(); STAFFSPath fromLogPath(logPath); for (int i = maxLogs; i > 0; --i) { fromLogPath.setExtension(STAFString(i)); if (fromLogPath.exists() && i < maxLogs) { // Rename PerlInterpreterLog.<i> to // PerlInterpreterLog.<i+1> STAFFSPath toLogPath(fromLogPath); toLogPath.setExtension(STAFString(i + 1)); fromLogPath.getEntry()->move(toLogPath.asString()); } } // Open a new empty current log file outfile.open(logName.toCurrentCodePage()->buffer(), ios::out | ios::trunc); if (!outfile) { STAFString errmsg( "Error constructing the Perl Interpreter using " "Perl Service Name: " + perlServiceName + ", PerlServiceExec " + perlServiceExec + ", Perl Interpreter: " + perlInterpreterExec + ", Perl Interpreter Options: " + perlInterpreterOptions + ", RC: " + STAFString(kSTAFFileOpenError) + ", Error opening file " + logName); *pErrorBuffer = errmsg.adoptImpl(); return kSTAFServiceConfigurationError; } } // Write the PerlInterpreter start information to the // PerlInterpreter log file STAFString separatorLine("***************************************" "***************************************"); STAFString line1("*** " + STAFTimestamp().asString() + " - Start of Log for PerlServiceName: " + perlServiceName); STAFString line2("*** PerlInterpreter Executable: " + perlInterpreterExec); STAFString line3("*** PerlService Executable: " + perlServiceExec); STAFString line4("*** PerlInterpreter Options :"); if (perlInterpreterOptions != STAFString(" ")) line4 += perlInterpreterOptions; else line4 += " none"; outfile << separatorLine.toCurrentCodePage()->buffer() << endl << line1.toCurrentCodePage()->buffer() << endl << line2.toCurrentCodePage()->buffer() << endl << line3.toCurrentCodePage()->buffer() << endl << line4.toCurrentCodePage()->buffer() << endl << separatorLine.toCurrentCodePage()->buffer() << endl; outfile.close(); // Start a process for the PerlInterpreter STAFProcessStartInfoLevel1 startInfo = { 0 }; startInfo.command = perlInterpreterExec.getImpl(); startInfo.parms = perlInterpreterStartString.getImpl(); startInfo.consoleMode = kSTAFProcessSameConsole; startInfo.stdoutMode = kSTAFProcessIOStdout; startInfo.stderrMode = kSTAFProcessIOStdout; startInfo.stdoutRedirect = logName.getImpl(); unsigned int osRC = 0; STAFString_t errorBuffer = 0; STAFRC_t rc = STAFProcessStart2( 0, 0, &startInfo, 1, &osRC, &errorBuffer); if (rc != kSTAFOk) { STAFString startError( "Error starting a process for the the Perl Interpreter " "using Perl Service Name: " + perlServiceName + ", PerlServiceExec " + perlServiceExec + ", Perl Interpreter: " + perlInterpreterExec + ", Perl Interpreter Options: " + perlInterpreterOptions + ", RC: " + STAFString(rc) + ", Result: " + STAFString(errorBuffer, STAFString::kShallow)); // Add more details to the error msg when the Perl executable // cannot be found. if (rc == 10) { if (!perlInterpreterSpecified) { startError = startError + " Make sure the perl " + "executable is in the PATH."; } else { startError = startError + " Make sure " + perlInterpreterExec + " exists."; } } *pErrorBuffer = startError.adoptImpl(); return kSTAFServiceConfigurationError; } // Now we need to wait for it to start bool perlInterpreterReady = false; for (int i = 0; (i < 30) & !perlInterpreterReady; ++i) { // XXX: Need to check to see if the PerlInterpreter is actually // still running try { // First connect to the PerlInterpreter STAFConnectionPtr connPtr = perlInterpreterData.fConnProv->connect(sLocal); // Now see if it's alive connPtr->writeUInt(PERL_SERVICE_PERL_INTERPRETER_PING); STAFRC_t perlInterpreterRC = connPtr->readUInt(); STAFString perlInterpreterResultString = connPtr->readString(); if (perlInterpreterRC != kSTAFOk) { perlInterpreterResultString = "Error starting PerlInterpreter: " + perlInterpreterResultString; *pErrorBuffer = perlInterpreterResultString.adoptImpl(); return kSTAFServiceConfigurationError; } perlInterpreterReady = true; } catch (STAFException e) { //e.trace("PLSTAF.STAFServiceConstruct - error connecting"); } if (!perlInterpreterReady) STAFThreadSleepCurrentThread(1000, 0); } // If we didn't time out waiting for the PerlInterpreter then we // add the PerlInterpreter to the map of PerlInterpreters if (!perlInterpreterReady) { *pErrorBuffer = STAFString( "Unable to connect to Perl Interpreter").adoptImpl(); return kSTAFServiceConfigurationError; } sPerlInterpreterDataMap[data.fName] = PerlInterpreterDataPtr(new PerlInterpreterData(perlInterpreterData), PerlInterpreterDataPtr::INIT); } else { *pErrorBuffer = STAFString("There is already a service named " + perlServiceName + " registered with STAF").adoptImpl(); return kSTAFServiceConfigurationError; } // Ok. We have a PerlInterpreter. Now let's try to load the service. data.fPerlInterpreter = sPerlInterpreterDataMap[data.fName]; STAFConnectionPtr connPtr = data.fPerlInterpreter->fConnProv->connect(sLocal); connPtr->writeUInt(PERL_SERVICE_LOAD); connPtr->writeString(data.fName); connPtr->writeString(data.fExec); connPtr->writeString(pInfo->writeLocation);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -