PageRenderTime 29ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/src/monitor/socket/MonitorSocketClient.cpp

https://gitlab.com/admin-github-cloud/cynara
C++ | 126 lines | 83 code | 18 blank | 25 comment | 11 complexity | 46533f0ff2ff25def98a74675daf905c MD5 | raw file
  1. /*
  2. * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
  3. *
  4. * Contact: Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. /**
  19. * @file src/monitor/socket/MonitorSocketClient.cpp
  20. * @author Zofia Abramowska <z.abramowska@samsung.com>
  21. * @version 1.0
  22. * @brief This file contains implementation of cynara's monitor socket client
  23. */
  24. #include <cerrno>
  25. #include <cstdio>
  26. #include <cstring>
  27. #include <fcntl.h>
  28. #include <poll.h>
  29. #include <unistd.h>
  30. #include <config/PathConfig.h>
  31. #include <log/log.h>
  32. #include <request/Request.h>
  33. #include <request/RequestContext.h>
  34. #include "MonitorSocketClient.h"
  35. namespace Cynara {
  36. MonitorSocketClient::MonitorSocketClient()
  37. : m_socket(PathConfig::SocketPath::monitorGet),
  38. m_notifyFd(-1)
  39. {
  40. }
  41. void MonitorSocketClient::setNotifyFd(int notifyFd) {
  42. m_notifyFd = notifyFd;
  43. }
  44. bool MonitorSocketClient::isConnected(void) {
  45. return m_socket.isConnected();
  46. }
  47. bool MonitorSocketClient::connect(void) {
  48. switch (m_socket.connect()) {
  49. case Socket::ConnectionStatus::CONNECTION_SUCCEEDED:
  50. return true;
  51. case Socket::ConnectionStatus::CONNECTION_IN_PROGRESS:
  52. return m_socket.completeConnection() == Socket::ConnectionStatus::CONNECTION_SUCCEEDED;
  53. default:
  54. return false;
  55. }
  56. }
  57. bool MonitorSocketClient::sendRequest(const Request &request) {
  58. std::lock_guard<std::mutex> lock(m_mutex);
  59. //pass request to protocol
  60. BinaryQueuePtr bbqSharedPtr(&m_writeQueue, [](BinaryQueue *) {});
  61. RequestContext context(ResponseTakerPtr(), bbqSharedPtr);
  62. request.execute(m_protocol, context);
  63. //send request to cynara
  64. if (m_socket.sendToServer(m_writeQueue) == Socket::SendStatus::CONNECTION_LOST) {
  65. LOGW("Disconnected while sending request to Cynara.");
  66. return false;
  67. }
  68. return true;
  69. }
  70. bool MonitorSocketClient::waitForEvent(MonitorSocketClient::Event &event) {
  71. int cynaraFd = m_socket.getSockFd();
  72. pollfd desc[2];
  73. desc[0].fd = cynaraFd;
  74. desc[0].events = POLLIN;
  75. desc[1].fd = m_notifyFd;
  76. desc[1].events = POLLIN;
  77. int ret = TEMP_FAILURE_RETRY(poll(desc, 2, -1));
  78. if (ret == -1) {
  79. int err = errno;
  80. LOGE("Poll returned with error: " << strerror(err));
  81. return false;
  82. }
  83. if (desc[1].revents & POLLIN) {
  84. LOGD("Poll returned with notification to return");
  85. event = Event::NOTIFY_RETURN;
  86. }
  87. if (desc[0].revents & POLLIN) {
  88. LOGD("Poll returned with fetch entries");
  89. event = Event::FETCH_ENTRIES;
  90. }
  91. return true;
  92. }
  93. MonitorGetEntriesResponsePtr MonitorSocketClient::fetchEntries(void) {
  94. while (true) {
  95. if (!m_socket.receiveFromServer(m_readQueue)) {
  96. LOGW("Disconnected while receiving response from Cynara.");
  97. return nullptr;
  98. }
  99. BinaryQueuePtr bbqSharedPtr(&m_readQueue, [](BinaryQueue *) {});
  100. ResponsePtr response = m_protocol.extractResponseFromBuffer(bbqSharedPtr);
  101. if (response) {
  102. MonitorGetEntriesResponsePtr monRes =
  103. std::dynamic_pointer_cast<MonitorGetEntriesResponse>(response);
  104. return monRes;
  105. }
  106. }
  107. }
  108. } /* namespace Cynara */