command.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. * Copyright (C) 2008 Brian Aker, Eric Day
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are
  11. * met:
  12. *
  13. * * Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * * Redistributions in binary form must reproduce the above
  17. * copyright notice, this list of conditions and the following disclaimer
  18. * in the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * * The names of its contributors may not be used to endorse or
  22. * promote products derived from this software without specific prior
  23. * written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. */
  38. #include "gear_config.h"
  39. #include <libgearman/common.h>
  40. #include <libgearman-1.0/visibility.h>
  41. #ifdef __clang__
  42. #pragma clang diagnostic push
  43. #pragma clang diagnostic ignored "-Wdeprecated-register"
  44. #pragma clang diagnostic ignored "-Wshorten-64-to-32"
  45. #endif
  46. #include "libgearman/command.hpp"
  47. #ifdef __clang__
  48. #pragma clang diagnostic pop
  49. #endif
  50. #include "libgearman/strcommand.h"
  51. #include "libgearman/assert.hpp"
  52. /**
  53. * Command info. Update GEARMAN_MAX_COMMAND_ARGS to the largest number in the
  54. * args column.
  55. */
  56. gearman_command_info_st gearmand_command_info_list[GEARMAN_COMMAND_MAX]=
  57. {
  58. { "GEARMAN_TEXT", GEARMAN_COMMAND_TEXT, 5, false },
  59. { "GEARMAN_CAN_DO", GEARMAN_COMMAND_CAN_DO, 1, false },
  60. { "GEARMAN_CANT_DO", GEARMAN_COMMAND_CANT_DO, 1, false },
  61. { "GEARMAN_RESET_ABILITIES", GEARMAN_COMMAND_RESET_ABILITIES, 0, false },
  62. { "GEARMAN_PRE_SLEEP", GEARMAN_COMMAND_PRE_SLEEP, 0, false },
  63. { "GEARMAN_UNUSED", GEARMAN_COMMAND_UNUSED, 0, false },
  64. { "GEARMAN_NOOP", GEARMAN_COMMAND_NOOP, 0, false },
  65. { "GEARMAN_SUBMIT_JOB", GEARMAN_COMMAND_SUBMIT_JOB, 2, true },
  66. { "GEARMAN_JOB_CREATED", GEARMAN_COMMAND_JOB_CREATED, 1, false },
  67. { "GEARMAN_GRAB_JOB", GEARMAN_COMMAND_GRAB_JOB, 0, false },
  68. { "GEARMAN_NO_JOB", GEARMAN_COMMAND_NO_JOB, 0, false },
  69. { "GEARMAN_JOB_ASSIGN", GEARMAN_COMMAND_JOB_ASSIGN, 2, true },
  70. { "GEARMAN_WORK_STATUS", GEARMAN_COMMAND_WORK_STATUS, 3, false },
  71. { "GEARMAN_WORK_COMPLETE", GEARMAN_COMMAND_WORK_COMPLETE, 1, true },
  72. { "GEARMAN_WORK_FAIL",GEARMAN_COMMAND_WORK_FAIL, 1, false },
  73. { "GEARMAN_GET_STATUS",GEARMAN_COMMAND_GET_STATUS, 1, false },
  74. { "GEARMAN_ECHO_REQ", GEARMAN_COMMAND_ECHO_REQ, 0, true },
  75. { "GEARMAN_ECHO_RES", GEARMAN_COMMAND_ECHO_RES, 0, true },
  76. { "GEARMAN_SUBMIT_JOB_BG", GEARMAN_COMMAND_SUBMIT_JOB_BG, 2, true },
  77. { "GEARMAN_ERROR", GEARMAN_COMMAND_ERROR, 2, false },
  78. { "GEARMAN_STATUS_RES", GEARMAN_COMMAND_STATUS_RES, 5, false },
  79. { "GEARMAN_SUBMIT_JOB_HIGH", GEARMAN_COMMAND_SUBMIT_JOB_HIGH, 2, true },
  80. { "GEARMAN_SET_CLIENT_ID", GEARMAN_COMMAND_SET_CLIENT_ID, 1, false },
  81. { "GEARMAN_CAN_DO_TIMEOUT", GEARMAN_COMMAND_CAN_DO_TIMEOUT, 2, false },
  82. { "GEARMAN_ALL_YOURS", GEARMAN_COMMAND_ALL_YOURS, 0, false },
  83. { "GEARMAN_WORK_EXCEPTION", GEARMAN_COMMAND_WORK_EXCEPTION, 1, true },
  84. { "GEARMAN_OPTION_REQ", GEARMAN_COMMAND_OPTION_REQ, 1, false },
  85. { "GEARMAN_OPTION_RES", GEARMAN_COMMAND_OPTION_RES, 1, false },
  86. { "GEARMAN_WORK_DATA", GEARMAN_COMMAND_WORK_DATA, 1, true },
  87. { "GEARMAN_WORK_WARNING", GEARMAN_COMMAND_WORK_WARNING, 1, true },
  88. { "GEARMAN_GRAB_JOB_UNIQ", GEARMAN_COMMAND_GRAB_JOB_UNIQ, 0, false },
  89. { "GEARMAN_JOB_ASSIGN_UNIQ", GEARMAN_COMMAND_JOB_ASSIGN_UNIQ, 3, true },
  90. { "GEARMAN_SUBMIT_JOB_HIGH_BG", GEARMAN_COMMAND_SUBMIT_JOB_HIGH_BG, 2, true },
  91. { "GEARMAN_SUBMIT_JOB_LOW", GEARMAN_COMMAND_SUBMIT_JOB_LOW, 2, true },
  92. { "GEARMAN_SUBMIT_JOB_LOW_BG", GEARMAN_COMMAND_SUBMIT_JOB_LOW_BG, 2, true },
  93. { "GEARMAN_SUBMIT_JOB_SCHED", GEARMAN_COMMAND_SUBMIT_JOB_SCHED, 7, true },
  94. { "GEARMAN_SUBMIT_JOB_EPOCH", GEARMAN_COMMAND_SUBMIT_JOB_EPOCH, 3, true },
  95. { "GEARMAN_SUBMIT_REDUCE_JOB", GEARMAN_COMMAND_SUBMIT_REDUCE_JOB, 4, true },
  96. { "GEARMAN_SUBMIT_REDUCE_JOB_BACKGROUND", GEARMAN_COMMAND_SUBMIT_REDUCE_JOB_BACKGROUND, 4, true },
  97. { "GEARMAN_GRAB_JOB_ALL", GEARMAN_COMMAND_GRAB_JOB_ALL, 0, false },
  98. { "GEARMAN_JOB_ASSIGN_ALL", GEARMAN_COMMAND_JOB_ASSIGN_ALL, 4, true },
  99. { "GEARMAN_GET_STATUS_UNIQUE", GEARMAN_COMMAND_GET_STATUS_UNIQUE, 1, false },
  100. { "GEARMAN_STATUS_RES_UNIQUE", GEARMAN_COMMAND_STATUS_RES_UNIQUE, 6, false }
  101. };
  102. const char *gearman_strcommand(gearman_command_t command)
  103. {
  104. if ((command >= GEARMAN_COMMAND_TEXT) and (command <= GEARMAN_COMMAND_STATUS_RES_UNIQUE))
  105. {
  106. const char* str= gearmand_command_info_list[command].name;
  107. str+= sizeof("GEARMAN_") -1;
  108. return str;
  109. }
  110. assert(0); // We should never reach this
  111. return "__UNKNOWN";
  112. }
  113. const char *gearman_enum_strcommand(gearman_command_t command)
  114. {
  115. if ((command >= GEARMAN_COMMAND_TEXT) and (command <= GEARMAN_COMMAND_STATUS_RES_UNIQUE))
  116. {
  117. return gearmand_command_info_list[command].name;
  118. }
  119. assert(0); // We should never reach this
  120. return "__UNKNOWN";
  121. }
  122. const gearman_command_info_st *gearman_command_info(gearman_command_t command)
  123. {
  124. if (uint32_t(command) >= uint32_t(GEARMAN_COMMAND_MAX))
  125. {
  126. return NULL;
  127. }
  128. const struct gearman_command_info_st* command_info= &gearmand_command_info_list[command];
  129. assert(command_info->code == command);
  130. return command_info;
  131. }
  132. const struct gearman_command_info_st * gearman_command_lookup (const char *str, unsigned int len)
  133. {
  134. const struct gearman_command_string_st* com_str= String2gearman_command_t::in_word_set(str, len);
  135. return gearman_command_info(com_str->code);
  136. }