You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

379 lines
11 KiB

  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2013 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your 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. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @author Gabor X Toth
  18. * @author Christian Grothoff
  19. *
  20. * @file
  21. * PSYC Slicer library
  22. *
  23. * @defgroup psyc-util-slicer PSYC Utilities library: Slicer
  24. * Try-and-slice processing of PSYC method names and environment.
  25. * @{
  26. */
  27. #ifndef GNUNET_PSYC_SLICER_H
  28. #define GNUNET_PSYC_SLICER_H
  29. #ifdef __cplusplus
  30. extern "C"
  31. {
  32. #if 0 /* keep Emacsens' auto-indent happy */
  33. }
  34. #endif
  35. #endif
  36. #include "gnunet_util_lib.h"
  37. /**
  38. * Handle to an implementation of try-and-slice.
  39. */
  40. struct GNUNET_PSYC_Slicer;
  41. /**
  42. * Function called upon receiving a message indicating a call to a @e method.
  43. *
  44. * This function is called one or more times for each message until all data
  45. * fragments arrive from the network.
  46. *
  47. * @param cls
  48. * Closure.
  49. * @param msg
  50. * Message part, as it arrived from the network.
  51. * @param message_id
  52. * Message counter, monotonically increasing from 1.
  53. * @param flags
  54. * OR'ed GNUNET_PSYC_MessageFlags
  55. * @param fragment_offset
  56. * Multicast message fragment offset.
  57. * @param tmit_flags
  58. * OR'ed GNUNET_PSYC_MasterTransmitFlags
  59. * @param nym
  60. * The sender of the message.
  61. * Can be NULL if the message is not connected to a pseudonym.
  62. * @param method_name
  63. * Original method name from PSYC.
  64. * May be more specific than the registered method name due to
  65. * try-and-slice matching.
  66. */
  67. typedef void
  68. (*GNUNET_PSYC_MethodCallback) (void *cls,
  69. const struct GNUNET_PSYC_MessageHeader *msg,
  70. const struct GNUNET_PSYC_MessageMethod *meth,
  71. uint64_t message_id,
  72. const char *method_name);
  73. /**
  74. * Function called upon receiving a modifier of a message.
  75. *
  76. * @param cls
  77. * Closure.
  78. * @param message_id
  79. * Message ID this data fragment belongs to.
  80. * @param flags
  81. * OR'ed GNUNET_PSYC_MessageFlags
  82. * @param fragment_offset
  83. * Multicast message fragment offset.
  84. * @param msg
  85. * Message part, as it arrived from the network.
  86. * @param oper
  87. * Operation to perform.
  88. * 0 in case of a modifier continuation.
  89. * @param name
  90. * Name of the modifier.
  91. * NULL in case of a modifier continuation.
  92. * @param value
  93. * Value of the modifier.
  94. * @param value_size
  95. * Size of @value.
  96. */
  97. typedef void
  98. (*GNUNET_PSYC_ModifierCallback) (void *cls,
  99. const struct GNUNET_PSYC_MessageHeader *msg,
  100. const struct GNUNET_MessageHeader *pmsg,
  101. uint64_t message_id,
  102. enum GNUNET_PSYC_Operator oper,
  103. const char *name,
  104. const void *value,
  105. uint16_t value_size,
  106. uint16_t full_value_size);
  107. /**
  108. * Function called upon receiving a data fragment of a message.
  109. *
  110. * @param cls
  111. * Closure.
  112. * @param msg
  113. * Message part, as it arrived from the network.
  114. * @param message_id
  115. * Message ID this data fragment belongs to.
  116. * @param flags
  117. * OR'ed GNUNET_PSYC_MessageFlags
  118. * @param fragment_offset
  119. * Multicast message fragment offset.
  120. * @param data
  121. * Data stream given to the method.
  122. * @param data_size
  123. * Number of bytes in @a data.
  124. * @param end
  125. * End of message?
  126. * #GNUNET_NO if there are further fragments,
  127. * #GNUNET_YES if this is the last fragment,
  128. * #GNUNET_SYSERR indicates the message was cancelled by the sender.
  129. */
  130. typedef void
  131. (*GNUNET_PSYC_DataCallback) (void *cls,
  132. const struct GNUNET_PSYC_MessageHeader *msg,
  133. const struct GNUNET_MessageHeader *pmsg,
  134. uint64_t message_id,
  135. const void *data,
  136. uint16_t data_size);
  137. /**
  138. * End of message.
  139. *
  140. * @param cls
  141. * Closure.
  142. * @param msg
  143. * Message part, as it arrived from the network.
  144. * @param message_id
  145. * Message ID this data fragment belongs to.
  146. * @param flags
  147. * OR'ed GNUNET_PSYC_MessageFlags
  148. * @param fragment_offset
  149. * Multicast message fragment offset.
  150. * @param cancelled
  151. * #GNUNET_YES if the message was cancelled,
  152. * #GNUNET_NO if the message is complete.
  153. */
  154. typedef void
  155. (*GNUNET_PSYC_EndOfMessageCallback) (void *cls,
  156. const struct GNUNET_PSYC_MessageHeader *msg,
  157. const struct GNUNET_MessageHeader *pmsg,
  158. uint64_t message_id,
  159. uint8_t is_cancelled);
  160. /**
  161. * Create a try-and-slice instance.
  162. *
  163. * A slicer processes incoming messages and notifies callbacks about matching
  164. * methods or modifiers encountered.
  165. *
  166. * @return A new try-and-slice construct.
  167. */
  168. struct GNUNET_PSYC_Slicer *
  169. GNUNET_PSYC_slicer_create (void);
  170. /**
  171. * Add a method to the try-and-slice instance.
  172. *
  173. * The callbacks are called for messages with a matching @a method_name prefix.
  174. *
  175. * @param slicer
  176. * The try-and-slice instance to extend.
  177. * @param method_name
  178. * Name of the given method, use empty string to match all.
  179. * @param method_cb
  180. * Method handler invoked upon a matching message.
  181. * @param modifier_cb
  182. * Modifier handler, invoked after @a method_cb
  183. * for each modifier in the message.
  184. * @param data_cb
  185. * Data handler, invoked after @a modifier_cb for each data fragment.
  186. * @param eom_cb
  187. * Invoked upon reaching the end of a matching message.
  188. * @param cls
  189. * Closure for the callbacks.
  190. */
  191. void
  192. GNUNET_PSYC_slicer_method_add (struct GNUNET_PSYC_Slicer *slicer,
  193. const char *method_name,
  194. GNUNET_PSYC_MessageCallback msg_cb,
  195. GNUNET_PSYC_MethodCallback method_cb,
  196. GNUNET_PSYC_ModifierCallback modifier_cb,
  197. GNUNET_PSYC_DataCallback data_cb,
  198. GNUNET_PSYC_EndOfMessageCallback eom_cb,
  199. void *cls);
  200. /**
  201. * Remove a registered method from the try-and-slice instance.
  202. *
  203. * Removes one matching handler registered with the given
  204. * @a method_name and callbacks.
  205. *
  206. * @param slicer
  207. * The try-and-slice instance.
  208. * @param method_name
  209. * Name of the method to remove.
  210. * @param method_cb
  211. * Only remove matching method handler, or NULL.
  212. * @param modifier_cb
  213. * Only remove matching modifier handler, or NULL.
  214. * @param data_cb
  215. * Only remove matching data handler, or NULL.
  216. * @param eom_cb
  217. * Only remove matching End of Message handler, or NULL.
  218. *
  219. * @return #GNUNET_OK if a method handler was removed,
  220. * #GNUNET_NO if no handler matched the given method name and callbacks.
  221. */
  222. int
  223. GNUNET_PSYC_slicer_method_remove (struct GNUNET_PSYC_Slicer *slicer,
  224. const char *method_name,
  225. GNUNET_PSYC_MessageCallback msg_cb,
  226. GNUNET_PSYC_MethodCallback method_cb,
  227. GNUNET_PSYC_ModifierCallback modifier_cb,
  228. GNUNET_PSYC_DataCallback data_cb,
  229. GNUNET_PSYC_EndOfMessageCallback eom_cb);
  230. /**
  231. * Watch a place for changed objects.
  232. *
  233. * @param slicer
  234. * The try-and-slice instance.
  235. * @param object_filter
  236. * Object prefix to match.
  237. * @param modifier_cb
  238. * Function to call when encountering a state modifier.
  239. * @param cls
  240. * Closure for callback.
  241. */
  242. void
  243. GNUNET_PSYC_slicer_modifier_add (struct GNUNET_PSYC_Slicer *slicer,
  244. const char *object_filter,
  245. GNUNET_PSYC_ModifierCallback modifier_cb,
  246. void *cls);
  247. /**
  248. * Remove a registered modifier from the try-and-slice instance.
  249. *
  250. * Removes one matching handler registered with the given
  251. * @a object_filter and callback.
  252. *
  253. * @param slicer
  254. * The try-and-slice instance.
  255. * @param object_filter
  256. * Object prefix to match.
  257. * @param modifier_cb
  258. * Function to call when encountering a state modifier changes.
  259. */
  260. int
  261. GNUNET_PSYC_slicer_modifier_remove (struct GNUNET_PSYC_Slicer *slicer,
  262. const char *object_filter,
  263. GNUNET_PSYC_ModifierCallback modifier_cb);
  264. /**
  265. * Process an incoming message and call matching handlers.
  266. *
  267. * @param slicer
  268. * The slicer to use.
  269. * @param msg
  270. * The message as it arrived from the network.
  271. */
  272. void
  273. GNUNET_PSYC_slicer_message (struct GNUNET_PSYC_Slicer *slicer,
  274. const struct GNUNET_PSYC_MessageHeader *msg);
  275. /**
  276. * Process an incoming message part and call matching handlers.
  277. *
  278. * @param slicer
  279. * The slicer to use.
  280. * @param message_id
  281. * ID of the message.
  282. * @param flags
  283. * Flags for the message.
  284. * @see enum GNUNET_PSYC_MessageFlags
  285. * @param fragment offset
  286. * Fragment offset of the message.
  287. * @param msg
  288. * The message part as it arrived from the network.
  289. */
  290. void
  291. GNUNET_PSYC_slicer_message_part (struct GNUNET_PSYC_Slicer *slicer,
  292. const struct GNUNET_PSYC_MessageHeader *msg,
  293. const struct GNUNET_MessageHeader *pmsg);
  294. /**
  295. * Remove all registered method handlers.
  296. *
  297. * @param slicer
  298. * Slicer to clear.
  299. */
  300. void
  301. GNUNET_PSYC_slicer_method_clear (struct GNUNET_PSYC_Slicer *slicer);
  302. /**
  303. * Remove all registered modifier handlers.
  304. *
  305. * @param slicer
  306. * Slicer to clear.
  307. */
  308. void
  309. GNUNET_PSYC_slicer_modifier_clear (struct GNUNET_PSYC_Slicer *slicer);
  310. /**
  311. * Remove all registered method & modifier handlers.
  312. *
  313. * @param slicer
  314. * Slicer to clear.
  315. */
  316. void
  317. GNUNET_PSYC_slicer_clear (struct GNUNET_PSYC_Slicer *slicer);
  318. /**
  319. * Destroy a given try-and-slice instance.
  320. *
  321. * @param slicer
  322. * Slicer to destroy
  323. */
  324. void
  325. GNUNET_PSYC_slicer_destroy (struct GNUNET_PSYC_Slicer *slicer);
  326. #if 0 /* keep Emacsens' auto-indent happy */
  327. {
  328. #endif
  329. #ifdef __cplusplus
  330. }
  331. #endif
  332. /* ifndef GNUNET_PSYC_SLICER_H */
  333. #endif
  334. /** @} */ /* end of group */