Loading...
Searching...
No Matches
outbox_queue.h
Go to the documentation of this file.
1
15#pragma once
16#include <stdbool.h>
17
19
24
25// a kind is held once at most, so the queue and the failed set never need more slots than there are
26// kinds. the weather, stock, calendar, settings, and fresh reply kinds fit in the 6 every face has,
27// with one to spare. phone battery and now playing each add a slot, only on a face that compiles them
28// in, so a face with every kind can have all of them fail at once and keep each one for the retry.
29// sizing to the kinds a face has keeps every other face's outbox the size it was
30#if defined(APPMESSAGE_HAS_PHONE)
31#define OUTBOX_PHONE_SLOTS 1
32#else
33#define OUTBOX_PHONE_SLOTS 0
34#endif
35#if defined(APPMESSAGE_HAS_MUSIC)
36#define OUTBOX_MUSIC_SLOTS 1
37#else
38#define OUTBOX_MUSIC_SLOTS 0
39#endif
40
42#define OUTBOX_QUEUE_MAX (6 + OUTBOX_PHONE_SLOTS + OUTBOX_MUSIC_SLOTS)
43
58
62typedef struct
63{
66} OutboxJob;
67
81
88static inline bool outbox_busy(const OutboxQueue *outbox)
89{
90 return outbox->inflight.kind != OUTBOX_NONE;
91}
92
103static inline bool outbox_pending(const OutboxQueue *outbox, OutboxKind kind)
104{
105 if (kind == OUTBOX_NONE)
106 {
107 // the empty flight slot and every unused array entry read as OUTBOX_NONE, so without this
108 // an idle outbox would answer that nothing is already pending
109 return false;
110 }
111
112 if (outbox->inflight.kind == kind)
113 {
114 return true;
115 }
116
117 for (int i = 0; i < outbox->queue_len; i++)
118 {
119 if (outbox->queue[i].kind == kind)
120 {
121 return true;
122 }
123 }
124
125 for (int i = 0; i < outbox->failed_len; i++)
126 {
127 if (outbox->failed[i].kind == kind)
128 {
129 return true;
130 }
131 }
132
133 return false;
134}
135
144static inline bool outbox_push(OutboxQueue *outbox, OutboxKind kind, int retries)
145{
146 if (outbox_pending(outbox, kind) || outbox->queue_len >= OUTBOX_QUEUE_MAX)
147 {
148 return false;
149 }
150
151 outbox->queue[outbox->queue_len].kind = kind;
152 outbox->queue[outbox->queue_len].retries_left = retries;
153 outbox->queue_len++;
154 return true;
155}
156
162static inline void outbox_pop_front(OutboxQueue *outbox)
163{
164 for (int i = 1; i < outbox->queue_len; i++)
165 {
166 outbox->queue[i - 1] = outbox->queue[i];
167 }
168
169 if (outbox->queue_len > 0)
170 {
171 outbox->queue_len--;
172 }
173}
174
183static inline void outbox_take_head(OutboxQueue *outbox)
184{
185 if (outbox->queue_len == 0)
186 {
187 return; // outbox_clear leaves the array behind, so an empty queue still has one to read
188 }
189
190 outbox->inflight = outbox->queue[0];
191 outbox_pop_front(outbox);
192}
193
201{
202 OutboxJob settled = outbox->inflight;
203 outbox->inflight.kind = OUTBOX_NONE;
204 return settled;
205}
206
213static inline void outbox_hold_failed(OutboxQueue *outbox, OutboxJob job)
214{
215 // a failure reported once the slot is already free comes with no job, and holding it would
216 // queue a send for nothing ahead of the real requests
217 if (job.kind == OUTBOX_NONE || job.retries_left <= 0)
218 {
219 return;
220 }
221
222 if (outbox->failed_len < OUTBOX_QUEUE_MAX)
223 {
224 outbox->failed[outbox->failed_len].kind = job.kind;
225 outbox->failed[outbox->failed_len].retries_left = job.retries_left - 1;
226 outbox->failed_len++;
227 }
228}
229
236static inline int outbox_retry_pass(OutboxQueue *outbox)
237{
238 int moved = 0;
239
240 while (outbox->failed_len > 0 && outbox->queue_len < OUTBOX_QUEUE_MAX)
241 {
242 outbox->queue[outbox->queue_len++] = outbox->failed[0];
243 for (int i = 1; i < outbox->failed_len; i++)
244 {
245 outbox->failed[i - 1] = outbox->failed[i];
246 }
247 outbox->failed_len--;
248 moved++;
249 }
250
251 return moved;
252}
253
262static inline void outbox_clear(OutboxQueue *outbox)
263{
264 outbox->queue_len = 0;
265 outbox->failed_len = 0;
266}
267
Which of the phone's optional features a face has, worked out from the message keys it declares....
static void outbox_hold_failed(OutboxQueue *outbox, OutboxJob job)
Hold a nacked job for the next pass, if it still has retries.
Definition outbox_queue.h:213
static bool outbox_push(OutboxQueue *outbox, OutboxKind kind, int retries)
Add a job to the work queue unless its kind is already pending.
Definition outbox_queue.h:144
OutboxKind
What an outbox job sends.
Definition outbox_queue.h:48
static bool outbox_busy(const OutboxQueue *outbox)
Whether anything is in flight, which is what says the outbox is busy.
Definition outbox_queue.h:88
static int outbox_retry_pass(OutboxQueue *outbox)
Move the whole failed set back into the work queue for another pass.
Definition outbox_queue.h:236
static bool outbox_pending(const OutboxQueue *outbox, OutboxKind kind)
Whether this kind is already somewhere in the outbox.
Definition outbox_queue.h:103
static void outbox_clear(OutboxQueue *outbox)
Forget everything waiting, for a phone that is no longer there.
Definition outbox_queue.h:262
static void outbox_take_head(OutboxQueue *outbox)
Take the head of the queue as the job now in flight.
Definition outbox_queue.h:183
static void outbox_pop_front(OutboxQueue *outbox)
Drop the front job off the work queue, shifting the rest forward.
Definition outbox_queue.h:162
static OutboxJob outbox_release(OutboxQueue *outbox)
Mark whatever was in flight as settled, whichever way it went.
Definition outbox_queue.h:200
#define OUTBOX_QUEUE_MAX
Most jobs the work queue or the failed set holds at once.
Definition outbox_queue.h:42
@ OUTBOX_STOCK
A stock request.
Definition outbox_queue.h:53
@ OUTBOX_CALENDAR
A calendar request.
Definition outbox_queue.h:54
@ OUTBOX_FRESH
The short settings reply, only whether the watch booted with nothing saved.
Definition outbox_queue.h:52
@ OUTBOX_MUSIC
A now playing request.
Definition outbox_queue.h:56
@ OUTBOX_SETTINGS
The settings reply.
Definition outbox_queue.h:51
@ OUTBOX_NONE
The zero value, so a default-initialized job slot reads as nothing rather than a real request.
Definition outbox_queue.h:49
@ OUTBOX_WEATHER
A weather request.
Definition outbox_queue.h:50
@ OUTBOX_PHONE
A phone battery request.
Definition outbox_queue.h:55
One job waiting to go out, or already in flight.
Definition outbox_queue.h:63
OutboxKind kind
What this job sends.
Definition outbox_queue.h:64
int retries_left
Retry passes left after a failed send.
Definition outbox_queue.h:65
Everything the outbox is holding: what is waiting, what nacked, and what is out.
Definition outbox_queue.h:74
OutboxJob queue[OUTBOX_QUEUE_MAX]
This pass's work queue, oldest first.
Definition outbox_queue.h:75
OutboxJob inflight
The job in flight, OUTBOX_NONE when nothing is out.
Definition outbox_queue.h:79
int failed_len
How many jobs are in the failed set.
Definition outbox_queue.h:78
OutboxJob failed[OUTBOX_QUEUE_MAX]
Requests that nacked this pass, held for the next.
Definition outbox_queue.h:77
int queue_len
How many jobs are in the queue.
Definition outbox_queue.h:76