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,
|
|
|
|
__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;
|
|
|
|
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-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
|
|
|
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
|
2018-11-07 15:30:22 +00:00
|
|
|
PopulateReqHeaders(__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-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()
|
|
|
|
{
|
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();
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
2018-11-07 15:30:22 +00:00
|
|
|
main(__attribute__((unused)) int argc, __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);
|
2018-10-24 18:02:42 +00:00
|
|
|
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);
|
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();
|
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));
|
|
|
|
}
|
|
|
|
}
|
2018-10-24 18:02:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|