submit.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #include "gear_config.h"
  38. #include <libgearman/common.h>
  39. #include "libgearman/vector.h"
  40. #include <cstdio>
  41. #include <cstring>
  42. namespace libgearman {
  43. namespace protocol {
  44. static inline gearman_return_t __submit(gearman_universal_st& universal,
  45. gearman_packet_st& message,
  46. const gearman_command_t command,
  47. const gearman_unique_t& unique,
  48. const gearman_string_t &function,
  49. const gearman_string_t &workload)
  50. {
  51. const void *args[3];
  52. size_t args_size[3];
  53. /**
  54. @todo fix it so that NULL is done by default by the API not by happenstance.
  55. */
  56. char function_buffer[1024];
  57. if (universal._namespace)
  58. {
  59. char *ptr= function_buffer;
  60. memcpy(ptr, gearman_string_value(universal._namespace), gearman_string_length(universal._namespace));
  61. ptr+= gearman_string_length(universal._namespace);
  62. memcpy(ptr, gearman_c_str(function), gearman_size(function) +1);
  63. ptr+= gearman_size(function);
  64. args[0]= function_buffer;
  65. args_size[0]= ptr -function_buffer +1;
  66. }
  67. else
  68. {
  69. args[0]= gearman_c_str(function);
  70. args_size[0]= gearman_size(function) +1;
  71. }
  72. args[1]= gearman_c_str(unique);
  73. args_size[1]= gearman_size(unique) +1;
  74. args[2]= gearman_c_str(workload);
  75. args_size[2]= gearman_size(workload);
  76. return gearman_packet_create_args(universal, message,
  77. GEARMAN_MAGIC_REQUEST,
  78. command,
  79. args, args_size,
  80. 3);
  81. }
  82. gearman_return_t submit(gearman_universal_st& universal,
  83. gearman_packet_st& message,
  84. const gearman_unique_t& unique,
  85. const gearman_command_t command,
  86. const gearman_string_t &function,
  87. const gearman_string_t &workload)
  88. {
  89. return __submit(universal, message, command, unique, function, workload);
  90. }
  91. gearman_return_t submit_background(gearman_universal_st& universal,
  92. gearman_packet_st& message,
  93. const gearman_unique_t& unique,
  94. const gearman_command_t command,
  95. const gearman_string_t &function,
  96. const gearman_string_t &workload)
  97. {
  98. return __submit(universal, message, command, unique, function, workload);
  99. }
  100. gearman_return_t submit_epoch(gearman_universal_st& universal,
  101. gearman_packet_st& message,
  102. const gearman_unique_t& unique,
  103. const gearman_string_t &function,
  104. const gearman_string_t &workload,
  105. time_t when)
  106. {
  107. const void *args[4];
  108. size_t args_size[4];
  109. /**
  110. @todo fix it so that NULL is done by default by the API not by happenstance.
  111. */
  112. char function_buffer[1024];
  113. if (universal._namespace)
  114. {
  115. char *ptr= function_buffer;
  116. memcpy(ptr, gearman_string_value(universal._namespace), gearman_string_length(universal._namespace));
  117. ptr+= gearman_string_length(universal._namespace);
  118. memcpy(ptr, gearman_c_str(function), gearman_size(function) +1);
  119. ptr+= gearman_size(function);
  120. args[0]= function_buffer;
  121. args_size[0]= ptr -function_buffer +1;
  122. }
  123. else
  124. {
  125. args[0]= gearman_c_str(function);
  126. args_size[0]= gearman_size(function) +1;
  127. }
  128. args[1]= gearman_c_str(unique);
  129. args_size[1]= gearman_size(unique) +1;
  130. char time_string[30];
  131. int length= snprintf(time_string, sizeof(time_string), "%" PRIu64, static_cast<int64_t>(when));
  132. args[2]= time_string;
  133. args_size[2]= length +1;
  134. args[3]= gearman_c_str(workload);
  135. args_size[3]= gearman_size(workload);
  136. return gearman_packet_create_args(universal, message,
  137. GEARMAN_MAGIC_REQUEST,
  138. GEARMAN_COMMAND_SUBMIT_JOB_EPOCH,
  139. args, args_size,
  140. 4);
  141. }
  142. } // namespace protocol
  143. } // namespace libgearman