diff options
Diffstat (limited to 'project2/json')
-rw-r--r-- | project2/json/couchSession.cpp | 61 |
1 files changed, 20 insertions, 41 deletions
diff --git a/project2/json/couchSession.cpp b/project2/json/couchSession.cpp index d2eeca2..c6a15c0 100644 --- a/project2/json/couchSession.cpp +++ b/project2/json/couchSession.cpp @@ -13,42 +13,6 @@ #include "conversion.h" #include <boost/program_options.hpp> -class CouchSession : public Session { - public: - CouchSession(const UUID & sid) : Session(sid) { - } - CouchSession(const UUID & sid, const json::Object & o) : Session(sid), obj(o) { - } - virtual VariableType GetValue(const Glib::ustring & name) const { - return boost::apply_visitor(JsonToProject2(), *safeMapFind<VariableNotFound>(obj, name)->second); - } - virtual Values GetValuesCopy() const { - Values vs; - BOOST_FOREACH(const json::Object::value_type & v, obj) { - vs.insert(Values::value_type(v.first, boost::apply_visitor(JsonToProject2(), *v.second))); - } - return vs; - } - virtual void SetValue(const Glib::ustring & name, const VariableType & v) { - obj[name] = json::ValuePtr(new json::Value(boost::apply_visitor(Project2ToJson(), v))); - } - virtual void ClearValue(const Glib::ustring & name) { - obj.erase(name); - } - virtual time_t ExpiryTime() const { - return boost::get<json::Number>(*safeMapFind<VariableNotFound>(obj, ExpiryKey)->second); - } - virtual void ExpiryTime(time_t t) { - obj[ExpiryKey] = json::ValuePtr(new json::Value(json::Number(t))); - } - private: - json::Object obj; - friend class CouchSessionContainer; - friend class CustomCouchSessionLoader; - static const Glib::ustring ExpiryKey; -}; -const Glib::ustring CouchSession::ExpiryKey("project2:expires"); - class CouchDBFailure : public std::exception { }; class CouchSessionContainer : public SessionContainer { @@ -57,21 +21,28 @@ class CouchSessionContainer : public SessionContainer { } virtual SessionPtr getSession(const UUID & uuid) const { try { - SessionPtr s = new CouchSession(uuid, getSessionFromServer(uuid)); - if (s->ExpiryTime() > time(NULL)) { + json::Object obj = getSessionFromServer(uuid); + if (boost::get<json::Number>(*safeMapFind<Session::VariableNotFound>(obj, ExpiryKey)->second) > time(NULL)) { + SessionPtr s = new Session(uuid); + BOOST_FOREACH(const json::Object::value_type & v, obj) { + s->SetValue(v.first, boost::apply_visitor(JsonToProject2(), *v.second)); + } return s; } } catch (...) { } - return new CouchSession(UUID::generate_random()); + return newSession(); } virtual void SaveSession(SessionPtr s) const { CurlPtr c = new Curl(); c->setopt(CURLOPT_UPLOAD, 1L); c->setopt(CURLOPT_FAILONERROR, 1); - Glib::ustring out = json::serializeObject(boost::dynamic_pointer_cast<CouchSession>(s)->obj); + json::Object obj; + s->ForeachValue(boost::bind(&CouchSessionContainer::addToObject, &obj, _1, _2)); + obj[ExpiryKey] = json::ValuePtr(new json::Value((json::Number)s->ExpiryTime())); + Glib::ustring out = json::serializeObject(obj); c->setopt(CURLOPT_INFILESIZE_LARGE, (curl_off_t)out.size()); unsigned int off = 0; BOOST_FOREACH(const std::string & b, baseUrls) { @@ -103,6 +74,11 @@ class CouchSessionContainer : public SessionContainer { throw CouchDBFailure(); } + private: + static void addToObject(json::Object * obj, const Glib::ustring & name, const VariableType & value) { + (*obj)[name] = json::ValuePtr(new json::Value(boost::apply_visitor(Project2ToJson(), value))); + } + static size_t send(Glib::ustring * buf, unsigned int * off, char * str, size_t l) { size_t len = std::min(buf->size() - *off, l); memcpy(str, buf->c_str() + *off, len); @@ -123,8 +99,11 @@ class CouchSessionContainer : public SessionContainer { } static std::vector<std::string> baseUrls; + static const Glib::ustring ExpiryKey; + friend class CustomCouchSessionLoader; }; std::vector<std::string> CouchSessionContainer::baseUrls; +const Glib::ustring CouchSessionContainer::ExpiryKey("project2:expires"); namespace po = boost::program_options; class CustomCouchSessionLoader : public SessionContainerLoaderImpl<CouchSessionContainer> { @@ -167,7 +146,7 @@ class CustomCouchSessionLoader : public SessionContainerLoaderImpl<CouchSessionC json::Object map; Buffer mapBuf; mapBuf.appendf("function(doc) { var exp = doc['%s']; if (exp < (new Date().getTime() / 1000)) { emit(exp, doc._rev); } }", - CouchSession::ExpiryKey.c_str()); + CouchSessionContainer::ExpiryKey.c_str()); map["map"] = json::ValuePtr(new json::Value(mapBuf.str())); Glib::ustring mapStr(json::serializeObject(map)); // Create the CURL handle |