Skip to content
Snippets Groups Projects
Commit 74e2a290 authored by Jacques Gouno's avatar Jacques Gouno
Browse files

TANGODEVIC-1647: updating source code

parent 5b5b8132
No related branches found
No related tags found
No related merge requests found
......@@ -17,16 +17,38 @@
namespace TIMIQLib_ns {
// ============================================================================
// static variables & functions
// ============================================================================
//- timiq internal web buffer
static std::string m_timiq_internal_buff;
// ----------------------------------------------------------------------------
// write callback functions
// @param buffer data , size*nmemb buffer size, void userp
// @return size*nmemb buffer size
// ----------------------------------------------------------------------------
static size_t write_callback( char *buffer, size_t size, size_t nmemb, void* )
{
// std::cout<<"static size_t write_callback <"<<size<<"> nmemb<"<<nmemb<<">"<<std::endl;
for (size_t idx = 0; idx < size*nmemb; idx++ )
m_timiq_internal_buff.push_back(buffer[idx]);
return size*nmemb;
}
// ============================================================================
// TIMIQCurl::TIMIQCurl
// ============================================================================
TIMIQCurl::TIMIQCurl(const std::string& ip_address, const std::string& num_port)
{
//std::cout << "TIMIQCurl constructor" << std::endl;
m_base_addr = kHTTP_WEB_PROTOCOL + ip_address + ":"+ num_port + "/";
m_base_addr = kHTTP_WEB_PROTOCOL + ip_address + ":"+ num_port;
m_error_string = "";
m_timiq_internal_buff.clear();
// use the static timiq internal buffer
// m_timiq_internal_buff.clear();
m_hw_curl = NULL;
}
......@@ -267,18 +289,6 @@ E_timiq_errno_t TIMIQCurl::read_state_and_status(std::string& status, E_timiq_c
return err_num;
}
// ============================================================================
// TIMIQCurl::writeTimIQCallback
// ============================================================================
size_t TIMIQCurl::writeTimIQCallback( char *buffer, size_t size, size_t nmemb, void*)
{
//- fill the buffer with the data collected from TIMQ webserver
for (size_t idx = 0; idx < size*nmemb; idx++ )
m_timiq_internal_buff.push_back(buffer[idx]);
return size*nmemb;
}
// ============================================================================
// TIMIQCurl::connect_i
// ============================================================================
......@@ -323,9 +333,6 @@ bool TIMIQCurl::write_i(std::string& url, std::string& strData)
m_error_string = "";
//- clean the timiq buffer data before perform
m_timiq_internal_buff.clear();
#if !defined (_SIMULATION_)
//- no connection is possible
if (this->connect_i() != timiq_NO_ERROR)
......@@ -333,15 +340,7 @@ bool TIMIQCurl::write_i(std::string& url, std::string& strData)
try
{
//send to webserver
curl_easy_setopt(m_hw_curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(m_hw_curl, CURLOPT_WRITEFUNCTION, &TIMIQCurl::writeTimIQCallback);
//- perform the query
result = curl_easy_perform(m_hw_curl);
//- clean up
curl_easy_cleanup(m_hw_curl);
result = send_to_webserver_i(url);
}
catch (...)
{
......@@ -395,27 +394,14 @@ bool TIMIQCurl::read_float_i(const std::string& url, const std::string& strData,
m_error_string = "";
//- clean the timiq buffer data before perform
m_timiq_internal_buff.clear();
#if !defined (_SIMULATION_)
//- no connection is possible
if (this->connect_i() != timiq_NO_ERROR)
{
return false;
}
try
{
//send to webserver
curl_easy_setopt(m_hw_curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(m_hw_curl, CURLOPT_WRITEFUNCTION, &TIMIQCurl::writeTimIQCallback);
//- perform the query
result = curl_easy_perform(m_hw_curl);
//- clean up
curl_easy_cleanup(m_hw_curl);
result = send_to_webserver_i(url);
}
catch (...)
{
......@@ -428,7 +414,10 @@ bool TIMIQCurl::read_float_i(const std::string& url, const std::string& strData,
if (result != CURLE_OK)
{
//- HTTP error
m_error_string = m_timiq_internal_buff;
char buff[kMAX_DATA_SIZE];
memset(buff, 0, sizeof(buff));
sprintf (buff, "TIMIQCurl::read_float_i - CURL code error <%d>",result);
m_error_string = buff;
return false;
}
else
......@@ -491,9 +480,6 @@ bool TIMIQCurl::read_state_and_status_i(const std::string& url, std::string& sta
m_error_string = "";
//- clean the timiq buffer data before perform
m_timiq_internal_buff.clear();
#if !defined (_SIMULATION_)
//- no connection is possible
if (this->connect_i() != timiq_NO_ERROR)
......@@ -501,15 +487,7 @@ bool TIMIQCurl::read_state_and_status_i(const std::string& url, std::string& sta
try
{
//- send to webserver
curl_easy_setopt(m_hw_curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(m_hw_curl, CURLOPT_WRITEFUNCTION, &TIMIQCurl::writeTimIQCallback);
//- perform the query
result = curl_easy_perform(m_hw_curl);
//- clean up
curl_easy_cleanup(m_hw_curl);
result = send_to_webserver_i(url);
}
catch (...)
{
......@@ -521,8 +499,11 @@ bool TIMIQCurl::read_state_and_status_i(const std::string& url, std::string& sta
//----------------------------
if (result != CURLE_OK)
{
//- HTTP error
m_error_string = m_timiq_internal_buff;
//- HTTP error
char buff[kMAX_DATA_SIZE];
memset(buff, 0, sizeof(buff));
sprintf (buff, "TIMIQCurl::read_state_and_status_i - CURL code error <%d>",result);
m_error_string = buff;
return false;
}
else
......@@ -583,4 +564,32 @@ bool TIMIQCurl::read_state_and_status_i(const std::string& url, std::string& sta
return false;
}
// ============================================================================
// TIMIQCurl::send_to_webserver_i
// ============================================================================
CURLcode TIMIQCurl:: send_to_webserver_i(const std::string& url)
{
CURLcode result;
//- clean the timiq buffer data before perform
m_timiq_internal_buff.clear();
/* ask libcurl to set options */
curl_easy_setopt(m_hw_curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(m_hw_curl, CURLOPT_PROXY, "");
curl_easy_setopt(m_hw_curl, CURLOPT_WRITEFUNCTION, &write_callback);
/* ask libcurl to show us the verbose output */
// curl_easy_setopt(m_hw_curl, CURLOPT_VERBOSE, 1L);
//- perform the query
result = curl_easy_perform(m_hw_curl);
//- clean up
curl_easy_cleanup(m_hw_curl);
return result;
}
}
\ No newline at end of file
......@@ -87,7 +87,7 @@ namespace TIMIQLib_ns
private:
//- internal members
//-------------------
//- IP address +":"+ port number
//- IP address
std::string m_base_addr;
//- CURL easy handle
......@@ -96,9 +96,6 @@ namespace TIMIQLib_ns
// message error
std::string m_error_string;
//- timiq internal web buffer
std::string m_timiq_internal_buff;
//- internal functions
//--------------------
......@@ -113,11 +110,7 @@ namespace TIMIQLib_ns
// connects to an easy handle using curl
// @return 0: no error, others: an internal error occurred
E_timiq_errno_t connect_i();
// write callback functions
// @param buffer data , size*nb_member buffer size, void pointer
// @return size*nb_member buffer size
size_t writeTimIQCallback( char *buffer, size_t size, size_t nb_member, void *);
// writes on timiq WebServer
// @param url of the page, strData data to send
......@@ -133,6 +126,12 @@ namespace TIMIQLib_ns
// @param url of the page, string state
// @return true if success, false otherwise
bool read_state_and_status_i(const std::string& url, std::string& state, E_timiq_code_t& codeRet );
// send data to webserver
// @param string
// @return CURLcode
CURLcode send_to_webserver_i(const std::string& url);
};
}
#endif //- _TIMIQ_CURL_H_
\ No newline at end of file
......@@ -30,7 +30,6 @@ if (!m_timiq_hw) \
// ============================================================================
TIMIQLib::TIMIQLib ()
{
//std::cout << "TIMIQLib constructor" << std::endl;
m_timiq_hw = NULL;
m_ipAddress = "";
m_numPort = "";
......@@ -40,8 +39,6 @@ TIMIQLib::TIMIQLib ()
// ============================================================================
TIMIQLib::~TIMIQLib()
{
//std::cout << "TIMIQLib destructor" << std::endl;
if (m_timiq_hw)
{
delete m_timiq_hw;
......
......@@ -60,7 +60,7 @@ const std::string kTIMIQ_MIXER_SIN_OUTPUT_KEY= "mixerSinOutput";
//- Web page: state
//-------------------------
const std::string kTIMIQ_STATE_PAGE= "/get_feedback.wsgi";
const std::string kTIMIQ_STATE_PAGE= "/get_state.wsgi";
//- message key
const std::string kTIMIQ_MESSAGE_KEY= "message=";
......@@ -70,7 +70,7 @@ const std::string kTIMIQ_STATE_KEY= "state=";
const std::string kTIMIQ_CODE_KEY= "code=";
//- buffer key size
const size_t kMAX_DATA_SIZE = 35;
const size_t kMAX_DATA_SIZE = 50;
// ============================================================================
// TYPES
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment