core-legacy/controllers/api_v1_devices_discover.cc
Tobias Reisinger 2d24339421 add: helpers
unstable: discovering
2019-07-15 00:39:37 +02:00

34 lines
965 B
C++

#include <netdb.h>
#include <unistd.h>
#include <config.h>
#include <helpers.h>
#include "api_v1_devices.h"
using namespace api::v1;
void devices::post_discover(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
{
LOG_DEBUG << "Discovering";
auto resp = HttpResponse::newHttpResponse();
int discovery_server_socket = helpers::bind_tcp_server("0.0.0.0", "0", config::discover_max_client_backlog);
int discover_server_port = helpers::get_server_port(discovery_server_socket);
if(discover_server_port == -1)
{
resp->setStatusCode(k500InternalServerError);
callback(resp);
return;
}
Json::Value payload;
payload["port"] = discover_server_port;
Json::StreamWriterBuilder wbuilder;
helpers::send_udp_broadcast("255.255.255.255", config::discover_port_dev, Json::writeString(wbuilder, payload).c_str());
close(discovery_server_socket);
callback(resp);
}