server.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include <server.h>
  2. #include <command_list.h>
  3. #include <iostream>
  4. #include <stdexcept>
  5. using std::exception;
  6. unordered_map<string, unsigned long>* server_::active_users = nullptr;
  7. unordered_map<unsigned long, vector<sensor_data>*>* server_::db = nullptr;
  8. unordered_map<command_type, command*>* server_::commands = nullptr;
  9. server_::server_()
  10. {
  11. }
  12. void server_::init_commands()
  13. {
  14. server_::commands = new unordered_map<command_type, command*>();
  15. server_::commands->insert({ LOGIN_CMD, new cmd_login() });
  16. server_::commands->insert({ LOGOUT_CMD, new cmd_logout() });
  17. server_::commands->insert({ ADD_CMD, new cmd_add() });
  18. server_::commands->insert({ UPDATE_CMD, new cmd_update() });
  19. server_::commands->insert({ DELETE_CMD, new cmd_delete() });
  20. server_::commands->insert({ READ_CMD, new cmd_read() });
  21. server_::commands->insert({ READ_ALL_CMD, new cmd_read_all() });
  22. server_::commands->insert({ GET_STAT_CMD, new cmd_get_stat() });
  23. server_::commands->insert({ GET_STAT_ALL_CMD, new cmd_get_stat_all() });
  24. server_::commands->insert({ LOAD_CMD, new cmd_load() });
  25. server_::commands->insert({ STORE_CMD, new cmd_store() });
  26. }
  27. void server_::init_resources()
  28. {
  29. server_::active_users = new unordered_map<string, unsigned long>();
  30. server_::db = new unordered_map<unsigned long, vector<sensor_data>*>();
  31. }
  32. void server_::init()
  33. {
  34. server_::init_resources();
  35. server_::init_commands();
  36. }
  37. response server_::execute(request* req)
  38. {
  39. if (req == nullptr)
  40. throw exception();
  41. return server_::commands->at(req->type)->execute(req);
  42. }