fixed state reuse in the receivables

pull/40/head
Matchman Green 2012-05-09 00:58:05 +08:00
parent d621aa4296
commit b0f5769cb2
14 changed files with 88 additions and 15 deletions

View File

@ -77,7 +77,7 @@ Drive::Drive( OAuth2& auth ) :
Json next ; Json next ;
has_next = resp["feed"]["link"].FindInArray( "rel", "next", next ) ; has_next = resp["feed"]["link"].FindInArray( "rel", "next", next ) ;
if ( has_next ) if ( has_next )
{ {
http.Get( next["href"].Str(), &str, m_http_hdr ) ; 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 ) void Drive::ConstructDirTree( http::Agent *http )
{ {
// http::XmlResponse xml ;
// http->Get( root_url + "/-/folder?showroot=true", &xml, m_http_hdr ) ;
//
// std::ofstream abc( "abc.xml" ) ;
// abc << xml.Response()["feed"] ;
http::JsonResponse jrsp ; http::JsonResponse jrsp ;
http->Get( root_url + "/-/folder?alt=json", &jrsp, m_http_hdr ) ; http->Get( root_url + "/-/folder?alt=json", &jrsp, m_http_hdr ) ;
Json resp = jrsp.Response() ; Json resp = jrsp.Response() ;
@ -145,7 +151,7 @@ void Drive::ConstructDirTree( http::Agent *http )
http->Get( next["href"].Str(), &jrsp, m_http_hdr ) ; http->Get( next["href"].Str(), &jrsp, m_http_hdr ) ;
resp = jrsp.Response() ; resp = jrsp.Response() ;
} }
// second, build up linkage between parent and child // second, build up linkage between parent and child
std::sort( m_coll.begin(), m_coll.end(), SortCollectionByHref() ) ; std::sort( m_coll.begin(), m_coll.end(), SortCollectionByHref() ) ;
for ( FolderListIterator i = m_coll.begin() ; i != m_coll.end() ; ++i ) 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 // lastly, iterating from the root, create the directories in the local file system
assert( m_root.Parent() == 0 ) ; assert( m_root.Parent() == 0 ) ;
m_root.CreateSubDir( Path() ) ; m_root.CreateSubDir( Path() ) ;
@ -193,6 +199,7 @@ void Drive::UpdateFile( const Json& entry )
if ( pit != m_coll.end() ) if ( pit != m_coll.end() )
path = pit->Dir() / file.Filename() ; path = pit->Dir() / file.Filename() ;
} }
// std::cout << "2:" << path << std::endl;
// compare checksum first if file exists // compare checksum first if file exists
std::ifstream ifile( path.Str().c_str(), std::ios::binary | std::ios::in ) ; std::ifstream ifile( path.Str().c_str(), std::ios::binary | std::ios::in ) ;

View File

@ -58,6 +58,11 @@ Download::~Download( )
::EVP_MD_CTX_destroy( m_mdctx ) ; ::EVP_MD_CTX_destroy( m_mdctx ) ;
} }
void Download::Clear()
{
// no need to do anything
}
std::string Download::Finish() const std::string Download::Finish() const
{ {
// Unregister the signal // Unregister the signal

View File

@ -38,6 +38,7 @@ public :
std::string Finish() const ; std::string Finish() const ;
void Clear() ;
std::size_t OnData( void *data, std::size_t count ) ; 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 ) ; static std::size_t Callback( char *data, std::size_t size, std::size_t nmemb, Download *pthis ) ;

View File

@ -106,8 +106,12 @@ std::size_t Agent::HeaderCallback( void *ptr, size_t size, size_t nmemb, Agent *
return size*nmemb ; return size*nmemb ;
} }
// std::ofstream g_log ;
std::size_t Agent::Receive( void* ptr, size_t size, size_t nmemb, Receivable *recv ) 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 ) ; assert( recv != 0 ) ;
return recv->OnData( ptr, size * nmemb ) ; return recv->OnData( ptr, size * nmemb ) ;
} }
@ -133,6 +137,7 @@ long Agent::Put(
SetHeader( hdr ) ; SetHeader( hdr ) ;
dest->Clear() ;
CURLcode curl_code = ::curl_easy_perform(curl); CURLcode curl_code = ::curl_easy_perform(curl);
long http_code = 0; long http_code = 0;
@ -165,9 +170,16 @@ long Agent::Get(
::curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); ::curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
SetHeader( hdr ) ; 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); CURLcode curl_code = ::curl_easy_perform(curl);
// g_log.close() ;
long http_code = 0; long http_code = 0;
::curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code); ::curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
@ -201,6 +213,7 @@ long Agent::Post(
SetHeader( hdr ) ; SetHeader( hdr ) ;
dest->Clear() ;
CURLcode curl_code = ::curl_easy_perform(curl); CURLcode curl_code = ::curl_easy_perform(curl);
long http_code = 0; long http_code = 0;
@ -232,6 +245,7 @@ long Agent::Custom(
SetHeader( hdr ) ; SetHeader( hdr ) ;
dest->Clear() ;
CURLcode curl_code = ::curl_easy_perform(curl); CURLcode curl_code = ::curl_easy_perform(curl);
long http_code = 0; long http_code = 0;

View File

@ -27,6 +27,7 @@ class Receivable
{ {
public : public :
virtual std::size_t OnData( void *data, std::size_t count ) = 0 ; virtual std::size_t OnData( void *data, std::size_t count ) = 0 ;
virtual void Clear() = 0 ;
} ; } ;
} } // end of namespace } } // end of namespace

