Esempio n. 1
0
int ACDAudio::run(void* pArg)
{
   Os::Logger::instance().log(FAC_ACD, gACD_DEBUG, "ACDAudio::run - starting get from: %s", mUriString.data());

   HttpMessage *pGetRequest = new HttpMessage;

   pGetRequest->get(mUri, HTTP_GET_TIMEOUT);

   UtlString status;

   pGetRequest->getResponseStatusText(&status);

   if (status == "OK") {
      UtlString audioData;
      ssize_t audioLength;

      const HttpBody* pResponseBody = pGetRequest->getBody();
      pResponseBody->getBytes(&audioData, &audioLength);

      Os::Logger::instance().log(FAC_ACD, gACD_DEBUG, "ACDAudio::run - received %zd bytes from: %s\n",
                    audioLength, mUriString.data());

      // Now save the downloaded audio to a local file
      size_t writeLength;

      OsFile audioFile(mAudioPath);
      if (audioFile.open(OsFile::CREATE) != OS_SUCCESS) {
         Os::Logger::instance().log(FAC_ACD, PRI_ERR, "ACDAudio::run - "
                       "Unable to create audio file: %s", mAudioPath.data());
      }
      else {
         audioFile.write(audioData, audioLength, writeLength);
         audioFile.close();
      }
   }
   else {
      Os::Logger::instance().log(FAC_ACD, PRI_ERR, "ACDAudio::run - failed get from: %s", mUriString.data());
   }

   delete pGetRequest;

   return 0;
}
Esempio n. 2
0
bool XmlRpcRequest::execute(XmlRpcResponse& response)
{
   bool result = false;

   // End of constructing the XML-RPC body
   mpRequestBody->append(END_PARAMS END_METHOD_CALL);

   if (OsSysLog::willLog(FAC_XMLRPC, PRI_INFO))
   {
      UtlString logString;
      ssize_t   logLength;
      mpRequestBody->getBytes(&logString, &logLength);
      if (logString.length() > XmlRpcBody::MAX_LOG)
      {
         logString.remove(XmlRpcBody::MAX_LOG);
         logString.append("\n...");
      }
      UtlString urlString;
      mUrl.toString(urlString);
      OsSysLog::add(FAC_XMLRPC, PRI_INFO,
                    "XmlRpcRequest::execute XML-RPC to '%s' request =\n%s",
                    urlString.data(),
                    logString.data());
   }

   mpHttpRequest->setContentLength(mpRequestBody->getLength());
   mpHttpRequest->setBody(mpRequestBody);
   mpRequestBody = NULL; // the HttpMessage now owns the request body

   // Create an empty response object and sent the built up request
   // to the XML-RPC server
   HttpMessage httpResponse;

   int statusCode = httpResponse.get(mUrl,*mpHttpRequest,XML_RPC_TIMEOUT,true /* persist conn */ );
   if (statusCode/100 == 2)
   {
      UtlString bodyString;
      ssize_t   bodyLength;

      httpResponse.getBody()->getBytes(&bodyString, &bodyLength);

      UtlString logString;
      if (bodyString.length() > XmlRpcBody::MAX_LOG)
      {
         logString.append(bodyString, 0, XmlRpcBody::MAX_LOG);
         logString.append("\n...");
      }
      else
      {
         logString = bodyString;
      }

      if (response.parseXmlRpcResponse(bodyString))
      {
         result = true;
         OsSysLog::add(FAC_XMLRPC, PRI_INFO,
                       "XmlRpcRequest::execute XML-RPC received valid response = \n%s",
                       logString.data());
      }
      else
      {
         OsSysLog::add(FAC_XMLRPC, PRI_ERR,
                       "XmlRpcRequest::execute XML-RPC received fault response = \n%s",
                       logString.data());
      }
   }
   else if (statusCode == -1)
   {
      response.setFault(XmlRpcResponse::ConnectionFailure, CONNECTION_FAILURE_FAULT_STRING);

      OsSysLog::add(FAC_XMLRPC, PRI_ERR,
                    "XmlRpcRequest::execute http connection failed");
   }
   else // some non-2xx HTTP response
   {
      UtlString statusText;

      httpResponse.getResponseStatusText(&statusText);
      response.setFault(XmlRpcResponse::HttpFailure, statusText.data());

      OsSysLog::add(FAC_XMLRPC, PRI_INFO,
                    "XmlRpcRequest::execute http request failed; status = %d %s",
                    statusCode, statusText.data());
   }

   return result;
}