summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrandomdan <randomdan@localhost>2011-07-31 17:18:04 +0000
committerrandomdan <randomdan@localhost>2011-07-31 17:18:04 +0000
commitf35090edb8664638d45e38f196763cbcb0d9bc25 (patch)
tree8e88f7142e5699d4bf31e74a8c323ce8c534c7fe
parentStrip out some useless wrappers (diff)
downloadproject2-f35090edb8664638d45e38f196763cbcb0d9bc25.tar.bz2
project2-f35090edb8664638d45e38f196763cbcb0d9bc25.tar.xz
project2-f35090edb8664638d45e38f196763cbcb0d9bc25.zip
Allow all plugable components to have settings injected into them
Add settings to SendMailTask and SessionXML
-rw-r--r--project2/cgi/cgiAppEngine.cpp2
-rw-r--r--project2/cgi/p2webCgi.cpp7
-rw-r--r--project2/cgi/p2webFCgi.cpp7
-rw-r--r--project2/console/consoleEnvironment.cpp13
-rw-r--r--project2/console/p2consoleMain.cpp7
-rw-r--r--project2/environment.cpp45
-rw-r--r--project2/sendmailTask.cpp26
-rw-r--r--project2/sendmailTask.h4
-rw-r--r--project2/sessionXml.cpp26
-rw-r--r--project2/sessionXml.h5
-rw-r--r--project2/xmlObjectLoader.cpp30
-rw-r--r--project2/xmlObjectLoader.h8
12 files changed, 128 insertions, 52 deletions
diff --git a/project2/cgi/cgiAppEngine.cpp b/project2/cgi/cgiAppEngine.cpp
index d601670..2d20614 100644
--- a/project2/cgi/cgiAppEngine.cpp
+++ b/project2/cgi/cgiAppEngine.cpp
@@ -90,7 +90,7 @@ CgiApplicationEngine::process() const
HttpHeaderPtr header = rs->getHeader();
if (!sessionID.is_nil()) {
header->setCookie(cgicc::HTTPCookie(SESSIONID, sessionID.str(), "Session ID",
- _env->getServerName().substr(_env->getServerName().find(".")), 3600, "/", false));
+ _env->getServerName().substr(_env->getServerName().find(".")), env()->sessionTimeOut, "/", false));
}
header->render(IO);
if (currentStage.get<2>()) {
diff --git a/project2/cgi/p2webCgi.cpp b/project2/cgi/p2webCgi.cpp
index f10573f..6168819 100644
--- a/project2/cgi/p2webCgi.cpp
+++ b/project2/cgi/p2webCgi.cpp
@@ -1,12 +1,13 @@
#include "cgiCommon.h"
#include "../xmlObjectLoader.h"
+#include <boost/bind.hpp>
int
main(void)
{
cgiServe(NULL, std::cout);
- LoaderBase::onIteration();
- LoaderBase::onPeriodic();
- LoaderBase::onIdle();
+ LoaderBase::onAllComponents(boost::bind(&ComponentLoader::onIteration, _1));
+ LoaderBase::onAllComponents(boost::bind(&ComponentLoader::onPeriodic, _1));
+ LoaderBase::onAllComponents(boost::bind(&ComponentLoader::onIdle, _1));
}
diff --git a/project2/cgi/p2webFCgi.cpp b/project2/cgi/p2webFCgi.cpp
index d1aa29e..9a47b24 100644
--- a/project2/cgi/p2webFCgi.cpp
+++ b/project2/cgi/p2webFCgi.cpp
@@ -1,6 +1,7 @@
#include "cgiCommon.h"
#include "FCgiIO.h"
#include "../xmlObjectLoader.h"
+#include <boost/bind.hpp>
time_t lastPeriodic = 0;
time_t periodicDelay = 600;
@@ -10,7 +11,7 @@ void
p2webPeriodic()
{
time(&lastPeriodic);
- LoaderBase::onPeriodic();
+ LoaderBase::onAllComponents(boost::bind(&ComponentLoader::onPeriodic, _1));
}
static
@@ -20,7 +21,7 @@ p2webGoingIdle(int)
if (time(NULL) > lastPeriodic + periodicDelay) {
p2webPeriodic();
}
- LoaderBase::onIdle();
+ LoaderBase::onAllComponents(boost::bind(&ComponentLoader::onIdle, _1));
}
int
@@ -44,7 +45,7 @@ main(void)
cgicc::FCgiIO IO(request);
cgiServe(&IO, IO);
alarm(60);
- LoaderBase::onIteration();
+ LoaderBase::onAllComponents(boost::bind(&ComponentLoader::onIteration, _1));
if (time(NULL) > lastPeriodic + periodicDelay) {
p2webPeriodic();
}
diff --git a/project2/console/consoleEnvironment.cpp b/project2/console/consoleEnvironment.cpp
index 5233a67..6d1471b 100644
--- a/project2/console/consoleEnvironment.cpp
+++ b/project2/console/consoleEnvironment.cpp
@@ -5,6 +5,7 @@
#include <string.h>
#include "../logger.h"
#include "../exceptions.h"
+#include "../xmlObjectLoader.h"
#include <iostream>
#include <string>
#include <boost/algorithm/string/predicate.hpp>
@@ -48,6 +49,15 @@ ConsoleEnvironment::addOptions(boost::program_options::positional_options_descri
return console;
}
+static
+void
+showHelpHelper(const boost::program_options::options_description * options)
+{
+ if (options) {
+ std::cout << *options << std::endl;
+ }
+}
+
void
ConsoleEnvironment::postinit(const boost::program_options::options_description & options, const boost::program_options::variables_map & settings)
{
@@ -56,7 +66,8 @@ ConsoleEnvironment::postinit(const boost::program_options::options_description &
exit(1);
}
if (settings.count("help")) {
- std::cout << options << std::endl;
+ showHelpHelper(&options);
+ LoaderBase::onAllComponents(boost::bind(showHelpHelper, boost::bind(&ComponentLoader::options, _1)));
exit(1);
}
}
diff --git a/project2/console/p2consoleMain.cpp b/project2/console/p2consoleMain.cpp
index 4584126..40995d2 100644
--- a/project2/console/p2consoleMain.cpp
+++ b/project2/console/p2consoleMain.cpp
@@ -4,6 +4,7 @@
#include "../xmlObjectLoader.h"
#include "../logger.h"
#include <boost/foreach.hpp>
+#include <boost/bind.hpp>
int
main(int argc, char ** argv)
@@ -18,9 +19,9 @@ main(int argc, char ** argv)
app.process();
Logger()->messagef(LOG_DEBUG, "%s: Complete", __FUNCTION__);
- LoaderBase::onIteration();
+ LoaderBase::onAllComponents(boost::bind(&ComponentLoader::onIteration, _1));
}
- LoaderBase::onPeriodic();
- LoaderBase::onIdle();
+ LoaderBase::onAllComponents(boost::bind(&ComponentLoader::onPeriodic, _1));
+ LoaderBase::onAllComponents(boost::bind(&ComponentLoader::onIdle, _1));
}
diff --git a/project2/environment.cpp b/project2/environment.cpp
index bdbc8f5..12591bf 100644
--- a/project2/environment.cpp
+++ b/project2/environment.cpp
@@ -1,9 +1,11 @@
#include "environment.h"
#include "loggers.h"
+#include "xmlObjectLoader.h"
#include <stdio.h>
#include <fstream>
#include <boost/filesystem/convenience.hpp>
#include <boost/foreach.hpp>
+#include <boost/bind.hpp>
namespace po = boost::program_options;
@@ -11,8 +13,8 @@ const Environment * Environment::currentEnv(NULL);
int Environment::clLevel(-1);
int Environment::slLevel(-1);
bool Environment::optionsBuilt(false);
-boost::program_options::options_description Environment::allOptions("Project2 options");
-boost::program_options::positional_options_description Environment::posOptions;
+po::options_description Environment::allOptions("Project2 options");
+po::positional_options_description Environment::posOptions;
Environment::Environment(int c, char ** v) :
argc(c),
@@ -21,6 +23,31 @@ Environment::Environment(int c, char ** v) :
currentEnv = this;
}
+static
+po::variables_map
+injectSettingsHelper(int argc, char ** argv, const po::options_description * opts, po::positional_options_description * posOpts)
+{
+ po::variables_map settings;
+ if (!opts) {
+ return settings;
+ }
+ if (argc > 0 && argv != NULL) {
+ if (posOpts) {
+ po::store(po::command_line_parser(argc, argv).options(*opts).positional(*posOpts).allow_unregistered().run(), settings);
+ }
+ else {
+ po::store(po::command_line_parser(argc, argv).options(*opts).allow_unregistered().run(), settings);
+ }
+ }
+ po::store(po::parse_environment(*opts, "P2_"), settings);
+ if (boost::filesystem::exists(".p2config")) {
+ std::ifstream f(".p2config");
+ po::store(po::parse_config_file(f, *opts, true), settings);
+ }
+ po::notify(settings);
+ return settings;
+}
+
void
Environment::init()
{
@@ -43,18 +70,10 @@ Environment::init()
allOptions.add(common).add(addOptions(posOptions));
optionsBuilt = true;
}
- po::variables_map settings;
- if (argc > 0 && argv != NULL) {
- po::store(po::command_line_parser(argc, argv).options(allOptions).positional(posOptions).run(), settings);
- }
- po::store(po::parse_environment(allOptions, "P2_"), settings);
- if (boost::filesystem::exists(".p2config")) {
- std::ifstream f(".p2config");
- po::store(po::parse_config_file(f, allOptions, true), settings);
- }
- po::notify(settings);
-
+ po::variables_map settings(injectSettingsHelper(argc, argv, &allOptions, &posOptions));
postinit(allOptions, settings);
+ LoaderBase::onAllComponents(boost::bind(
+ injectSettingsHelper, argc, argv, boost::bind(&ComponentLoader::options, _1), (po::positional_options_description*)NULL));
if (clLevel >= 0) {
Logger()->addLogger(new ConsoleLogDriver(stderr, clLevel, false));
diff --git a/project2/sendmailTask.cpp b/project2/sendmailTask.cpp
index fa9a4a3..73afb20 100644
--- a/project2/sendmailTask.cpp
+++ b/project2/sendmailTask.cpp
@@ -11,7 +11,29 @@
#include "transformHtml.h"
#include "transformText.h"
-DECLARE_LOADER("sendmail", SendMailTask);
+std::string SendMailTask::defaultMailServer;
+
+namespace po = boost::program_options;
+class CustomSendMailTaskLoader : public ElementLoaderImpl<SendMailTask> {
+ public:
+ CustomSendMailTaskLoader() :
+ opts("Send Email Task options")
+ {
+ opts.add_options()
+ ("sendmail.defaultserver", po::value(&SendMailTask::defaultMailServer),
+ "The address of the default mail relay server")
+ ;
+ }
+ po::options_description *
+ options()
+ {
+ return &opts;
+ }
+
+ private:
+ po::options_description opts;
+};
+DECLARE_CUSTOM_LOADER("sendmail", CustomSendMailTaskLoader);
uint8_t SendMailTask::MailPart::mimeIdx;
@@ -28,7 +50,7 @@ SendMailTask::SendMailTask(const xmlpp::Element * p) :
to(p, "to"),
subject(p, "subject"),
from(p, "from"),
- server(p, "server"),
+ server(p, "server", defaultMailServer.empty(), defaultMailServer),
present(p->get_attribute_value("present").raw())
{
}
diff --git a/project2/sendmailTask.h b/project2/sendmailTask.h
index f871574..fa8427f 100644
--- a/project2/sendmailTask.h
+++ b/project2/sendmailTask.h
@@ -50,6 +50,10 @@ class SendMailTask : public Task {
mutable boost::intrusive_ptr<Parts> parts;
mutable PartList::iterator part;
+
+ // Configurables
+ friend class CustomSendMailTaskLoader;
+ static std::string defaultMailServer;
};
#endif
diff --git a/project2/sessionXml.cpp b/project2/sessionXml.cpp
index fc552a9..c1e2158 100644
--- a/project2/sessionXml.cpp
+++ b/project2/sessionXml.cpp
@@ -7,6 +7,7 @@
#include <stdio.h>
#include <boost/foreach.hpp>
#include <boost/lexical_cast.hpp>
+#include <boost/program_options.hpp>
/// Session implementation that stores its contents in an XML file on the local filesystem
class SessionXml : public Session {
@@ -31,8 +32,29 @@ class SessionXml : public Session {
friend class SessionContainerXml;
};
-const char * xmlFile = "/tmp/project2sessions.xml";
-DECLARE_COMPONENT_LOADER("xml", SessionContainerXml, SessionContainerLoader);
+namespace po = boost::program_options;
+class CustomSessionContainerLoaderXml : public SessionContainerLoaderImpl<SessionContainerXml> {
+ public:
+ CustomSessionContainerLoaderXml() :
+ 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")
+ ;
+ }
+ po::options_description *
+ options()
+ {
+ return &opts;
+ }
+
+ private:
+ po::options_description opts;
+};
+
+std::string SessionContainerXml::xmlFile;
+DECLARE_CUSTOM_COMPONENT_LOADER("xml", SessionContainerXml, CustomSessionContainerLoaderXml, SessionContainerLoader);
SessionContainerXml::SessionContainerXml()
{
diff --git a/project2/sessionXml.h b/project2/sessionXml.h
index d8bc8aa..9b5c3e1 100644
--- a/project2/sessionXml.h
+++ b/project2/sessionXml.h
@@ -14,6 +14,11 @@ class SessionContainerXml : public SessionContainer {
SessionPtr getSession(UUID & sid);
typedef boost::intrusive_ptr<SessionXml> SessionXmlPtr;
SessionXmlPtr currentSession;
+
+ private:
+ // Configurables
+ static std::string xmlFile;
+ friend class CustomSessionContainerLoaderXml;
};
#endif
diff --git a/project2/xmlObjectLoader.cpp b/project2/xmlObjectLoader.cpp
index faa5b2a..5cc28f0 100644
--- a/project2/xmlObjectLoader.cpp
+++ b/project2/xmlObjectLoader.cpp
@@ -94,6 +94,7 @@ LoaderBase::collectAll(const CommonObjects * co, const xmlpp::Element * node, bo
if (depth != 0) {
throw std::logic_error("Cannot set CommonObjects in subloader");
}
+ loadedObjects.clear();
collectAll(node, childrenOnly, uh);
BOOST_FOREACH(SourceObjectPtr o, loadedObjects) {
o->loadComplete(co);
@@ -102,29 +103,10 @@ LoaderBase::collectAll(const CommonObjects * co, const xmlpp::Element * node, bo
}
void
-LoaderBase::onIdle()
-{
- BOOST_FOREACH(ComponentLoaderSet::value_type l, *componentLoaders()) {
- l->onIdle();
- }
-}
-
-void
-LoaderBase::onIteration()
-{
- // This is a fail safe in the event that something goes pearshape
- loadedObjects.clear();
-
- BOOST_FOREACH(ComponentLoaderSet::value_type l, *componentLoaders()) {
- l->onIteration();
- }
-}
-
-void
-LoaderBase::onPeriodic()
+LoaderBase::onAllComponents(const boost::function1<void, ComponentLoader *> & func)
{
BOOST_FOREACH(ComponentLoaderSet::value_type l, *componentLoaders()) {
- l->onPeriodic();
+ func(l.get());
}
}
@@ -159,3 +141,9 @@ ComponentLoader::onPeriodic()
{
}
+boost::program_options::options_description *
+ComponentLoader::options()
+{
+ return NULL;
+}
+
diff --git a/project2/xmlObjectLoader.h b/project2/xmlObjectLoader.h
index c86720d..4795717 100644
--- a/project2/xmlObjectLoader.h
+++ b/project2/xmlObjectLoader.h
@@ -4,6 +4,7 @@
#include <set>
#include <string>
#include <boost/intrusive_ptr.hpp>
+#include <boost/function.hpp>
#include <boost/shared_ptr.hpp>
#include "intrusivePtrBase.h"
#include "sourceObject.h"
@@ -32,9 +33,7 @@ class LoaderBase {
std::set<boost::intrusive_ptr<Storer> > supportedStorers;
- static void onIdle();
- static void onIteration();
- static void onPeriodic();
+ static void onAllComponents(const boost::function1<void, ComponentLoader *> &);
static std::set<boost::shared_ptr<ComponentLoader> > * & componentLoaders();
@@ -109,11 +108,14 @@ class LoaderBase {
DECLARE_COMPONENT_LOADER(N, T, ElementLoader)
/// Helper for loading and maintaining Project2 components
+namespace boost { namespace program_options { class options_description; } }
class ComponentLoader {
public:
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
+ virtual boost::program_options::options_description *
+ options(); // Options to be populated from the common config file/env/etc
};
/// Helper for loading and maintaining Project2 script components
class ElementLoader : public ComponentLoader {