base.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are
  10. * met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following disclaimer
  17. * in the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * The names of its contributors may not be used to endorse or
  21. * promote products derived from this software without specific prior
  22. * written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. */
  37. #pragma once
  38. #include <boost/program_options.hpp>
  39. #include <libgearman-server/error.h>
  40. #include <libgearman-server/constants.h>
  41. struct gearman_server_con_st;
  42. struct gearman_server_job_st;
  43. struct gearman_server_st;
  44. struct gearmand_packet_st;
  45. namespace gearmand {
  46. class Plugin {
  47. public:
  48. Plugin(const std::string&);
  49. const std::string &name() const
  50. {
  51. return _name;
  52. }
  53. int compare(const std::string&);
  54. virtual ~Plugin()= 0;
  55. boost::program_options::options_description &command_line_options();
  56. bool has_error()
  57. {
  58. return _error_string.size();
  59. }
  60. const std::string& error_string()
  61. {
  62. return _error_string;
  63. }
  64. protected:
  65. void reset_error()
  66. {
  67. _error_string.clear();
  68. }
  69. std::string _error_string;
  70. private:
  71. boost::program_options::options_description _command_line_options;
  72. std::string _name;
  73. std::string _match;
  74. };
  75. namespace queue {
  76. class Context {
  77. public:
  78. Context():
  79. _store_on_shutdown(false)
  80. {
  81. }
  82. virtual ~Context()= 0;
  83. gearmand_error_t store(gearman_server_st *server,
  84. const char *unique,
  85. size_t unique_size,
  86. const char *function_name,
  87. size_t function_name_size,
  88. const void *data,
  89. size_t data_size,
  90. gearman_job_priority_t priority,
  91. int64_t when);
  92. protected:
  93. virtual gearmand_error_t add(gearman_server_st *server,
  94. const char *unique,
  95. size_t unique_size,
  96. const char *function_name,
  97. size_t function_name_size,
  98. const void *data,
  99. size_t data_size,
  100. gearman_job_priority_t priority,
  101. int64_t when)= 0;
  102. public:
  103. virtual gearmand_error_t flush(gearman_server_st *server)= 0;
  104. virtual gearmand_error_t done(gearman_server_st *server,
  105. const char *unique,
  106. size_t unique_size,
  107. const char *function_name,
  108. size_t function_name_size)= 0;
  109. virtual gearmand_error_t replay(gearman_server_st *server)= 0;
  110. void save_job(gearman_server_st& server,
  111. const gearman_server_job_st* server_job);
  112. static gearmand_error_t replay_add(gearman_server_st *server,
  113. void *context,
  114. const char *unique, size_t unique_size,
  115. const char *function_name, size_t function_name_size,
  116. const void *data, size_t data_size,
  117. gearman_job_priority_t priority,
  118. int64_t when);
  119. void store_on_shutdown(bool store_on_shutdown_)
  120. {
  121. _store_on_shutdown= store_on_shutdown_;
  122. }
  123. bool is_store_on_shutdown()
  124. {
  125. return _store_on_shutdown;
  126. }
  127. bool has_error()
  128. {
  129. return _error_string.size();
  130. }
  131. const std::string& error_string()
  132. {
  133. return _error_string;
  134. }
  135. protected:
  136. void reset_error()
  137. {
  138. _error_string.clear();
  139. }
  140. std::string _error_string;
  141. private:
  142. bool _store_on_shutdown;
  143. };
  144. } // namespace queue
  145. namespace protocol {
  146. class Context {
  147. public:
  148. virtual ~Context()= 0;
  149. // If the caller should free the Context, or leave it up to the plugin
  150. virtual bool is_owner()
  151. {
  152. return true;
  153. }
  154. // Notify on disconnect
  155. virtual void notify(gearman_server_con_st*)
  156. {
  157. return;
  158. }
  159. virtual size_t pack(const gearmand_packet_st *packet,
  160. gearman_server_con_st *con,
  161. void *data, const size_t data_size,
  162. gearmand_error_t& ret_ptr)= 0;
  163. virtual size_t unpack(gearmand_packet_st *packet,
  164. gearman_server_con_st *con,
  165. const void *data,
  166. const size_t data_size,
  167. gearmand_error_t& ret_ptr)= 0;
  168. bool has_error()
  169. {
  170. return _error_string.size();
  171. }
  172. const std::string& error_string()
  173. {
  174. return _error_string;
  175. }
  176. protected:
  177. void reset_error()
  178. {
  179. _error_string.clear();
  180. }
  181. std::string _error_string;
  182. };
  183. } // namespace protocol
  184. } // namespace gearmand