lokinet/libabyss/main.cpp

155 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,
__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;
llarp::Logic* m_Logic;
DemoCall(abyss::http::ConnImpl* impl, llarp::Logic* logic,
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
static void
CallCallback(void* u)
{
static_cast< DemoCall* >(u)->m_Callback();
}
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_job({this, &CallCallback});
2018-11-01 12:47:14 +00:00
return true;
}
void
PopulateReqHeaders(__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-02-26 15:09:37 +00:00
llarp::Logic* m_Logic;
2019-04-08 12:01:52 +00:00
DemoClient(llarp_ev_loop_ptr l, llarp::Logic* logic)
: 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-04-08 12:01:52 +00:00
return new DemoCall(impl, m_Logic,
std::bind(&llarp_ev_loop_stop, m_Loop.get()));
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();
};
};
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(__attribute__((unused)) int argc, __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();
2019-04-08 12:01:52 +00:00
llarp_ev_loop_ptr loop = llarp_make_ev_loop();
llarp::Logic* logic = new 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;
}