📄 ecostest.cpp
字号:
};bool CeCosTest::Value ( const char *pszStr, struct tm &t, bool &bSim, StatusType &status, TargetType &target, CeCosTestUtils::String &strExecutionHostPort, CeCosTestUtils::String &strExecutableTail, CeCosTestUtils::String &strTitle, int &nFileSize, CeCosTestUtils::Duration &nTotalTime, CeCosTestUtils::Duration &nMaxInactiveTime, CeCosTestUtils::Duration &nDownloadTime, CeCosTestUtils::Duration &nElapsedTimeout, CeCosTestUtils::Duration &nActiveTimeout, int &nDownloadedSize){ bSim=(0!=strstr(pszStr,"Cpu:")); int nLen=strlen(pszStr); strExecutionHostPort.SetLength(nLen); CeCosTestUtils::String strTarget; strTarget.SetLength(nLen); CeCosTestUtils::String strStatus; strStatus.SetLength(nLen); strExecutableTail.SetLength(nLen); strTitle.SetLength(nLen); nFileSize=nTotalTime=nMaxInactiveTime=nDownloadTime=nElapsedTimeout=nActiveTimeout=nDownloadedSize=0; int nTotalTimeFrac=0; int nMaxInactiveTimeFrac=0; int nActiveTimeoutFrac=0; int nDownloadTimeFrac=0; int nElapsedTimeoutFrac=0; static CeCosTestUtils::String arstrFormat[2]; CeCosTestUtils::String &strFormat=arstrFormat[bSim]; if(0==strFormat.GetLength()){ // Construct a version of the format string sans length attributes for %s items const char *c=arpszFormat[bSim]; char *d=strFormat.GetBuffer(strlen(arpszFormat[bSim])); while(*c){ if('%'==c[0] && isdigit(c[1])){ *d++='%'; do { c++; } while (isdigit(*c)); } *d++=*c++; } *d='\0'; strFormat.ReleaseBuffer(); } if(bSim){ sscanf(pszStr, strFormat, &t.tm_year,&t.tm_mon,&t.tm_mday, &t.tm_hour,&t.tm_min,&t.tm_sec, // CeCosTestUtils::Time of day strExecutionHostPort.GetBuffer(), // Execution host:port strTarget.GetBuffer(), // Target strExecutableTail.GetBuffer(), // Executable strStatus.GetBuffer(), // Result &nDownloadedSize,&nFileSize, // Sizes &nTotalTime,&nTotalTimeFrac, // Times &nMaxInactiveTime,&nMaxInactiveTimeFrac, &nActiveTimeout,&nActiveTimeoutFrac, strTitle.GetBuffer() // Title ); } else { sscanf(pszStr, strFormat, &t.tm_year,&t.tm_mon,&t.tm_mday, &t.tm_hour,&t.tm_min,&t.tm_sec, // CeCosTestUtils::Time of day strExecutionHostPort.GetBuffer(), // Execution host:port strTarget.GetBuffer(), // Target strExecutableTail.GetBuffer(), // Executable strStatus.GetBuffer(), // Result &nDownloadedSize,&nFileSize, // Sizes &nDownloadTime,&nDownloadTimeFrac, // Times &nElapsedTimeout,&nElapsedTimeoutFrac, &nTotalTime,&nTotalTimeFrac, &nMaxInactiveTime,&nMaxInactiveTimeFrac, &nActiveTimeout,&nActiveTimeoutFrac, strTitle.GetBuffer() // Title ); } // Hack TargetType tt=FromStr(strExecutionHostPort); if(TargetTypeMax!=tt){ CeCosTestUtils::String str(pszStr); char *c=str.GetBuffer(); c[21]='x'; c[22]=':'; c[23]='0'; bool rc=Value( c, t, bSim, status, target,// exetype, strExecutionHostPort, strExecutableTail, strTitle, nFileSize, nTotalTime, nMaxInactiveTime, nDownloadTime, nElapsedTimeout, nActiveTimeout, nDownloadedSize); strExecutionHostPort=""; return rc; } // end hack const char *c1=strchr(pszStr,'"'); if(c1){ c1++; const char *c2=strchr(c1+1,'"'); if(c2){ strTitle.SetLength(c2-c1); strncpy(strTitle.GetBuffer(),c1,c2-c1)[c2-c1]='\0'; } } nTotalTime=nTotalTime*1000+nTotalTimeFrac*100; nMaxInactiveTime=nMaxInactiveTime*1000+nMaxInactiveTimeFrac*100; nActiveTimeout=nActiveTimeout*1000+nActiveTimeoutFrac*100; nDownloadTime=nDownloadTime*1000+nDownloadTimeFrac*100; nElapsedTimeout=nElapsedTimeout*1000+nElapsedTimeoutFrac*100; strExecutionHostPort.ReleaseBuffer(); target=FromStr(strTarget); strTarget.ReleaseBuffer(); strExecutableTail.ReleaseBuffer(); status=StatusTypeValue(strStatus); strStatus.ReleaseBuffer(); strTitle.ReleaseBuffer(); nFileSize*=1024; nDownloadedSize*=1024; t.tm_year-=1900; t.tm_mon--; return t.tm_year>=0 && t.tm_year<=200 && t.tm_mon>=0 && t.tm_mon<=11 && t.tm_mday>=1 && t.tm_mday<=31 && t.tm_hour>=0 && t.tm_hour<=23 && t.tm_min>=0 && t.tm_min<=59 && t.tm_sec>=0 && t.tm_sec<=59 && status!=StatusTypeMax && target!=TargetTypeMax //&& exetype!=ExecutionParameters::ExecutableTypeMax ;}const char * const CeCosTest::ResultString() const{ CeCosTestUtils::String strTitle(m_strTitle); CeCosTestUtils::String strExecutionHostPort(m_strExecutionHostPort); if(0==strTitle.GetLength()){ GetSimpleHostName(strTitle); strTitle+=':'; strTitle+=m_strExecutable; } if(0==strExecutionHostPort.GetLength()){ GetSimpleHostName(strExecutionHostPort); strExecutionHostPort+=":0"; } ENTERCRITICAL; time_t ltime; time(<ime); struct tm *now=localtime( <ime ); if(Sim()){ m_strResultString.Format( arpszFormat[1], 1900+now->tm_year,1+now->tm_mon,now->tm_mday, now->tm_hour,now->tm_min,now->tm_sec, // CeCosTestUtils::Time of day (const char *)strExecutionHostPort, // Execution host:port Image(Target()), // Target ExecutableTail(), // Executable Image(Status()), // Result m_nStrippedSize/1024,m_nFileSize/1024, // Sizes WF(m_nTotalTime), // Times WF(m_nMaxInactiveTime),WF(ActiveTimeout()), (const char *)strTitle // Title ); } else { m_strResultString.Format( arpszFormat[0], 1900+now->tm_year,1+now->tm_mon,now->tm_mday, now->tm_hour,now->tm_min,now->tm_sec, // CeCosTestUtils::Time of day (const char *)strExecutionHostPort, // Execution host:port Image(Target()), // Target ExecutableTail(), // Executable Image(Status()), // Result m_nStrippedSize/1024,m_nFileSize/1024, // Sizes WF(m_nDownloadTime),WF(ElapsedTimeout()),WF(m_nTotalTime),// Times WF(m_nMaxInactiveTime),WF(ActiveTimeout()), (const char *)strTitle // Title ); } if(m_strOutput.GetLength()>0){ m_strResultString+='\n'; m_strResultString+=m_strOutput; } LEAVECRITICAL; return m_strResultString;}static void CALLBACK AcceptThreadFuncCallback(CeCosTest*pTest,void *pParam){ pParam; // prevent compiler warnings VTRACE("AcceptThreadFuncCallback() - Deleting pTest\n"); unlink(pTest->Executable()); delete pTest; VTRACE("AcceptThreadFuncCallback() - pTest deleted\n");}bool CeCosTest::RunAgent(int nTcpPort){ bool bLocked=false; char szMyname[256]; gethostname(szMyname,sizeof szMyname); nAuxPort=nTcpPort+3000;//hack nAuxListenSock=CeCosTestSocket::Listen(nAuxPort);//hack if(-1!=nAuxListenSock){ // Create socket int nSock = CeCosTestSocket::Listen(nTcpPort); int nLastClient=0; int nRejectionCount=0; if (-1!=nSock) { for (;;) { CeCosTestSocket *pSock=new CeCosTestSocket(nSock); // AcceptThreadFunc deletes if not deleted below //pSock->SetSocketOptions(); ExecutionParameters::Data buf; // Read the execution parameters if(!pSock->recv(buf, sizeof buf)){ // Socket error on the recv - nothing much we can do TRACE("RunAgent : could not read execution parameters\n"); delete pSock; pSock=0; } else { const ExecutionParameters e(buf); bool bNuisance=false; ServerStatus s; CPort *pPort=0; if(!e.IsValid()){ // Looks like a confused client ... TRACE("Bad target value %8x read from client\n",e.Target()); s=SERVER_CANT_RUN; } else if(0==CPort::Count(e)){ // No chance of running this test TRACE("Cannot run a %s test from this server\n",Image(e.Target())); s=SERVER_CANT_RUN; } else { switch(e.Request()) { case ExecutionParameters::LOCK: if(bLocked){ s=SERVER_BUSY; } else { bLocked=true; s=SERVER_LOCKED; } break; case ExecutionParameters::UNLOCK: if(bLocked){ bLocked=false; s=SERVER_READY; } else { s=SERVER_BUSY; } break; case ExecutionParameters::QUERY: case ExecutionParameters::RUN: if (bLocked) { s=SERVER_LOCKED; } else { pPort=CPort::GetPort(e); if(0==pPort){ // We must disappoint our client nRejectionCount++; s=SERVER_BUSY; /* } else if(nLastClient==pSock->Client() && nRejectionCount>10) { // Don't answer the phone to a nuisance caller s=SERVER_BUSY; bNuisance=true; nRejectionCount--; pPort->Release(); */ } else { s=SERVER_READY; nRejectionCount=0; nLastClient=pSock->Client(); } } break; case ExecutionParameters::STOP: s=SERVER_READY; break; default: s=SERVER_CANT_RUN; } } #ifndef VERBOSE if(ExecutionParameters::QUERY!=e.Request()) #endif TRACE("RunAgent : %s request tActive=%d tElapsed=%d Target=%s Reply status=%s Nuisance=%d\n", e.Image(e.Request()), e.ActiveTimeout(),e.ElapsedTimeout(),Image(e.Target()), Image(s), bNuisance); bool bSendok=pSock->send (&s, sizeof s); VTRACE("RunAgent() : Send complete = rc was <%d>\n", bSendok); if(SERVER_READY==s && bSendok && ExecutionParameters::RUN==e.Request()){ // Create a new class instance // AcceptThreadFunc deletes the instance and closes new_sock // RunLocal, called by AcceptThreadFunc, releases the port // We dream up a temporary name for the executable // No need for meaningful callback, but must run asynchronously CeCosTest *pTest=new CeCosTest(e,tmpnam(0)); pTest->m_pSock=pSock; pTest->m_strExecutionHostPort.Format("%s:%d",szMyname,nTcpPort); pTest->m_pPort=pPort; pTest->RUNTHREAD(AcceptThreadFunc,0,Callback(AcceptThreadFuncCallback,0)); // AcceptThreadFunc deletes pSock } else { delete pSock; pSock=0; if(pPort){ pPort->Release(); pPort=0; } if(CeCosTest::ExecutionParameters::STOP==e.Request()){ CancelAllInstances(); WaitForAllInstances(1000,20*1000); break; } } } } CeCosTestSocket::CloseSocket (nSock); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -