#include "tile/rpc/server.h"

#include "tile/base/chrono.h"

#include "gflags/gflags.h"

DEFINE_int32(tile_rpc_server_max_packet_size, 4 * 1024 * 1024, "");
DEFINE_int32(tile_rpc_server_max_connections, 10000, "");
DEFINE_int32(tile_rpc_server_max_request_queuing_delay, 0, "");
DEFINE_int32(tile_rpc_server_max_ongoing_calls, 10000, "");

namespace tile {

struct Server::ConnectionContext {
    ConnectionId conn_id;
    Endpoint remote_peer;
    rpc::detail::ServerConnectionHandler *handler;
};

Server::Server() : Server(Options())
{
    idle_connn_cleaner_ = internal::TimeKeeper::Instance()->AddTimer(
        ReadSteadyClock(), 15 * std::chrono::seconds(1), [this]() { OnConnectionCleanupTimer(); }, true);
}

Server::Server(Options opt) : options_(std::move(opt)) {}

Server::~Server()
{
    if (state_ == ServerState::Initialized || state_ == ServerState::Joined) {
        // Nothing to do then
    } else if (state_ == ServerState::Running) {
        Stop();
        Join();
    } else {
        TILE_CHECK(state_ == ServerState::Stopped);
        TILE_LOG_FATAL("!!!");
    }
}

void
Server::AddProtocol(const std::string &name)
{
    if (known_protocols_.find(name) == known_protocols_.end()) {}
}
}// namespace tile
