diff options
Diffstat (limited to 'components/wisp_relay/sub_manager.h')
| -rw-r--r-- | components/wisp_relay/sub_manager.h | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/components/wisp_relay/sub_manager.h b/components/wisp_relay/sub_manager.h new file mode 100644 index 0000000..64afb04 --- /dev/null +++ b/components/wisp_relay/sub_manager.h | |||
| @@ -0,0 +1,92 @@ | |||
| 1 | #ifndef SUB_MANAGER_H | ||
| 2 | #define SUB_MANAGER_H | ||
| 3 | |||
| 4 | #include <stdbool.h> | ||
| 5 | #include <stdint.h> | ||
| 6 | #include "esp_err.h" | ||
| 7 | #include "freertos/FreeRTOS.h" | ||
| 8 | #include "freertos/semphr.h" | ||
| 9 | #include "relay_types.h" | ||
| 10 | |||
| 11 | #define SUB_MAX_TOTAL 64 | ||
| 12 | #define SUB_MAX_PER_CONN 8 | ||
| 13 | #define SUB_MAX_FILTERS 4 | ||
| 14 | #define SUB_MAX_ID_LEN 64 | ||
| 15 | |||
| 16 | #define SUB_MAX_FILTER_IDS 20 | ||
| 17 | #define SUB_MAX_FILTER_AUTHORS 20 | ||
| 18 | #define SUB_MAX_FILTER_KINDS 20 | ||
| 19 | #define SUB_MAX_FILTER_ETAGS 20 | ||
| 20 | #define SUB_MAX_FILTER_PTAGS 20 | ||
| 21 | |||
| 22 | typedef enum { | ||
| 23 | SUB_OK = 0, | ||
| 24 | SUB_ERR_INVALID, | ||
| 25 | SUB_ERR_TOO_MANY_FILTERS, | ||
| 26 | SUB_ERR_MEMORY, | ||
| 27 | SUB_ERR_NOT_FOUND, | ||
| 28 | } sub_error_t; | ||
| 29 | |||
| 30 | typedef struct { | ||
| 31 | char *ids[SUB_MAX_FILTER_IDS]; | ||
| 32 | size_t ids_count; | ||
| 33 | char *authors[SUB_MAX_FILTER_AUTHORS]; | ||
| 34 | size_t authors_count; | ||
| 35 | int32_t kinds[SUB_MAX_FILTER_KINDS]; | ||
| 36 | size_t kinds_count; | ||
| 37 | char *e_tags[SUB_MAX_FILTER_ETAGS]; | ||
| 38 | size_t e_tags_count; | ||
| 39 | char *p_tags[SUB_MAX_FILTER_PTAGS]; | ||
| 40 | size_t p_tags_count; | ||
| 41 | int64_t since; | ||
| 42 | int64_t until; | ||
| 43 | int limit; | ||
| 44 | } sub_filter_t; | ||
| 45 | |||
| 46 | typedef struct { | ||
| 47 | char sub_id[SUB_MAX_ID_LEN + 1]; | ||
| 48 | int conn_fd; | ||
| 49 | sub_filter_t filters[SUB_MAX_FILTERS]; | ||
| 50 | uint8_t filter_count; | ||
| 51 | uint16_t events_sent; | ||
| 52 | bool active; | ||
| 53 | } subscription_t; | ||
| 54 | |||
| 55 | typedef struct sub_manager { | ||
| 56 | subscription_t subs[SUB_MAX_TOTAL]; | ||
| 57 | SemaphoreHandle_t lock; | ||
| 58 | uint16_t active_count; | ||
| 59 | } sub_manager_t; | ||
| 60 | |||
| 61 | typedef struct { | ||
| 62 | int conn_fd; | ||
| 63 | char sub_id[SUB_MAX_ID_LEN + 1]; | ||
| 64 | } sub_match_entry_t; | ||
| 65 | |||
| 66 | typedef struct { | ||
| 67 | sub_match_entry_t matches[SUB_MAX_TOTAL]; | ||
| 68 | uint8_t count; | ||
| 69 | } sub_match_result_t; | ||
| 70 | |||
| 71 | esp_err_t sub_manager_init(sub_manager_t *mgr); | ||
| 72 | void sub_manager_destroy(sub_manager_t *mgr); | ||
| 73 | |||
| 74 | sub_error_t sub_manager_add(sub_manager_t *mgr, int conn_fd, | ||
| 75 | const char *sub_id, | ||
| 76 | const sub_filter_t *filters, | ||
| 77 | size_t filter_count); | ||
| 78 | |||
| 79 | sub_error_t sub_manager_remove(sub_manager_t *mgr, int conn_fd, | ||
| 80 | const char *sub_id); | ||
| 81 | |||
| 82 | void sub_manager_remove_all(sub_manager_t *mgr, int conn_fd); | ||
| 83 | |||
| 84 | void sub_manager_match_json(sub_manager_t *mgr, const char *event_json, | ||
| 85 | size_t event_len, int event_kind, | ||
| 86 | const char *event_pubkey_hex, | ||
| 87 | uint64_t event_created_at, | ||
| 88 | sub_match_result_t *result); | ||
| 89 | |||
| 90 | uint8_t sub_manager_count(sub_manager_t *mgr, int conn_fd); | ||
| 91 | |||
| 92 | #endif | ||