unix_control_client.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright (C) 2015 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #ifndef UNIX_CONTROL_CLIENT_H
  7. #define UNIX_CONTROL_CLIENT_H
  8. #include <string>
  9. namespace isc {
  10. namespace dhcp {
  11. namespace test {
  12. /// @brief Class that acts as a UnixCommandSocket client
  13. ///
  14. /// This class is expected to be used unit-tests that attempt to communicate
  15. /// with the servers that use control channel (see src/lib/config/command_mgr.h)
  16. /// It can connect to an open UnixCommandSocket and exchange ControlChannel
  17. /// commands and responses.
  18. class UnixControlClient {
  19. public:
  20. /// @brief Default constructor
  21. UnixControlClient();
  22. /// @brief Destructor
  23. ~UnixControlClient();
  24. /// @brief Closes the Control Channel socket
  25. void disconnectFromServer();
  26. /// @brief Connects to a Unix socket at the given path
  27. /// @param socket_path pathname of the socket to open
  28. /// @return true if the connect was successful, false otherwise
  29. bool connectToServer(const std::string& socket_path);
  30. /// @brief Sends the given command across the open Control Channel
  31. /// @param command the command text to execute in JSON form
  32. /// @return true if the send succeeds, false otherwise
  33. bool sendCommand(const std::string& command);
  34. /// @brief Reads the response text from the open Control Channel
  35. /// @param response variable into which the received response should be
  36. /// placed.
  37. /// @param timeout_sec Timeout for receiving response in seconds.
  38. /// @return true if data was successfully read from the socket,
  39. /// false otherwise
  40. bool getResponse(std::string& response, const unsigned int timeout_sec = 0);
  41. /// @brief Uses select to poll the Control Channel for data waiting
  42. ///
  43. /// @param timeout_sec Select timeout in seconds
  44. /// @return -1 on error, 0 if no data is available, 1 if data is ready
  45. int selectCheck(const unsigned int timeout_sec);
  46. /// @brief Retains the fd of the open socket
  47. int socket_fd_;
  48. };
  49. }; // end of isc::dhcp::test namespace
  50. }; // end of isc::dhcp namespace
  51. }; // end of isc namespace
  52. #endif // UNIX_CONTROL_CLIENT_H