core-legacy/controllers/api_v1_devices_discover.cc

35 lines
965 B
C++
Raw Normal View History

2019-07-14 22:39:37 +00:00
#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);
}