/src/client-async/sockets/SocketClientAsync.cpp

https://gitlab.com/github-cloud-corporation/cynara · C++ · 86 lines · 49 code · 15 blank · 22 comment · 5 complexity · 2ed441b2f365f61876ad75034e7e68a7 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/client-async/sockets/SocketClientAsync.cpp
  18. * @author Marcin Niesluchowski <m.niesluchow@samsung.com>
  19. * @version 1.0
  20. * @brief This file contains definition of cynara's socket asynchronous
  21. * client
  22. */
  23. #include <request/Request.h>
  24. #include <request/RequestContext.h>
  25. #include "SocketClientAsync.h"
  26. namespace Cynara {
  27. SocketClientAsync::SocketClientAsync(const std::string &socketPath, ProtocolPtr protocol)
  28. : m_socket(socketPath, 0), m_protocol(protocol) {
  29. m_readQueue = std::make_shared<BinaryQueue>();
  30. m_writeQueue = std::make_shared<BinaryQueue>();
  31. }
  32. Socket::ConnectionStatus SocketClientAsync::connect(void) {
  33. Socket::ConnectionStatus status = m_socket.connect();
  34. if (status != Socket::ConnectionStatus::ALREADY_CONNECTED)
  35. clear();
  36. return status;
  37. }
  38. Socket::ConnectionStatus SocketClientAsync::completeConnection(void) {
  39. Socket::ConnectionStatus status = m_socket.completeConnection();
  40. if (status == Socket::ConnectionStatus::CONNECTION_FAILED)
  41. clear();
  42. return status;
  43. }
  44. int SocketClientAsync::getSockFd(void) {
  45. return m_socket.getSockFd();
  46. }
  47. bool SocketClientAsync::isConnected(void) {
  48. return m_socket.isConnected();
  49. }
  50. void SocketClientAsync::appendRequest(const Request &request) {
  51. RequestContext context(ResponseTakerPtr(), m_writeQueue);
  52. request.execute(*m_protocol, context);
  53. }
  54. bool SocketClientAsync::isDataToSend(void) {
  55. return m_socket.isDataToSend() || !m_writeQueue->empty();
  56. }
  57. Socket::SendStatus SocketClientAsync::sendToCynara(void) {
  58. return m_socket.sendToServer(*m_writeQueue);
  59. }
  60. bool SocketClientAsync::receiveFromCynara(void) {
  61. return m_socket.receiveFromServer(*m_readQueue);
  62. }
  63. ResponsePtr SocketClientAsync::getResponse(void) {
  64. return m_protocol->extractResponseFromBuffer(m_readQueue);
  65. }
  66. void SocketClientAsync::clear(void)
  67. {
  68. m_readQueue->clear();
  69. m_writeQueue->clear();
  70. }
  71. } // namespace Cynara