]> git.scottworley.com Git - tattlekey/blob - client/press.c
client: Hold presses in queues, one queue per send_count
[tattlekey] / client / press.c
1 #include "press.h"
2 #include "blink.h"
3 #include "config.h"
4
5 static uint32_t press_next_send(press_t *s) {
6 return s->timestamp + (1 << s->send_count) - 1;
7 }
8
9 static uint32_t press_queue_next_send(queue_t *q) {
10 press_t press;
11 return queue_try_peek(q, &press) ? press_next_send(&press) : UINT32_MAX;
12 }
13
14 static bool next_send_less_than(void *user_data, pheap_node_id_t a,
15 pheap_node_id_t b) {
16 queue_t **sleeps = (queue_t **)user_data;
17 return press_queue_next_send(sleeps[a]) < press_queue_next_send(sleeps[b]);
18 }
19
20 static void *xcalloc(size_t nmemb, size_t size) {
21 void *p = calloc(nmemb, size);
22 if (p == NULL)
23 signal_error_by_blinking();
24 return p;
25 }
26
27 press_pile_t *create_press_pile() {
28 press_pile_t *pp = (press_pile_t *)xcalloc(1, sizeof(press_pile_t));
29 pp->presses = (queue_t *)xcalloc(config_resend_count, sizeof(queue_t));
30 pp->sleeps = (queue_t **)xcalloc(config_resend_count, sizeof(queue_t *));
31 for (int i = 0; i < config_resend_count; i++) {
32 uint element_count = 1 << i;
33 element_count = MAX(element_count, 32);
34 element_count = MIN(element_count, 512);
35 queue_init(&pp->presses[i], sizeof(press_t), element_count);
36 }
37 pp->sleeps_heap =
38 ph_create(config_resend_count, next_send_less_than, pp->sleeps);
39 if (pp->sleeps_heap == NULL)
40 signal_error_by_blinking();
41 return pp;
42 }
43
44 void add_press(press_pile_t *pp, press_t *press) {
45 int sc = press->send_count;
46 if (sc >= config_resend_count)
47 signal_error_by_blinking();
48 bool was_empty = queue_is_empty(&pp->presses[sc]);
49 /* No error check; blithely continue if the queue was full. */
50 queue_try_add(&pp->presses[sc], press);
51 if (was_empty) {
52 pheap_node_id_t i = ph_new_node(pp->sleeps_heap);
53 pp->sleeps[i] = &pp->presses[sc];
54 ph_insert_node(pp->sleeps_heap, i);
55 }
56 }
57
58 int32_t next_scheduled_send(press_pile_t *pp) {
59 pheap_node_id_t i = ph_peek_head(pp->sleeps_heap);
60 if (i == 0)
61 return -1;
62 return press_queue_next_send(pp->sleeps[i]);
63 }
64
65 bool get_press_due_for_resend(press_pile_t *pp, uint32_t now, press_t *press) {
66 pheap_node_id_t i = ph_peek_head(pp->sleeps_heap);
67 if (i == 0 || press_queue_next_send(pp->sleeps[i]) > now)
68 return false;
69 if (!queue_try_remove(pp->sleeps[i], press))
70 signal_error_by_blinking();
71 bool became_empty = queue_is_empty(pp->sleeps[i]);
72 if (ph_remove_head(pp->sleeps_heap, became_empty) != i)
73 signal_error_by_blinking();
74 if (became_empty) {
75 pp->sleeps[i] = NULL;
76 } else {
77 ph_insert_node(pp->sleeps_heap, i);
78 }
79 return true;
80 }