/include/nebu-app-framework/vmManager.h

https://github.com/deltaforge/nebu-app-framework-cpp · C Header · 81 lines · 44 code · 15 blank · 22 comment · 0 complexity · ea8f2bf178e78a6dafc22be4e18befcf MD5 · raw file

  1. #ifndef NEBUAPPFRAMEWORK_VMMANAGER_H_
  2. #define NEBUAPPFRAMEWORK_VMMANAGER_H_
  3. #include "nebu-app-framework/vmEventHandler.h"
  4. #include "nebu/appVirtRequest.h"
  5. #include "nebu/virtualMachine.h"
  6. #include <list>
  7. #include <memory>
  8. #include <string>
  9. #include <unordered_map>
  10. #include <vector>
  11. namespace nebu
  12. {
  13. namespace app
  14. {
  15. namespace framework
  16. {
  17. /** Manages the list and states of VirtualMachines in the application. */
  18. class VMManager
  19. {
  20. public:
  21. /** Creates a VMManager using the given connection to the Nebu middleware.
  22. * @param[in] appVirtRequest a connection to the Nebu middleware for the AppVirtRequest.
  23. */
  24. VMManager(std::shared_ptr<nebu::common::AppVirtRequest> appVirtRequest) :
  25. vmEventHandlers(), appVirtRequest(appVirtRequest), vmList() { }
  26. /** Empty destructor provided for inheritance. */
  27. virtual ~VMManager() { }
  28. /** Refreshes the list of VMs and detects any changes in that list.
  29. * @return true iff no exceptions occured in contacting the middleware.
  30. */
  31. virtual bool refreshVMList();
  32. /** Retrieves a list of all VirtualMachines known to the VMManager.
  33. * @return a vector of VirtualMachines.
  34. */
  35. virtual std::vector<std::shared_ptr<nebu::common::VirtualMachine>> getVMs() const;
  36. /** Retrieves a single VirtualMachine by its unique identifier.
  37. * @param[in] uuid the unique ID of the VM.
  38. * @return the VirtualMachine.
  39. */
  40. virtual std::shared_ptr<nebu::common::VirtualMachine> getVM(const std::string &uuid) const;
  41. /** Checks if a VirtualMachine with the given identifier exists.
  42. * @return true iff the VirtualMachine exists.
  43. */
  44. virtual bool hasVM(const std::string &uuid) const;
  45. /** Registers a VMEventHandler, allowing it to receive notifications of events detected
  46. * in refreshVMList().
  47. * @param[in] eventHandler the VMEventhandler to register.
  48. */
  49. virtual void registerVMEventHandler(std::shared_ptr<VMEventHandler> eventHandler);
  50. protected:
  51. void addVM(std::shared_ptr<nebu::common::VirtualMachine> vm);
  52. void updateVM(std::shared_ptr<nebu::common::VirtualMachine> vm,
  53. const nebu::common::VirtualMachine &updated);
  54. void removeVM(std::shared_ptr<nebu::common::VirtualMachine> vm);
  55. bool addNewVMs(std::vector<std::string> &retrievedVMIds);
  56. bool updateVMs(std::vector<std::string> &filteredVMIds);
  57. void removeVMs(std::vector<std::string> &retrievedVMIds);
  58. private:
  59. std::list<std::shared_ptr<VMEventHandler>> vmEventHandlers;
  60. std::shared_ptr<nebu::common::AppVirtRequest> appVirtRequest;
  61. std::unordered_map<std::string, std::shared_ptr<nebu::common::VirtualMachine>> vmList;
  62. };
  63. }
  64. }
  65. }
  66. #endif