lokinet/libabyss/main.cpp

134 lines
3.0 KiB
C++
Raw Normal View History

#include <libabyss.hpp>
2019-01-11 01:59:44 +00:00
#include <net/net.hpp>
2018-10-25 17:31:09 +00:00
#ifndef _WIN32
#include <sys/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)
{
}
bool
HandleJSONRPC(Method_t method, __attribute__((unused)) const Params& params,
Response& resp) override
{
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());
return true;
}
};
2018-11-01 12:47:14 +00:00
struct DemoCall : public abyss::http::IRPCClientHandler
{
2018-11-01 12:47:14 +00:00
DemoCall(abyss::http::ConnImpl* impl) : abyss::http::IRPCClientHandler(impl)
{
2018-11-01 12:47:14 +00:00
llarp::LogInfo("new call");
}
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
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
{
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*
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
2018-11-01 12:47:14 +00:00
llarp::SetLogLevel(llarp::eLogDebug);
llarp_threadpool* threadpool = llarp_init_same_process_threadpool();
llarp_ev_loop* loop = nullptr;
llarp_ev_loop_alloc(&loop);
2018-12-10 14:14:55 +00:00
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;
2018-11-01 12:47:14 +00:00
DemoClient client;
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;
}