echo_client.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2011 Data Differential, http://datadifferential.com/
  6. * Copyright (C) 2008 Brian Aker, Eric Day
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are
  11. * met:
  12. *
  13. * * Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * * Redistributions in binary form must reproduce the above
  17. * copyright notice, this list of conditions and the following disclaimer
  18. * in the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * * The names of its contributors may not be used to endorse or
  22. * promote products derived from this software without specific prior
  23. * written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. */
  38. #include "gear_config.h"
  39. #include <iostream>
  40. #include <cstdlib>
  41. #include <string>
  42. #include <libgearman/gearman.h>
  43. #include <boost/program_options.hpp>
  44. #ifndef __INTEL_COMPILER
  45. #pragma GCC diagnostic ignored "-Wold-style-cast"
  46. #endif
  47. int main(int args, char *argv[])
  48. {
  49. std::string text_to_echo;
  50. std::string host;
  51. in_port_t port;
  52. boost::program_options::options_description desc("Options");
  53. desc.add_options()
  54. ("help", "Options related to the program.")
  55. ("host,h", boost::program_options::value<std::string>(&host)->default_value("localhost"),"Connect to the host")
  56. ("port,p", boost::program_options::value<in_port_t>(&port)->default_value(GEARMAN_DEFAULT_TCP_PORT), "Port number use for connection")
  57. ("text", boost::program_options::value<std::string>(&text_to_echo), "Text used for echo")
  58. ;
  59. boost::program_options::positional_options_description text_options;
  60. text_options.add("text", -1);
  61. boost::program_options::variables_map vm;
  62. try
  63. {
  64. boost::program_options::store(boost::program_options::command_line_parser(args, argv).
  65. options(desc).positional(text_options).run(), vm);
  66. boost::program_options::notify(vm);
  67. }
  68. catch(std::exception &e)
  69. {
  70. std::cout << e.what() << std::endl;
  71. return EXIT_FAILURE;
  72. }
  73. if (vm.count("help"))
  74. {
  75. std::cout << desc << std::endl;
  76. return EXIT_SUCCESS;
  77. }
  78. if (text_to_echo.empty())
  79. {
  80. while(std::cin.good())
  81. {
  82. char buffer[1024];
  83. std::cin.read(buffer, sizeof(buffer));
  84. text_to_echo.append(buffer, std::cin.gcount());
  85. }
  86. if (text_to_echo.empty())
  87. {
  88. std::cerr << "No text was provided for --text or via stdin" << std::endl;
  89. std::cerr << desc << std::endl;
  90. return EXIT_FAILURE;
  91. }
  92. }
  93. gearman_client_st client;
  94. if (gearman_client_create(&client) == NULL)
  95. {
  96. std::cerr << "Memory allocation failure on client creation" << std::endl;
  97. return EXIT_FAILURE;
  98. }
  99. gearman_return_t ret;
  100. ret= gearman_client_add_server(&client, host.c_str(), port);
  101. if (ret != GEARMAN_SUCCESS)
  102. {
  103. std::cerr << gearman_client_error(&client) << std::endl;
  104. return EXIT_FAILURE;
  105. }
  106. ret= gearman_client_echo(&client, text_to_echo.c_str(), text_to_echo.size());
  107. gearman_client_free(&client);
  108. if (ret != GEARMAN_SUCCESS)
  109. {
  110. std::cerr << gearman_client_error(&client) << std::endl;
  111. return EXIT_FAILURE;
  112. }
  113. std::cout << text_to_echo;
  114. return EXIT_SUCCESS;
  115. }