#include <cppcms/application.h>
#include <cppcms/url_dispatcher.h>
#include <cppcms/applications_pool.h>
#include <cppcms/service.h>
#include <cppcms/http_response.h>
#include <cppcms/http_request.h>
#include <cppcms/http_context.h>
#include <booster/intrusive_ptr.h>
#include <set>
#ifdef USE_STD_TR1_BIND
#include <tr1/functional>
using std::tr1::bind;
#elif defined USE_STD_BIND
#include <functional>
using std::bind;
#else
#include <boost/bind.hpp>
using boost::bind;
#endif
public:
{
}
void redirect()
{
}
void post()
{
if(
request().request_method()==
"POST") {
messages_.push_back(
request().post(
"message"));
broadcast();
}
}
void get(std::string no)
{
unsigned pos=atoi(no.c_str());
if(pos < messages_.size()) {
return;
}
if(pos > messages_.size() ){
return;
}
waiters_.insert(context);
bind(
&chat::remove_context,
context));
}
{
waiters_.erase(context);
}
void broadcast()
{
for(waiters_type::iterator it=waiters_.begin();it!=waiters_.end();++it) {
}
waiters_.clear();
}
private:
std::vector<std::string> messages_;
typedef std::set<booster::shared_ptr<cppcms::http::context> > waiters_type;
waiters_type waiters_;
};
int main(
int argc,
char **argv)
{
try {
service.applications_pool().mount(c);
service.run();
}
catch(std::exception const &e) {
std::cerr<<"Catched exception: "<<e.what()<<std::endl;
return 1;
}
return 0;
}