From 74e2a290405474821839c19cc309f56161e5daec Mon Sep 17 00:00:00 2001
From: Jacques Gouno <jacques.gouno@synchrotron-soleil.fr>
Date: Wed, 15 Feb 2017 09:28:35 +0000
Subject: [PATCH] TANGODEVIC-1647: updating source code

---
 src/TIMIQCurl.cpp         | 121 ++++++++++++++++++++------------------
 src/TIMIQCurl.h           |  17 +++---
 src/TIMIQLib.cpp          |   3 -
 src/TIMIQTypesAndConsts.h |   4 +-
 4 files changed, 75 insertions(+), 70 deletions(-)

diff --git a/src/TIMIQCurl.cpp b/src/TIMIQCurl.cpp
index e3e989f..225fd4c 100644
--- a/src/TIMIQCurl.cpp
+++ b/src/TIMIQCurl.cpp
@@ -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
diff --git a/src/TIMIQCurl.h b/src/TIMIQCurl.h
index 02f92af..4afeb56 100644
--- a/src/TIMIQCurl.h
+++ b/src/TIMIQCurl.h
@@ -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
diff --git a/src/TIMIQLib.cpp b/src/TIMIQLib.cpp
index d809ea8..491fb2c 100644
--- a/src/TIMIQLib.cpp
+++ b/src/TIMIQLib.cpp
@@ -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;
diff --git a/src/TIMIQTypesAndConsts.h b/src/TIMIQTypesAndConsts.h
index a516d34..eecdadd 100644
--- a/src/TIMIQTypesAndConsts.h
+++ b/src/TIMIQTypesAndConsts.h
@@ -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
-- 
GitLab