2018-10-24 18:02:42 +00:00
|
|
|
#include <libabyss.hpp>
|
2019-01-11 01:59:44 +00:00
|
|
|
#include <net/net.hpp>
|
2018-12-12 02:52:51 +00:00
|
|
|
|
2019-03-03 15:01:05 +00:00
|
|
|
#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-10-24 18:02:42 +00:00
|
|
|
|
2018-11-01 12:47:14 +00:00
|
|
|
struct DemoHandler : public abyss::httpd::IRPCHandler
|
2018-10-24 18:02:42 +00:00
|
|
|
{
|
2018-11-01 12:47:14 +00:00
|
|
|
DemoHandler(abyss::httpd::ConnImpl* impl) : abyss::httpd::IRPCHandler(impl)
|
2018-10-24 18:02:42 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-03-02 02:27:38 +00:00
|
|
|
absl::optional< Response >
|
|
|
|
HandleJSONRPC(Method_t method,
|
2019-04-19 18:24:33 +00:00
|
|
|
ABSL_ATTRIBUTE_UNUSED const Params& params) override
|
2018-10-24 18:02:42 +00:00
|
|
|
{
|
2018-10-25 17:03:25 +00:00
|
|
|
llarp::LogInfo("method: ", method);
|
2019-03-02 02:27:38 +00:00
|
|
|
return Response::object();
|
2018-10-24 18:02:42 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-01 12:47:14 +00:00
|
|
|
struct DemoCall : public abyss::http::IRPCClientHandler
|
2018-10-24 18:02:42 +00:00
|
|
|
{
|
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-10-24 18:02:42 +00:00
|
|
|
{
|
2018-11-01 12:47:14 +00:00
|
|
|
llarp::LogInfo("new call");
|
2018-10-24 18:02:42 +00:00
|
|
|
}
|
|
|
|
|
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");
|
2019-09-12 18:19:25 +00:00
|
|
|
m_Logic->queue_func([=]() { m_Callback(); });
|
2018-11-01 12:47:14 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-04-24 23:27:31 +00:00
|
|
|
PopulateReqHeaders(ABSL_ATTRIBUTE_UNUSED abyss::http::Headers_t& hdr) override
|
2018-11-01 12:47:14 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-11-07 15:30:22 +00:00
|
|
|
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()
|
|
|
|
{
|
2019-03-02 02:27:38 +00:00
|
|
|
QueueRPC("test", nlohmann::json::object(),
|
2018-11-01 12:47:14 +00:00
|
|
|
std::bind(&DemoClient::NewConn, this, std::placeholders::_1));
|
|
|
|
Flush();
|
2019-04-24 23:27:31 +00:00
|
|
|
}
|
2018-11-01 12:47:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct DemoServer : public abyss::httpd::BaseReqHandler
|
|
|
|
{
|
|
|
|
DemoServer() : abyss::httpd::BaseReqHandler(1000)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
abyss::httpd::IRPCHandler*
|
2018-11-02 18:02:45 +00:00
|
|
|
CreateHandler(abyss::httpd::ConnImpl* impl)
|
2018-10-24 18:02:42 +00:00
|
|
|
{
|
|
|
|
return new DemoHandler(impl);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
int
|
2019-04-19 18:24:33 +00:00
|
|
|
main(ABSL_ATTRIBUTE_UNUSED int argc, ABSL_ATTRIBUTE_UNUSED char* argv[])
|
2018-10-24 18:02:42 +00:00
|
|
|
{
|
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
|
2018-10-24 18:02:42 +00:00
|
|
|
signal(SIGPIPE, SIG_IGN);
|
2018-10-25 22:16:03 +00:00
|
|
|
#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
|
2019-03-03 15:01:05 +00:00
|
|
|
|
|
|
|
#ifdef LOKINET_DEBUG
|
|
|
|
absl::SetMutexDeadlockDetectionMode(absl::OnDeadlockCycle::kAbort);
|
|
|
|
#endif
|
2018-11-01 12:47:14 +00:00
|
|
|
llarp::SetLogLevel(llarp::eLogDebug);
|
2019-06-18 04:18:53 +00:00
|
|
|
// 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();
|
2019-07-09 13:47:24 +00:00
|
|
|
auto logic = std::make_shared< llarp::Logic >();
|
2018-10-24 18:02:42 +00:00
|
|
|
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);
|
2018-10-24 18:02:42 +00:00
|
|
|
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();
|
2019-07-09 13:47:24 +00:00
|
|
|
llarp_ev_loop_run_single_process(loop, logic);
|
2018-10-25 17:03:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
llarp::LogError("Failed to serve: ", strerror(errno));
|
|
|
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
|
|
|
}
|
|
|
|
}
|
2018-10-24 18:02:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|