fixed state reuse in the receivables

pull/40/head
Matchman Green 2012-05-09 22:25:42 +08:00
parent d621aa4296
commit 06158e6bf0
14 changed files with 88 additions and 15 deletions

View File

@ -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 ) ;

View File

@ -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

View File

@ -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 ) ;

View File

@ -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;

View File

@ -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

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 )
{
m_resp.append( reinterpret_cast<char*>(data), count ) ;

View File

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

View File

@ -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<char*>(data), count ) ;
m_tb->ParseData( reinterpret_cast<char*>(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

View File

@ -21,11 +21,12 @@
#include "Receivable.hh"
#include "xml/TreeBuilder.hh"
#include <memory>
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<xml::TreeBuilder> m_tb ;
} ;
} } // end of namespace

View File

@ -76,6 +76,7 @@ private :
Json( struct json_object *json, NotOwned ) ;
private :
public :
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 )
{
return m_resp.OnData( data, count ) ;

View File

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

View File

@ -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 << "&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
{
return iterator( m_ptr->Begin() ) ;

View File

@ -19,6 +19,7 @@
#pragma once
#include <iosfwd>
#include <string>
#include <vector>
#include <utility>
@ -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 ) ;