store_res.cpp 958 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include "store_res.h"
  2. #include "rpcdb.h"
  3. #include <status_codes.h>
  4. #include <fstream>
  5. #include <ios>
  6. #include <stdexcept>
  7. #include <vector>
  8. #define DB_EXTENSION ".rpcdb"
  9. #define FILE_ERROR "Error opening database file."
  10. using std::ios;
  11. using std::ofstream;
  12. using std::runtime_error;
  13. using std::vector;
  14. store_res::store_res()
  15. {
  16. }
  17. store_res::~store_res()
  18. {
  19. }
  20. command_type store_res::get_command()
  21. {
  22. return command_type::STORE_CMD;
  23. }
  24. int store_res::perform(response *res)
  25. {
  26. if (res->response_u.get_stat_all.response != status::VALID)
  27. throw runtime_error(
  28. status_codes::get(res->response_u.read.response));
  29. ofstream out(get_username() + DB_EXTENSION, ios::binary);
  30. char *src = reinterpret_cast<char *>(
  31. res->response_u.read_all.data.data_val);
  32. size_t size =
  33. res->response_u.read_all.data.data_len * sizeof(sensor_data);
  34. if (out.fail())
  35. throw runtime_error(FILE_ERROR);
  36. out.write(src, size);
  37. out.close();
  38. return status::VALID;
  39. }