Commit f8b2d8fc authored by octal's avatar octal

Client is now using the new Reactor class

parent 537f30ab
......@@ -11,7 +11,7 @@
using namespace Net;
int main(int argc, char *argv[]) {
if (argc < 1) {
if (argc < 2) {
std::cerr << "Usage: http_client page [count]" << std::endl;
return 1;
}
......
......@@ -8,7 +8,7 @@
#include "async.h"
#include "os.h"
#include "http.h"
#include "io.h"
#include "reactor.h"
#include "timer_pool.h"
#include "view.h"
#include <atomic>
......@@ -159,14 +159,14 @@ private:
size_t maxConnectionsPerHost;
};
class Transport : public Io::Handler {
class Transport : public Aio::Handler {
public:
PROTOTYPE_OF(Io::Handler, Transport)
PROTOTYPE_OF(Aio::Handler, Transport)
typedef std::function<void()> OnResponseParsed;
void onReady(const Io::FdSet& fds);
void onReady(const Aio::FdSet& fds);
void registerPoller(Polling::Epoll& poller);
Async::Promise<void>
......@@ -328,11 +328,11 @@ public:
void shutdown();
private:
Io::ServiceGroup io_;
std::shared_ptr<Aio::Reactor> reactor_;
ConnectionPool pool;
std::shared_ptr<Transport> transport_;
Aio::Reactor::Key transportKey;
std::atomic<uint64_t> ioIndex;
......
......@@ -17,6 +17,7 @@
#include <unistd.h>
#include "os.h"
#include "io.h"
#include "reactor.h"
namespace Net {
......@@ -55,11 +56,13 @@ public:
}
void disarm();
void registerIo(Io::Service* io) {
void
registerReactor(const Aio::Reactor::Key& key, Aio::Reactor* reactor) {
if (!registered) {
io->registerFd(fd, Polling::NotifyOn::Read);
reactor->registerFd(key, fd, Polling::NotifyOn::Read);
registered = true;
}
registered = true;
}
private:
......
......@@ -145,7 +145,7 @@ namespace {
}
void
Transport::onReady(const Io::FdSet& fds) {
Transport::onReady(const Aio::FdSet& fds) {
for (const auto& entry: fds) {
if (entry.getTag() == connectionsQueue.tag()) {
handleConnectionQueue();
......@@ -180,7 +180,7 @@ Transport::onReady(const Io::FdSet& fds) {
else {
conn.resolve();
// We are connected, we can start reading data now
io()->modifyFd(conn.connection->fd, NotifyOn::Read);
reactor()->modifyFd(key(), conn.connection->fd, NotifyOn::Read);
}
} else {
throw std::runtime_error("Unknown fd");
......@@ -212,7 +212,8 @@ Transport::asyncSendRequest(
const Buffer& buffer) {
return Async::Promise<ssize_t>([&](Async::Resolver& resolve, Async::Rejection& reject) {
if (std::this_thread::get_id() != io()->thread()) {
auto ctx = context();
if (std::this_thread::get_id() != ctx.thread()) {
RequestEntry req(std::move(resolve), std::move(reject), connection, std::move(timer), buffer.detach());
auto *e = requestsQueue.allocEntry(std::move(req));
requestsQueue.push(e);
......@@ -250,7 +251,7 @@ Transport::asyncSendRequestImpl(
if (status == FirstTry) {
throw std::runtime_error("Unimplemented, fix me!");
}
io()->modifyFd(fd, NotifyOn::Write, Polling::Mode::Edge);
reactor()->modifyFd(key(), fd, NotifyOn::Write, Polling::Mode::Edge);
}
else {
cleanUp();
......@@ -265,7 +266,7 @@ Transport::asyncSendRequestImpl(
if (req.timer) {
timeouts.insert(
std::make_pair(req.timer->fd, conn));
req.timer->registerIo(io());
req.timer->registerReactor(key(), reactor());
}
req.resolve(totalWritten);
break;
......@@ -298,7 +299,7 @@ Transport::handleConnectionQueue() {
int res = ::connect(conn->fd, data.addr, data.addr_len);
if (res == -1) {
if (errno == EINPROGRESS) {
io()->registerFdOneShot(conn->fd, NotifyOn::Write | NotifyOn::Hangup | NotifyOn::Shutdown);
reactor()->registerFdOneShot(key(), conn->fd, NotifyOn::Write | NotifyOn::Hangup | NotifyOn::Shutdown);
}
else {
data.reject(Error::system("Failed to connect"));
......@@ -738,7 +739,8 @@ Client::Options::maxConnectionsPerHost(int val) {
}
Client::Client()
: ioIndex(0)
: reactor_(Aio::Reactor::create())
, ioIndex(0)
{
}
......@@ -763,13 +765,14 @@ void
Client::init(const Client::Options& options) {
pool.init(options.maxConnectionsPerHost_);
transport_.reset(new Transport);
io_.init(options.threads_, transport_);
io_.start();
reactor_->init(Aio::AsyncContext(options.threads_));
transportKey = reactor_->addHandler(transport_);
reactor_->run();
}
void
Client::shutdown() {
io_.shutdown();
reactor_->shutdown();
}
RequestBuilder
......@@ -835,10 +838,10 @@ Client::doRequest(
else {
if (!conn->hasTransport()) {
auto index = ioIndex.fetch_add(1) % io_.size();
auto service = io_.service(index);
auto transports = reactor_->handlers(transportKey);
auto index = ioIndex.fetch_add(1) % transports.size();
auto transport = std::static_pointer_cast<Transport>(service->handler());
auto transport = std::static_pointer_cast<Transport>(transports[index]);
conn->associateTransport(transport);
}
......
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