summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrandomdan <randomdan@localhost>2011-12-02 17:03:50 +0000
committerrandomdan <randomdan@localhost>2011-12-02 17:03:50 +0000
commitf0e9e42607ac4cfa935185c9cd1983889645266e (patch)
tree1ec9119b70881a01dd2f6389bc08a818292b6bd9
parentRemove no longer required type handlers (diff)
downloadproject2-f0e9e42607ac4cfa935185c9cd1983889645266e.tar.bz2
project2-f0e9e42607ac4cfa935185c9cd1983889645266e.tar.xz
project2-f0e9e42607ac4cfa935185c9cd1983889645266e.zip
Add component events for process begin and before request
Store XML sessions in a single folder, one file per session, not one big file
-rw-r--r--project2/cgi/cgiCommon.cpp1
-rw-r--r--project2/cgi/p2webCgi.cpp1
-rw-r--r--project2/cgi/p2webFCgi.cpp1
-rw-r--r--project2/common/xmlObjectLoader.cpp10
-rw-r--r--project2/common/xmlObjectLoader.h2
-rw-r--r--project2/console/p2consoleMain.cpp2
-rw-r--r--project2/xml/sessionXml.cpp89
-rw-r--r--project2/xml/sessionXml.h4
8 files changed, 62 insertions, 48 deletions
diff --git a/project2/cgi/cgiCommon.cpp b/project2/cgi/cgiCommon.cpp
index 3703c07..579e3f4 100644
--- a/project2/cgi/cgiCommon.cpp
+++ b/project2/cgi/cgiCommon.cpp
@@ -47,6 +47,7 @@ cgiServe(cgicc::CgiInput * i, std::ostream & IO)
try {
CgiApplicationEngine app(&env, IO);
+ LoaderBase::onAllComponents(boost::bind(&ComponentLoader::onBefore, _1));
Logger()->messagef(LOG_DEBUG, "%s: Processing request", __FUNCTION__);
app.process();
Logger()->messagef(LOG_DEBUG, "%s: Completed request", __FUNCTION__);
diff --git a/project2/cgi/p2webCgi.cpp b/project2/cgi/p2webCgi.cpp
index 940717b..be48f4f 100644
--- a/project2/cgi/p2webCgi.cpp
+++ b/project2/cgi/p2webCgi.cpp
@@ -5,6 +5,7 @@
int
main(void)
{
+ LoaderBase::onAllComponents(boost::bind(&ComponentLoader::onBegin, _1));
cgiServe(NULL, std::cout);
LoaderBase::onAllComponents(boost::bind(&ComponentLoader::onIteration, _1));
LoaderBase::onAllComponents(boost::bind(&ComponentLoader::onPeriodic, _1));
diff --git a/project2/cgi/p2webFCgi.cpp b/project2/cgi/p2webFCgi.cpp
index c193c1b..72dfa44 100644
--- a/project2/cgi/p2webFCgi.cpp
+++ b/project2/cgi/p2webFCgi.cpp
@@ -40,6 +40,7 @@ main(void)
fprintf(stderr, "Failed to set signal handler\n");
}
alarm(60);
+ LoaderBase::onAllComponents(boost::bind(&ComponentLoader::onBegin, _1));
while (FCGX_Accept_r(&request) == 0) {
alarm(0);
cgicc::FCgiIO IO(request);
diff --git a/project2/common/xmlObjectLoader.cpp b/project2/common/xmlObjectLoader.cpp
index 0e55cb4..81cab9e 100644
--- a/project2/common/xmlObjectLoader.cpp
+++ b/project2/common/xmlObjectLoader.cpp
@@ -134,6 +134,16 @@ xmlChildText(const xmlpp::Node * p, const Glib::ustring & t)
}
void
+ComponentLoader::onBegin()
+{
+}
+
+void
+ComponentLoader::onBefore()
+{
+}
+
+void
ComponentLoader::onIdle()
{
}
diff --git a/project2/common/xmlObjectLoader.h b/project2/common/xmlObjectLoader.h
index 202dbeb..0552a9e 100644
--- a/project2/common/xmlObjectLoader.h
+++ b/project2/common/xmlObjectLoader.h
@@ -111,6 +111,8 @@ class LoaderBase {
namespace boost { namespace program_options { class options_description; } }
class ComponentLoader {
public:
+ virtual void onBegin(); // App engine start up (before settings are processed)
+ virtual void onBefore(); // Before the app engine processes a request (after settings are processed)
virtual void onIdle(); // When the app engine goes idle
virtual void onIteration(); // When the app engine has completed an iteration
virtual void onPeriodic(); // When the app engine feels like it
diff --git a/project2/console/p2consoleMain.cpp b/project2/console/p2consoleMain.cpp
index 6201e72..c194510 100644
--- a/project2/console/p2consoleMain.cpp
+++ b/project2/console/p2consoleMain.cpp
@@ -11,8 +11,10 @@ int
main(int argc, char ** argv)
{
ConsoleEnvironment env(argc, argv);
+ LoaderBase::onAllComponents(boost::bind(&ComponentLoader::onBegin, _1));
env.init();
BOOST_FOREACH(const boost::filesystem::path & file, env.todoList()) {
+ LoaderBase::onAllComponents(boost::bind(&ComponentLoader::onBefore, _1));
Logger()->messagef(LOG_DEBUG, "%s: Beginning file '%s'", __FUNCTION__, file.string().c_str());
ConsoleApplicationEngine app(&env, file);
diff --git a/project2/xml/sessionXml.cpp b/project2/xml/sessionXml.cpp
index 834c461..12235b1 100644
--- a/project2/xml/sessionXml.cpp
+++ b/project2/xml/sessionXml.cpp
@@ -3,10 +3,8 @@
#include "uuid.h"
#include <libxml++/nodes/element.h>
#include <libxml++/parsers/domparser.h>
-#include <libxml++/nodes/textnode.h>
-#include <set>
-#include <stdio.h>
#include <boost/foreach.hpp>
+#include <boost/filesystem/convenience.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/program_options.hpp>
@@ -17,10 +15,26 @@ class CustomSessionContainerLoaderXml : public SessionContainerLoaderImpl<Sessio
opts("SessionXML options")
{
opts.add_options()
- ("session.xml.path", po::value(&SessionContainerXml::xmlFile)->default_value("/tmp/project2sessions.xml"),
- "Path of the XML file in which to store session information")
+ ("session.xml.path", po::value(&SessionContainerXml::xmlDir)->default_value("/tmp/project2.sessions"),
+ "Path of the folder in which to store XML files for session information")
;
}
+ void onBefore()
+ {
+ boost::filesystem::create_directories(SessionContainerXml::xmlDir);
+ }
+ void onPeriodic()
+ {
+ boost::filesystem::directory_iterator end;
+ for (boost::filesystem::directory_iterator p(SessionContainerXml::xmlDir); p != end; ++p) {
+ xmlpp::DomParser session(p->path().string());
+ xmlpp::Element * sess = session.get_document()->get_root_node();
+ time_t expires = boost::lexical_cast<time_t>(sess->get_attribute_value("expires"));
+ if (expires < time(NULL)) {
+ boost::filesystem::remove(*p);
+ }
+ }
+ }
po::options_description *
options()
{
@@ -31,7 +45,7 @@ class CustomSessionContainerLoaderXml : public SessionContainerLoaderImpl<Sessio
po::options_description opts;
};
-std::string SessionContainerXml::xmlFile;
+boost::filesystem::path SessionContainerXml::xmlDir;
DECLARE_CUSTOM_COMPONENT_LOADER("xml", SessionContainerXml, CustomSessionContainerLoaderXml, SessionContainerLoader);
SessionContainerXml::SessionContainerXml()
@@ -46,65 +60,48 @@ static
void
appendToXmlNode(xmlpp::Element * sess, const Glib::ustring & name, const VariableType & value)
{
- xmlpp::Element * v = sess->add_child("var");
+ xmlpp::Element * v = sess->add_child(name);
v->add_child_text(value);
- v->set_attribute("name", name);
}
void
SessionContainerXml::SaveSession(SessionPtr currentSession) const
{
- xmlpp::DomParser parser;
- xmlpp::Document * doc;
- try {
- parser.parse_file(xmlFile);
- doc = parser.get_document();
- char xpath[200];
- snprintf(xpath, 200, "/sessions/session[@id='%s' or @expires < %lu]",
- currentSession->ID.str().c_str(), time(NULL));
- xmlpp::NodeSet sess = doc->get_root_node()->find(xpath);
- BOOST_FOREACH(xmlpp::Node * n, sess) {
- n->get_parent()->remove_child(n);
- }
- }
- catch (...) {
- doc = new xmlpp::Document();
- doc->create_root_node("sessions");
- }
- xmlpp::Element * sess = doc->get_root_node()->add_child("session");
- sess->set_attribute("id", currentSession->ID.str());
+ xmlpp::Document d;
+ xmlpp::Element * sess = d.create_root_node("session");
sess->set_attribute("expires", boost::lexical_cast<Glib::ustring>(currentSession->ExpiryTime()));
currentSession->ForeachValue(boost::bind(appendToXmlNode, sess, _1, _2));
- doc->write_to_file(xmlFile);
- if (!parser) {
- delete doc;
- }
- currentSession = NULL;
+ boost::filesystem::path p = xmlDir / currentSession->ID.str();
+ d.write_to_file(p.string());
}
SessionPtr
SessionContainerXml::getSession(const UUID & sid) const
{
try {
- xmlpp::DomParser sessions(xmlFile);
- char xpath[200];
- snprintf(xpath, 200, "/sessions/session[@id='%s' and @expires > %lu]",
- sid.str().c_str(), time(NULL));
- xmlpp::NodeSet sess = sessions.get_document()->get_root_node()->find(xpath);
- if (sess.size() == 1) {
- if (const xmlpp::Element * elem = dynamic_cast<const xmlpp::Element *>(sess[0])) {
- SessionPtr s = new Session(UUID(elem->get_attribute_value("id")));
- BOOST_FOREACH(const xmlpp::Node * n, elem->find("var")) {
- if (const xmlpp::Element * e = dynamic_cast<const xmlpp::Element *>(n)) {
- s->SetValue(e->get_attribute_value("name"), e->get_child_text() ? e->get_child_text()->get_content() : "");
- }
+ boost::filesystem::path p = xmlDir / sid.str();
+ xmlpp::DomParser session(p.string());
+ xmlpp::Element * sess = session.get_document()->get_root_node();
+ time_t expires = boost::lexical_cast<time_t>(sess->get_attribute_value("expires"));
+ if (expires < time(NULL)) {
+ boost::filesystem::remove(p);
+ return NULL;
+ }
+ SessionPtr s = new Session(sid);
+ BOOST_FOREACH(const xmlpp::Node * n, sess->get_children()) {
+ if (const xmlpp::Element * e = dynamic_cast<const xmlpp::Element *>(n)) {
+ if (e->get_child_text()) {
+ s->SetValue(e->get_name(), e->get_child_text()->get_content());
+ }
+ else {
+ s->SetValue(e->get_name(), Null());
}
- return s;
}
}
+ return s;
}
catch (...) {
+ return NULL;
}
- return NULL;
}
diff --git a/project2/xml/sessionXml.h b/project2/xml/sessionXml.h
index 2679bff..0ce502a 100644
--- a/project2/xml/sessionXml.h
+++ b/project2/xml/sessionXml.h
@@ -2,7 +2,7 @@
#define SESSIONXML_H
#include "sessionContainer.h"
-#include <map>
+#include <boost/filesystem/path.hpp>
class SessionContainerXml : public SessionContainer {
public:
@@ -15,7 +15,7 @@ class SessionContainerXml : public SessionContainer {
private:
// Configurables
- static std::string xmlFile;
+ static boost::filesystem::path xmlDir;
friend class CustomSessionContainerLoaderXml;
};