예제 #1
0
bool AMySQLServer::reconnect(AString& error)
{
  AASSERT_EX(this, mp_mydata, ASWNL("mp_mydata is NULL, init must not have been called"));
  if (!mp_mydata)
  {
    error = "MySQL library not initialized";
    return false;
  }

  if (!mysql_real_connect( 
    mp_mydata,
    m_urlConnection.getServer().c_str(),
    m_urlConnection.getUsername().c_str(),
    m_urlConnection.getPassword().c_str(),
    m_urlConnection.getBaseDirName().c_str(),
    m_urlConnection.getPort(),
    NULL,
    0 )
  )
  {
    error = "Unable to connect to MySQL server: ";
    m_urlConnection.emit(error);
    error += ";";
    return false;
  }

  return true;
}
예제 #2
0
bool AMySQLServer::init(AString& error)
{
  AASSERT_EX(this, !mp_mydata, ASWNL("mp_mydata is not NULL, init must have been already called"));
  mp_mydata = mysql_init((MYSQL*) 0);
  if (!mp_mydata)
  {
    error = "Unable to initialize MySQL library in call to init";
    return false;
  }
  
  if (!mysql_real_connect( 
    mp_mydata,
    m_urlConnection.getServer().c_str(),
    m_urlConnection.getUsername().c_str(),
    m_urlConnection.getPassword().c_str(),
    m_urlConnection.getBaseDirName().c_str(),
    m_urlConnection.getPort(),
    NULL,
    0 )
  )
  {
    error = "Unable to connect to MySQL server: ";
    m_urlConnection.emit(error);
    error += ";";
    return false;
  }
  
  mbool_Initialized = true;
  return true;
}
예제 #3
0
파일: AXmlElement.cpp 프로젝트: achacha/AOS
AXmlElement& AXmlElement::addContent(
  AXmlElement *pnode, 
  const AString& path, // = AConstant::ASTRING_EMPTY
  bool insertIntoFront // = false
)
{
  if (path.isEmpty() || path.equals(AConstant::ASTRING_SLASH))
  {
    AASSERT(this, m_Content.size() < DEBUG_MAXSIZE_AXmlElement);  //Debug only limit
    AASSERT(this, pnode);

    pnode->setParent(this);
    if (insertIntoFront)
      m_Content.push_front(pnode);
    else
      m_Content.push_back(pnode);
  }
  else
  {
    LIST_AString parts;
    path.split(parts, '/');
    if (!parts.size())
      ATHROW(this, AException::InvalidParameter);

    AXmlElement *pNewParent = _createAndAppend(parts, this, insertIntoFront);
    AASSERT_EX(this, pNewParent, path);
    pNewParent->addContent(pnode, AConstant::ASTRING_EMPTY, insertIntoFront);
  }
  return *this;
}
예제 #4
0
size_t AFile_Physical::_write(const void *buf, size_t size)
{
  AASSERT_EX(this, m_fid >= 0, ASWNL("Probably forgot to call open() or invalid file handle after call to open"));   // m_fid == -1? forgot to call open()?
  if (mp_file)
  {
    size_t written = ::_write(m_fid, buf, size);
    if (AConstant::npos == written || written < size)
      ATHROW_ERRNO(this, AException::UnableToWrite, errno);
    
    return written;
  }
  else
    ATHROW(this, AException::NotOpen);
}
예제 #5
0
size_t AFile_Physical::_read(void *buf, size_t size)
{
  AASSERT_EX(this, m_fid >= 0, ASWNL("Probably forgot to call open() or invalid file handle after call to open"));   // m_fid == -1? forgot to call open()?
  if (mp_file)
  {
    size_t bytesread = ::_read(m_fid, buf, size);
    if (AConstant::npos == bytesread)
      ATHROW_ERRNO(this, AException::UnableToRead, errno);
    
    return bytesread;
  }
  else
    ATHROW(this, AException::NotOpen);
}
예제 #6
0
size_t AFile_Physical::access(
  AOutputBuffer& target, 
  size_t index,          //= 0 
  size_t bytes           // = AConstant::npos
) const
{
  AASSERT_EX(this, m_fid >= 0, ASWNL("Probably forgot to call open() or invalid file handle after call to open"));   // m_fid == -1? forgot to call open()?
  if (!mp_file)
    ATHROW(this, AException::NotOpen);

  //a_Get original position
  u8 originalIndex = 0;
#ifdef _ftelli64
  originalIndex = _ftelli64(mp_file);
#else
  originalIndex = (u8)ftell(mp_file);
#endif

  //a_Seek to new position
#ifdef _fseeki64
  if (_fseeki64(mp_file, index, 0))
    ATHROW_EX(this, AException::InvalidParameter, AString("Unable to seek position: ")+AString::fromSize_t(index));
#else
  if (fseek(mp_file, (long)index, 0))
    ATHROW_EX(this, AException::InvalidParameter, AString("Unable to seek position: ")+AString::fromSize_t(index));
#endif

  //a_Peek some data
  const size_t BUFFER_SIZE = 10240;
  char buffer[BUFFER_SIZE];
  size_t totalBytes = 0;
  while (bytes)
  {
    size_t bytesToRead = (bytes > BUFFER_SIZE ? BUFFER_SIZE : bytes);
    size_t bytesRead = ::_read(m_fid, buffer, bytesToRead);

    //a_EOF or unavail
    if (AConstant::npos == bytesRead || AConstant::unavail == bytesRead)
      return bytesRead;
    
    //a_Partial read
    if (bytesRead < bytesToRead)
    {
      totalBytes += bytesRead;
      break;
    }

    size_t written = target.append(buffer, bytesRead);
    if (AConstant::unavail == written || AConstant::npos == written)
    {
      return (totalBytes > 0 ? totalBytes : written);
    }
    else
    {
      bytes -= bytesRead;
      totalBytes += bytesRead;
    }
  }
  
#ifdef _fseeki64
  if (_fseeki64(mp_file, originalIndex, 0))
    ATHROW_EX(this, AException::InvalidParameter, AString("Unable to restore seek position: ")+AString::fromSize_t(originalIndex));
#else
  if (fseek(mp_file, (long)originalIndex, 0))
    ATHROW_EX(this, AException::InvalidParameter, AString("Unable to restore seek position: ")+AString::fromSize_t(originalIndex));
#endif

  return totalBytes;
}
예제 #7
0
void AOSContextManager::setQueueForState(AOSContextManager::ContextQueueState state, AOSContextQueueInterface *pQueue)
{
  AASSERT_EX(this, !m_Queues.at(state), ASWNL("State already associated with a queue, deleting and overwriting"));
  m_Queues[state] = pQueue;
}
예제 #8
0
u4 AOSContextQueue_ErrorExecutor::_threadproc(AThread& thread)
{
  AOSContextQueue_ErrorExecutor *pThis = dynamic_cast<AOSContextQueue_ErrorExecutor *>(thread.getThis());
  AASSERT(&thread, pThis);
  AOSContext *pContext = NULL;

  thread.setRunning(true);
  while(thread.isRun())
  {
    try
    {
      while (pContext = pThis->_nextContext()) 
      {
#ifndef NDEBUG
        if (!ADebugDumpable::isPointerValid(pContext))
        {
          AString error("AOSContext pointer is invalid: ");
          error.append(AString::fromPointer(pContext));
          AASSERT_EX(NULL, false, error);
          continue;
        }
#endif
        pContext->useEventVisitor().startEvent(ASW("AOSContextQueue_ErrorExecutor: Processing error condition", 57));

        //a_Should only be here if an error occured, if status not set >200, then assume 500
        if (pContext->useResponseHeader().getStatusCode() == AHTTPResponseHeader::SC_200_Ok)
          pContext->useResponseHeader().setStatusCode(AHTTPResponseHeader::SC_500_Internal_Server_Error);

        //a_Check is socket was closed, if so do nothing else, we are done
        if (pContext->useConnectionFlags().isSet(AOSContext::CONFLAG_IS_SOCKET_ERROR))
        {
          //a_Proceed
          m_Services.useContextManager().changeQueueState(AOSContextManager::STATE_TERMINATE, &pContext);
          continue;
        }

        if (
             pContext->useContextFlags().isSet(AOSContext::CTXFLAG_IS_RESPONSE_HEADER_SENT)
          || pContext->useContextFlags().isSet(AOSContext::CTXFLAG_IS_OUTPUT_SENT)
        )
        {
          //a_Log to file, output is done already
          m_Services.useLog().add(pContext->useEventVisitor(), ALog::EVENT_FAILURE);
        }
        else
        {          

          //a_Add XSLT stylesheet for the error XML
          //if (m_Services.useConfiguration().exists(ASW("/config/server/error-stylesheet",31)))
          //{
          //  AString errorStylesheet;
          //  m_Services.useConfiguration().emitString(ASW("/config/server/error-stylesheet",31), errorStylesheet);
          //  pContext->useModelXmlDocument().addInstruction(AXmlInstruction::XML_STYLESHEET)
          //    .addAttribute(ASW("type",4), ASW("text/xsl",8))
          //    .addAttribute(ASW("href",4), errorStylesheet);
          //}

          //a_Add request header to result XML
          if (!pContext->useModel().exists(ASW("REQUEST",7)))
            pContext->useRequestHeader().emitXml(pContext->useModel().overwriteElement(ASW("REQUEST",7)));
          pContext->useResponseHeader().emitXml(pContext->useModel().overwriteElement(ASW("RESPONSE",8)));

          //a_Check if dumpContext is specified to override and emit XML
          int dumpContextLevel = pContext->getDumpContextLevel();
          pContext->dumpContext(dumpContextLevel);
          if (dumpContextLevel > 0)
          {
            //a_Write contents of the output XML instead of output buffer
            m_Services.useConfiguration().setMimeTypeFromExt(ASW("xml",3), *pContext);
            pContext->useResponseHeader().setStatusCode(AHTTPResponseHeader::SC_200_Ok);
            pContext->writeOutputBuffer(true);
          }
          else
          {
            //a_Set the current content type as text/html
            pContext->useResponseHeader().set(AHTTPHeader::HT_ENT_Content_Type, ASW("text/html; charset=utf-8",24));

            int statusCode = pContext->useResponseHeader().getStatusCode();
            
            AAutoPtr<ATemplate> pTemplate(NULL, false);  //a_Call to cache manager will set a template
            pContext->clearOutputBuffer();
            if (m_Services.useCacheManager().getStatusTemplate(statusCode, pTemplate))
            {
              //a_Template for this status code is found, so process and emit into output buffer
              pTemplate->process(pContext->useLuaTemplateContext(), pContext->useOutputBuffer());
              if (pContext->useEventVisitor().isLogging(AEventVisitor::EL_DEBUG))
              {
                ARope rope("Using error template for status ");
                rope.append(AString::fromInt(statusCode));
                pContext->useEventVisitor().startEvent(rope, AEventVisitor::EL_DEBUG);
              }
            }
            else
            {
              if (pContext->useEventVisitor().isLogging(AEventVisitor::EL_WARN))
              {
                ARope rope("Did not find error template for status ");
                rope.append(AString::fromInt(statusCode));
                pContext->useEventVisitor().startEvent(rope, AEventVisitor::EL_WARN);
              }
            }

            if (pContext->isOutputBufferEmpty())
            {
              AString strError(1024, 256);
              strError.assign("Error ",6);
              strError.append(AString::fromInt(pContext->useResponseHeader().getStatusCode()));
              strError.append(": ", 2);
              strError.append(pContext->useResponseHeader().getStatusCodeReasonPhrase(pContext->useResponseHeader().getStatusCode()));

              //a_Put some generic stuff since there is no error template
              pContext->useOutputBuffer().append("<html><head><title>",19);
              pContext->useOutputBuffer().append(strError);
              pContext->useOutputBuffer().append("</title></head>",15);
              pContext->useOutputBuffer().append("<body>",6);
              pContext->useOutputBuffer().append(strError);
              pContext->useOutputBuffer().append("</body></html>",14);
            }

            try
            {
              pContext->writeOutputBuffer();
            }
            catch(ASocketException& ex)
            {
              pContext->useEventVisitor().addEvent(ex, AEventVisitor::EL_ERROR);
              pContext->useConnectionFlags().setBit(AOSContext::CONFLAG_IS_SOCKET_ERROR);
              m_Services.useContextManager().changeQueueState(AOSContextManager::STATE_TERMINATE, &pContext);
              continue;
            }
          }
        }

        //a_Close connection
        pContext->useSocket().close();

        //a_Proceed
        m_Services.useContextManager().changeQueueState(AOSContextManager::STATE_TERMINATE, &pContext);
        continue;
      }
      AThread::sleep(pThis->m_SleepDelay);  //a_Empty queue, avoid thrashing
    }
    catch(AException& e)
    {
      pContext->useEventVisitor().addEvent(e, AEventVisitor::EL_ERROR);
      m_Services.useLog().add(pContext->useEventVisitor(), ALog::EVENT_FAILURE);
      m_Services.useContextManager().changeQueueState(AOSContextManager::STATE_TERMINATE, &pContext);
    }
    catch(std::exception& e)
    {
      pContext->useEventVisitor().addEvent(ASWNL(e.what()), AEventVisitor::EL_ERROR);
      m_Services.useLog().add(pContext->useEventVisitor(), ALog::EVENT_FAILURE);
      m_Services.useContextManager().changeQueueState(AOSContextManager::STATE_TERMINATE, &pContext);
    }
    catch(...)
    {
      m_Services.useLog().add(pContext->useEventVisitor(), ALog::EVENT_FAILURE);
      pContext->useEventVisitor().addEvent(ASWNL("Unknown exception caught in AOSContextQueue_ErrorExecutor::_threadproc"), AEventVisitor::EL_ERROR);
      m_Services.useContextManager().changeQueueState(AOSContextManager::STATE_TERMINATE, &pContext);
      break;
    }
  }

  thread.setRunning(false);
  return 0;
}