From 06158e6bf05e619064386499c54135355563df7e Mon Sep 17 00:00:00 2001 From: Matchman Green Date: Wed, 9 May 2012 22:25:42 +0800 Subject: [PATCH] fixed state reuse in the receivables --- libgrive/src/drive/Drive.cc | 13 ++++++++++--- libgrive/src/http/Download.cc | 5 +++++ libgrive/src/http/Download.hh | 1 + libgrive/src/http/HTTP.cc | 18 ++++++++++++++++-- libgrive/src/http/Receivable.hh | 1 + libgrive/src/http/StringResponse.cc | 5 +++++ libgrive/src/http/StringResponse.hh | 1 + libgrive/src/http/XmlResponse.cc | 14 ++++++++++---- libgrive/src/http/XmlResponse.hh | 6 ++++-- libgrive/src/protocol/Json.hh | 1 + libgrive/src/protocol/JsonResponse.cc | 5 +++++ libgrive/src/protocol/JsonResponse.hh | 1 + libgrive/src/xml/Node.cc | 27 +++++++++++++++++++++++++-- libgrive/src/xml/Node.hh | 5 +++-- 14 files changed, 88 insertions(+), 15 deletions(-) diff --git a/libgrive/src/drive/Drive.cc b/libgrive/src/drive/Drive.cc index 76a1501..cd8c8fd 100644 --- a/libgrive/src/drive/Drive.cc +++ b/libgrive/src/drive/Drive.cc @@ -77,7 +77,7 @@ Drive::Drive( OAuth2& auth ) : Json next ; has_next = resp["feed"]["link"].FindInArray( "rel", "next", next ) ; - + if ( has_next ) { http.Get( next["href"].Str(), &str, m_http_hdr ) ; @@ -113,6 +113,12 @@ Drive::FolderListIterator Drive::FindFolder( const std::string& href ) void Drive::ConstructDirTree( http::Agent *http ) { + http::XmlResponse xml ; + http->Get( root_url + "/-/folder?showroot=true&max-results=10", &xml, m_http_hdr ) ; + + std::ofstream abc( "abc.xml" ) ; +abc << xml.Response()["feed"] ; + http::JsonResponse jrsp ; http->Get( root_url + "/-/folder?alt=json", &jrsp, m_http_hdr ) ; Json resp = jrsp.Response() ; @@ -145,7 +151,7 @@ void Drive::ConstructDirTree( http::Agent *http ) http->Get( next["href"].Str(), &jrsp, m_http_hdr ) ; resp = jrsp.Response() ; } - + // second, build up linkage between parent and child std::sort( m_coll.begin(), m_coll.end(), SortCollectionByHref() ) ; for ( FolderListIterator i = m_coll.begin() ; i != m_coll.end() ; ++i ) @@ -170,7 +176,7 @@ void Drive::ConstructDirTree( http::Agent *http ) } } } - + // lastly, iterating from the root, create the directories in the local file system assert( m_root.Parent() == 0 ) ; m_root.CreateSubDir( Path() ) ; @@ -193,6 +199,7 @@ void Drive::UpdateFile( const Json& entry ) if ( pit != m_coll.end() ) path = pit->Dir() / file.Filename() ; } +// std::cout << "2:" << path << std::endl; // compare checksum first if file exists std::ifstream ifile( path.Str().c_str(), std::ios::binary | std::ios::in ) ; diff --git a/libgrive/src/http/Download.cc b/libgrive/src/http/Download.cc index 77fa291..4237765 100644 --- a/libgrive/src/http/Download.cc +++ b/libgrive/src/http/Download.cc @@ -58,6 +58,11 @@ Download::~Download( ) ::EVP_MD_CTX_destroy( m_mdctx ) ; } +void Download::Clear() +{ + // no need to do anything +} + std::string Download::Finish() const { // Unregister the signal diff --git a/libgrive/src/http/Download.hh b/libgrive/src/http/Download.hh index 4c9e078..fffde2a 100644 --- a/libgrive/src/http/Download.hh +++ b/libgrive/src/http/Download.hh @@ -38,6 +38,7 @@ public : std::string Finish() const ; + void Clear() ; std::size_t OnData( void *data, std::size_t count ) ; static std::size_t Callback( char *data, std::size_t size, std::size_t nmemb, Download *pthis ) ; diff --git a/libgrive/src/http/HTTP.cc b/libgrive/src/http/HTTP.cc index b1a2f9a..89f4787 100644 --- a/libgrive/src/http/HTTP.cc +++ b/libgrive/src/http/HTTP.cc @@ -106,8 +106,12 @@ std::size_t Agent::HeaderCallback( void *ptr, size_t size, size_t nmemb, Agent * return size*nmemb ; } +// std::ofstream g_log ; + std::size_t Agent::Receive( void* ptr, size_t size, size_t nmemb, Receivable *recv ) { +// g_log.rdbuf()->sputn( (char*)ptr, size*nmemb ) ; + assert( recv != 0 ) ; return recv->OnData( ptr, size * nmemb ) ; } @@ -133,6 +137,7 @@ long Agent::Put( SetHeader( hdr ) ; + dest->Clear() ; CURLcode curl_code = ::curl_easy_perform(curl); long http_code = 0; @@ -165,9 +170,16 @@ long Agent::Get( ::curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); SetHeader( hdr ) ; - +/* + static int s_count = 0 ; + std::ostringstream logss ; + logss << "get" << s_count++ << ".txt" ; + g_log.open( logss.str().c_str() ) ; + */ + dest->Clear() ; CURLcode curl_code = ::curl_easy_perform(curl); - +// g_log.close() ; + long http_code = 0; ::curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code); @@ -201,6 +213,7 @@ long Agent::Post( SetHeader( hdr ) ; + dest->Clear() ; CURLcode curl_code = ::curl_easy_perform(curl); long http_code = 0; @@ -232,6 +245,7 @@ long Agent::Custom( SetHeader( hdr ) ; + dest->Clear() ; CURLcode curl_code = ::curl_easy_perform(curl); long http_code = 0; diff --git a/libgrive/src/http/Receivable.hh b/libgrive/src/http/Receivable.hh index 8db569b..2982aa1 100644 --- a/libgrive/src/http/Receivable.hh +++ b/libgrive/src/http/Receivable.hh @@ -27,6 +27,7 @@ class Receivable { public : virtual std::size_t OnData( void *data, std::size_t count ) = 0 ; + virtual void Clear() = 0 ; } ; } } // end of namespace diff --git a/libgrive/src/http/StringResponse.cc b/libgrive/src/http/StringResponse.cc index 8f03076..d183be3 100644 --- a/libgrive/src/http/StringResponse.cc +++ b/libgrive/src/http/StringResponse.cc @@ -25,6 +25,11 @@ StringResponse::StringResponse() { } +void StringResponse::Clear() +{ + m_resp.clear() ; +} + std::size_t StringResponse::OnData( void *data, std::size_t count ) { m_resp.append( reinterpret_cast(data), count ) ; diff --git a/libgrive/src/http/StringResponse.hh b/libgrive/src/http/StringResponse.hh index efdda74..e361d01 100644 --- a/libgrive/src/http/StringResponse.hh +++ b/libgrive/src/http/StringResponse.hh @@ -31,6 +31,7 @@ public : StringResponse() ; std::size_t OnData( void *data, std::size_t count ) ; + void Clear() ; const std::string& Response() const ; diff --git a/libgrive/src/http/XmlResponse.cc b/libgrive/src/http/XmlResponse.cc index 88ea082..49395f1 100644 --- a/libgrive/src/http/XmlResponse.cc +++ b/libgrive/src/http/XmlResponse.cc @@ -20,27 +20,33 @@ #include "XmlResponse.hh" #include "xml/Node.hh" +#include "xml/TreeBuilder.hh" namespace gr { namespace http { -XmlResponse::XmlResponse() +XmlResponse::XmlResponse() : m_tb( new xml::TreeBuilder ) { } std::size_t XmlResponse::OnData( void *data, std::size_t count ) { - m_tb.ParseData( reinterpret_cast(data), count ) ; + m_tb->ParseData( reinterpret_cast(data), count ) ; return count ; } +void XmlResponse::Clear() +{ + m_tb.reset( new xml::TreeBuilder ) ; +} + void XmlResponse::Finish() { - m_tb.ParseData( 0, 0, true ) ; + m_tb->ParseData( 0, 0, true ) ; } xml::Node XmlResponse::Response() const { - return m_tb.Result() ; + return m_tb->Result() ; } } } // end of namespace diff --git a/libgrive/src/http/XmlResponse.hh b/libgrive/src/http/XmlResponse.hh index aba7cce..6af2b74 100644 --- a/libgrive/src/http/XmlResponse.hh +++ b/libgrive/src/http/XmlResponse.hh @@ -21,11 +21,12 @@ #include "Receivable.hh" -#include "xml/TreeBuilder.hh" +#include namespace gr { namespace xml { class Node ; + class TreeBuilder ; } } namespace gr { namespace http { @@ -35,13 +36,14 @@ class XmlResponse : public Receivable public : XmlResponse() ; + void Clear() ; std::size_t OnData( void *data, std::size_t count ) ; void Finish() ; xml::Node Response() const ; private : - xml::TreeBuilder m_tb ; + std::auto_ptr m_tb ; } ; } } // end of namespace diff --git a/libgrive/src/protocol/Json.hh b/libgrive/src/protocol/Json.hh index 52c44c1..ca21478 100644 --- a/libgrive/src/protocol/Json.hh +++ b/libgrive/src/protocol/Json.hh @@ -76,6 +76,7 @@ private : Json( struct json_object *json, NotOwned ) ; private : +public : struct json_object *m_json ; } ; diff --git a/libgrive/src/protocol/JsonResponse.cc b/libgrive/src/protocol/JsonResponse.cc index fc66d66..f270af6 100644 --- a/libgrive/src/protocol/JsonResponse.cc +++ b/libgrive/src/protocol/JsonResponse.cc @@ -27,6 +27,11 @@ JsonResponse::JsonResponse() { } +void JsonResponse::Clear() +{ + m_resp.Clear() ; +} + std::size_t JsonResponse::OnData( void *data, std::size_t count ) { return m_resp.OnData( data, count ) ; diff --git a/libgrive/src/protocol/JsonResponse.hh b/libgrive/src/protocol/JsonResponse.hh index 8856872..3adae2d 100644 --- a/libgrive/src/protocol/JsonResponse.hh +++ b/libgrive/src/protocol/JsonResponse.hh @@ -35,6 +35,7 @@ public : JsonResponse() ; std::size_t OnData( void *data, std::size_t count ) ; + void Clear() ; Json Response() const ; diff --git a/libgrive/src/xml/Node.cc b/libgrive/src/xml/Node.cc index c2305b4..c86b005 100644 --- a/libgrive/src/xml/Node.cc +++ b/libgrive/src/xml/Node.cc @@ -365,16 +365,39 @@ std::ostream& operator<<( std::ostream& os, const Node& node ) } else if ( node.GetType() == Node::attr ) { - os << node.Name() << "=\"" << node.Value() << "\"" ; + os << node.Name() << "=\"" ; + Node::PrintString( os, node.Value() ) ; + os << "\"" ; } else { - os << node.Value() ; + Node::PrintString( os, node.Value() ) ; } return os ; } +std::ostream& Node::PrintString( std::ostream& os, const std::string& s ) +{ + for ( std::string::const_iterator i = s.begin() ; i != s.end() ; ++i ) + Node::PrintChar( os, *i ) ; + return os ; +} + +std::ostream& Node::PrintChar( std::ostream& os, char c ) +{ + switch ( c ) + { + case '\"': os << """ ; break ; + case '\'': os << "'" ; break ; + case '&': os << "&" ; break ; + case '<': os << "<" ; break ; + case '>': os << ">" ; break ; + default : os << c ; break ; + } + return os ; +} + Node::iterator Node::begin() const { return iterator( m_ptr->Begin() ) ; diff --git a/libgrive/src/xml/Node.hh b/libgrive/src/xml/Node.hh index a3b6709..d16f39b 100644 --- a/libgrive/src/xml/Node.hh +++ b/libgrive/src/xml/Node.hh @@ -19,6 +19,7 @@ #pragma once +#include #include #include #include @@ -58,6 +59,8 @@ public : Type GetType() const ; static bool IsCompatible( Type parent, Type child ) ; + static std::ostream& PrintChar( std::ostream& os, char c ) ; + static std::ostream& PrintString( std::ostream& os, const std::string& s ) ; iterator begin() const ; iterator end() const ; @@ -92,8 +95,6 @@ public : ImplVec::iterator m_it ; } ; - - private : explicit Node( Impl *impl ) ;