From 5daef8d9ccfe91d4bb8ebc00bc47db8b3387bfb0 Mon Sep 17 00:00:00 2001 From: tostadanevera Date: Sun, 5 May 2024 16:39:52 +0200 Subject: [PATCH] Errors fixed --- src/c/base_DB.cpp | 48 +++++++++++++++++++++----------------- src/c/main_RouteServer.cpp | 2 +- src/h/base_DB.h | 9 ++++--- 3 files changed, 31 insertions(+), 28 deletions(-) diff --git a/src/c/base_DB.cpp b/src/c/base_DB.cpp index e792af0..10c11fc 100644 --- a/src/c/base_DB.cpp +++ b/src/c/base_DB.cpp @@ -15,15 +15,15 @@ void do_exit(PGconn *conn) { PGconn* DB_connection(const char* user, const char* password) { char conninfo[1024]; - // memset(conninfo, '\0', sizeof(conninfo)); + memset(conninfo, '\0', sizeof(conninfo)); snprintf(conninfo, sizeof(conninfo), "host=oroneta.core-system.postgres port=5432 dbname=core-system.postgres user=%s password=%s", user, password); PGconn *conn = PQconnectdb(conninfo); if (PQstatus(conn) != CONNECTION_OK) { - fprintf(stderr, "Connection to database failed: %s\n", PQerrorMessage(conn)); + fprintf(stderr, "Connection to database failed: %s.\n", PQerrorMessage(conn)); } else { - printf("Connection to database done successfully \n"); + printf("Connection to database done successfully.\n"); } return conn; @@ -40,17 +40,20 @@ bool checkDic(PGconn* conn, const char* dic) { result = PQexec(conn, query); if (PQresultStatus(result) != PGRES_TUPLES_OK) { - printf("Error executing query: %s\n", PQerrorMessage(conn)); + printf("Error executing query: %s.\n", PQerrorMessage(conn)); PQclear(result); } + bool isValid = false; if (strcmp(PQgetvalue(result, 0, 0), "t") == 0) { - printf("Existe\n"); - PQclear(result); - return true; - } - printf("No existe\n"); + printf("The drone exists in the database.\n"); + isValid = true; + } else { printf("The drone does not exist in the database.\n"); } + + PQclear(result); + return isValid; } + printf("Error: Connection failed.\n"); return false; } @@ -61,9 +64,10 @@ void insertRoute(PGconn* conn, const char* dic, const char* auth_code, const cha snprintf(query, sizeof(query), "INSERT INTO routes (dic, auth_code, flight) VALUES ('%s', '%s', '%s')", dic, auth_code, route); if (PQresultStatus(PQexec(conn, query)) != PGRES_COMMAND_OK) { - printf("Error executing query: %s\n", PQerrorMessage(conn)); + printf("Error executing query: %s.\n", PQerrorMessage(conn)); } } + printf("Error: Connection failed.\n"); } // check if dic and auth-code match @@ -77,23 +81,24 @@ bool checkAuthCode(PGconn* conn, const char* dic, const char* authCode) { result = PQexec(conn, query); if (PQresultStatus(result) != PGRES_TUPLES_OK) { - printf("Error executing query: %s\n", PQerrorMessage(conn)); + printf("Error executing query: %s.\n", PQerrorMessage(conn)); PQclear(result); } + bool isValid = false; if (strcmp(PQgetvalue(result, 0, 0), "t") == 0) { - printf("Coincide\n"); - PQclear(result); - return true; - } - printf("No coincide\n"); - + printf("The authorization code is valid.\n"); + isValid = true; + } else { printf("The authorization code is not valid.\n"); } + PQclear(result); + return isValid; } - return false; + printf("Error: Connection failed.\n"); + return false; } // check colissions route -int checkColissions(PGconn* conn, const char* dic, const char* route) { +int checkColissions(PGconn* conn, const char* route) { if (PQstatus(conn) == CONNECTION_OK) { char query[256]; PGresult* result; @@ -109,14 +114,13 @@ int checkColissions(PGconn* conn, const char* dic, const char* route) { result = PQexec(conn, query); if (PQresultStatus(result) != PGRES_TUPLES_OK) { - printf("Error executing query: %s\n", PQerrorMessage(conn)); + printf("Error executing query: %s.\n", PQerrorMessage(conn)); PQclear(result); } int count = atoi(PQgetvalue(result, 0, 0)); return count; } + printf("Error: Connection failed.\n"); return -1; - printf("Error: No se pudo establecer la conexión.\n"); - } \ No newline at end of file diff --git a/src/c/main_RouteServer.cpp b/src/c/main_RouteServer.cpp index 44fec7b..6cb0acc 100644 --- a/src/c/main_RouteServer.cpp +++ b/src/c/main_RouteServer.cpp @@ -80,7 +80,7 @@ int main(){ //check collisions - collisionsCount = checkColissions(conn, dic.c_str(), coordsString.c_str()); + collisionsCount = checkColissions(conn, coordsString.c_str()); if(collisionsCount > MAX_COLLISION) { crow::json::wvalue responseJson; diff --git a/src/h/base_DB.h b/src/h/base_DB.h index e3565e8..7d44955 100644 --- a/src/h/base_DB.h +++ b/src/h/base_DB.h @@ -11,7 +11,7 @@ * \param user : The username for the databse. * \param password : The password for the database. * - * \return A pointer to the connection object. + * \return A pointer to the connection. */ PGconn* DB_connection(const char* user, const char* password); @@ -34,7 +34,7 @@ bool checkDic(PGconn* conn, const char* dic); void insertRoute(PGconn* conn, const char* dic, const char* auth_code, const char* route); /*! - * \brief Cheks if the authorization code for a drone is valid. + * \brief Checks if the authorization code for a drone is valid. * \param dic : The drone identifier code. * \param authCode : The authentication code to check. * @@ -44,11 +44,10 @@ bool checkAuthCode(PGconn* conn, const char* dic, const char* authCode); /*! * \brief Checks for collisions on a given route. - * \param dic : The drone identifier code. * \param route : The route to check for collisions. The route should be a string with the format: "[[x0,y0],[x1,y1],...]". * - * \return The number of potential collisions on the route + * \return The number of potential collisions on the route. */ -int checkColissions(PGconn* conn, const char* dic, const char* route); +int checkColissions(PGconn* conn, const char* route); #endif \ No newline at end of file