diff --git a/prak3/src/server.cpp b/prak3/src/server.cpp index f0abe53afe660fa6dbc70b565c53224dfad7f97a..57251b37a77819c65964d56c7b6e1ebead149d83 100644 --- a/prak3/src/server.cpp +++ b/prak3/src/server.cpp @@ -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 -_____- !";