PageRenderTime 26ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/common/protocol/ProtocolMonitorGet.cpp

https://gitlab.com/admin-github-cloud/cynara
C++ | 185 lines | 128 code | 35 blank | 22 comment | 6 complexity | 6ec1d908df469ba9ab1aaa1f635057e7 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/common/protocol/ProtocolMonitorGet.cpp
  18. * @author Zofia Abramowska <z.abramowska@samsung.com>
  19. * @version 1.0
  20. * @brief This file implements protocol class for communication with
  21. * monitor getters API
  22. */
  23. #include <cinttypes>
  24. #include <memory>
  25. #include <exceptions/InvalidProtocolException.h>
  26. #include <log/log.h>
  27. #include <protocol/ProtocolFrameSerializer.h>
  28. #include <protocol/ProtocolOpCode.h>
  29. #include <protocol/ProtocolSerialization.h>
  30. #include <request/MonitorGetEntriesRequest.h>
  31. #include <request/MonitorGetFlushRequest.h>
  32. #include <request/RequestContext.h>
  33. #include <response/MonitorGetEntriesResponse.h>
  34. #include "ProtocolMonitorGet.h"
  35. namespace Cynara {
  36. ProtocolMonitorGet::ProtocolMonitorGet() {
  37. }
  38. ProtocolMonitorGet::~ProtocolMonitorGet() {
  39. }
  40. ProtocolPtr ProtocolMonitorGet::clone(void) {
  41. return std::make_shared<ProtocolMonitorGet>();
  42. }
  43. RequestPtr ProtocolMonitorGet::deserializeMonitorGetEntriesRequest(void) {
  44. uint64_t bufferSize;
  45. ProtocolDeserialization::deserialize(m_frameHeader, bufferSize);
  46. LOGD("Deserialized MonitorGetEntriesRequest: bufferSize [%" PRIu16 "]", bufferSize);
  47. return std::make_shared<MonitorGetEntriesRequest>(static_cast<size_t>(bufferSize),
  48. m_frameHeader.sequenceNumber());
  49. }
  50. RequestPtr ProtocolMonitorGet::deserializeMonitorGetFlushRequest(void) {
  51. LOGD("Deserialized MonitorGetFlushRequest");
  52. return std::make_shared<MonitorGetFlushRequest>(m_frameHeader.sequenceNumber());
  53. }
  54. RequestPtr ProtocolMonitorGet::extractRequestFromBuffer(BinaryQueuePtr bufferQueue) {
  55. ProtocolFrameSerializer::deserializeHeader(m_frameHeader, bufferQueue);
  56. if (!m_frameHeader.isFrameComplete()) {
  57. return nullptr;
  58. }
  59. ProtocolOpCode opCode;
  60. m_frameHeader.resetState();
  61. ProtocolDeserialization::deserialize(m_frameHeader, opCode);
  62. LOGD("Deserialized opCode [%" PRIu8 "]", opCode);
  63. switch (opCode) {
  64. case OpMonitorGetEntriesRequest:
  65. return deserializeMonitorGetEntriesRequest();
  66. case OpMonitorGetFlushRequest:
  67. return deserializeMonitorGetFlushRequest();
  68. default:
  69. throw InvalidProtocolException(InvalidProtocolException::WrongOpCode);
  70. }
  71. }
  72. ResponsePtr ProtocolMonitorGet::deserializeMonitorGetEntriesResponse(void) {
  73. ProtocolFrameFieldsCount entriesCount;
  74. ProtocolDeserialization::deserialize(m_frameHeader, entriesCount);
  75. std::vector<MonitorEntry> entries;
  76. entries.reserve(entriesCount);
  77. for (ProtocolFrameFieldsCount fields = 0; fields < entriesCount; fields++) {
  78. std::string clientId, userId, privilegeId;
  79. int64_t result, tv_sec, tv_nsec;
  80. ProtocolDeserialization::deserialize(m_frameHeader, clientId);
  81. ProtocolDeserialization::deserialize(m_frameHeader, userId);
  82. ProtocolDeserialization::deserialize(m_frameHeader, privilegeId);
  83. ProtocolDeserialization::deserialize(m_frameHeader, result);
  84. ProtocolDeserialization::deserialize(m_frameHeader, tv_sec);
  85. ProtocolDeserialization::deserialize(m_frameHeader, tv_nsec);
  86. PolicyKey key(clientId, userId, privilegeId);
  87. struct timespec timestamp;
  88. timestamp.tv_sec = static_cast<__time_t>(tv_sec);
  89. timestamp.tv_nsec = static_cast<__syscall_slong_t>(tv_nsec);
  90. entries.emplace_back(MonitorEntry(key, static_cast<size_t>(result), timestamp));
  91. }
  92. LOGD("Deserialized MonitorGetEntriesResponse: number of entries [%" PRIu16 "]", entriesCount);
  93. return std::make_shared<MonitorGetEntriesResponse>(entries, m_frameHeader.sequenceNumber());
  94. }
  95. ResponsePtr ProtocolMonitorGet::extractResponseFromBuffer(BinaryQueuePtr bufferQueue) {
  96. ProtocolFrameSerializer::deserializeHeader(m_frameHeader, bufferQueue);
  97. if (!m_frameHeader.isFrameComplete()) {
  98. return nullptr;
  99. }
  100. ProtocolOpCode opCode;
  101. m_frameHeader.resetState();
  102. ProtocolDeserialization::deserialize(m_frameHeader, opCode);
  103. LOGD("Deserialized opCode [%" PRIu8 "]", opCode);
  104. switch (opCode) {
  105. case OpMonitorGetEntriesResponse:
  106. return deserializeMonitorGetEntriesResponse();
  107. default:
  108. throw InvalidProtocolException(InvalidProtocolException::WrongOpCode);
  109. }
  110. }
  111. void ProtocolMonitorGet::execute(const RequestContext &context,
  112. const MonitorGetEntriesRequest &request)
  113. {
  114. LOGD("Serializing MonitorGetEntriesRequest: op [%" PRIu8 "], bufferSize [%" PRIu16 "]",
  115. OpMonitorGetEntriesRequest, request.bufferSize());
  116. ProtocolFrame frame = ProtocolFrameSerializer::startSerialization(request.sequenceNumber());
  117. ProtocolSerialization::serialize(frame, OpMonitorGetEntriesRequest);
  118. ProtocolSerialization::serialize(frame, static_cast<uint64_t>(request.bufferSize()));
  119. ProtocolFrameSerializer::finishSerialization(frame, *context.responseQueue());
  120. }
  121. void ProtocolMonitorGet::execute(const RequestContext &context,
  122. const MonitorGetFlushRequest &request)
  123. {
  124. LOGD("Serializing MonitorGetFlushRequest: op [%" PRIu8 "]", OpMonitorGetFlushRequest);
  125. ProtocolFrame frame = ProtocolFrameSerializer::startSerialization(request.sequenceNumber());
  126. ProtocolSerialization::serialize(frame, OpMonitorGetFlushRequest);
  127. ProtocolFrameSerializer::finishSerialization(frame, *context.responseQueue());
  128. }
  129. void ProtocolMonitorGet::execute(const RequestContext &context,
  130. const MonitorGetEntriesResponse &response)
  131. {
  132. ProtocolFrameFieldsCount entriesCount
  133. = static_cast<ProtocolFrameFieldsCount>(response.entries().size());
  134. LOGD("Serializing MonitorGetEntriesResponse: op [%" PRIu8 "], sequenceNumber [%" PRIu16 "], "
  135. "number of entries [%" PRIu16 "]",
  136. OpMonitorGetEntriesResponse, response.sequenceNumber(), entriesCount);
  137. ProtocolFrame frame = ProtocolFrameSerializer::startSerialization(response.sequenceNumber());
  138. ProtocolSerialization::serialize(frame, OpMonitorGetEntriesResponse);
  139. ProtocolSerialization::serialize(frame, entriesCount);
  140. for (const auto &entry : response.entries()) {
  141. ProtocolSerialization::serialize(frame, entry.key().client().toString());
  142. ProtocolSerialization::serialize(frame, entry.key().user().toString());
  143. ProtocolSerialization::serialize(frame, entry.key().privilege().toString());
  144. ProtocolSerialization::serialize(frame, static_cast<int64_t>(entry.result()));
  145. ProtocolSerialization::serialize(frame, static_cast<int64_t>(entry.timestamp().tv_sec));
  146. ProtocolSerialization::serialize(frame, static_cast<int64_t>(entry.timestamp().tv_nsec));
  147. }
  148. ProtocolFrameSerializer::finishSerialization(frame, *context.responseQueue());
  149. }
  150. } // namespace Cynara