client.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2012 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 "libgearman/actions.hpp"
  39. #include "libgearman/interface/universal.hpp"
  40. #include "libgearman/is.hpp"
  41. struct Client
  42. {
  43. struct Options {
  44. bool non_blocking;
  45. bool unbuffered_result;
  46. bool no_new;
  47. bool free_tasks;
  48. bool generate_unique;
  49. bool exceptions;
  50. Options():
  51. non_blocking(false),
  52. unbuffered_result(false),
  53. no_new(false),
  54. free_tasks(false),
  55. generate_unique(false),
  56. exceptions(false)
  57. {
  58. }
  59. } options;
  60. enum gearman_client_t state;
  61. uint32_t new_tasks;
  62. uint32_t running_tasks;
  63. uint32_t task_count;
  64. void *context;
  65. struct gearman_connection_st *con;
  66. gearman_task_st *task;
  67. gearman_task_st *task_list;
  68. gearman_task_context_free_fn *task_context_free_fn;
  69. gearman_universal_st universal;
  70. struct gearman_actions_t actions;
  71. gearman_job_handle_t _do_handle; // Backwards compatible
  72. Client(gearman_client_st* shell_) :
  73. state(GEARMAN_CLIENT_STATE_IDLE),
  74. new_tasks(0),
  75. running_tasks(0),
  76. task_count(0),
  77. context(NULL),
  78. con(NULL),
  79. task(NULL),
  80. task_list(NULL),
  81. task_context_free_fn(NULL),
  82. actions(gearman_actions_default()),
  83. _shell(shell_)
  84. {
  85. _do_handle[0]= 0;
  86. if (shell_ and shell_ == &_owned_shell)
  87. {
  88. gearman_set_allocated(_shell, true);
  89. }
  90. else if (shell_)
  91. {
  92. gearman_set_allocated(_shell, false);
  93. }
  94. else
  95. {
  96. _shell= &_owned_shell;
  97. gearman_set_allocated(_shell, true);
  98. }
  99. _shell->impl(this);
  100. gearman_set_initialized(_shell, true);
  101. }
  102. gearman_return_t add_server(const char *host, const char* service_);
  103. ~Client()
  104. {
  105. if (_shell)
  106. {
  107. #ifndef NDBUG
  108. if (_shell == &_owned_shell)
  109. {
  110. assert(gearman_is_allocated(_shell));
  111. }
  112. else // _shell != &_owned_shell
  113. #endif
  114. {
  115. gearman_set_allocated(_shell, false);
  116. gearman_set_initialized(_shell, false);
  117. _shell->_impl= NULL;
  118. }
  119. }
  120. }
  121. gearman_client_st* shell()
  122. {
  123. return _shell;
  124. }
  125. bool ssl() const
  126. {
  127. return universal.ssl();
  128. }
  129. void ssl(bool ssl_)
  130. {
  131. universal.ssl(ssl_);
  132. }
  133. const char* error() const
  134. {
  135. return universal.error();
  136. }
  137. gearman_return_t error_code() const
  138. {
  139. return universal.error_code();
  140. }
  141. private:
  142. gearman_client_st* _shell;
  143. gearman_client_st _owned_shell;
  144. };