Skip to content

Commit

Permalink
Merge pull request #76 from algosup/dev
Browse files Browse the repository at this point in the history
Second stable version
  • Loading branch information
EnzoGuillouche authored Feb 6, 2025
2 parents a47ec27 + ea48fc3 commit f14a7dd
Show file tree
Hide file tree
Showing 20 changed files with 923 additions and 39 deletions.
2 changes: 2 additions & 0 deletions Src/Client/Display/display.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ void creditsMenu(){
<< "| -> the Project Manager: |\n"
<< "| Evan UHRING |\n"
<< "+-------------------------------------------------------------------------+\n";

cout << endl << "Algorithm's worst-case complexity: O(N^2)." << endl;

handleException(server_is_online, page);
cout << "0| Go to main menu? -> ";
Expand Down
2 changes: 2 additions & 0 deletions Src/Client/startClient.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
cd "$(dirname "$0")"
clear
g++ -std=c++17 main.cpp ../Libraries/Tinyxml2/tinyxml2.cpp -o Bin/pathQuick
# Compile for unit tests
g++ -std=c++17 main.cpp ../Libraries/Tinyxml2/tinyxml2.cpp -o ../UnitTesting/Bin/pathQuick
./Bin/pathQuick
136 changes: 113 additions & 23 deletions Src/Server/Algorithm/loadData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ namespace fs = std::filesystem;
inline int min_id = 1, max_id = 0; // Track minimum and maximum node IDs encountered.
inline ifstream file; // File stream for reading CSV data.
inline bool ended = false; // Flag indicating completion of data loading.
inline bool datasetLoaded = false; // Flag indicating completion of graph construction.
inline const int INF = numeric_limits<int>::max();
inline vector<string> found_paths = {};

// -----------------------------------------------------------------------------
// Edge Structure
Expand All @@ -28,7 +30,6 @@ struct Edge {
// Graph Data Structures
// -----------------------------------------------------------------------------
// The graph is stored in two maps; landmarks will be computed dynamically.
inline unordered_map<int, vector<Edge>> graph;
inline unordered_map<int, vector<Edge>> data_graph;
// Replace the unordered_map version of landmark distances with a vector-of-vectors.
// For each landmark (in the same order as in the 'landmarks' vector), the inner vector
Expand Down Expand Up @@ -62,6 +63,42 @@ inline void loadingCat(string s);
// Function Definitions
// -----------------------------------------------------------------------------

/*
Checks if a given string represents a valid integer
This functions returns true if the string represents an integer, false otherwise
Parameters:
- input, the string to check
*/
bool isInteger(const string &input) {
int number;
size_t i = 0;

// Ensure all characters are digits (does not handle negative numbers correctly)
while (i < input.size()) {
if (!isdigit(input[i])) return false;
i++;
}

// Try converting string to integer
try {
number = stoi(input);
} catch (const out_of_range &) {
return false; // Handle cases where the number is too large
}

return true; // String is a valid integer
}

// Function to clear the console screen based on OS
void clearScreen(){
#if defined _WIN32
system("cls"); // Windows
#elif defined (__LINUX__) || defined(__gnu_linux__) || defined(__linux__) || defined (__APPLE__)
system("clear"); // Linux/macOS
#endif
}

inline void getData(const string& file_name) {
if (!file.is_open()) {
file.open(file_name);
Expand All @@ -78,6 +115,9 @@ inline void getData(const string& file_name) {
data_graph[from].push_back({to, time});
data_graph[to].push_back({from, time});
}
datasetLoaded = true;
computeDynamicLandmarks(8);
computeLandmarkDistances();
ended = true;
}

Expand Down Expand Up @@ -219,6 +259,65 @@ inline void verifyData(const string& file_name) {
cout << "No duplicate connections found." << endl;
cout << endl << endl;
}
/**
* @brief getCsvFile returns the name of the chosen .csv file to analyze.
* This function checks all possible .csv file in the Src directory, and let the user choose which one he wants
*/
string getCsvFile(){
string file;
string path = "../../Src";
string ext(".csv");
bool file_found = false;

// First, check the current folder
for (const auto& p : fs::recursive_directory_iterator(path)) {
if (p.is_regular_file() && p.path().extension() == ext) {
found_paths.push_back(p.path().string());
file_found = true;
}
}
// If no csv found, return false
if (!file_found) {
return "File not Found";
} else if (found_paths.size() > 1) {
bool found = false;
while (found == false){
clearScreen();
cout << "Files found:" << endl;
int i = 1;
for (const string& path : found_paths) {
cout << i << ". " << path << endl;
i++;
}

cout << endl << "Choose the .csv file: ";
i = 1;
string input;

getline(cin, input); // Read input from the user


if (input != "" && isInteger(input)){
for (const string& path : found_paths) {
if (stoi(input) == i) {
clearScreen();
file = path;
cout << "\nOpening " << path << endl << endl;
found = true;
break;
}
i++;
}
}
}
} else {
clearScreen();
file = found_paths[0];
cout << "Opening " << file << endl << endl;
}

return file;
}

/*
* loadDataset:
Expand All @@ -228,38 +327,23 @@ inline void verifyData(const string& file_name) {
*/
inline bool loadDataset() {
auto start = high_resolution_clock::now();
string file_name;
string path = "../../Src";
string ext(".csv");
bool file_found = false;
for (auto& p : fs::recursive_directory_iterator(path)) {
if (p.path().extension() == ext) {
cout << "Opening " << p.path().string() << "\n" << endl;
file_name = p.path().string();
file_found = true;
break;
}
}
string file_name = getCsvFile();

if (!file_found)
{
return false; // exit if the .csv file is not found
if (file_name == "File not Found"){
return false;
}


thread getDataThread(getData, file_name);
thread loadingCatThread(loadingCat, "Loading the file");
getDataThread.join();
loadingCatThread.join();

file.close();
computeDynamicLandmarks(8);
computeLandmarkDistances();
auto stop = high_resolution_clock::now();
auto duration = duration_cast<seconds>(stop - start);
cout << "\nLoaded in " << duration.count() << " seconds!\n"
<< "Available landmarks from " << min_id << " to " << max_id << "\n" << endl;

graph = data_graph;
cout << "Graph loaded successfully." << endl;
return true;
}
Expand All @@ -283,16 +367,22 @@ inline bool initServer() {
// -----------------------------------------------------------------------------
// Loading Cat Animation (Moved to the Bottom)
// -----------------------------------------------------------------------------
inline void loadingCat(string s) {
for (size_t i = 0; i < s.length(); i++) {
inline void loadingCat(string s)
{
for (int i = 0; i < s.length(); i++)
{
cout << " ";
}

cout << " /\\_/\\\n" << flush;
while (!ended) {
while(!ended){
cout << s << ".. / o.o \\" << "\r" << flush;
wait_ms(1000);
cout << s << ".. / -.- \\" << "\r" << flush;
wait_ms(150);
if (datasetLoaded && s != "Setting the data"){
s = "Setting the data";
}
}
cout << "Dataset loaded! / ^.^ \\" << "\r" << flush;
}
Expand Down
15 changes: 1 addition & 14 deletions Src/Server/Api/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,6 @@
bool sleeping = false;
void sleepingAnimation();

// Checks if a string represents a valid integer
bool isInteger(const std::string &input) {
for (char c : input) {
if (!std::isdigit(c)) return false;
}
try {
std::stoi(input);
} catch (const std::out_of_range &) {
return false;
}
return true;
}

// Close a socket using the appropriate platform-specific call
void Api::closeSocket(int socket) {
#ifdef _WIN32
Expand Down Expand Up @@ -188,7 +175,7 @@ std::string Api::processRequest(const std::string &request) {
int end = std::stoi(destination);
int path_time;
sleeping = false;
std::vector<int> path = bidirectionalDijkstra(graph, start, end, &path_time);
std::vector<int> path = bidirectionalDijkstra(data_graph, start, end, &path_time);

// Generate JSON or XML response based on the format
if (format == "json") {
Expand Down
3 changes: 2 additions & 1 deletion Src/Server/Api/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ void enableInput() {
}

int main() {
disableInput();
const int PORT = 8080;

// Initialize the server (loads dataset and builds the graph).
if (!initServer()) {
cout << "No .csv file found. Server shutting down." << "\r" << flush << endl;
return 0;
}

disableInput();

Api apiServer(PORT);
apiServer.start();
Expand Down
1 change: 0 additions & 1 deletion Src/Server/DataValidation/validateCsv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ bool processFile(const string& file_name, unordered_map<int, vector<int>>& graph

}

cout << endl;
file.close();
return true;
}
Expand Down
2 changes: 2 additions & 0 deletions Src/Server/startServer.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
cd "$(dirname "$0")"
g++ -std=gnu++20 -O3 -o Bin/server Api/server.cpp ../Libraries/Tinyxml2/tinyxml2.cpp
# Compile for unit tests
g++ -std=gnu++20 -O3 -o ../UnitTesting/Bin/server Api/server.cpp ../Libraries/Tinyxml2/tinyxml2.cpp
clear
./Bin/server
3 changes: 3 additions & 0 deletions Src/UnitTesting/startUnitTests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
g++ -std=gnu++20 -O3 -o Bin/unitTesting unitTesting.cpp
clear
./Bin/unitTesting
43 changes: 43 additions & 0 deletions Src/unitTesting/Bin/pathQuick.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"path": [
123,
82031,
100,
99,
120,
121,
122,
82045,
82044,
82043,
82037,
82035,
82036,
82033,
82163,
82164,
82135,
82134,
82136,
4386,
93,
83,
56,
189,
186,
187,
283,
143,
132,
128,
129,
280,
127,
126,
242,
319,
320,
321
],
"time": 296801
}
Binary file added Src/unitTesting/Bin/server.exe
Binary file not shown.
Binary file added Src/unitTesting/Bin/unitTesting.exe
Binary file not shown.
20 changes: 20 additions & 0 deletions Src/unitTesting/csv/correct.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
1,2,18
1,3,6
1,5,15
1,7,13
1,9,19
2,3,9
2,4,7
2,8,5
3,4,17
3,6,2
3,8,11
4,5,10
4,6,3
4,10,16
5,6,14
5,8,4
6,7,12
7,8,8
7,9,20
8,9,1
Loading

0 comments on commit f14a7dd

Please sign in to comment.