proxy.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef HADOOP_CORE_RPC_PROXY_H
  19. #define HADOOP_CORE_RPC_PROXY_H
  20. #include "rpc/call.h"
  21. #include <stdint.h> /* for uint8_t */
  22. #include <uv.h> /* for uv_buf_t */
  23. struct hadoop_err;
  24. struct hrpc_messenger;
  25. struct hrpc_response {
  26. uint8_t *pb_base;
  27. int pb_len;
  28. void *base;
  29. };
  30. struct hrpc_sync_ctx {
  31. uv_sem_t sem;
  32. struct hadoop_err *err;
  33. struct hrpc_response resp;
  34. };
  35. typedef size_t (*hrpc_pack_cb_t)(const void *, uint8_t *);
  36. #define RPC_PROXY_USERDATA_MAX 64
  37. struct hrpc_proxy {
  38. /**
  39. * The messenger that this proxy is associated with.
  40. */
  41. struct hrpc_messenger *msgr;
  42. /**
  43. * String describing the protocol this proxy speaks.
  44. */
  45. const char *protocol;
  46. /**
  47. * String describing the username this proxy uses.
  48. */
  49. const char *username;
  50. /**
  51. * The current call.
  52. */
  53. struct hrpc_call call;
  54. /**
  55. * A memory area which can be used by the current call.
  56. *
  57. * This will be null if userdata_len is 0.
  58. */
  59. uint8_t userdata[RPC_PROXY_USERDATA_MAX];
  60. };
  61. /**
  62. * Initialize a Hadoop proxy.
  63. *
  64. * @param proxy The Hadoop proxy to initialize.
  65. * @param msgr The messenger to associate the proxy with.
  66. * This messenger must not be de-allocated while the proxy
  67. * still exists.
  68. * @param protocol The protocol to use for this proxy.
  69. * This string must remain valid for the lifetime of the
  70. * proxy.
  71. * @param remote The remote to contact. Will be copied.
  72. * @param username The username to use.
  73. * This string must remain valid for the lifetime of the
  74. * proxy.
  75. */
  76. void hrpc_proxy_init(struct hrpc_proxy *proxy,
  77. struct hrpc_messenger *msgr,
  78. const struct sockaddr_in *remote,
  79. const char *protocol, const char *username);
  80. /**
  81. * Mark the proxy as active.
  82. *
  83. * @param proxy The proxy
  84. *
  85. * @return NULL on success. If the proxy is already
  86. * active, an error will be returned.
  87. */
  88. struct hadoop_err *hrpc_proxy_activate(struct hrpc_proxy *proxy);
  89. /**
  90. * Mark the proxy as inactive.
  91. *
  92. * This function should not be called after hrpc_proxy_start, since a proxy
  93. * that has been started will mark itself as inactive when appropriate.
  94. *
  95. * @param proxy The proxy.
  96. */
  97. void hrpc_proxy_deactivate(struct hrpc_proxy *proxy);
  98. /**
  99. * Allocate some data in the proxy's userdata area.
  100. *
  101. * This will overwrite anything previously allocated in the proxy's userdata
  102. * area. It is not necessary to free this memory later; it will be freed when
  103. * the proxy is freed.
  104. *
  105. * @param proxy The proxy
  106. *
  107. * @return NULL on OOM; a pointer to the userdata
  108. * otherwise.
  109. */
  110. void *hrpc_proxy_alloc_userdata(struct hrpc_proxy *proxy, size_t size);
  111. /**
  112. * Allocate a sync context from a proxy via hrpc_proxy_alloc_userdata.
  113. *
  114. * @param proxy The proxy
  115. *
  116. * @return NULL on OOM; the sync context otherwise.
  117. */
  118. struct hrpc_sync_ctx *hrpc_proxy_alloc_sync_ctx(struct hrpc_proxy *proxy);
  119. /**
  120. * Free a sync context allocated from a proxy.
  121. *
  122. * @param proxy The sync context.
  123. */
  124. void hrpc_free_sync_ctx(struct hrpc_sync_ctx *ctx);
  125. /**
  126. * A callback which synchronous RPCs can use.
  127. */
  128. void hrpc_proxy_sync_cb(struct hrpc_response *resp, struct hadoop_err *err,
  129. void *cb_data);
  130. /**
  131. * Start an outgoing RPC from the proxy.
  132. *
  133. * This method will return after queuing up the RPC to be sent.
  134. *
  135. * Note: after the proxy has been started, you may __not__ de-allocate the
  136. * proxy until the callback has happened.
  137. *
  138. * @param proxy The Hadoop proxy to use. A single proxy can
  139. * only make one call at once.
  140. * @param method The method we're calling.
  141. * @param payload The protobuf message we're sending.
  142. * @param payload_packed_len Length of payload when serialized.
  143. * @param payload_pack_cb Function used to pack the payload.
  144. * @param cb Callback invoked when the message is done.
  145. * @param cb_data Data provided along with cb.
  146. */
  147. void hrpc_proxy_start(struct hrpc_proxy *proxy,
  148. const char *method, const void *payload, int payload_packed_len,
  149. hrpc_pack_cb_t payload_pack_cb,
  150. hrpc_raw_cb_t cb, void *cb_data);
  151. #endif
  152. // vim: ts=4:sw=4:tw=79:et