diff options
author | Christian Grothoff <christian@grothoff.org> | 2021-08-01 13:20:49 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2021-08-01 13:20:55 +0200 |
commit | 06f45b04aaa8a9832599035cafee142f09700b9b (patch) | |
tree | b928a166e1dec9b55c85562de57cd67e03f60906 /src | |
parent | 2032f3ce3ed0ed7a6c1c6fb9df6222d85758bf70 (diff) | |
download | gnunet-06f45b04aaa8a9832599035cafee142f09700b9b.tar.gz gnunet-06f45b04aaa8a9832599035cafee142f09700b9b.zip |
-move DB-agnostic types to GNUNET_DB
Diffstat (limited to 'src')
-rw-r--r-- | src/include/gnunet_db_lib.h | 43 | ||||
-rw-r--r-- | src/include/gnunet_pq_lib.h | 67 | ||||
-rw-r--r-- | src/pq/pq_event.c | 64 |
3 files changed, 84 insertions, 90 deletions
diff --git a/src/include/gnunet_db_lib.h b/src/include/gnunet_db_lib.h index 85ad13e31..63c45112d 100644 --- a/src/include/gnunet_db_lib.h +++ b/src/include/gnunet_db_lib.h | |||
@@ -61,4 +61,47 @@ enum GNUNET_DB_QueryStatus | |||
61 | that returned more than one result. */ | 61 | that returned more than one result. */ |
62 | }; | 62 | }; |
63 | 63 | ||
64 | |||
65 | /** | ||
66 | * Handle for an active LISTENer to a database. | ||
67 | */ | ||
68 | struct GNUNET_DB_EventHandler; | ||
69 | |||
70 | /** | ||
71 | * Function called on events received from Postgres. | ||
72 | * | ||
73 | * @param cls closure | ||
74 | * @param extra additional event data provided | ||
75 | * @param extra_size number of bytes in @a extra | ||
76 | */ | ||
77 | typedef void | ||
78 | (*GNUNET_DB_EventCallback)(void *cls, | ||
79 | const void *extra, | ||
80 | size_t extra_size); | ||
81 | |||
82 | GNUNET_NETWORK_STRUCT_BEGIN | ||
83 | |||
84 | |||
85 | /** | ||
86 | * Header of a structure that describes an | ||
87 | * event channel we may subscribe to or notify on. | ||
88 | */ | ||
89 | struct GNUNET_DB_EventHeaderP | ||
90 | { | ||
91 | /** | ||
92 | * The length of the struct (in bytes, including the length field itself), | ||
93 | * in big-endian format. | ||
94 | */ | ||
95 | uint16_t size GNUNET_PACKED; | ||
96 | |||
97 | /** | ||
98 | * The type of the message (GNUNET_DB_EVENT_TYPE_XXXX), in big-endian format. | ||
99 | */ | ||
100 | uint16_t type GNUNET_PACKED; | ||
101 | |||
102 | }; | ||
103 | |||
104 | GNUNET_NETWORK_STRUCT_END | ||
105 | |||
106 | |||
64 | #endif | 107 | #endif |
diff --git a/src/include/gnunet_pq_lib.h b/src/include/gnunet_pq_lib.h index 1f2915165..ecc2b9719 100644 --- a/src/include/gnunet_pq_lib.h +++ b/src/include/gnunet_pq_lib.h | |||
@@ -853,7 +853,7 @@ GNUNET_PQ_reconnect (struct GNUNET_PQ_Context *db); | |||
853 | 853 | ||
854 | 854 | ||
855 | /** | 855 | /** |
856 | * Function called whenever the socket needed for | 856 | * Function called whenever the socket needed for |
857 | * notifications from postgres changes. | 857 | * notifications from postgres changes. |
858 | * | 858 | * |
859 | * @param cls closure | 859 | * @param cls closure |
@@ -868,7 +868,7 @@ typedef void | |||
868 | * Obtain the file descriptor to poll on for notifications. | 868 | * Obtain the file descriptor to poll on for notifications. |
869 | * Useful if the GNUnet scheduler is NOT to be used for | 869 | * Useful if the GNUnet scheduler is NOT to be used for |
870 | * such notifications. | 870 | * such notifications. |
871 | * | 871 | * |
872 | * @param db database handle | 872 | * @param db database handle |
873 | * @param sc function to call with the socket | 873 | * @param sc function to call with the socket |
874 | * @param sc_cls closure for @a sc | 874 | * @param sc_cls closure for @a sc |
@@ -911,55 +911,8 @@ GNUNET_PQ_event_scheduler_stop (struct GNUNET_PQ_Context *db); | |||
911 | 911 | ||
912 | 912 | ||
913 | /** | 913 | /** |
914 | * Handle for an active LISTENer to the database. | ||
915 | */ | ||
916 | struct GNUNET_PQ_EventHandler; | ||
917 | |||
918 | /** | ||
919 | * Function called on events received from Postgres. | ||
920 | * | ||
921 | * @param cls closure | ||
922 | * @param extra additional event data provided | ||
923 | * @param extra_size number of bytes in @a extra | ||
924 | */ | ||
925 | typedef void | ||
926 | (*GNUNET_PQ_EventCallback)(void *cls, | ||
927 | const void *extra, | ||
928 | size_t extra_size); | ||
929 | |||
930 | GNUNET_NETWORK_STRUCT_BEGIN | ||
931 | |||
932 | |||
933 | /** | ||
934 | * Header of a structure that describes an | ||
935 | * event channel we may subscribe to or notify on. | ||
936 | */ | ||
937 | struct GNUNET_PQ_EventHeaderP | ||
938 | { | ||
939 | /** | ||
940 | * The length of the struct (in bytes, including the length field itself), | ||
941 | * in big-endian format. | ||
942 | */ | ||
943 | uint16_t size GNUNET_PACKED; | ||
944 | |||
945 | /** | ||
946 | * The type of the message (GNUNET_PQ_EVENT_TYPE_XXXX), in big-endian format. | ||
947 | */ | ||
948 | uint16_t type GNUNET_PACKED; | ||
949 | |||
950 | }; | ||
951 | |||
952 | GNUNET_NETWORK_STRUCT_END | ||
953 | |||
954 | |||
955 | /** | ||
956 | * Handle for an active LISTENer to the database. | ||
957 | */ | ||
958 | struct GNUNET_PQ_EventHandler; | ||
959 | |||
960 | /** | ||
961 | * Register callback to be invoked on events of type @a es. | 914 | * Register callback to be invoked on events of type @a es. |
962 | * | 915 | * |
963 | * Unlike many other calls, this function is thread-safe | 916 | * Unlike many other calls, this function is thread-safe |
964 | * and may be called from threads that are different | 917 | * and may be called from threads that are different |
965 | * from the one that setup @a db. However, the @a cb | 918 | * from the one that setup @a db. However, the @a cb |
@@ -973,16 +926,16 @@ struct GNUNET_PQ_EventHandler; | |||
973 | * @param cb_cls closure for @a cb | 926 | * @param cb_cls closure for @a cb |
974 | * @return handle useful to cancel the listener | 927 | * @return handle useful to cancel the listener |
975 | */ | 928 | */ |
976 | struct GNUNET_PQ_EventHandler * | 929 | struct GNUNET_DB_EventHandler * |
977 | GNUNET_PQ_event_listen (struct GNUNET_PQ_Context *db, | 930 | GNUNET_PQ_event_listen (struct GNUNET_PQ_Context *db, |
978 | const struct GNUNET_PQ_EventHeaderP *es, | 931 | const struct GNUNET_DB_EventHeaderP *es, |
979 | GNUNET_PQ_EventCallback cb, | 932 | GNUNET_DB_EventCallback cb, |
980 | void *cb_cls); | 933 | void *cb_cls); |
981 | 934 | ||
982 | 935 | ||
983 | /** | 936 | /** |
984 | * Stop notifications. | 937 | * Stop notifications. |
985 | * | 938 | * |
986 | * Unlike many other calls, this function is thread-safe | 939 | * Unlike many other calls, this function is thread-safe |
987 | * and may be called from threads that are different | 940 | * and may be called from threads that are different |
988 | * from the one that setup @a db. However, the @a cb | 941 | * from the one that setup @a db. However, the @a cb |
@@ -992,12 +945,12 @@ GNUNET_PQ_event_listen (struct GNUNET_PQ_Context *db, | |||
992 | * @param eh handle to unregister. | 945 | * @param eh handle to unregister. |
993 | */ | 946 | */ |
994 | void | 947 | void |
995 | GNUNET_PQ_event_listen_cancel (struct GNUNET_PQ_EventHandler *eh); | 948 | GNUNET_PQ_event_listen_cancel (struct GNUNET_DB_EventHandler *eh); |
996 | 949 | ||
997 | 950 | ||
998 | /** | 951 | /** |
999 | * Notify all that listen on @a es of an event. | 952 | * Notify all that listen on @a es of an event. |
1000 | * | 953 | * |
1001 | * Unlike many other calls, this function is thread-safe | 954 | * Unlike many other calls, this function is thread-safe |
1002 | * and may be called from threads that are different | 955 | * and may be called from threads that are different |
1003 | * from the one that setup @a db. However, the @a cb | 956 | * from the one that setup @a db. However, the @a cb |
@@ -1011,7 +964,7 @@ GNUNET_PQ_event_listen_cancel (struct GNUNET_PQ_EventHandler *eh); | |||
1011 | */ | 964 | */ |
1012 | void | 965 | void |
1013 | GNUNET_PQ_event_notify (struct GNUNET_PQ_Context *db, | 966 | GNUNET_PQ_event_notify (struct GNUNET_PQ_Context *db, |
1014 | const struct GNUNET_PQ_EventHeaderP *es, | 967 | const struct GNUNET_DB_EventHeaderP *es, |
1015 | const void *extra, | 968 | const void *extra, |
1016 | size_t extra_size); | 969 | size_t extra_size); |
1017 | 970 | ||
diff --git a/src/pq/pq_event.c b/src/pq/pq_event.c index 74b8b3653..e1134a5fa 100644 --- a/src/pq/pq_event.c +++ b/src/pq/pq_event.c | |||
@@ -29,8 +29,8 @@ | |||
29 | 29 | ||
30 | /** | 30 | /** |
31 | * Handle for an active LISTENer to the database. | 31 | * Handle for an active LISTENer to the database. |
32 | */ | 32 | */ |
33 | struct GNUNET_PQ_EventHandler | 33 | struct GNUNET_DB_EventHandler |
34 | { | 34 | { |
35 | /** | 35 | /** |
36 | * Channel name. | 36 | * Channel name. |
@@ -40,7 +40,7 @@ struct GNUNET_PQ_EventHandler | |||
40 | /** | 40 | /** |
41 | * Function to call on events. | 41 | * Function to call on events. |
42 | */ | 42 | */ |
43 | GNUNET_PQ_EventCallback cb; | 43 | GNUNET_DB_EventCallback cb; |
44 | 44 | ||
45 | /** | 45 | /** |
46 | * Closure for @e cb. | 46 | * Closure for @e cb. |
@@ -54,8 +54,6 @@ struct GNUNET_PQ_EventHandler | |||
54 | }; | 54 | }; |
55 | 55 | ||
56 | 56 | ||
57 | |||
58 | |||
59 | /** | 57 | /** |
60 | * Convert @a es to a short hash. | 58 | * Convert @a es to a short hash. |
61 | * | 59 | * |
@@ -63,7 +61,7 @@ struct GNUNET_PQ_EventHandler | |||
63 | * @param[out] sh short hash to set | 61 | * @param[out] sh short hash to set |
64 | */ | 62 | */ |
65 | static void | 63 | static void |
66 | es_to_sh (const struct GNUNET_PQ_EventHeaderP *es, | 64 | es_to_sh (const struct GNUNET_DB_EventHeaderP *es, |
67 | struct GNUNET_ShortHashCode *sh) | 65 | struct GNUNET_ShortHashCode *sh) |
68 | { | 66 | { |
69 | struct GNUNET_HashCode h_channel; | 67 | struct GNUNET_HashCode h_channel; |
@@ -111,7 +109,7 @@ sh_to_channel (struct GNUNET_ShortHashCode *sh, | |||
111 | * @return end position of the identifier | 109 | * @return end position of the identifier |
112 | */ | 110 | */ |
113 | static char * | 111 | static char * |
114 | es_to_channel (const struct GNUNET_PQ_EventHeaderP *es, | 112 | es_to_channel (const struct GNUNET_DB_EventHeaderP *es, |
115 | char identifier[64]) | 113 | char identifier[64]) |
116 | { | 114 | { |
117 | struct GNUNET_ShortHashCode sh; | 115 | struct GNUNET_ShortHashCode sh; |
@@ -141,12 +139,12 @@ struct NotifyContext | |||
141 | 139 | ||
142 | 140 | ||
143 | /** | 141 | /** |
144 | * Function called on every event handler that | 142 | * Function called on every event handler that |
145 | * needs to be triggered. | 143 | * needs to be triggered. |
146 | * | 144 | * |
147 | * @param cls a `struct NotifyContext` | 145 | * @param cls a `struct NotifyContext` |
148 | * @param sh channel name | 146 | * @param sh channel name |
149 | * @param value a `struct GNUNET_PQ_EventHandler` | 147 | * @param value a `struct GNUNET_DB_EventHandler` |
150 | * @return #GNUNET_OK continue to iterate | 148 | * @return #GNUNET_OK continue to iterate |
151 | */ | 149 | */ |
152 | static int | 150 | static int |
@@ -155,13 +153,13 @@ do_notify (void *cls, | |||
155 | void *value) | 153 | void *value) |
156 | { | 154 | { |
157 | struct NotifyContext *ctx = cls; | 155 | struct NotifyContext *ctx = cls; |
158 | struct GNUNET_PQ_EventHandler *eh = value; | 156 | struct GNUNET_DB_EventHandler *eh = value; |
159 | 157 | ||
160 | eh->cb (eh->cb_cls, | 158 | eh->cb (eh->cb_cls, |
161 | ctx->extra, | 159 | ctx->extra, |
162 | ctx->extra_size); | 160 | ctx->extra_size); |
163 | return GNUNET_OK; | 161 | return GNUNET_OK; |
164 | } | 162 | } |
165 | 163 | ||
166 | 164 | ||
167 | void | 165 | void |
@@ -265,7 +263,7 @@ do_scheduler_notify (void *cls) | |||
265 | = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_ZERO, | 263 | = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_ZERO, |
266 | db->rfd, | 264 | db->rfd, |
267 | &do_scheduler_notify, | 265 | &do_scheduler_notify, |
268 | db); | 266 | db); |
269 | } | 267 | } |
270 | 268 | ||
271 | 269 | ||
@@ -281,7 +279,7 @@ scheduler_fd_cb (void *cls, | |||
281 | int fd) | 279 | int fd) |
282 | { | 280 | { |
283 | struct GNUNET_PQ_Context *db = cls; | 281 | struct GNUNET_PQ_Context *db = cls; |
284 | 282 | ||
285 | if (NULL != db->event_task) | 283 | if (NULL != db->event_task) |
286 | { | 284 | { |
287 | GNUNET_SCHEDULER_cancel (db->event_task); | 285 | GNUNET_SCHEDULER_cancel (db->event_task); |
@@ -305,7 +303,7 @@ void | |||
305 | GNUNET_PQ_event_scheduler_start (struct GNUNET_PQ_Context *db) | 303 | GNUNET_PQ_event_scheduler_start (struct GNUNET_PQ_Context *db) |
306 | { | 304 | { |
307 | int fd; | 305 | int fd; |
308 | 306 | ||
309 | GNUNET_assert (! db->scheduler_on); | 307 | GNUNET_assert (! db->scheduler_on); |
310 | GNUNET_assert (NULL == db->sc); | 308 | GNUNET_assert (NULL == db->sc); |
311 | db->scheduler_on = true; | 309 | db->scheduler_on = true; |
@@ -335,7 +333,7 @@ GNUNET_PQ_event_scheduler_stop (struct GNUNET_PQ_Context *db) | |||
335 | static void | 333 | static void |
336 | manage_subscribe (struct GNUNET_PQ_Context *db, | 334 | manage_subscribe (struct GNUNET_PQ_Context *db, |
337 | const char *cmd, | 335 | const char *cmd, |
338 | struct GNUNET_PQ_EventHandler *eh) | 336 | struct GNUNET_DB_EventHandler *eh) |
339 | { | 337 | { |
340 | char sql[16 + 64]; | 338 | char sql[16 + 64]; |
341 | char *end; | 339 | char *end; |
@@ -379,13 +377,13 @@ register_notify (void *cls, | |||
379 | void *value) | 377 | void *value) |
380 | { | 378 | { |
381 | struct GNUNET_PQ_Context *db = cls; | 379 | struct GNUNET_PQ_Context *db = cls; |
382 | struct GNUNET_PQ_EventHandler *eh = value; | 380 | struct GNUNET_DB_EventHandler *eh = value; |
383 | 381 | ||
384 | manage_subscribe (db, | 382 | manage_subscribe (db, |
385 | "LISTEN ", | 383 | "LISTEN ", |
386 | eh); | 384 | eh); |
387 | return GNUNET_OK; | 385 | return GNUNET_OK; |
388 | } | 386 | } |
389 | 387 | ||
390 | 388 | ||
391 | void | 389 | void |
@@ -401,16 +399,16 @@ GNUNET_PQ_event_reconnect_ (struct GNUNET_PQ_Context *db) | |||
401 | } | 399 | } |
402 | 400 | ||
403 | 401 | ||
404 | struct GNUNET_PQ_EventHandler * | 402 | struct GNUNET_DB_EventHandler * |
405 | GNUNET_PQ_event_listen (struct GNUNET_PQ_Context *db, | 403 | GNUNET_PQ_event_listen (struct GNUNET_PQ_Context *db, |
406 | const struct GNUNET_PQ_EventHeaderP *es, | 404 | const struct GNUNET_DB_EventHeaderP *es, |
407 | GNUNET_PQ_EventCallback cb, | 405 | GNUNET_DB_EventCallback cb, |
408 | void *cb_cls) | 406 | void *cb_cls) |
409 | { | 407 | { |
410 | struct GNUNET_PQ_EventHandler *eh; | 408 | struct GNUNET_DB_EventHandler *eh; |
411 | bool was_zero; | 409 | bool was_zero; |
412 | 410 | ||
413 | eh = GNUNET_new (struct GNUNET_PQ_EventHandler); | 411 | eh = GNUNET_new (struct GNUNET_DB_EventHandler); |
414 | eh->db = db; | 412 | eh->db = db; |
415 | es_to_sh (es, | 413 | es_to_sh (es, |
416 | &eh->sh); | 414 | &eh->sh); |
@@ -421,14 +419,14 @@ GNUNET_PQ_event_listen (struct GNUNET_PQ_Context *db, | |||
421 | was_zero = (0 == GNUNET_CONTAINER_multishortmap_size (db->channel_map)); | 419 | was_zero = (0 == GNUNET_CONTAINER_multishortmap_size (db->channel_map)); |
422 | GNUNET_assert (GNUNET_OK == | 420 | GNUNET_assert (GNUNET_OK == |
423 | GNUNET_CONTAINER_multishortmap_put (db->channel_map, | 421 | GNUNET_CONTAINER_multishortmap_put (db->channel_map, |
424 | &eh->sh, | 422 | &eh->sh, |
425 | eh, | 423 | eh, |
426 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); | 424 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); |
427 | if ( (NULL != db->sc) && | 425 | if ( (NULL != db->sc) && |
428 | was_zero) | 426 | was_zero) |
429 | { | 427 | { |
430 | int fd = PQsocket (db->conn); | 428 | int fd = PQsocket (db->conn); |
431 | 429 | ||
432 | if (-1 != fd) | 430 | if (-1 != fd) |
433 | db->sc (db->sc_cls, | 431 | db->sc (db->sc_cls, |
434 | fd); | 432 | fd); |
@@ -443,7 +441,7 @@ GNUNET_PQ_event_listen (struct GNUNET_PQ_Context *db, | |||
443 | 441 | ||
444 | 442 | ||
445 | void | 443 | void |
446 | GNUNET_PQ_event_listen_cancel (struct GNUNET_PQ_EventHandler *eh) | 444 | GNUNET_PQ_event_listen_cancel (struct GNUNET_DB_EventHandler *eh) |
447 | { | 445 | { |
448 | struct GNUNET_PQ_Context *db = eh->db; | 446 | struct GNUNET_PQ_Context *db = eh->db; |
449 | 447 | ||
@@ -451,9 +449,9 @@ GNUNET_PQ_event_listen_cancel (struct GNUNET_PQ_EventHandler *eh) | |||
451 | pthread_mutex_lock (&db->notify_lock)); | 449 | pthread_mutex_lock (&db->notify_lock)); |
452 | GNUNET_assert (GNUNET_OK == | 450 | GNUNET_assert (GNUNET_OK == |
453 | GNUNET_CONTAINER_multishortmap_remove (db->channel_map, | 451 | GNUNET_CONTAINER_multishortmap_remove (db->channel_map, |
454 | &eh->sh, | 452 | &eh->sh, |
455 | eh)); | 453 | eh)); |
456 | 454 | ||
457 | manage_subscribe (db, | 455 | manage_subscribe (db, |
458 | "UNLISTEN ", | 456 | "UNLISTEN ", |
459 | eh); | 457 | eh); |
@@ -471,7 +469,7 @@ GNUNET_PQ_event_listen_cancel (struct GNUNET_PQ_EventHandler *eh) | |||
471 | 469 | ||
472 | void | 470 | void |
473 | GNUNET_PQ_event_notify (struct GNUNET_PQ_Context *db, | 471 | GNUNET_PQ_event_notify (struct GNUNET_PQ_Context *db, |
474 | const struct GNUNET_PQ_EventHeaderP *es, | 472 | const struct GNUNET_DB_EventHeaderP *es, |
475 | const void *extra, | 473 | const void *extra, |
476 | size_t extra_size) | 474 | size_t extra_size) |
477 | { | 475 | { |
@@ -512,5 +510,5 @@ GNUNET_PQ_event_notify (struct GNUNET_PQ_Context *db, | |||
512 | PQclear (result); | 510 | PQclear (result); |
513 | } | 511 | } |
514 | 512 | ||
515 | /* end of pq_event.c */ | ||
516 | 513 | ||
514 | /* end of pq_event.c */ | ||