Menu

[r691]: / framework / trunk / hello_world.cpp  Maximize  Restore  History

Download this file

182 lines (165 with data), 3.6 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
#include "application.h"
#include "manager.h"
#include "hello_world_view.h"
using namespace cppcms;
class my_hello_world : public application {
public:
my_hello_world(worker_thread &w) :
application(w)
{
url.add("^/?$",
boost::bind(&my_hello_world::std,this));
url.add("^/test$",boost::bind(&my_hello_world::test,this));
url.add("^/test2$",boost::bind(&my_hello_world::test2,this));
url.add("^/cache$",boost::bind(&my_hello_world::cache_test,this));
url.add("^/png$",boost::bind(&my_hello_world::png,this));
use_template("view2");
};
void test();
void png();
void test2();
void std();
void cache_test();
};
void my_hello_world::png()
{
ifstream file("test.png");
if(!file) {
cout<<"File test.png not found";
return ;
}
vector<char> buffer(1024);
set_header(new cgicc::HTTPContentHeader("image/png"));
set_user_io();
ostream &cout=cgi_conn->cout();
for(;;) {
file.read(&buffer.front(),1024);
cout.write(&buffer.front(),file.gcount());
if(file.eof())
break;
}
file.close();
}
void my_hello_world::test2()
{
ostringstream cout;
if(!session.is_set("test")) {
session["test"]="1";
cout<<"Set 1";
}
else {
int state=session.get<int>("test");
switch(state) {
case 1:
if(!session.is_exposed("test")) {
cout<<"Expose 1";
session.expose("test");
}
else {
session["test"]="2";
cout<<"Change exposed to 2";
}
break;
case 2:
if(session.is_exposed("test")) {
session.hide("test");
cout<<"Hidden 2";
}
else {
session["test"]="3";
cout<<"Hidden 2 moved to 3 and exposed";
session.expose("test");
}
break;
case 3:
session.del("test");
cout<<"Remove 3 and remove from hidden";
break;
default:
cout<<"Error";
}
}
flush_headers();
this->cout<<cout.str();
}
void my_hello_world::test()
{
ostringstream cout;
if(!session.is_set("time")) {
cout<<"No Time\n";
}
else {
time_t given=session.get<time_t>("time");
cout<<asctime(gmtime(&given))<<"<br/>\n";
if(session.is_set("msg")) {
cout<<session["msg"]<<"<br/>";
}
if(given % 3 == 0) {
cout<<"SET LONG MESSAGE";
session["msg"]="Looooooooooooooooooooooooooooooong msg";
}
else {
cout<<"UNSET LONG MESSAGE";
session.del("msg");
}
cout<<"<br/>"<<endl;
if(session.is_set("msg")) {
int val=given % 2;
session.expose("msg",val);
cout<<(val ? "exposed" : "hidden")<<endl;
}
//session.clear();
}
session.set<time_t>("time",time(NULL));
flush_headers();
this->cout<<cout.str();
}
void my_hello_world::std()
{
view::hello v(this);
if(env->getRequestMethod()=="POST") {
v.form.load(*cgi);
if(v.form.validate()) {
session["name"]=v.form.username.get();
v.username=v.form.username.get();
v.realname=v.form.name.get();
v.ok=v.form.ok.get();
v.password=v.form.p1.get();
v.form.clear();
}
}
v.title="Cool";
if(session.is_set("name"))
v.title+=":"+session["name"];
v.msg=gettext("Hello World");
for(int i=0;i<15;i++)
v.numbers.push_back(i);
v.lst.push_back(view::data("Hello",10));
render("hello",v);
}
void my_hello_world::cache_test()
{
string tmp;
bool from_cache=true;
flush_headers();
if(!cache.fetch_frame("test",tmp,true)) {
tmp="test value";
from_cache=false;
cache.store_frame("test",tmp,5);
}
if(from_cache)
cout <<"Fetched ["<<tmp<<"] from cache";
else
cout <<"Fetched ["<<tmp<<"] from start";
}
int main(int argc,char ** argv)
{
try {
manager app(argc,argv);
app.set_worker(new application_factory<my_hello_world>());
app.execute();
}
catch(std::exception const &e) {
cerr<<e.what()<<endl;
}
}
MongoDB Logo MongoDB