Commit 064d0ce0 authored by Marian Triebe's avatar Marian Triebe

Rework simple_http_broker

Removed the cout bottleneck. The server now print the finished requests per second.
parent 4ecdc865
#include <iostream>
#include <chrono>
#include "caf/all.hpp"
#include "caf/io/all.hpp"
......@@ -28,48 +29,38 @@ constexpr size_t cstr_size(const char (&)[Size]) {
return Size;
}
actor_ostream out(broker* self, const char* role) {
return aout(self) << "[" << role << ":" << self->id() << "] ";
}
actor_ostream wout(broker* self) {
return out(self, "worker");
}
actor_ostream sout(broker* self) {
return out(self, "server");
}
behavior connection_worker(broker* self, connection_handle hdl) {
self->configure_read(hdl, receive_policy::at_most(1024));
return {
[=](const new_data_msg& msg) {
wout(self) << "received HTTP request, send http_ok" << endl;
self->write(msg.handle, cstr_size(http_ok), http_ok);
self->quit();
},
[=](const connection_closed_msg&) {
wout(self) << "connection closed by remote host, quit" << endl;
self->quit();
}
};
}
behavior server(broker* self) {
sout(self) << "running" << endl;
auto counter = std::make_shared<int>(0);
self->delayed_send(self, std::chrono::seconds(1), atom("tick"));
return {
[=](const new_connection_msg& ncm) {
auto worker = self->fork(connection_worker, ncm.handle);
self->monitor(worker);
self->link_to(worker);
sout(self) << "forked connection, handle ID: " << ncm.handle.id()
<< ", worker ID: " << worker.id() << endl;
},
[=](const down_msg& dm) {
sout(self) << "worker with ID " << dm.source.id() << " is done" << endl;
[=](const down_msg&) {
++*counter;
},
on(atom("tick")) >> [=] {
aout(self) << "Finished " << *counter << " requests per second." << endl;
*counter = 0;
self->delayed_send(self, std::chrono::seconds(1), atom("tick"));
},
others() >> [=] {
sout(self) << "unexpected: " << to_string(self->last_dequeued()) << endl;
aout(self) << "unexpected: " << to_string(self->last_dequeued()) << endl;
}
};
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment