testresource.cpp

来自「基于ecos的redboot」· C++ 代码 · 共 544 行 · 第 1/2 页

CPP
544
字号
  Add(_T("Originator"), pResource->m_strUser);
  Add(_T("Reason"),     pResource->m_strReason);
  Add(_T("Reset"),      pResource->m_strReset);
  Add(_T("Serial"),     pResource->m_strPort);
  Add(_T("Target"),     pResource->m_Target);
  Add(_T("User"),       pResource->m_strUser);
}

bool CTestResource::Lock()
{
  if(!m_bLocked){
    m_bLocked=true;
    return true;
  } else {
    return false;
  }
}

bool CTestResource::Unlock()
{
  if(m_bLocked){
    m_bLocked=false;
    return true;
  } else {
    return false;
  }
}

bool CTestResource::LoadSocket(LPCTSTR pszResourceHostPort,Duration dTimeout/*=10*1000*/)
{
  bool rc=false;
  ENTERCRITICAL;
  CTestResource *pResource;
  // If the resource is in use, we don't dare delete it!
  for(pResource=CTestResource::First();pResource;pResource=pResource->Next()){
    pResource->m_bFlag=false;
  }
  CeCosSocket sock;
  if(sock.Connect(pszResourceHostPort,dTimeout)){
    // Write the message to the socket
    int nRequest=0; // read
    if(!sock.sendInteger(nRequest)){
      ERROR(_T("Failed to write to socket\n"));
    } else {
      int nResources;
      if(sock.recvInteger(nResources,_T(""),dTimeout)){
        rc=true;
        while(nResources--){
          String strImage;
          if(sock.recvString(strImage,_T(""),dTimeout)){
            VTRACE(_T("Recv \"%s\"\n"),(LPCTSTR)strImage);
            CTestResource tmp;
            tmp.FromStr(strImage);
            CTestResource *pResource=Lookup(tmp.HostPort());
            if(0==pResource){
              pResource=new CTestResource(_T(""),_T(""));
            }
            pResource->FromStr(strImage);
            pResource->m_bFlag=true;
          } else {
            rc=false;
            break;
          }
        }
      }
    }
  }
  // If the resource is in use, we don't dare delete it!
  CTestResource *pNext;
  for(pResource=CTestResource::First();pResource;pResource=pNext){
    pNext=pResource->Next();
    if(!pResource->m_bFlag && !pResource->m_bInUse){
      delete pResource;
    }
  }
  
  LEAVECRITICAL;
  return rc;
}

bool CTestResource::SaveSocket(LPCTSTR pszResourceHostPort,Duration dTimeout)
{
  bool rc=true;
  ENTERCRITICAL;
  CeCosSocket sock(pszResourceHostPort, dTimeout);
  if(sock.Ok()){
    // Write the message to the socket
    int nRequest=1; //write
    if(!sock.sendInteger(nRequest, _T(""),dTimeout)){
      ERROR(_T("Failed to write to socket\n"));
      rc=false;
    } else {
      int nResources=0;
      CTestResource *pResource;
      for(pResource=CTestResource::First();pResource;pResource=pResource->Next()){
        nResources++;
      }
      if(sock.sendInteger(nResources,_T("resource count"),dTimeout)){
        for(pResource=CTestResource::First();pResource;pResource=pResource->Next()){
          String strImage;
          CTestResourceProperties prop(pResource);
          strImage=prop.MakeCommandString();
          TRACE(_T("Send \"%s\"\n"),(LPCTSTR)strImage);
          if(!sock.sendString (strImage, _T("reply"),dTimeout)){
            rc=false;
            break;
          }
        }
      } else {
        rc=false;
      }
    }
  } else {
    rc=false;
  }
  LEAVECRITICAL;
  return rc;
}

/*
void CTestResource::Image(String &str)
{
  CTestResourceProperties prop(this);
  str=prop.MakeCommandString();
  VTRACE(_T("Make command string %s\n"),(LPCTSTR)str);
}
*/

bool CTestResource::FromStr(LPCTSTR pszImage)
{
  CTestResourceProperties prop(this);
  prop.LoadFromCommandString(pszImage);
  VTRACE(_T("From command string %s\n"),pszImage);
  return true;
}

