base.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2011-2013 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 <cstring>
  39. #include <memory>
  40. #include "libgearman/vector.h"
  41. enum gearman_function_error_t {
  42. GEARMAN_FUNCTION_SUCCESS= GEARMAN_SUCCESS,
  43. GEARMAN_FUNCTION_FATAL= GEARMAN_FATAL,
  44. GEARMAN_FUNCTION_SHUTDOWN= GEARMAN_SHUTDOWN,
  45. GEARMAN_FUNCTION_ERROR= GEARMAN_ERROR
  46. };
  47. struct _worker_function_st
  48. {
  49. struct _options {
  50. bool packet_in_use;
  51. bool change;
  52. bool remove;
  53. _options() :
  54. packet_in_use(true),
  55. change(true),
  56. remove(false)
  57. { }
  58. } options;
  59. struct _worker_function_st *next;
  60. struct _worker_function_st *prev;
  61. private:
  62. char* _function_name;
  63. size_t _function_length;
  64. size_t _namespace_length;
  65. void* _context;
  66. struct gearman_function_t _function;
  67. int _timeout;
  68. public:
  69. _worker_function_st(const gearman_function_t& function_, void *context_) :
  70. next(NULL),
  71. prev(NULL),
  72. _function_name(NULL),
  73. _function_length(0),
  74. _namespace_length(0),
  75. _context(context_),
  76. _function(function_),
  77. _timeout(0)
  78. { }
  79. virtual bool has_callback() const= 0;
  80. virtual gearman_function_error_t callback(gearman_job_st* job, void *context_arg)= 0;
  81. bool init(gearman_vector_st* namespace_,
  82. const char *name_, const size_t size,
  83. const int timeout_)
  84. {
  85. _timeout= timeout_;
  86. _namespace_length= gearman_string_length(namespace_);
  87. _function_length= _namespace_length +size;
  88. _function_name= new (std::nothrow) char[_function_length +1];
  89. if (_function_name == NULL)
  90. {
  91. return false;
  92. }
  93. char *ptr= _function_name;
  94. if (gearman_string_length(namespace_))
  95. {
  96. memcpy(ptr, gearman_string_value(namespace_), gearman_string_length(namespace_));
  97. ptr+= gearman_string_length(namespace_);
  98. }
  99. memcpy(ptr, name_, size);
  100. _function_name[_function_length]= 0;
  101. return true;
  102. }
  103. int timeout() const
  104. {
  105. return _timeout;
  106. }
  107. const char *name() const
  108. {
  109. return _function_name;
  110. }
  111. const char *function_name() const
  112. {
  113. return _function_name +_namespace_length;
  114. }
  115. size_t function_length() const
  116. {
  117. return length() -_namespace_length;
  118. }
  119. size_t length() const
  120. {
  121. return _function_length;
  122. }
  123. const gearman_function_t& function()
  124. {
  125. return _function;
  126. }
  127. void* context()
  128. {
  129. return _context;
  130. }
  131. virtual ~_worker_function_st()
  132. {
  133. if (options.packet_in_use)
  134. {
  135. gearman_packet_free(&_packet);
  136. }
  137. delete [] _function_name;
  138. }
  139. gearman_packet_st& packet()
  140. {
  141. return _packet;
  142. }
  143. private:
  144. gearman_packet_st _packet;
  145. };