multi_client_test.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2013 Keyur Govande
  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. /*
  38. * This client takes in 2 gearmand server definitions and support the 4
  39. * combinations possible of of gearman_client(s).
  40. */
  41. #pragma once
  42. #include <vector>
  43. #define ARRAY_LENGTH 4
  44. struct multi_client_test_st
  45. {
  46. // index 0: no connected clients index 1: only connected to server 1 index 2:
  47. // only connected to server 2 index 3: connected to both servers
  48. gearman_client_st* _client_array[ARRAY_LENGTH];
  49. gearman_client_st* _clone_array[ARRAY_LENGTH];
  50. multi_client_test_st(server_startup_st& container_, int timeout= 0) :
  51. _server_container(container_)
  52. {
  53. bool has_failed= false;
  54. for (size_t x= 0; (x < ARRAY_LENGTH) and has_failed == false; x++)
  55. {
  56. _clone_array[x]= NULL;
  57. _client_array[x]= gearman_client_create(NULL);
  58. has_failed= (_client_array[x] == NULL);
  59. if (has_failed == false and (timeout > 0))
  60. {
  61. gearman_client_set_timeout(_client_array[x], timeout);
  62. }
  63. }
  64. if (has_failed)
  65. {
  66. FATAL("new gearman_client_create() failed");
  67. }
  68. }
  69. ~multi_client_test_st()
  70. {
  71. for (size_t x= 0; x < ARRAY_LENGTH; ++x)
  72. {
  73. if (_client_array[x])
  74. {
  75. gearman_client_free(_client_array[x]);
  76. _client_array[x]= NULL;
  77. }
  78. if (_clone_array[x])
  79. {
  80. gearman_client_free(_clone_array[x]);
  81. _clone_array[x]= NULL;
  82. }
  83. }
  84. }
  85. void add_server(const char* hostname_1, in_port_t port_arg_1, const char* hostname_2, in_port_t port_arg_2)
  86. {
  87. gearman_client_add_server(_client_array[1], hostname_1, port_arg_1);
  88. gearman_client_add_server(_client_array[2], hostname_2, port_arg_2);
  89. gearman_client_add_server(_client_array[3], hostname_1, port_arg_1);
  90. gearman_client_add_server(_client_array[3], hostname_2, port_arg_2);
  91. }
  92. server_startup_st& server_container(void) const
  93. {
  94. return _server_container;
  95. }
  96. void push_port(in_port_t arg)
  97. {
  98. _ports.push_back(arg);
  99. }
  100. in_port_t port(size_t pos)
  101. {
  102. FATAL_IF(pos >= _ports.size());
  103. return _ports[pos];
  104. }
  105. #define CLIENT_METHODS(NAME, I) \
  106. gearman_client_st* NAME##_client() \
  107. { \
  108. return client(I); \
  109. } \
  110. void reset_##NAME##_clone() \
  111. { \
  112. reset_clone(I); \
  113. } \
  114. void clear_##NAME##_clone() \
  115. { \
  116. clear_clone(I); \
  117. }
  118. CLIENT_METHODS(unconnected, 0);
  119. CLIENT_METHODS(connected_to_1, 1);
  120. CLIENT_METHODS(connected_to_2, 2);
  121. CLIENT_METHODS(connected_to_both, 3);
  122. private:
  123. gearman_client_st *client(int index)
  124. {
  125. if (_clone_array[index] == NULL)
  126. {
  127. _clone_array[index]= gearman_client_clone(NULL, _client_array[index]);
  128. }
  129. return _clone_array[index];
  130. }
  131. void clear_clone(int index)
  132. {
  133. if (_clone_array[index])
  134. {
  135. gearman_client_free(_clone_array[index]);
  136. }
  137. _clone_array[index]= gearman_client_create(NULL);
  138. }
  139. void reset_clone(int index)
  140. {
  141. if (_clone_array[index])
  142. {
  143. gearman_client_free(_clone_array[index]);
  144. }
  145. _clone_array[index]= gearman_client_clone(NULL, _client_array[index]);
  146. }
  147. private:
  148. server_startup_st& _server_container;
  149. std::vector<in_port_t> _ports;
  150. };