add: binn for communication
This commit is contained in:
parent
6e6d29f410
commit
b4eec336a2
6 changed files with 4331 additions and 30 deletions
controllers
|
@ -1,9 +1,12 @@
|
|||
#include <netdb.h>
|
||||
#include <unistd.h>
|
||||
#include <cmath>
|
||||
#include <uuid/uuid.h>
|
||||
|
||||
#include <config.h>
|
||||
#include <helpers.h>
|
||||
#include <cmath>
|
||||
#include <models/controller_dbo.h>
|
||||
#include <binn.h>
|
||||
#include "api_v1_controllers.h"
|
||||
|
||||
using namespace api::v1;
|
||||
|
@ -22,12 +25,10 @@ void controllers::post_discover(const HttpRequestPtr &req, std::function<void(co
|
|||
return;
|
||||
}
|
||||
|
||||
Json::Value payload;
|
||||
payload["port"] = discover_server_port;
|
||||
int16_t payload[1];
|
||||
payload[0] = discover_server_port;
|
||||
|
||||
Json::StreamWriterBuilder json_writer;
|
||||
|
||||
if(helpers::send_udp_broadcast("255.255.255.255", config::discover_port_dev, Json::writeString(json_writer, payload).c_str()) < 0)
|
||||
if(helpers::send_udp_broadcast("255.255.255.255", config::discover_port_dev, payload, sizeof(payload)) < 0)
|
||||
{
|
||||
auto resp = HttpResponse::newHttpResponse();
|
||||
resp->setStatusCode(k500InternalServerError);
|
||||
|
@ -42,7 +43,6 @@ void controllers::post_discover(const HttpRequestPtr &req, std::function<void(co
|
|||
struct timeval timeout{};
|
||||
|
||||
uint8_t discover_answer_buf[1];
|
||||
uint8_t discover_header_buf[1];
|
||||
|
||||
controller_dbo **known_controllers = controller_dbo::get_all();
|
||||
|
||||
|
@ -69,16 +69,18 @@ void controllers::post_discover(const HttpRequestPtr &req, std::function<void(co
|
|||
continue;
|
||||
}
|
||||
|
||||
if(recv(client_fd, discover_header_buf, 1, 0) < 0)
|
||||
size_t payload_length;
|
||||
|
||||
if(recv(client_fd, &payload_length, sizeof(payload_length), 0) <= 0)
|
||||
{
|
||||
LOG_ERROR << "Error Receiving header from client";
|
||||
continue;
|
||||
}
|
||||
|
||||
size_t payload_length = discover_header_buf[0];
|
||||
char *answer_payload = (char*)malloc((payload_length + 1) * sizeof(*answer_payload));
|
||||
void *answer_payload = malloc((payload_length + 1));
|
||||
ssize_t bytes_transferred;
|
||||
|
||||
if(recv(client_fd, answer_payload, payload_length, 0) < 0)
|
||||
if((bytes_transferred = recv(client_fd, answer_payload, payload_length, 0)) <= 0)
|
||||
{
|
||||
LOG_ERROR << "Error Receiving payload from client";
|
||||
continue;
|
||||
|
@ -93,26 +95,19 @@ void controllers::post_discover(const HttpRequestPtr &req, std::function<void(co
|
|||
continue;
|
||||
}
|
||||
|
||||
answer_payload[payload_length] = '\0';
|
||||
std::istringstream answer_payload_stream(answer_payload);
|
||||
uuid_t uuid;
|
||||
int uuid_size = sizeof(uuid_t);
|
||||
memcpy(&uuid, binn_object_blob(answer_payload, "uuid", &uuid_size), uuid_size);
|
||||
|
||||
Json::CharReaderBuilder json_reader;
|
||||
json_reader["strictRoot"] = true;
|
||||
|
||||
std::string errors;
|
||||
Json::Value client_info;
|
||||
|
||||
if (!Json::parseFromStream(json_reader, answer_payload_stream, &client_info, &errors))
|
||||
{
|
||||
LOG_ERROR << "Failed to parse response: " << errors;
|
||||
discover_answer_buf[0] = config::discover_code_reject;
|
||||
send(client_fd, discover_answer_buf, sizeof(uint8_t), 0);
|
||||
close(client_fd);
|
||||
continue;
|
||||
}
|
||||
char uuid_str[38];
|
||||
uuid_unparse(uuid, uuid_str);
|
||||
LOG_DEBUG << uuid_str;
|
||||
continue;
|
||||
|
||||
free(answer_payload);
|
||||
|
||||
Json::Value client_info;
|
||||
|
||||
const char *discovered_id = client_info["id"].asCString();
|
||||
|
||||
bool found_discovered_in_list = false;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue