forked from artyom-beilis/cppcms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.cpp
More file actions
108 lines (99 loc) · 3.16 KB
/
chat.cpp
File metadata and controls
108 lines (99 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
//
// See accompanying file COPYING.TXT file for licensing details.
//
///////////////////////////////////////////////////////////////////////////////
#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
class chat : public cppcms::application {
public:
chat(cppcms::service &srv) : cppcms::application(srv)
{
dispatcher().assign("/post",&chat::post,this);
dispatcher().assign("/get/(\\d+)",&chat::get,this,1);
dispatcher().assign(".*",&chat::redirect,this);
}
void redirect()
{
response().set_redirect_header("/the_chat.html");
}
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()) {
response().set_plain_text_header();
response().out()<<messages_[pos];
return;
}
if(pos > messages_.size() ){
response().status(404);
return;
}
booster::shared_ptr<cppcms::http::context> context=release_context();
waiters_.insert(context);
context->async_on_peer_reset(
bind(
&chat::remove_context,
booster::intrusive_ptr<chat>(this),
context));
}
void remove_context(booster::shared_ptr<cppcms::http::context> context)
{
waiters_.erase(context);
}
void broadcast()
{
for(waiters_type::iterator it=waiters_.begin();it!=waiters_.end();++it) {
booster::shared_ptr<cppcms::http::context> waiter = *it;
waiter->response().set_plain_text_header();
waiter->response().out() << messages_.back();
waiter->async_complete_response();
}
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 {
cppcms::service service(argc,argv);
booster::intrusive_ptr<chat> c=new chat(service);
service.applications_pool().mount(c);
service.run();
}
catch(std::exception const &e) {
std::cerr<<"Catched exception: "<<e.what()<<std::endl;
return 1;
}
return 0;
}
// vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4