lokinet/libabyss/main.cpp

149 lines
3.6 KiB
C++
Raw Normal View History

#include <libabyss.hpp>
2019-01-11 01:59:44 +00:00
#include <net/net.hpp>
#include <absl/synchronization/mutex.h>
2018-10-25 17:31:09 +00:00
#ifndef _WIN32
2019-03-08 12:26:21 +00:00
#include <signal.h>
2018-10-25 17:31:09 +00:00
#endif
2018-11-01 12:47:14 +00:00
struct DemoHandler : public abyss::httpd::IRPCHandler
{
2018-11-01 12:47:14 +00:00
DemoHandler(abyss::httpd::ConnImpl* impl) : abyss::httpd::IRPCHandler(impl)
{
}
absl::optional< Response >
HandleJSONRPC(Method_t method,
ABSL_ATTRIBUTE_UNUSED const Params& params) override
{
2018-10-25 17:03:25 +00:00
llarp::LogInfo("method: ", method);
return Response::object();
}
};
2018-11-01 12:47:14 +00:00
struct DemoCall : public abyss::http::IRPCClientHandler
{
2019-02-26 15:09:37 +00:00
std::function< void(void) > m_Callback;
2019-05-22 16:20:50 +00:00
std::shared_ptr< llarp::Logic > m_Logic;
2019-02-26 15:09:37 +00:00
2019-05-22 16:20:50 +00:00
DemoCall(abyss::http::ConnImpl* impl, std::shared_ptr< llarp::Logic > logic,
2019-02-26 15:09:37 +00:00
std::function< void(void) > callback)
: abyss::http::IRPCClientHandler(impl)
, m_Callback(callback)
, m_Logic(logic)
{
2018-11-01 12:47:14 +00:00
llarp::LogInfo("new call");
}
2019-02-26 15:09:37 +00:00
bool HandleResponse(abyss::http::RPC_Response) override
2018-11-01 12:47:14 +00:00
{
2019-02-26 15:09:37 +00:00
llarp::LogInfo("response get");
m_Logic->queue_func(m_Callback);
2018-11-01 12:47:14 +00:00
return true;
}
void
PopulateReqHeaders(ABSL_ATTRIBUTE_UNUSED abyss::http::Headers_t& hdr) override
2018-11-01 12:47:14 +00:00
{
}
void
HandleError() override
2018-11-01 12:47:14 +00:00
{
llarp::LogError("error while handling call: ", strerror(errno));
}
};
struct DemoClient : public abyss::http::JSONRPC
{
2019-04-08 12:01:52 +00:00
llarp_ev_loop_ptr m_Loop;
2019-05-22 16:20:50 +00:00
std::shared_ptr< llarp::Logic > m_Logic;
2019-02-26 15:09:37 +00:00
2019-05-22 16:20:50 +00:00
DemoClient(llarp_ev_loop_ptr l, std::shared_ptr< llarp::Logic > logic)
2019-04-08 12:01:52 +00:00
: abyss::http::JSONRPC(), m_Loop(std::move(l)), m_Logic(logic)
2019-02-26 15:09:37 +00:00
{
}
2018-11-01 12:47:14 +00:00
abyss::http::IRPCClientHandler*
NewConn(abyss::http::ConnImpl* impl)
{
2019-06-02 21:17:05 +00:00
return new DemoCall(impl, m_Logic, std::bind(&llarp_ev_loop_stop, m_Loop));
2018-11-01 12:47:14 +00:00
}
void
DoDemoRequest()
{
QueueRPC("test", nlohmann::json::object(),
2018-11-01 12:47:14 +00:00
std::bind(&DemoClient::NewConn, this, std::placeholders::_1));
Flush();
}
2018-11-01 12:47:14 +00:00
};
struct DemoServer : public abyss::httpd::BaseReqHandler
{
DemoServer() : abyss::httpd::BaseReqHandler(1000)
{
}
abyss::httpd::IRPCHandler*
CreateHandler(abyss::httpd::ConnImpl* impl)
{
return new DemoHandler(impl);
}
};
int
main(ABSL_ATTRIBUTE_UNUSED int argc, ABSL_ATTRIBUTE_UNUSED char* argv[])
{
2018-10-25 17:31:09 +00:00
// Ignore on Windows, we don't even get SIGPIPE (even though native *and*
// emulated UNIX pipes exist - CreatePipe(2), pipe(3))
// Microsoft libc only covers six signals
#ifndef _WIN32
signal(SIGPIPE, SIG_IGN);
#else
WSADATA wsockd;
int err;
err = ::WSAStartup(MAKEWORD(2, 2), &wsockd);
if(err)
{
perror("Failed to start Windows Sockets");
return err;
}
2018-10-25 17:31:09 +00:00
#endif
#ifdef LOKINET_DEBUG
absl::SetMutexDeadlockDetectionMode(absl::OnDeadlockCycle::kAbort);
#endif
2018-11-01 12:47:14 +00:00
llarp::SetLogLevel(llarp::eLogDebug);
llarp_threadpool* threadpool = llarp_init_same_process_threadpool();
// Now that libuv is the single non-Windows event loop impl, we can
// go back to using the normal function
2019-06-18 04:28:44 +00:00
llarp_ev_loop_ptr loop = llarp_make_ev_loop();
auto logic = std::make_shared< llarp::Logic >(threadpool);
sockaddr_in addr;
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
addr.sin_port = htons(1222);
addr.sin_family = AF_INET;
DemoServer serv;
2019-02-26 15:09:37 +00:00
DemoClient client(loop, logic);
llarp::Addr a(addr);
2018-10-25 17:03:25 +00:00
while(true)
{
llarp::LogInfo("bind to ", a);
if(serv.ServeAsync(loop, logic, a))
{
2018-11-01 12:47:14 +00:00
client.RunAsync(loop, a.ToString());
client.DoDemoRequest();
2018-10-25 17:03:25 +00:00
llarp_ev_loop_run_single_process(loop, threadpool, logic);
return 0;
}
else
{
llarp::LogError("Failed to serve: ", strerror(errno));
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
return 0;
}