#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 <cppcms/rpc_json.h>
#include <cppcms/json.h>
#include <booster/intrusive_ptr.h>
#include <booster/aio/deadline_timer.h>
#include <booster/system_error.h>
#include <set>
#include <boost/bind.hpp>
using boost::bind;
public:
cppcms::rpc::json_rpc_server(srv),
timer_(srv.get_io_service())
{
bind(
"post",cppcms::rpc::json_method(&chat::post,
this),notification_role);
bind(
"get",cppcms::rpc::json_method(&chat::get,
this),method_role);
}
void post(std::string
const &author,std::string
const &
message)
{
obj["author"]=author;
messages_.push_back(obj);
broadcast(messages_.size()-1);
}
{
if(e) return;
if(
time(0) - last_wake_ > 10) {
broadcast(messages_.size());
}
}
void get(unsigned from)
{
if(from < messages_.size()) {
return;
}
else if(from == messages_.size()) {
waiters_.insert(call);
boost::bind(
&chat::remove_context,
call));
}
else {
}
}
{
waiters_.erase(call);
}
void broadcast(size_t from)
{
for(waiters_type::iterator waiter=waiters_.begin();waiter!=waiters_.end();++waiter) {
}
waiters_.clear();
}
{
ar.reserve(messages_.size() - n);
for(size_t i=n;i<messages_.size();i++) {
ar.push_back(messages_[i]);
}
return v;
}
private:
std::vector<cppcms::json::value> messages_;
typedef std::set<booster::shared_ptr<cppcms::rpc::json_call> > waiters_type;
waiters_type waiters_;
time_t last_wake_;
};
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;
}