Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Errors fixed #12

Merged
merged 1 commit into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 26 additions & 22 deletions src/c/base_DB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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");

}
2 changes: 1 addition & 1 deletion src/c/main_RouteServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 4 additions & 5 deletions src/h/base_DB.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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.
*
Expand All @@ -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
Loading