void CTestResource::Chain()
{
  ENTERCRITICAL;
  nCount++;
  m_pNextInstance=pFirstInstance;
  if(m_pNextInstance){
    m_pNextInstance->m_pPrevInstance=this;
  }
  m_pPrevInstance=0;
  pFirstInstance=this;
  LEAVECRITICAL;
}

bool CTestResource::Matches  (const CeCosTest::ExecutionParameters &e,bool bIgnoreLocking) const
{
  return (bIgnoreLocking||(!m_bLocked)) && (0==_tcsicmp(e.PlatformName(),m_Target)); 
};

CeCosTest::ServerStatus CTestResource::Query() 
{
  CeCosTest::ExecutionParameters e(CeCosTest::ExecutionParameters::QUERY,m_Target);
  CeCosSocket *pSock=0;
  CeCosTest::ServerStatus s=CeCosTest::Connect(HostPort(),pSock,e,m_strInfo);
  delete pSock;
  return s;
}

int CTestResource::Count(const CeCosTest::ExecutionParameters &e)
{
  int nCount=0;
  for(const CTestResource *p=pFirstInstance;p;p=p->m_pNextInstance){
    if(p->Matches(e)){
      nCount++;
    }
  }
  return nCount;
}

bool CTestResource::Use()
{
  bool rc;
  ENTERCRITICAL;
  if(m_bInUse){
    rc=false;
  } else {
    m_bInUse=true;
    rc=true;
  }
  LEAVECRITICAL;
  return rc;
}

CTestResource *CTestResource::GetResource (const CeCosTest::ExecutionParameters &e)
{
  CTestResource *p=0;
  if(0==Count(e)){
    ERROR(_T("GetResource: no candidates available\n"));
  } else {
    ENTERCRITICAL;
    for(p=pFirstInstance;p;p=p->m_pNextInstance){
      if(p->Matches(e) && !p->m_bInUse){
        TRACE(_T("Acquired %s\n"),p->Serial());
				    p->Use();
            break;
      }
    }
    LEAVECRITICAL;
  }
  return p;
}

const String CTestResource::Image() const
{
  String str;
  str.Format(_T("%10s %20s %8s"),(LPCTSTR)HostPort(),(LPCTSTR)Target(),(LPCTSTR)Serial());
  if(IsLocked()){
    str+=_T(" [RL]");
  }
  return str;
}

bool CTestResource::Matches(LPCTSTR pszHostPort, const CeCosTest::ExecutionParameters &e)
{
  bool rc=false;
  ENTERCRITICAL;
  if(Load()){
    CTestResource *pResource=Lookup(pszHostPort);
    if(pResource){
      rc=pResource->Matches(e);
    }
  }
  LEAVECRITICAL;
  return rc;
}

CResetAttributes::ResetResult CTestResource::Reset(LogFunc *pfnLog, void *pfnLogparam)
{
  String strReset;
  strReset.Format(_T("port(%s,%d) %s"),Serial(),Baud(),ResetString());
  return CResetAttributes(strReset).Reset(pfnLog,pfnLogparam);
}

CResetAttributes::ResetResult CTestResource::Reset(String &str)
{
  return Reset(StringLogFunc,&str);
}

void CALLBACK CTestResource::StringLogFunc (void *pParam,LPCTSTR psz)
{
  *((String *)pParam)+=psz;
}


CResetAttributes::ResetResult CTestResource::RemoteReset(LogFunc *pfnLog, void *pfnLogparam)
{
  String strHost;
  int nPort;
  CeCosSocket::ParseHostPort(HostPort(),strHost,nPort);
  String strCmd;
  strCmd.Format(_T("rsh %s x10reset %s\n"),(LPCTSTR)strHost,ResetString());
  pfnLog(pfnLogparam,strCmd);

  CSubprocess sp;
  sp.Run(pfnLog,pfnLogparam,strCmd);
  return CResetAttributes::RESET_OK; // FIXME
}

String CTestResource::FileName() const
{
  String strHost;
  int nPort;
  CeCosSocket::ParseHostPort(HostPort(),strHost,nPort);
  return String::SFormat(_T("%s-%d"),(LPCTSTR)strHost,nPort);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?