command.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "command.h"
  2. #include <server.h>
  3. #include <exception>
  4. #include <unordered_map>
  5. #include <utility>
  6. using std::exception;
  7. command::command()
  8. {
  9. }
  10. command::~command()
  11. {
  12. }
  13. bool command::user_exists(string username)
  14. {
  15. auto it = server_::active_users->find(username);
  16. if (it == server_::active_users->end())
  17. return false;
  18. return true;
  19. }
  20. bool command::session_key_exists(unsigned long key)
  21. {
  22. auto it = server_::db->find(key);
  23. return it == server_::db->end() ? false : true;
  24. }
  25. void command::insert_new_user(string username, unsigned long key)
  26. {
  27. server_::active_users->insert({ username, key });
  28. server_::db->insert({ key, new vector<sensor_data>() });
  29. }
  30. void command::remove_user(string username, unsigned long key)
  31. {
  32. server_::active_users->erase(username);
  33. server_::db->erase(key);
  34. }
  35. // void command::cleanup_db()
  36. // {
  37. // unordered_map<unsigned long, vector<sensor_data> *> *db_cache =
  38. // new unordered_map<unsigned long, vector<sensor_data> *>();
  39. // for (auto it : *server_::active_users) {
  40. // db_cache[it.second] = server_::db[it.second];
  41. // server_::db->erase(it.second);
  42. // }
  43. // for (auto it : *server_::db) {
  44. // delete it.second;
  45. // }
  46. // delete server_::db;
  47. // server_::db = db_cache;
  48. // }
  49. unsigned long command::get_user_key(string username)
  50. {
  51. auto it = server_::active_users->find(username);
  52. if (it == server_::active_users->end())
  53. return 0;
  54. return it->second;
  55. }
  56. vector<sensor_data> *command::get_user_db(unsigned long key)
  57. {
  58. auto it = server_::db->find(key);
  59. if (it == server_::db->end())
  60. throw exception();
  61. return it->second;
  62. }
  63. response command::execute(request *req)
  64. {
  65. if (req->type != get_command())
  66. throw exception();
  67. return perform(req);
  68. }