Browse Source

Added "Application" base class for mutltiple

app. sites
master
Artyom Beilis 15 years ago
parent
commit
a3415711ff
9 changed files with 165 additions and 20 deletions
  1. +2
    -2
      Makefile.am
  2. +45
    -0
      application.cpp
  3. +79
    -0
      application.h
  4. +5
    -0
      configure.in
  5. +8
    -5
      hello_world.cpp
  6. +3
    -2
      hello_world_view.h
  7. +8
    -1
      manager.cpp
  8. +4
    -0
      worker_thread.cpp
  9. +11
    -10
      worker_thread.h

+ 2
- 2
Makefile.am View File

@@ -19,7 +19,7 @@ hello_world_view2.cpp: hello_world_skin2.tmpl hello_world_view1.tmpl
lib_LTLIBRARIES = libcppcms.la
libcppcms_la_SOURCES = global_config.cpp manager.cpp url.cpp worker_thread.cpp \
text_tool.cpp cache_interface.cpp base_cache.cpp thread_cache.cpp scgi.cpp \
base_view.cpp util.cpp form.cpp
base_view.cpp util.cpp form.cpp application.cpp

libcppcms_la_LDFLAGS = -no-undefined -version-info 0:0:0
libcppcms_la_LIBADD = @CPPCMS_LIBS@ transtext/libcppcmstranstext.la
@@ -37,7 +37,7 @@ nobase_pkginclude_HEADERS = global_config.h text_tool.h url.h cppcms_error.h \
manager.h worker_thread.h fcgi.h cache_interface.h archive.h \
base_cache.h thread_cache.h cgicc_connection.h scgi.h cgi_api.h \
process_cache.h shmem_allocator.h posix_mutex.h config.h cgi.h base_view.h \
util.h form.h
util.h form.h application.h

if EN_TCP_CACHE
libcppcms_la_SOURCES += tcp_cache.cpp


+ 45
- 0
application.cpp View File

@@ -0,0 +1,45 @@
#include "application.h"

namespace cppcms {
application::application(worker_thread &w) :
worker(w),
url(w.url),
app(worker.app),
cgi(worker.cgi),
env(worker.env),
cache(worker.cache),
cout(worker.cout),
on_start(worker.on_start),
on_end(worker.on_end)
{
}

application::~application()
{
}

void application::on_404()
{
set_header(new cgicc::HTTPStatusHeader(404,"Not found"));
cout<< "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
"<html>\n"
" <head>\n"
" <title>404 - Not Found</title>\n"
" </head>\n"
" <body>\n"
" <h1>404 - Not Found</h1>\n"
" </body>\n"
"</html>\n";
}

void application::main()
{
on_start();
if(url.parse()<0) {
on_404();
}
on_end();
}

}

+ 79
- 0
application.h View File

@@ -0,0 +1,79 @@
#ifndef CPPCMS_APPLICATION_H
#define CPPCMS_APPLICATION_H
#include "worker_thread.h"
#include "manager.h"

namespace cppcms {

struct application {

// Data
worker_thread &worker;
url_parser &url;
manager const &app;
Cgicc *&cgi;
CgiEnvironment const *&env;

cache_iface &cache;
ostream &cout;

boost::signal<void()> &on_start;
boost::signal<void()> &on_end;

// Construction
application(worker_thread &w);
virtual ~application();
// API

void set_header(HTTPHeader *h) { worker.set_header(h); }
void add_header(string s) { worker.add_header(s); }
void set_cookie(cgicc::HTTPCookie const &c) { worker.set_cookie(c); }

HTTPHeader &header() { return worker.header(); }

void set_lang() { worker.set_lang(); }
void set_lang(string const &s) { worker.set_lang(s) ; }

void use_template(string s="") { worker.use_template(s); }

void render(string n,base_content &c) { worker.render(n,c); }
void render(string t,string n,base_content &c) { worker.render(t,n,c); }
void render(string n,base_content &c,ostream &o) { worker.render(n,c,o); }
void render(string t,string n,base_content &c,ostream &o) { worker.render(t,n,c,o); }

virtual void on_404();

inline char const *gettext(char const *s) { return worker.gettext(s); };
inline char const *ngettext(char const *s,char const *p,int n) { return worker.ngettext(s,p,n); }

transtext::trans const *domain_gettext(string const &d) { return worker.domain_gettext(d); }

virtual void main();

};

template<typename T>
class application_worker : public worker_thread {
T app;
public:
application_worker(manager const &m) :
worker_thread(m),
app(*this)
{
}
virtual void main()
{
app.main();
}
};

template<typename T>
class application_factory : public simple_factory<application_worker<T> >
{
};

}

#endif



+ 5
- 0
configure.in View File