View File

@ -25,6 +25,11 @@ StringResponse::StringResponse()
{ {
} }
void StringResponse::Clear()
{
m_resp.clear() ;
}
std::size_t StringResponse::OnData( void *data, std::size_t count ) std::size_t StringResponse::OnData( void *data, std::size_t count )
{ {
m_resp.append( reinterpret_cast<char*>(data), count ) ; m_resp.append( reinterpret_cast<char*>(data), count ) ;

View File

@ -31,6 +31,7 @@ public :
StringResponse() ; StringResponse() ;
std::size_t OnData( void *data, std::size_t count ) ; std::size_t OnData( void *data, std::size_t count ) ;
void Clear() ;
const std::string& Response() const ; const std::string& Response() const ;

View File

@ -20,27 +20,33 @@
#include "XmlResponse.hh" #include "XmlResponse.hh"
#include "xml/Node.hh" #include "xml/Node.hh"
#include "xml/TreeBuilder.hh"
namespace gr { namespace http { namespace gr { namespace http {
XmlResponse::XmlResponse() XmlResponse::XmlResponse() : m_tb( new xml::TreeBuilder )
{ {
} }
std::size_t XmlResponse::OnData( void *data, std::size_t count ) std::size_t XmlResponse::OnData( void *data, std::size_t count )
{ {
m_tb.ParseData( reinterpret_cast<char*>(data), count ) ; m_tb->ParseData( reinterpret_cast<char*>(data), count ) ;
return count ; return count ;
} }
void XmlResponse::Clear()
{
m_tb.reset( new xml::TreeBuilder ) ;
}
void XmlResponse::Finish() void XmlResponse::Finish()
{ {
m_tb.ParseData( 0, 0, true ) ; m_tb->ParseData( 0, 0, true ) ;
} }
xml::Node XmlResponse::Response() const xml::Node XmlResponse::Response() const
{ {
return m_tb.Result() ; return m_tb->Result() ;
} }
} } // end of namespace } } // end of namespace

View File

@ -21,11 +21,12 @@
#include "Receivable.hh" #include "Receivable.hh"
#include "xml/TreeBuilder.hh" #include <memory>
namespace gr { namespace xml namespace gr { namespace xml
{ {
class Node ; class Node ;
class TreeBuilder ;
} } } }
namespace gr { namespace http { namespace gr { namespace http {
@ -35,13 +36,14 @@ class XmlResponse : public Receivable
public : public :
XmlResponse() ; XmlResponse() ;
void Clear() ;
std::size_t OnData( void *data, std::size_t count ) ; std::size_t OnData( void *data, std::size_t count ) ;
void Finish() ; void Finish() ;
xml::Node Response() const ; xml::Node Response() const ;
private : private :
xml::TreeBuilder m_tb ; std::auto_ptr<xml::TreeBuilder> m_tb ;
} ; } ;
} } // end of namespace } } // end of namespace

View File

@ -76,6 +76,7 @@ private :
Json( struct json_object *json, NotOwned ) ; Json( struct json_object *json, NotOwned ) ;
private : private :
public :
struct json_object *m_json ; struct json_object *m_json ;
} ; } ;

View File

@ -27,6 +27,11 @@ JsonResponse::JsonResponse()
{ {
} }
void JsonResponse::Clear()
{
m_resp.Clear() ;
}
std::size_t JsonResponse::OnData( void *data, std::size_t count ) std::size_t JsonResponse::OnData( void *data, std::size_t count )
{ {
return m_resp.OnData( data, count ) ; return m_resp.OnData( data, count ) ;

View File

@ -35,6 +35,7 @@ public :
JsonResponse() ; JsonResponse() ;
std::size_t OnData( void *data, std::size_t count ) ; std::size_t OnData( void *data, std::size_t count ) ;
void Clear() ;
Json Response() const ; Json Response() const ;

View File

@ -365,16 +365,39 @@ std::ostream& operator<<( std::ostream& os, const Node& node )
} }
else if ( node.GetType() == Node::attr ) else if ( node.GetType() == Node::attr )
{ {
os << node.Name() << "=\"" << node.Value() << "\"" ; os << node.Name() << "=\"" ;
Node::PrintString( os, node.Value() ) ;
os << "\"" ;
} }
else else
{ {
os << node.Value() ; Node::PrintString( os, node.Value() ) ;
} }
return os ; 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 << "&quot;" ; break ;
case '\'': os << "&apos;" ; break ;
case '&': os << "&amp;" ; break ;
case '<': os << "&lt;" ; break ;
case '>': os << "&gt;" ; break ;
default : os << c ; break ;
}
return os ;
}
Node::iterator Node::begin() const Node::iterator Node::begin() const
{ {
return iterator( m_ptr->Begin() ) ; return iterator( m_ptr->Begin() ) ;

View File

@ -19,6 +19,7 @@
#pragma once #pragma once
#include <iosfwd>
#include <string> #include <string>
#include <vector> #include <vector>
#include <utility> #include <utility>
@ -58,6 +59,8 @@ public :
Type GetType() const ; Type GetType() const ;
static bool IsCompatible( Type parent, Type child ) ; 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 begin() const ;
iterator end() const ; iterator end() const ;
@ -92,8 +95,6 @@ public :
ImplVec::iterator m_it ; ImplVec::iterator m_it ;
} ; } ;
private : private :
explicit Node( Impl *impl ) ; explicit Node( Impl *impl ) ;