ping.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (c) 2018-2019 SignalWire, Inc
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. #pragma once
  23. /* The method name for a ping request */
  24. #define BLADE_PING_METHOD "blade.ping"
  25. /* Flags for the command, in our case we don't get replies */
  26. #define BLADE_PING_FLAGS SWCLT_CMD_FLAG_NOREPLY
  27. /* Default time to live for ping */
  28. #define BLADE_PING_TTL_MS BLADE_DEFAULT_CMD_TTL_MS
  29. /* Create our ping request template */
  30. typedef struct blade_ping_rqu_s {
  31. double timestamp;
  32. const char *payload;
  33. } blade_ping_rqu_t;
  34. SWCLT_JSON_MARSHAL_BEG(BLADE_PING_RQU, blade_ping_rqu_t)
  35. SWCLT_JSON_MARSHAL_DOUBLE(timestamp)
  36. SWCLT_JSON_MARSHAL_STRING_OPT(payload)
  37. SWCLT_JSON_MARSHAL_END()
  38. SWCLT_JSON_DESTROY_BEG(BLADE_PING_RQU, blade_ping_rqu_t)
  39. SWCLT_JSON_DESTROY_DOUBLE(timestamp)
  40. SWCLT_JSON_DESTROY_STRING(payload)
  41. SWCLT_JSON_DESTROY_END()
  42. SWCLT_JSON_PARSE_BEG(BLADE_PING_RQU, blade_ping_rqu_t)
  43. SWCLT_JSON_PARSE_DOUBLE_OPT(timestamp)
  44. SWCLT_JSON_PARSE_STRING_OPT(payload)
  45. SWCLT_JSON_PARSE_END()
  46. typedef struct blade_ping_rpl_s {
  47. double timestamp;
  48. const char *payload;
  49. } blade_ping_rpl_t;
  50. SWCLT_JSON_MARSHAL_BEG(BLADE_PING_RPL, blade_ping_rpl_t)
  51. SWCLT_JSON_MARSHAL_DOUBLE(timestamp)
  52. SWCLT_JSON_MARSHAL_STRING_OPT(payload)
  53. SWCLT_JSON_MARSHAL_END()
  54. SWCLT_JSON_DESTROY_BEG(BLADE_PING_RPL, blade_ping_rpl_t)
  55. SWCLT_JSON_DESTROY_DOUBLE(timestamp)
  56. SWCLT_JSON_DESTROY_STRING(payload)
  57. SWCLT_JSON_DESTROY_END()
  58. SWCLT_JSON_PARSE_BEG(BLADE_PING_RPL, blade_ping_rpl_t)
  59. SWCLT_JSON_PARSE_DOUBLE_OPT(timestamp)
  60. SWCLT_JSON_PARSE_STRING_OPT(payload)
  61. SWCLT_JSON_PARSE_END()
  62. /**
  63. * CREATE_BLADE_PING_CMD_ASYNC - Creates a command with a request
  64. * in it setup to submit a ping method to blade.
  65. */
  66. static inline swclt_cmd_t CREATE_BLADE_PING_CMD_ASYNC(
  67. swclt_cmd_cb_t cb,
  68. void *cb_data,
  69. double timestamp,
  70. const char *payload)
  71. {
  72. ks_json_t *obj = NULL;
  73. swclt_cmd_t cmd = KS_NULL_HANDLE;
  74. ks_pool_t *pool;
  75. if (ks_pool_open(&pool))
  76. return cmd;
  77. /* Fill in the ping request then marshal it, it will create copies
  78. * of all the fields so caller doesn't lose ownership here */
  79. if (!(obj = BLADE_PING_RQU_MARSHAL(&(blade_ping_rqu_t){
  80. timestamp,
  81. payload}))) {
  82. ks_pool_close(&pool);
  83. /* Since params is last, on error here we can be sure params was
  84. * not freed so do not set the callers params to NULL */
  85. return cmd;
  86. }
  87. /* Now give it to the new command */
  88. if (swclt_cmd_create_ex(
  89. &cmd,
  90. &pool,
  91. cb,
  92. cb_data,
  93. BLADE_PING_METHOD,
  94. &obj,
  95. BLADE_PING_TTL_MS,
  96. BLADE_PING_FLAGS,
  97. ks_uuid_null()))
  98. goto done;
  99. done:
  100. ks_json_delete(&obj);
  101. ks_pool_close(&pool);
  102. return cmd;
  103. }
  104. static inline swclt_cmd_t CREATE_BLADE_PING_CMD(double timestamp, const char *payload)
  105. {
  106. return CREATE_BLADE_PING_CMD_ASYNC(
  107. NULL,
  108. NULL,
  109. timestamp,
  110. payload);
  111. }