示例#1
0
int ArgusTVRPC(const std::string& command, const std::string& arguments, std::string& json_response)
{
    PLATFORM::CLockObject critsec(communication_mutex);
    std::string url = g_szBaseURL + command;
    int retval = E_FAILED;
    XBMC->Log(LOG_DEBUG, "URL: %s\n", url.c_str());
    void* hFile = XBMC->OpenFileForWrite(url.c_str(), 0);
    if (hFile != NULL)
    {
        int rc = XBMC->WriteFile(hFile, arguments.c_str(), arguments.length());
        if (rc >= 0)
        {
            std::string result;
            result.clear();
            char buffer[1024];
            while (XBMC->ReadFileString(hFile, buffer, 1023))
                result.append(buffer);
            json_response = result;
            retval = 0;
        }
        else
        {
            XBMC->Log(LOG_ERROR, "can not write to %s", url.c_str());
        }
        XBMC->CloseFile(hFile);
    }
    else
    {
        XBMC->Log(LOG_ERROR, "can not open %s for write", url.c_str());
    }
    return retval;
}
示例#2
0
  int ForTheRecordRPC(const std::string& command, const std::string& arguments, std::string& json_response)
  {
    CURL *curl;
    CURLcode res;
    std::string url = g_szBaseURL + command;
    PLATFORM::CLockObject critsec(communication_mutex);

    XBMC->Log(LOG_DEBUG, "URL: %s\n", url.c_str());

    curl = curl_easy_init();

    if(curl)
    {
      struct curl_slist *chunk = NULL;

      chunk = curl_slist_append(chunk, "Content-type: application/json; charset=UTF-8");
      chunk = curl_slist_append(chunk, "Accept: application/json; charset=UTF-8");

      /* Specify the URL */
      if ((res = curl_easy_setopt(curl, CURLOPT_URL, url.c_str())) != CURLE_OK)
        XBMC->Log(LOG_NOTICE, "curl_easy_setop CURLOPT_URL returned %d (%s).\n", res, curl_easy_strerror(res));
      if ((res = curl_easy_setopt(curl, CURLOPT_TIMEOUT, g_iConnectTimeout)) != CURLE_OK)
        XBMC->Log(LOG_NOTICE, "curl_easy_setop CURLOPT_TIMEOUT returned %d (%s).\n", res, curl_easy_strerror(res));
      if ((res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk)) != CURLE_OK)
        XBMC->Log(LOG_NOTICE, "curl_easy_setop CURLOPT_HTTPHEADER returned %d (%s).\n", res, curl_easy_strerror(res));
      /* Now specify the POST data */
      if ((res = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, arguments.c_str())) != CURLE_OK)
        XBMC->Log(LOG_NOTICE, "curl_easy_setop CURLOPT_POSTFIELDS returned %d (%s).\n", res, curl_easy_strerror(res));
      /* Define our callback to get called when there's data to be written */ 
      if ((res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_data)) != CURLE_OK)
        XBMC->Log(LOG_NOTICE, "curl_easy_setop CURLOPT_WRITEFUNCTION returned %d (%s).\n", res, curl_easy_strerror(res));
      /* Set a pointer to our struct to pass to the callback */ 
      json_response = "";
      if ((res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &json_response)) != CURLE_OK)
        XBMC->Log(LOG_NOTICE, "curl_easy_setop CURLOPT_WRITEDATA returned %d (%s).\n", res, curl_easy_strerror(res));

      /* debugging only */
      if (l_logCurl)
      {
        if ((res = curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_curl_debug_callback)) != CURLE_OK)
          XBMC->Log(LOG_NOTICE, "curl_easy_setop CURLOPT_DEBUGFUNCTION returned %d (%s).\n", res, curl_easy_strerror(res));
        if ((res = curl_easy_setopt(curl, CURLOPT_VERBOSE, 1)) != CURLE_OK)
          XBMC->Log(LOG_NOTICE, "curl_easy_setop CURLOPT_VERBOSE returned %d (%s).\n", res, curl_easy_strerror(res));
      }

      /* Perform the request */
      if ((res = curl_easy_perform(curl)) != CURLE_OK)
        XBMC->Log(LOG_NOTICE, "curl_easy_perform returned %d (%s).\n", res, curl_easy_strerror(res));

      /* always cleanup */
      curl_easy_cleanup(curl);
      return 0;
    }
    else
    {
      return E_FAILED;
    }
  }
