gnunet_client_lib.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2001-2013 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. /**
  18. * @author Christian Grothoff
  19. *
  20. * @file
  21. * Functions related to accessing services
  22. * @defgroup client Client library
  23. * Generic client-side communication with services
  24. * @{
  25. */
  26. #ifndef GNUNET_CLIENT_LIB_H
  27. #define GNUNET_CLIENT_LIB_H
  28. #ifdef __cplusplus
  29. extern "C"
  30. {
  31. #if 0 /* keep Emacsens' auto-indent happy */
  32. }
  33. #endif
  34. #endif
  35. /**
  36. * Opaque handle for a connection to a service.
  37. */
  38. struct GNUNET_CLIENT_Connection;
  39. /**
  40. * Get a connection with a service.
  41. *
  42. * @param service_name name of the service
  43. * @param cfg configuration to use
  44. * @return NULL on error (service unknown to configuration)
  45. */
  46. struct GNUNET_CLIENT_Connection *
  47. GNUNET_CLIENT_connect (const char *service_name,
  48. const struct GNUNET_CONFIGURATION_Handle *cfg);
  49. /**
  50. * Destroy connection with the service. This will automatically
  51. * cancel any pending "receive" request (however, the handler will
  52. * *NOT* be called, not even with a NULL message). Any pending
  53. * transmission request will also be cancelled UNLESS the callback for
  54. * the transmission request has already been called, in which case the
  55. * transmission 'finish_pending_write' argument determines whether or
  56. * not the write is guaranteed to complete before the socket is fully
  57. * destroyed (unless, of course, there is an error with the server in
  58. * which case the message may still be lost).
  59. *
  60. * @param client handle to the service connection
  61. */
  62. void
  63. GNUNET_CLIENT_disconnect (struct GNUNET_CLIENT_Connection *client);
  64. /**
  65. * Type of a function to call when we receive a message
  66. * from the service.
  67. *
  68. * @param cls closure
  69. * @param msg message received, NULL on timeout or fatal error
  70. */
  71. typedef void (*GNUNET_CLIENT_MessageHandler) (void *cls,
  72. const struct GNUNET_MessageHeader *msg);
  73. /**
  74. * Read from the service.
  75. *
  76. * @param client connection to the service
  77. * @param handler function to call with the message
  78. * @param handler_cls closure for @a handler
  79. * @param timeout how long to wait until timing out
  80. */
  81. void
  82. GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *client,
  83. GNUNET_CLIENT_MessageHandler handler, void *handler_cls,
  84. struct GNUNET_TIME_Relative timeout);
  85. /**
  86. * Transmit handle for client connections.
  87. */
  88. struct GNUNET_CLIENT_TransmitHandle;
  89. /**
  90. * Ask the client to call us once the specified number of bytes
  91. * are free in the transmission buffer. Will never call the @a notify
  92. * callback in this task, but always first go into the scheduler.
  93. *
  94. * @param client connection to the service
  95. * @param size number of bytes to send
  96. * @param timeout after how long should we give up (and call
  97. * @a notify with buf NULL and size 0)?
  98. * @param auto_retry if the connection to the service dies, should we
  99. * automatically re-connect and retry (within the timeout period)
  100. * or should we immediately fail in this case? Pass #GNUNET_YES
  101. * if the caller does not care about temporary connection errors,
  102. * for example because the protocol is stateless
  103. * @param notify function to call
  104. * @param notify_cls closure for @a notify
  105. * @return NULL if someone else is already waiting to be notified
  106. * non-NULL if the notify callback was queued (can be used to cancel
  107. * using #GNUNET_CONNECTION_notify_transmit_ready_cancel)
  108. */
  109. struct GNUNET_CLIENT_TransmitHandle *
  110. GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *client,
  111. size_t size,
  112. struct GNUNET_TIME_Relative timeout,
  113. int auto_retry,
  114. GNUNET_CONNECTION_TransmitReadyNotify notify,
  115. void *notify_cls);
  116. /**
  117. * Cancel a request for notification.
  118. *
  119. * @param th handle from the original request.
  120. */
  121. void
  122. GNUNET_CLIENT_notify_transmit_ready_cancel (struct GNUNET_CLIENT_TransmitHandle
  123. *th);
  124. /**
  125. * Convenience API that combines sending a request
  126. * to the service and waiting for a response.
  127. * If either operation times out, the callback
  128. * will be called with a "NULL" response (in which
  129. * case the connection should probably be destroyed).
  130. *
  131. * @param client connection to use
  132. * @param hdr message to transmit
  133. * @param timeout when to give up (for both transmission
  134. * and for waiting for a response)
  135. * @param auto_retry if the connection to the service dies, should we
  136. * automatically re-connect and retry (within the timeout period)
  137. * or should we immediately fail in this case? Pass #GNUNET_YES
  138. * if the caller does not care about temporary connection errors,
  139. * for example because the protocol is stateless
  140. * @param rn function to call with the response
  141. * @param rn_cls closure for @a rn
  142. * @return #GNUNET_OK on success, #GNUNET_SYSERR if a request
  143. * is already pending
  144. */
  145. int
  146. GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection *client,
  147. const struct GNUNET_MessageHeader *hdr,
  148. struct GNUNET_TIME_Relative timeout,
  149. int auto_retry,
  150. GNUNET_CLIENT_MessageHandler rn,
  151. void *rn_cls);
  152. /**
  153. * Handle for a test to check if a service is running.
  154. */
  155. struct GNUNET_CLIENT_TestHandle;
  156. /**
  157. * Function called with the result on the service test.
  158. *
  159. * @param cls closure
  160. * @param result #GNUNET_YES if the service is running,
  161. * #GNUNET_NO if the service is not running
  162. * #GNUNET_SYSERR if the configuration is invalid
  163. */
  164. typedef void (*GNUNET_CLIENT_TestResultCallback)(void *cls,
  165. int result);
  166. /**
  167. * Test if the service is running. If we are given a UNIXPATH or a
  168. * local address, we do this NOT by trying to connect to the service,
  169. * but by trying to BIND to the same port. If the BIND fails, we know
  170. * the service is running.
  171. *
  172. * @param service name of the service to wait for
  173. * @param cfg configuration to use
  174. * @param timeout how long to wait at most
  175. * @param cb function to call with the result
  176. * @param cb_cls closure for @a cb
  177. * @return handle to cancel the test
  178. */
  179. struct GNUNET_CLIENT_TestHandle *
  180. GNUNET_CLIENT_service_test (const char *service,
  181. const struct GNUNET_CONFIGURATION_Handle *cfg,
  182. struct GNUNET_TIME_Relative timeout,
  183. GNUNET_CLIENT_TestResultCallback cb, void *cb_cls);
  184. /**
  185. * Abort testing for service.
  186. *
  187. * @param th test handle
  188. */
  189. void
  190. GNUNET_CLIENT_service_test_cancel (struct GNUNET_CLIENT_TestHandle *th);
  191. #if 0 /* keep Emacsens' auto-indent happy */
  192. {
  193. #endif
  194. #ifdef __cplusplus
  195. }
  196. #endif
  197. /* ifndef GNUNET_CLIENT_LIB_H */
  198. #endif
  199. /** @} */ /* end of group client */
  200. /* end of gnunet_client_lib.h */