submit.cc 5.4 KB

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