示例#3
0
int ArgusTVRPCToFile(const std::string& command, const std::string& arguments, std::string& filename, long& http_response)
{
    PLATFORM::CLockObject critsec(communication_mutex);
    std::string url = g_szBaseURL + command;
    int retval = E_FAILED;
    XBMC->Log(LOG_DEBUG, "URL: %s writing to file %s\n", url.c_str(), filename.c_str());
    /* Open the output file */
    FILE *ofile = fopen(filename.c_str(), "w+b");
    if (ofile == NULL)
    {
        XBMC->Log(LOG_ERROR, "can not open %s", filename.c_str());
        return E_FAILED;
    }
    else
    {
        void* hFile = XBMC->OpenFileForWrite(url.c_str(), 0);
        if (hFile != NULL)
        {
            http_response = XBMC->WriteFile(hFile, arguments.c_str(), arguments.length());
            if (http_response >= 0)
            {
                byte buffer[1024];
                int bytesRead = 0;
                retval = 0;
                do
                {
                    bytesRead = XBMC->ReadFile(hFile, buffer, sizeof(buffer));
                    int written = fwrite(buffer, sizeof(byte), bytesRead, ofile);
                    if (bytesRead != written)
                    {
                        XBMC->Log(LOG_ERROR, "Error while writing to %s (%d bytes written, while asked to write %d bytes).",
                                  filename.c_str(), written, bytesRead);
                        retval = E_FAILED;
                        break;
                    }
                } while (bytesRead == sizeof(buffer));
            }
            else
            {
                XBMC->Log(LOG_ERROR, "can not write to %s", url.c_str());
            }
            XBMC->CloseFile(hFile);
        }
        else
        {
            XBMC->Log(LOG_ERROR, "can not open %s for write", url.c_str());
        }
        /* close output file */
        fclose(ofile);
    }
    return retval;
}
示例#4
0
  int ForTheRecordRPCToFile(const std::string& command, const std::string& arguments, std::string& filename, long& http_response)
  {
    CURL *curl;
    CURLcode res;
    std::string url = g_szBaseURL + command;
    PLATFORM::CLockObject critsec(communication_mutex);

    XBMC->Log(LOG_DEBUG, "URL: %s writing to file %s\n", url.c_str(), filename.c_str());

    curl = curl_easy_init();

    if(curl)
    {
      struct curl_slist *chunk = NULL;

      chunk = curl_slist_append(chunk, "Content-type: application/json; charset=UTF-8");
      chunk = curl_slist_append(chunk, "Accept: application/json; charset=UTF-8");

      /* Specify the URL */
      if ((res = curl_easy_setopt(curl, CURLOPT_URL, url.c_str())) != CURLE_OK)
        XBMC->Log(LOG_NOTICE, "curl_easy_setop CURLOPT_URL returned %d (%s).\n", res, curl_easy_strerror(res));
      if ((res = curl_easy_setopt(curl, CURLOPT_TIMEOUT, g_iConnectTimeout)) != CURLE_OK)
        XBMC->Log(LOG_NOTICE, "curl_easy_setop CURLOPT_TIMEOUT returned %d (%s).\n", res, curl_easy_strerror(res));
      if ((res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk)) != CURLE_OK)
        XBMC->Log(LOG_NOTICE, "curl_easy_setop CURLOPT_HTTPHEADER returned %d (%s).\n", res, curl_easy_strerror(res));
      /* Now specify the POST data */
      if ((res = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, arguments.c_str())) != CURLE_OK)
        XBMC->Log(LOG_NOTICE, "curl_easy_setop CURLOPT_POSTFIELDS returned %d (%s).\n", res, curl_easy_strerror(res));
      /* ask curl to write to file */ 
      if ((res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_data_to_file)) != CURLE_OK)
        XBMC->Log(LOG_NOTICE, "curl_easy_setop CURLOPT_WRITEFUNCTION returned %d (%s).\n", res, curl_easy_strerror(res));
      /* Open the output file */
      FILE *ofile = fopen(filename.c_str(), "w+b");
      if (ofile == NULL)
      {
        XBMC->Log(LOG_ERROR, "can not open %s", filename.c_str());
        curl_easy_cleanup(curl);
        return E_FAILED;
      }
      if ((res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, ofile)) != CURLE_OK)
        XBMC->Log(LOG_NOTICE, "curl_easy_setop CURLOPT_WRITEDATA returned %d (%s).\n", res, curl_easy_strerror(res));

      /* debugging only */
      if (l_logCurl)
      {
        if ((res = curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_curl_debug_callback)) != CURLE_OK)
          XBMC->Log(LOG_NOTICE, "curl_easy_setop CURLOPT_DEBUGFUNCTION returned %d (%s).\n", res, curl_easy_strerror(res));
        if ((res = curl_easy_setopt(curl, CURLOPT_VERBOSE, 1)) != CURLE_OK)
          XBMC->Log(LOG_NOTICE, "curl_easy_setop CURLOPT_VERBOSE returned %d (%s).\n", res, curl_easy_strerror(res));
      }

      /* Perform the request */
      if ((res = curl_easy_perform(curl)) != CURLE_OK)
        XBMC->Log(LOG_NOTICE, "curl_easy_perform returned %d (%s).\n", res, curl_easy_strerror(res));

      /* Retrieve HTTP response code */
      if ((res = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_response)) != CURLE_OK)
        XBMC->Log(LOG_NOTICE, "curl_easy_getinfo CURLINFO_RESPONSE_CODE returned %d (%s).\n", res, curl_easy_strerror(res));

      /* close output file */
      fclose(ofile);

      /* always cleanup */
      curl_easy_cleanup(curl);
      return 0;
    }
    else
    {
      return E_FAILED;
    }
  }