PageRenderTime 37ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/service/monitor/MonitorLogic.cpp

https://gitlab.com/admin-github-cloud/cynara
C++ | 97 lines | 68 code | 8 blank | 21 comment | 14 complexity | e805bf7c9f00e0e1f30bc3d8fbeb66f6 MD5 | raw file
  1. /*
  2. * Copyright (c) 2016 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/monitor/MonitorLogic.cpp
  18. * @author Zofia Abramowska <z.abramowska@samsung.com>
  19. * @version 1.0
  20. * @brief This file implements logic class of monitor part of cynara service
  21. */
  22. #include <log/log.h>
  23. #include "MonitorLogic.h"
  24. namespace Cynara {
  25. void MonitorLogic::addClient(const RequestContext &context, ProtocolFrameSequenceNumber seq,
  26. int bufferSize) {
  27. auto insertIt = m_clients.find(context.clientId());
  28. if (insertIt == m_clients.end()) {
  29. if (!m_manager.addClient(context.clientId(), bufferSize)) {
  30. LOGE("Error adding client to entries manager");
  31. return;
  32. }
  33. m_clients.emplace(context.clientId(), MonitorResponseInfo(context, seq));
  34. } else {
  35. if (!m_manager.modifyClient(context.clientId(), bufferSize)) {
  36. LOGE("Error modifying client in entries manager");
  37. return;
  38. }
  39. insertIt->second.seq = seq;
  40. }
  41. if (m_manager.isClientFilled(context.clientId())) {
  42. m_responseCache.emplace(context.clientId(),
  43. MonitorResponse(context, seq, m_manager.fetchEntriesForClient(context.clientId())));
  44. }
  45. }
  46. void MonitorLogic::addEntry(const MonitorEntry &e) {
  47. if (!m_manager.addEntry(e)) {
  48. LOGD("No entry added.");
  49. return;
  50. }
  51. RequestContext::ClientId id;
  52. while ((id = m_manager.getFilledClientId()) != -1) {
  53. auto clientIt = m_clients.find(id);
  54. if (clientIt == m_clients.end()) {
  55. LOGE("Client [" << id << "] doesn't exist in logic but kept in manager!");
  56. return;
  57. }
  58. m_responseCache.emplace(id, MonitorResponse(clientIt->second,
  59. m_manager.fetchEntriesForClient(id)));
  60. }
  61. }
  62. void MonitorLogic::removeClient(const RequestContext &context) {
  63. (void)m_manager.removeClient(context.clientId());
  64. m_clients.erase(context.clientId());
  65. m_responseCache.erase(context.clientId());
  66. }
  67. void MonitorLogic::flushClient(const RequestContext &context) {
  68. auto clientIt = m_clients.find(context.clientId());
  69. if (clientIt == m_clients.end()) {
  70. LOGE("Client [" << context.clientId() << "] doesn't exist");
  71. return;
  72. }
  73. m_responseCache.emplace(context.clientId(),
  74. MonitorResponse(clientIt->second.context, clientIt->second.seq,
  75. m_manager.fetchEntriesForClient(context.clientId(), true)));
  76. }
  77. bool MonitorLogic::shouldSend(void) {
  78. return m_responseCache.size() > 0;
  79. }
  80. std::vector<MonitorLogic::MonitorResponse> MonitorLogic::getResponses(void) {
  81. std::vector<MonitorLogic::MonitorResponse> responses;
  82. responses.reserve(m_responseCache.size());
  83. for (auto &responseInfoPair : m_responseCache) {
  84. responses.push_back(std::move(std::get<1>(responseInfoPair)));
  85. }
  86. m_responseCache.clear();
  87. return responses;
  88. }
  89. } /* namespace Cynara */