Menu

[r902]: / framework / branches / refactoring / application.cpp  Maximize  Restore  History

Download this file

211 lines (174 with data), 3.4 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#define CPPCMS_SOURCE
#include "application.h"
#include "http_context.h"
#include "service.h"
#include "cppcms_error.h"
#include "url_dispatcher.h"
#include "intrusive_ptr.h"
#include "applications_pool.h"
#include "http_response.h"
#include <set>
#include <vector>
#include <boost/bind.hpp>
namespace cppcms {
struct application::data {
data(cppcms::service *s):
service(s),
conn(0),
pool_id(-1)
{
}
cppcms::service *service;
intrusive_ptr<http::context> conn;
int pool_id;
url_dispatcher url;
std::vector<application *> managed_children;
};
application::application(cppcms::service &srv,application *p) :
d(new data(&srv)),
refs_(0)
{
if(p==0)
parent_=root_=this;
else
parent(p);
}
application::~application()
{
for(unsigned i=0;i<d->managed_children.size();i++) {
delete d->managed_children[i];
d->managed_children[i]=0;
}
}
long application::refs()
{
return refs_;
}
cppcms::service &application::service()
{
return *d->service;
}
json::value const &application::settings()
{
return service().settings();
}
http::request &application::request()
{
return context().request();
}
http::response &application::response()
{
return context().response();
}
url_dispatcher &application::dispatcher()
{
return d->url;
}
intrusive_ptr<http::context> application::get_context()
{
return root()->d->conn;
}
http::context &application::context()
{
if(!root()->d->conn)
throw cppcms_error("Access to unassigned context");
return *root()->d->conn;
}
intrusive_ptr<http::context> application::release_context()
{
intrusive_ptr<http::context> ptr=root()->d->conn;
assign_context(0);
return ptr;
}
bool application::is_asynchronous()
{
return pool_id() < 0;
}
void application::assign_context(intrusive_ptr<http::context> conn)
{
root()->d->conn=conn;
}
void application::pool_id(int id)
{
d->pool_id=id;
}
int application::pool_id()
{
return d->pool_id;
}
application *application::parent()
{
return parent_;
}
application *application::root()
{
return root_;
}
void application::parent(application *app)
{
parent_=app;
root_=app->root();
}
void application::add(application &app)
{
if(app.parent()!=this)
app.parent(this);
}
void application::add(application &app,std::string regex,int part)
{
add(app);
url_dispatcher().mount(regex,app,part);
}
void application::assign(application *app)
{
d->managed_children.push_back(app);
add(*app);
}
void application::assign(application *app,std::string regex,int part)
{
d->managed_children.push_back(app);
add(*app,regex,part);
}
void application::recycle()
{
if(root()->d->conn) {
response().out() << std::flush;
response().finalize();
context().async_complete_response();
}
assign_context(0);
}
void intrusive_ptr_add_ref(application *app)
{
++(app->root()->refs_);
}
// REMEMBER THIS IS CALLED FROM DESTRUCTOR!!!
void intrusive_ptr_release(application *app)
{
// it is called in destructors... So be very careful
try {
app = app->root();
long refs=--(app->refs_);
if(refs > 0)
return;
cppcms::service &service=app->service();
try {
app->recycle();
}
catch(...) {
if(app->pool_id() < 0) {
service.applications_pool().put(app);
}
else
delete app;
throw;
}
service.applications_pool().put(app);
// return the application to pool... or delete it if "pooled"
}
catch(...)
{
// FIXME LOG IT?
}
}
} // cppcms
MongoDB Logo MongoDB