wc_worker.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 <cerrno>
  40. #include <cstdio>
  41. #include <cstdlib>
  42. #include <cstring>
  43. #include <iostream>
  44. #include <libgearman/gearman.h>
  45. #include <boost/program_options.hpp>
  46. #include <boost/lexical_cast.hpp>
  47. #ifndef __INTEL_COMPILER
  48. #pragma GCC diagnostic ignored "-Wold-style-cast"
  49. #endif
  50. static void *wc(gearman_job_st *job, void *context, size_t *result_size,
  51. gearman_return_t *ret_ptr);
  52. int main(int args, char *argv[])
  53. {
  54. uint32_t count;
  55. int timeout;
  56. in_port_t port;
  57. std::string host;
  58. boost::program_options::options_description desc("Options");
  59. desc.add_options()
  60. ("help", "Options related to the program.")
  61. ("host,h", boost::program_options::value<std::string>(&host)->default_value("localhost"),"Connect to the host")
  62. ("port,p", boost::program_options::value<in_port_t>(&port)->default_value(GEARMAN_DEFAULT_TCP_PORT), "Port number use for connection")
  63. ("count,c", boost::program_options::value<uint32_t>(&count)->default_value(0), "Number of jobs to run before exiting")
  64. ("timeout,u", boost::program_options::value<int>(&timeout)->default_value(-1), "Timeout in milliseconds")
  65. ;
  66. boost::program_options::variables_map vm;
  67. try
  68. {
  69. boost::program_options::store(boost::program_options::parse_command_line(args, argv, desc), vm);
  70. boost::program_options::notify(vm);
  71. }
  72. catch(std::exception &e)
  73. {
  74. std::cout << e.what() << std::endl;
  75. return EXIT_FAILURE;
  76. }
  77. if (vm.count("help"))
  78. {
  79. std::cout << desc << std::endl;
  80. return EXIT_SUCCESS;
  81. }
  82. gearman_worker_st worker;
  83. if (gearman_worker_create(&worker) == NULL)
  84. {
  85. std::cerr << "Memory allocation failure on worker creation." << std::endl;
  86. return EXIT_FAILURE;
  87. }
  88. if (timeout >= 0)
  89. gearman_worker_set_timeout(&worker, timeout);
  90. gearman_return_t ret;
  91. ret= gearman_worker_add_server(&worker, host.c_str(), port);
  92. if (ret != GEARMAN_SUCCESS)
  93. {
  94. std::cerr << gearman_worker_error(&worker) << std::endl;
  95. return EXIT_FAILURE;
  96. }
  97. ret= gearman_worker_add_function(&worker, "wc", 0, wc, NULL);
  98. if (ret != GEARMAN_SUCCESS)
  99. {
  100. std::cerr << gearman_worker_error(&worker) << std::endl;
  101. return EXIT_FAILURE;
  102. }
  103. while (1)
  104. {
  105. ret= gearman_worker_work(&worker);
  106. if (ret != GEARMAN_SUCCESS)
  107. {
  108. std::cerr << gearman_worker_error(&worker) << std::endl;
  109. break;
  110. }
  111. if (count > 0)
  112. {
  113. count--;
  114. if (count == 0)
  115. break;
  116. }
  117. }
  118. gearman_worker_free(&worker);
  119. return EXIT_SUCCESS;
  120. }
  121. static void *wc(gearman_job_st *job, void *context, size_t *result_size,
  122. gearman_return_t *ret_ptr)
  123. {
  124. (void)context;
  125. const char *workload;
  126. workload= (const char *)gearman_job_workload(job);
  127. *result_size= gearman_job_workload_size(job);
  128. uint64_t count= 0;
  129. if (workload != NULL)
  130. {
  131. if (workload[0] != ' ' && workload[0] != '\t' && workload[0] != '\n')
  132. count++;
  133. for (size_t x= 0; x < *result_size; x++)
  134. {
  135. if (workload[x] != ' ' && workload[x] != '\t' && workload[x] != '\n')
  136. continue;
  137. count++;
  138. while (workload[x] == ' ' || workload[x] == '\t' || workload[x] == '\n')
  139. {
  140. x++;
  141. if (x == *result_size)
  142. {
  143. count--;
  144. break;
  145. }
  146. }
  147. }
  148. }
  149. std::string result= boost::lexical_cast<std::string>(count);
  150. std::cerr << "Job= " << gearman_job_handle(job) << " Workload=";
  151. std::cerr.write(workload, *result_size);
  152. std::cerr << " Result=" << result << std::endl;
  153. *result_size= result.size();
  154. *ret_ptr= GEARMAN_SUCCESS;
  155. return strdup(result.c_str());
  156. }