2018-10-24 18:02:42 +00:00
|
|
|
#include <libabyss.hpp>
|
|
|
|
#include <llarp/net.hpp>
|
2018-10-25 17:31:09 +00:00
|
|
|
#ifndef _WIN32
|
2018-10-24 18:02:42 +00:00
|
|
|
#include <sys/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
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-11-07 15:30:22 +00:00
|
|
|
HandleJSONRPC(Method_t method, __attribute__((unused)) const Params& params,
|
|
|
|
Response& resp) override
|
2018-10-24 18:02:42 +00:00
|
|
|
{
|
2018-10-25 17:03:25 +00:00
|
|
|
llarp::LogInfo("method: ", method);
|
2018-10-25 17:19:53 +00:00
|
|
|
resp.AddMember("result", abyss::json::Value().SetInt(1),
|
2018-10-25 17:03:25 +00:00
|
|
|
resp.GetAllocator());
|
2018-10-24 18:02:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-01 12:47:14 +00:00
|
|
|
struct DemoCall : public abyss::http::IRPCClientHandler
|
2018-10-24 18:02:42 +00:00
|
|
|
{
|
2018-11-01 12:47:14 +00:00
|
|
|
DemoCall(abyss::http::ConnImpl* impl) : abyss::http::IRPCClientHandler(impl)
|
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
|
|
|
}
|
|
|
|
|
2018-11-01 12:47:14 +00:00
|
|
|
bool
|
2018-11-21 17:46:33 +00:00
|
|
|
HandleResponse(abyss::http::RPC_Response resp) override
|
2018-11-01 12:47:14 +00:00
|
|
|
{
|
|
|
|
std::string body;
|
|
|
|
abyss::json::ToString(resp, body);
|
|
|
|
llarp::LogInfo("got response body: ", body);
|
|
|
|
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
|
|
|
|
{
|
|
|
|
abyss::http::IRPCClientHandler*
|
|
|
|
NewConn(abyss::http::ConnImpl* impl)
|
|
|
|
{
|
|
|
|
return new DemoCall(impl);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DoDemoRequest()
|
|
|
|
{
|
|
|
|
abyss::json::Value params;
|
|
|
|
params.SetObject();
|
|
|
|
QueueRPC("test", std::move(params),
|
|
|
|
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
|
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();
|
|
|
|
llarp_ev_loop* loop = nullptr;
|
|
|
|
llarp_ev_loop_alloc(&loop);
|
|
|
|
llarp_logic* logic = llarp_init_single_process_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;
|
2018-11-01 12:47:14 +00:00
|
|
|
DemoClient client;
|
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;
|
|
|
|
}
|