reverse_client_cb.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 <cstdlib>
  40. #include <iomanip>
  41. #include <iostream>
  42. #include <string>
  43. #include <libgearman/gearman.h>
  44. #include <boost/program_options.hpp>
  45. #include "util/string.hpp"
  46. #include "gearmand/error.hpp"
  47. #ifndef __INTEL_COMPILER
  48. #pragma GCC diagnostic ignored "-Wold-style-cast"
  49. #endif
  50. #define REVERSE_TASKS 10
  51. static gearman_return_t created(gearman_task_st *task);
  52. static gearman_return_t data(gearman_task_st *task);
  53. static gearman_return_t status(gearman_task_st *task);
  54. static gearman_return_t complete(gearman_task_st *task);
  55. static gearman_return_t fail(gearman_task_st *task);
  56. int main(int args, char *argv[])
  57. {
  58. gearmand::error::init(argv[0]);
  59. in_port_t port;
  60. int timeout;
  61. std::string host;
  62. std::string text_to_echo;
  63. boost::program_options::options_description desc("Options");
  64. desc.add_options()
  65. ("help", "Options related to the program.")
  66. ("verbose", "Send status to stdout")
  67. ("host,h", boost::program_options::value<std::string>(&host)->default_value("localhost"),"Connect to the host")
  68. ("port,p", boost::program_options::value<in_port_t>(&port)->default_value(GEARMAN_DEFAULT_TCP_PORT), "Port number use for connection")
  69. ("timeout,u", boost::program_options::value<int>(&timeout)->default_value(-1), "Timeout in milliseconds")
  70. ("text", boost::program_options::value<std::string>(&text_to_echo), "Text used for echo")
  71. ;
  72. boost::program_options::positional_options_description text_options;
  73. text_options.add("text", -1);
  74. boost::program_options::variables_map vm;
  75. try
  76. {
  77. boost::program_options::store(boost::program_options::command_line_parser(args, argv).
  78. options(desc).positional(text_options).run(), vm);
  79. boost::program_options::notify(vm);
  80. }
  81. catch(std::exception &e)
  82. {
  83. std::cout << e.what() << std::endl;
  84. return EXIT_FAILURE;
  85. }
  86. if (vm.count("help"))
  87. {
  88. std::cout << desc << std::endl;
  89. return EXIT_SUCCESS;
  90. }
  91. if (vm.count("verbose") == 0)
  92. {
  93. close(STDOUT_FILENO);
  94. }
  95. gearman_client_st client;
  96. if (gearman_client_create(&client) == NULL)
  97. {
  98. gearmand::error::message("Memory allocation failure on client creation");
  99. return EXIT_FAILURE;
  100. }
  101. gearman_return_t ret;
  102. ret= gearman_client_add_server(&client, host.c_str(), port);
  103. if (ret != GEARMAN_SUCCESS)
  104. {
  105. gearmand::error::message(gearman_client_error(&client));
  106. gearman_client_free(&client);
  107. return EXIT_FAILURE;
  108. }
  109. gearman_task_st task[REVERSE_TASKS];
  110. for (uint32_t x= 0; x < REVERSE_TASKS; x++)
  111. {
  112. if (gearman_client_add_task(&client, &(task[x]), NULL, "reverse", NULL,
  113. text_to_echo.c_str(), text_to_echo.size(),
  114. &ret) == NULL ||
  115. ret != GEARMAN_SUCCESS)
  116. {
  117. gearmand::error::message(gearman_client_error(&client));
  118. gearman_client_free(&client);
  119. return EXIT_FAILURE;
  120. }
  121. }
  122. gearman_client_set_created_fn(&client, created);
  123. gearman_client_set_data_fn(&client, data);
  124. gearman_client_set_status_fn(&client, status);
  125. gearman_client_set_complete_fn(&client, complete);
  126. gearman_client_set_fail_fn(&client, fail);
  127. ret= gearman_client_run_tasks(&client);
  128. if (ret != GEARMAN_SUCCESS)
  129. {
  130. gearmand::error::message(gearman_client_error(&client));
  131. gearman_client_free(&client);
  132. return EXIT_FAILURE;
  133. }
  134. gearman_client_free(&client);
  135. return EXIT_SUCCESS;
  136. }
  137. static gearman_return_t created(gearman_task_st *task)
  138. {
  139. std::cout << "Created: " << gearman_task_job_handle(task) << std::endl;
  140. return GEARMAN_SUCCESS;
  141. }
  142. static gearman_return_t data(gearman_task_st *task)
  143. {
  144. std::cout << "Data: " << gearman_task_job_handle(task) << " ";
  145. std::cout.write((char *)gearman_task_data(task), gearman_task_data_size(task));
  146. std::cout << std::endl;
  147. return GEARMAN_SUCCESS;
  148. }
  149. static gearman_return_t status(gearman_task_st *task)
  150. {
  151. std::clog << "Status: "
  152. << gearman_task_job_handle(task)
  153. << " (" << gearman_task_numerator(task) << "/" << gearman_task_denominator(task) << ")"
  154. << std::endl;
  155. return GEARMAN_SUCCESS;
  156. }
  157. static gearman_return_t complete(gearman_task_st *task)
  158. {
  159. std::cout << "Completed: " << gearman_task_job_handle(task) << " ";
  160. std::cout.write((char *)gearman_task_data(task), gearman_task_data_size(task));
  161. std::cout << std::endl;
  162. return GEARMAN_SUCCESS;
  163. }
  164. static gearman_return_t fail(gearman_task_st *task)
  165. {
  166. std::cerr << "Failed: " << gearman_task_job_handle(task) << std::endl;
  167. return GEARMAN_SUCCESS;
  168. }