@@ -151,6 +151,11 @@ AC_CHECK_LIB(boost_iostreams,main,[
],[AC_CHECK_LIB(boost_iostreams-gcc-mt,main,[
CPPCMS_LIBS="-lboost_iostreams-gcc-mt $CPPCMS_LIBS"
],[ echo "boost::iostreams not found" ; exit -1])])
AC_CHECK_LIB(boost_signals,main,[
CPPCMS_LIBS="-lboost_signals $CPPCMS_LIBS"
],[AC_CHECK_LIB(boost_signals-gcc-mt,main,[
CPPCMS_LIBS="-lboost_signals-gcc-mt $CPPCMS_LIBS"
],[ echo "boost::signals not found" ; exit -1])])

AC_SUBST(CPPCMS_LIBS)
AC_OUTPUT

+ 8
- 5
hello_world.cpp View File

@@ -1,15 +1,18 @@
#include "worker_thread.h"
#include "application.h"
#include "manager.h"
#include "hello_world_view.h"
using namespace cppcms;

class my_hello_world : public worker_thread {
class my_hello_world : public application {
public:
my_hello_world(manager const &s) : worker_thread(s)
my_hello_world(worker_thread &w) :
application(w)
{
url.add("^/hello?$",
boost::bind(&my_hello_world::main,this));
use_template("view2");
};
virtual void main();
void main();
};

void my_hello_world::main()
@@ -40,7 +43,7 @@ int main(int argc,char ** argv)
{
try {
manager app(argc,argv);
app.set_worker(new simple_factory<my_hello_world>());
app.set_worker(new application_factory<my_hello_world>());
app.execute();
}
catch(std::exception const &e) {


+ 3
- 2
hello_world_view.h View File

@@ -1,4 +1,5 @@
#include "base_view.h"
#include "application.h"
#include <list>
#include "form.h"
using namespace cppcms;
@@ -27,7 +28,7 @@ struct my_form : public form {
//widgets::radio fruit;
widgets::select_multiple meat;
widgetset my_set;
my_form(worker_thread *w) :
my_form(application *w) :
username("user",w->gettext("Username")),
name("name",w->gettext("Real Name")),
mail("mail"),
@@ -63,7 +64,7 @@ struct hello : public master {
std::list<int> numbers;
std::list<data> lst;
my_form form;
hello(worker_thread *w) : form(w) {}
hello(application *w) : form(w) {}
};

};


+ 8
- 1
manager.cpp View File

@@ -529,7 +529,14 @@ void manager::execute()

void manager::load_templates()
{
string ext=config.sval("templates.ext",".so");
string ext=config.sval("templates.ext",
#ifdef __CYGWIN__
".dll"
#else
".so"
#endif
);
// FIXME to something that works with autotools
unsigned len=ext.size();
vector<string> const &dirs=config.slist("templates.dirs");
for(vector<string>::const_iterator dir=dirs.begin();dir!=dirs.end();++dir) {


+ 4
- 0
worker_thread.cpp View File

@@ -26,9 +26,13 @@ worker_thread::~worker_thread()
{
app.cache->del(caching_module);
}


void worker_thread::main()
{
on_start();
cout<<"<h1>Hello World</h2>\n";
on_end();
}
void worker_thread::set_header(HTTPHeader *h)
{


+ 11
- 10
worker_thread.h View File

@@ -11,6 +11,7 @@
#include <cgicc/HTMLClasses.h>
#include <boost/noncopyable.hpp>
#include <memory>
#include <boost/signal.hpp>

#include "cppcms_error.h"
#include "url.h"
@@ -30,6 +31,9 @@ using cgicc::Cgicc;
using cgicc::HTTPHeader;

class worker_thread: private boost::noncopyable {
int id;
pthread_t pid;

friend class url_parser;
friend class cache_iface;
friend class base_view;
@@ -44,10 +48,10 @@ class worker_thread: private boost::noncopyable {
string lang;
auto_ptr<HTTPHeader> response_header;

protected:
string current_template;
public:

url_parser url;
manager const &app;
Cgicc *cgi;
@@ -56,6 +60,9 @@ protected:
cache_iface cache;
ostream cout;

boost::signal<void()> on_start;
boost::signal<void()> on_end;

void set_header(HTTPHeader *h);
void add_header(string s);
void set_cookie(cgicc::HTTPCookie const &c);
@@ -65,24 +72,18 @@ protected:
void set_lang();
void set_lang(string const &s);



string current_template;

inline void use_template(string s="") { current_template=s; };

void render(string name,base_content &content);
void render(string templ,string name,base_content &content);
void render(string name,base_content &content,ostream &);
void render(string templ,string name,base_content &content,ostream &);

virtual void main();
public:

inline char const *gettext(char const *s) { return gt->gettext(s); };
inline char const *ngettext(char const *s,char const *p,int n) { return gt->ngettext(s,p,n); };

int id;
pthread_t pid;
ostream &get_cout() { return cout; }
transtext::trans const *domain_gettext(string const &domain);


Loading…
Cancel
Save