fix: save id as blob, not as string

add: some progress to new discovery
add: some tests
remove: migrations. restarting for now
This commit is contained in:
Tobias Reisinger 2020-02-23 20:06:14 +01:00
parent b4eec336a2
commit 51ab1d7982
22 changed files with 345 additions and 121 deletions

View file

@ -23,9 +23,17 @@ controllers::get_all(const HttpRequestPtr &req, std::function<void(const HttpRes
void
controllers::get_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback,
const std::string& controller_id)
const std::string& controller_id_str)
{
controller_dbo **controllers = controller_dbo::get_by_simple("id", controller_id.c_str(), (intptr_t) &sqlite3_bind_text);
uuid_t controller_id;
if(uuid_parse(controller_id_str.c_str(), controller_id))
{
auto resp = HttpResponse::newHttpResponse();
resp->setStatusCode(k400BadRequest);
callback(resp);
return;
}
controller_dbo **controllers = controller_dbo::get_by_simple("id", controller_id, (intptr_t) &sqlite3_bind_blob, sizeof(uuid_t));
if(controllers[0])
{
@ -47,7 +55,7 @@ void
controllers::get_one_by_tag(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback,
const std::string &controller_tag)
{
controller_dbo **controllers = controller_dbo::get_by_simple("tag", controller_tag.c_str(), (intptr_t) &sqlite3_bind_text);
controller_dbo **controllers = controller_dbo::get_by_simple("tag", controller_tag.c_str(), (intptr_t) &sqlite3_bind_text, -1);
if(controllers[0])
{
@ -66,9 +74,17 @@ controllers::get_one_by_tag(const HttpRequestPtr &req, std::function<void(const
}
void
controllers::delete_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, const std::string& controller_id)
controllers::delete_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, const std::string& controller_id_str)
{
controller_dbo **controllers = controller_dbo::get_by_simple("id", controller_id.c_str(), (intptr_t) &sqlite3_bind_text);
uuid_t controller_id;
if(uuid_parse(controller_id_str.c_str(), controller_id))
{
auto resp = HttpResponse::newHttpResponse();
resp->setStatusCode(k400BadRequest);
callback(resp);
return;
}
controller_dbo **controllers = controller_dbo::get_by_simple("id", controller_id, (intptr_t) &sqlite3_bind_blob, sizeof(uuid_t));
if(controllers[0])
{
@ -92,11 +108,19 @@ controllers::delete_one_by_id(const HttpRequestPtr &req, std::function<void(cons
void
controllers::put_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback,
const std::string &controller_id)
const std::string &controller_id_str)
{
Json::Value body = *req->getJsonObject();
controller_dbo **controllers = controller_dbo::get_by_simple("id", controller_id.c_str(), (intptr_t) &sqlite3_bind_text);
uuid_t controller_id;
if(uuid_parse(controller_id_str.c_str(), controller_id))
{
auto resp = HttpResponse::newHttpResponse();
resp->setStatusCode(k400BadRequest);
callback(resp);
return;
}
controller_dbo **controllers = controller_dbo::get_by_simple("id", controller_id, (intptr_t) &sqlite3_bind_blob, sizeof(uuid_t));
if(controllers[0])
{
@ -127,4 +151,4 @@ controllers::put_one_by_id(const HttpRequestPtr &req, std::function<void(const H
callback(resp);
}
controller_dbo::free_list(controllers);
}
}

View file

@ -21,13 +21,13 @@ namespace api::v1
static void post_discover(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback);
static void get_all(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback);
static void get_one_by_id(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& controller_id);
static void get_one_by_id(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& controller_id_str);
static void get_one_by_tag(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& controller_tag);
static void delete_one_by_id(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& controller_id);
static void delete_one_by_id(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& controller_id_str);
static void put_one_by_id(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& controller_id);
static void get_relays_all(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& controller_id);
static void get_relays_one_by_id_and_num(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& controller_id, int relay_num);
static void put_relays_one_by_id_and_num(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& controller_id, int relay_num);
static void get_relays_all(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& controller_id_str);
static void get_relays_one_by_id_and_num(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& controller_id_str, int relay_num);
static void put_relays_one_by_id_and_num(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& controller_id_str, int relay_num);
};
}

View file

@ -11,6 +11,14 @@
using namespace api::v1;
enum DISCOVERY_MAPPING
{
DISCOVERY_MAPPING_ID = 0,
DISCOVERY_MAPPING_NAME = 1,
DISCOVERY_MAPPING_COMMAND_PORT = 2,
DISCOVERY_MAPPING_RELAY_COUNT = 3,
};
void controllers::post_discover(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
{
@ -95,12 +103,13 @@ void controllers::post_discover(const HttpRequestPtr &req, std::function<void(co
continue;
}
uuid_t uuid;
int uuid_size = sizeof(uuid_t);
memcpy(&uuid, binn_object_blob(answer_payload, "uuid", &uuid_size), uuid_size);
uuid_t discovered_id;
int id_size = sizeof(uuid_t);
memcpy(&discovered_id, binn_map_blob(answer_payload, DISCOVERY_MAPPING_ID, &id_size), id_size);
//strncpy(&id, binn_map_blob(answer_payload, DISCOVERY_MAPPING_ID, &uuid_size), uuid_size);
char uuid_str[38];
uuid_unparse(uuid, uuid_str);
uuid_unparse(discovered_id, uuid_str);
LOG_DEBUG << uuid_str;
continue;
@ -108,15 +117,13 @@ void controllers::post_discover(const HttpRequestPtr &req, std::function<void(co
Json::Value client_info;
const char *discovered_id = client_info["id"].asCString();
bool found_discovered_in_list = false;
for(int i = 0; known_controllers[i] != nullptr; i++)
{
if(!found_discovered_in_list)
{
if(strcmp(known_controllers[i]->id, discovered_id) == 0)
if(uuid_compare(known_controllers[i]->id, discovered_id) == 0)
{
known_controllers[i]->active = true;
known_controllers[i]->update();
@ -135,7 +142,7 @@ void controllers::post_discover(const HttpRequestPtr &req, std::function<void(co
{
controller_dbo discovered_controller{};
strcpy(discovered_controller.ip, inet_ntoa(addr.sin_addr));
strcpy(discovered_controller.id, discovered_id);
memcpy(discovered_controller.id, discovered_id, sizeof(uuid_t));
strcpy(discovered_controller.name, client_info["name"].asCString());
discovered_controller.relay_count = client_info["relay_count"].asInt();
discovered_controller.port = client_info["port"].asInt();

View file

@ -10,9 +10,17 @@ using namespace api::v1;
void
controllers::get_relays_all(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback,
const std::string& controller_id)
const std::string& controller_id_str)
{
relay_dbo **all_controller_relays = relay_dbo::get_by_simple("controller_id", (void *) controller_id.c_str(), (intptr_t) sqlite3_bind_text);
uuid_t controller_id;
if(uuid_parse(controller_id_str.c_str(), controller_id))
{
auto resp = HttpResponse::newHttpResponse();
resp->setStatusCode(k400BadRequest);
callback(resp);
return;
}
relay_dbo **all_controller_relays = relay_dbo::get_by_simple("controller_id", (void *) controller_id, (intptr_t) sqlite3_bind_blob, sizeof(uuid_t));
Json::Value all_relays_json(Json::arrayValue);
for(int i = 0; all_controller_relays[i] != nullptr; i++)
@ -29,10 +37,18 @@ controllers::get_relays_all(const HttpRequestPtr &req, std::function<void(const
void
controllers::get_relays_one_by_id_and_num(const HttpRequestPtr &req,
std::function<void(const HttpResponsePtr &)> &&callback, const std::string& controller_id,
std::function<void(const HttpResponsePtr &)> &&callback, const std::string& controller_id_str,
int relay_num)
{
relay_dbo *relay = relay_dbo::get_relay_for_controller(controller_id.c_str(), relay_num);
uuid_t controller_id;
if(uuid_parse(controller_id_str.c_str(), controller_id))
{
auto resp = HttpResponse::newHttpResponse();
resp->setStatusCode(k400BadRequest);
callback(resp);
return;
}
relay_dbo *relay = relay_dbo::get_relay_for_controller(controller_id, relay_num);
if(relay)
{
@ -53,10 +69,18 @@ controllers::get_relays_one_by_id_and_num(const HttpRequestPtr &req,
void
controllers::put_relays_one_by_id_and_num(const HttpRequestPtr &req,
std::function<void(const HttpResponsePtr &)> &&callback, const std::string& controller_id,
std::function<void(const HttpResponsePtr &)> &&callback, const std::string& controller_id_str,
int relay_num)
{
if(!relay_dbo::valid_num_for_controller(controller_id.c_str(), relay_num))
uuid_t controller_id;
if(uuid_parse(controller_id_str.c_str(), controller_id))
{
auto resp = HttpResponse::newHttpResponse();
resp->setStatusCode(k400BadRequest);
callback(resp);
return;
}
if(!relay_dbo::valid_num_for_controller(controller_id, relay_num))
{
auto resp = HttpResponse::newHttpResponse();
resp->setStatusCode(k400BadRequest);
@ -64,15 +88,24 @@ controllers::put_relays_one_by_id_and_num(const HttpRequestPtr &req,
return;
}
relay_dbo *relay = relay_dbo::get_relay_for_controller(controller_id.c_str(), relay_num);
Json::Value body = *req->getJsonObject();
uuid_t active_schedule_id;
if(!schedule_dbo::parse_id(body["active_schedule"].asCString(), active_schedule_id))
{
auto resp = HttpResponse::newHttpResponse();
resp->setStatusCode(k400BadRequest);
callback(resp);
return;
}
relay_dbo *relay = relay_dbo::get_relay_for_controller(controller_id, relay_num);
bool db_action_result;
if(relay)
{
strncpy(relay->name, body["name"].asCString(), 127);
strncpy(relay->active_schedule_id, body["active_schedule"].asCString(), 32);
uuid_copy(relay->active_schedule_id, active_schedule_id);
db_action_result = relay->update();
}
@ -81,8 +114,9 @@ controllers::put_relays_one_by_id_and_num(const HttpRequestPtr &req,
relay = new relay_dbo();
relay->number = relay_num;
strncpy(relay->name, body["name"].asCString(), 127);
strncpy(relay->active_schedule_id, body["active_schedule"].asCString(), 32);
strncpy(relay->controller_id, controller_id.c_str(), 32);
uuid_copy(relay->active_schedule_id, active_schedule_id);
uuid_copy(relay->controller_id, controller_id);
relay->reload_active_schedule();
@ -97,8 +131,8 @@ controllers::put_relays_one_by_id_and_num(const HttpRequestPtr &req,
}
else
{
auto schedules = schedule_dbo::get_by_simple("id", body["active_schedule"].asCString(), (intptr_t)&sqlite3_bind_text);
auto controllers = controller_dbo::get_by_simple("id", controller_id.c_str(), (intptr_t)&sqlite3_bind_text);
auto schedules = schedule_dbo::get_by_simple("id", active_schedule_id, (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t));
auto controllers = controller_dbo::get_by_simple("id", controller_id, (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t));
Json::Value payload;
payload["target"] = relay_num;
@ -116,4 +150,4 @@ controllers::put_relays_one_by_id_and_num(const HttpRequestPtr &req,
}
delete relay;
}
}

View file

@ -26,7 +26,7 @@ void
relays::get_one_by_tag(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback,
const std::string &relay_tag)
{
relay_dbo **relays = relay_dbo::get_by_simple("tag", relay_tag.c_str(), (intptr_t)&sqlite3_bind_text);
relay_dbo **relays = relay_dbo::get_by_simple("tag", relay_tag.c_str(), (intptr_t)&sqlite3_bind_text, -1);
if(relays[0])
{
@ -42,4 +42,4 @@ relays::get_one_by_tag(const HttpRequestPtr &req, std::function<void(const HttpR
callback(resp);
}
relay_dbo::free_list(relays);
}
}

View file

@ -25,9 +25,18 @@ schedules::get_all(const HttpRequestPtr &req, std::function<void(const HttpRespo
}
void
schedules::get_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, const std::string& schedule_id)
schedules::get_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, const std::string& schedule_id_str)
{
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", schedule_id.c_str(), (intptr_t) &sqlite3_bind_text);
uuid_t schedule_id;
if(schedule_dbo::parse_id(schedule_id_str.c_str(), schedule_id))
{
auto resp = HttpResponse::newHttpResponse();
resp->setStatusCode(k400BadRequest);
callback(resp);
return;
}
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", schedule_id, (intptr_t) &sqlite3_bind_blob, sizeof(uuid_t));
if(schedules[0])
{
@ -46,9 +55,9 @@ schedules::get_one_by_id(const HttpRequestPtr &req, std::function<void(const Htt
}
void
schedules::delete_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, const std::string& schedule_id)
schedules::delete_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, const std::string& schedule_id_str)
{
if(strcmp(schedule_id.c_str(), "off") == 0 || strcmp(schedule_id.c_str(), "on") == 0)
if(strcmp(schedule_id_str.c_str(), "off") == 0 || strcmp(schedule_id_str.c_str(), "on") == 0)
{
auto resp = HttpResponse::newHttpResponse();
resp->setStatusCode(k403Forbidden);
@ -57,7 +66,15 @@ schedules::delete_one_by_id(const HttpRequestPtr &req, std::function<void(const
return;
}
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", schedule_id.c_str(), (intptr_t) &sqlite3_bind_text);
uuid_t schedule_id;
if(schedule_dbo::parse_id(schedule_id_str.c_str(), schedule_id))
{
auto resp = HttpResponse::newHttpResponse();
resp->setStatusCode(k400BadRequest);
callback(resp);
return;
}
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", schedule_id, (intptr_t) &sqlite3_bind_blob, sizeof(uuid_t));
if(schedules[0])
{
@ -88,7 +105,7 @@ schedules::post_new(const HttpRequestPtr &req, std::function<void(const HttpResp
strncpy(new_schedule.name, body["name"].asCString(), 127);
new_schedule.name[127] = '\0';
strncpy(new_schedule.id, drogon::utils::getUuid().c_str(), 32);
uuid_generate(new_schedule.id);
new_schedule.id[32] = '\0';
new_schedule.periods = helpers::parse_periods(body["periods"]);
@ -107,9 +124,9 @@ schedules::post_new(const HttpRequestPtr &req, std::function<void(const HttpResp
void
schedules::put_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback,
const std::string &schedule_id)
const std::string &schedule_id_str)
{
if(strcmp(schedule_id.c_str(), "off") == 0 || strcmp(schedule_id.c_str(), "on") == 0)
if(strcmp(schedule_id_str.c_str(), "off") == 0 || strcmp(schedule_id_str.c_str(), "on") == 0)
{
auto resp = HttpResponse::newHttpResponse();
resp->setStatusCode(k403Forbidden);
@ -118,9 +135,17 @@ schedules::put_one_by_id(const HttpRequestPtr &req, std::function<void(const Htt
return;
}
Json::Value body = *req->jsonObject();
uuid_t schedule_id;
if(schedule_dbo::parse_id(schedule_id_str.c_str(), schedule_id))
{
auto resp = HttpResponse::newHttpResponse();
resp->setStatusCode(k400BadRequest);
callback(resp);
return;
}
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", schedule_id, (intptr_t) &sqlite3_bind_blob, sizeof(uuid_t));
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", schedule_id.c_str(), (intptr_t) &sqlite3_bind_text);
Json::Value body = *req->jsonObject();
if(schedules[0])
{
@ -149,4 +174,4 @@ schedules::put_one_by_id(const HttpRequestPtr &req, std::function<void(const Htt
callback(resp);
}
schedule_dbo::free_list(schedules);
}
}