/src/service/main/Cynara.cpp

https://gitlab.com/github-cloud-corporation/cynara · C++ · 99 lines · 58 code · 18 blank · 23 comment · 3 complexity · 1e356e99a7b12d363728eeff6ad44d2c MD5 · raw file

  1. /*
  2. * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /**
  17. * @file src/service/main/Cynara.cpp
  18. * @author Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
  19. * @author Aleksander Zdyb <a.zdyb@samsung.com>
  20. * @author Pawel Wieczorek <p.wieczorek2@samsung.com>
  21. * @version 1.0
  22. * @brief This file implements main class of cynara service
  23. */
  24. #include <memory>
  25. #include <stddef.h>
  26. #include <config/PathConfig.h>
  27. #include <log/log.h>
  28. #include <exceptions/InitException.h>
  29. #include <agent/AgentManager.h>
  30. #include <logic/Logic.h>
  31. #include <plugin/PluginManager.h>
  32. #include <sockets/SocketManager.h>
  33. #include <storage/InMemoryStorageBackend.h>
  34. #include <storage/Storage.h>
  35. #include <storage/StorageBackend.h>
  36. #include "Cynara.h"
  37. namespace Cynara {
  38. Cynara::Cynara()
  39. : m_logic(nullptr), m_socketManager(nullptr), m_storage(nullptr), m_storageBackend(nullptr),
  40. m_lockFile(PathConfig::StoragePath::lockFile), m_databaseLock(m_lockFile) {
  41. }
  42. Cynara::~Cynara() {
  43. finalize();
  44. }
  45. void Cynara::init(void) {
  46. m_agentManager = std::make_shared<AgentManager>();
  47. m_logic = std::make_shared<Logic>();
  48. m_pluginManager = std::make_shared<PluginManager>(PathConfig::PluginPath::serviceDir);
  49. m_socketManager = std::make_shared<SocketManager>();
  50. m_storageBackend = std::make_shared<InMemoryStorageBackend>(PathConfig::StoragePath::dbDir);
  51. m_storage = std::make_shared<Storage>(*m_storageBackend);
  52. m_logic->bindAgentManager(m_agentManager);
  53. m_logic->bindPluginManager(m_pluginManager);
  54. m_logic->bindStorage(m_storage);
  55. m_logic->bindSocketManager(m_socketManager);
  56. m_socketManager->bindLogic(m_logic);
  57. m_databaseLock.lock(); // Wait until database lock can be acquired
  58. m_logic->loadDb();
  59. m_pluginManager->loadPlugins();
  60. }
  61. void Cynara::run(void) {
  62. if (!m_socketManager) {
  63. throw InitException();
  64. }
  65. m_socketManager->run();
  66. }
  67. void Cynara::finalize(void) {
  68. if (m_logic) {
  69. m_logic->unbindAll();
  70. }
  71. if (m_socketManager) {
  72. m_socketManager->unbindAll();
  73. }
  74. m_agentManager.reset();
  75. m_logic.reset();
  76. m_pluginManager.reset();
  77. m_socketManager.reset();
  78. m_storageBackend.reset();
  79. m_storage.reset();
  80. }
  81. } // namespace Cynara