universal.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. #pragma once
  39. // Get next connection that is ready for I/O.
  40. gearman_connection_st *gearman_ready(gearman_universal_st&);
  41. void gearman_universal_initialize(gearman_universal_st &self, const gearman_universal_options_t *options= NULL);
  42. void gearman_universal_clone(gearman_universal_st &destination, const gearman_universal_st &source, bool has_wakeup_fd= false);
  43. void gearman_universal_free(gearman_universal_st &gearman);
  44. void gearman_free_all_packets(gearman_universal_st &gearman);
  45. gearman_return_t gearman_universal_set_option(gearman_universal_st &self, gearman_universal_options_t option, bool value);
  46. void gearman_set_log_fn(gearman_universal_st &self, gearman_log_fn *function, void *context, gearman_verbose_t verbose);
  47. void gearman_universal_set_timeout(gearman_universal_st &self, int timeout);
  48. int gearman_universal_timeout(gearman_universal_st &self);
  49. void gearman_universal_set_namespace(gearman_universal_st &self, const char *namespace_key, size_t namespace_key_size);
  50. void gearman_reset(gearman_universal_st& universal);
  51. // Flush the send buffer for all connections.
  52. void gearman_flush_all(gearman_universal_st&);
  53. /**
  54. * Set custom memory allocation function for workloads. Normally gearman uses
  55. * the standard system malloc to allocate memory used with workloads. The
  56. * provided function will be used instead.
  57. */
  58. void gearman_set_workload_malloc_fn(gearman_universal_st&,
  59. gearman_malloc_fn *function,
  60. void *context);
  61. /**
  62. * Set custom memory free function for workloads. Normally gearman uses the
  63. * standard system free to free memory used with workloads. The provided
  64. * function will be used instead.
  65. */
  66. void gearman_set_workload_free_fn(gearman_universal_st&,
  67. gearman_free_fn *function,
  68. void *context);
  69. // Free all connections for a gearman structure.
  70. void gearman_free_all_cons(gearman_universal_st&);
  71. // Test echo with all connections.
  72. gearman_return_t gearman_echo(gearman_universal_st&, const void *workload, size_t workload_size);
  73. /**
  74. * Wait for I/O on connections.
  75. *
  76. */
  77. gearman_return_t gearman_wait(gearman_universal_st&);
  78. void gearman_nap(gearman_universal_st &self);
  79. void gearman_nap(int arg);
  80. static inline void gearman_universal_add_options(gearman_universal_st &self, gearman_universal_options_t options)
  81. {
  82. (void)gearman_universal_set_option(self, options, true);
  83. }
  84. static inline void gearman_universal_remove_options(gearman_universal_st &self, gearman_universal_options_t options)
  85. {
  86. (void)gearman_universal_set_option(self, options, false);
  87. }
  88. static inline bool gearman_universal_is_non_blocking(gearman_universal_st &self)
  89. {
  90. return self.options.non_blocking;
  91. }
  92. static inline const char *gearman_universal_error(const gearman_universal_st &self)
  93. {
  94. if (self.error.last_error[0] == 0)
  95. {
  96. return NULL;
  97. }
  98. return static_cast<const char *>(self.error.last_error);
  99. }
  100. static inline gearman_return_t gearman_universal_error_code(const gearman_universal_st &self)
  101. {
  102. return self.error.rc;
  103. }
  104. static inline int gearman_universal_errno(const gearman_universal_st &self)
  105. {
  106. return self.error.last_errno;
  107. }
  108. gearman_id_t gearman_universal_id(gearman_universal_st &universal);
  109. gearman_return_t gearman_set_identifier(gearman_universal_st& universal,
  110. const char *id,
  111. size_t id_size);
  112. const char *gearman_univeral_namespace(gearman_universal_st& universal);
  113. #define PUSH(__original, __temp_value) Push _push((__original),(__temp_value));
  114. class Push {
  115. public:
  116. Push(bool& original_, const bool temp_value) :
  117. _saved(original_),
  118. _origin(original_)
  119. {
  120. _origin= temp_value;
  121. }
  122. ~Push()
  123. {
  124. _origin= _saved;
  125. }
  126. private:
  127. bool _saved;
  128. bool& _origin;
  129. };
  130. /**
  131. Push the state of IO
  132. */
  133. #define PUSH_BLOCKING(__univeral) Push push_blocking_((__univeral).options.non_blocking, false);
  134. #define PUSH_NON_BLOCKING(__univeral) Push push_non_blocking_((__univeral).options.non_blocking, true);
  135. class Check {
  136. public:
  137. virtual gearman_return_t success(gearman_connection_st*)= 0;
  138. virtual ~Check() {};
  139. };
  140. class EchoCheck : public Check {
  141. public:
  142. EchoCheck(gearman_universal_st& universal_,
  143. const void *workload_, const size_t workload_size_);
  144. gearman_return_t success(gearman_connection_st* con);
  145. private:
  146. gearman_universal_st& _universal;
  147. const void *_workload;
  148. const size_t _workload_size;
  149. };
  150. class OptionCheck : public Check {
  151. public:
  152. OptionCheck(gearman_universal_st& universal_);
  153. gearman_return_t success(gearman_connection_st* con);
  154. private:
  155. gearman_universal_st& _universal;
  156. };