Skip to content
Snippets Groups Projects
Commit ce34bce8 authored by Saif Eddine Askri's avatar Saif Eddine Askri
Browse files

server version 1.0.0

parent 69f56ae7
No related branches found
No related tags found
No related merge requests found
......@@ -5,7 +5,7 @@
#include <unistd.h>
#include <arpa/inet.h>
#include <mutex>
#include <map>
#define PORT 2525
......@@ -21,7 +21,7 @@ struct Request {
};
std::mutex mtx;
std::vector<Request*> DB;
std::map<std::string,std::string> DB;
......@@ -80,27 +80,24 @@ void handle_client(int client_socket) {
if(request->operation == STORE_OPERATION){
std::lock_guard<std::mutex> lock(mtx);
response = "Key " + request->key +" Not Found for [get]";
DB.push_back(request);
DB[request->key] = request->value;
response = "freue dich :)";
}else if (request->operation == GET_OPERATION){
std::lock_guard<std::mutex> lock(mtx);
for (Request* current : DB) {
if(current->key == request->key){
response = current->value;
break;
}
auto it = DB.find(request->key);
if (it != DB.end()) {
response = "The Value of the [" + request->key + "] is => " + it->second;
}else{
response = "Key [" + request->key +"] Not Found";
}
}else if (request->operation == DELETE_OPERATION){
std::lock_guard<std::mutex> lock(mtx);
response = "Key " + request->key +" Not Found for [delete]";
for(size_t i=0; i< DB.size();i++){
if(DB[i]->key == request->key){
delete DB[i];
DB.erase(DB.begin() + i);
response = "Key " + request->key +" is deleted";
break;
}
if(DB.erase(request->key) != 0){
response = "Item with Key [" + request->key +"] is deleted";
}else{
response = "Key " + request->key +" Not Found for [delete]";
}
}else{
response = "bad request -_____- !";
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment