Skip to main content
Version: Dev (main)

session_lifecycle.c File

A local app that follows its session through a hub's new session, a join to another hub, and a reset that requires onboarding again. More...

Included Headers​

#include "common.h" #include <qar_streaming.h>

Classes Index​

structLifecycleMailbox

[lifecycle_callback] More...

Typedefs Index​

typedefstruct LifecycleMailbox LifecycleMailbox

[lifecycle_callback] More...

Functions Index​

static const char *cause_name (QarSessionChangeCause cause)
static boolget_or_create_owned_objects (QarSession *session)

Get or create what this app owns in the session: here one app volume. Idempotent: the same id every time; an existing volume is taken over and carried into the hub's next session, a missing one is created. Called on every start, on every new session id and after onboarding again. More...

intmain (int argc, char **argv)
static voidon_lifecycle (const QarSessionLifecycleInfo *info, void *user_state)
static voidprint_session_id (const char *label, const QarSessionId *id)
static boolstart_session (QarRuntime *runtime, LifecycleMailbox *mailbox, QarSession **out_session, QarCancelToken **out_token, QarSessionId *out_seen_id)

[lifecycle_subscribe] More...

static const char *state_name (QarSessionLifecycleState state)

[lifecycle_callback] More...

static voidstop_session (QarSession *session, QarCancelToken *token, LifecycleMailbox *mailbox)

End the subscription before the session: the token waits for a running callback, so no callback touches the mailbox afterwards. A state the old session delivered since main last looked is dropped with it. More...

static QarCancelToken *subscribe_lifecycle (QarSession *session, LifecycleMailbox *mailbox)

[lifecycle_subscribe] More...

Macro Definitions Index​

#defineAPP_TAG   "main.session-lifecycle.examples.quaternar"

Description​

A local app that follows its session through a hub's new session, a join to another hub, and a reset that requires onboarding again.

Typedefs​

LifecycleMailbox​

typedef struct LifecycleMailbox LifecycleMailbox

[lifecycle_callback]

The newest lifecycle state, handed from the library thread to main.

Definition at line 94 of file session_lifecycle.c.

Functions​

cause_name()​

const char * cause_name (QarSessionChangeCause cause)
static

Definition at line 128 of file session_lifecycle.c.

get_or_create_owned_objects()​

bool get_or_create_owned_objects (QarSession * session)
static

Get or create what this app owns in the session: here one app volume. Idempotent: the same id every time; an existing volume is taken over and carried into the hub's next session, a missing one is created. Called on every start, on every new session id and after onboarding again.

Definition at line 160 of file session_lifecycle.c.

main()​

int main (int argc, char ** argv)

Definition at line 258 of file session_lifecycle.c.

on_lifecycle()​

void on_lifecycle (const QarSessionLifecycleInfo * info, void * user_state)
static

Definition at line 97 of file session_lifecycle.c.

print_session_id()​

void print_session_id (const char * label, const QarSessionId * id)
static

Definition at line 142 of file session_lifecycle.c.

start_session()​

bool start_session (QarRuntime * runtime, LifecycleMailbox * mailbox, QarSession ** out_session, QarCancelToken ** out_token, QarSessionId * out_seen_id)
static

[lifecycle_subscribe]

Join as a local app, create what the app owns and subscribe. Returns false, with nothing left open, on failure.

Definition at line 205 of file session_lifecycle.c.

state_name()​

const char * state_name (QarSessionLifecycleState state)
static

[lifecycle_callback]

Definition at line 112 of file session_lifecycle.c.

stop_session()​

void stop_session (QarSession * session, QarCancelToken * token, LifecycleMailbox * mailbox)
static

End the subscription before the session: the token waits for a running callback, so no callback touches the mailbox afterwards. A state the old session delivered since main last looked is dropped with it.

Definition at line 250 of file session_lifecycle.c.

subscribe_lifecycle()​

QarCancelToken * subscribe_lifecycle (QarSession * session, LifecycleMailbox * mailbox)
static

[lifecycle_subscribe]

Subscribe to the session's lifecycle. Returns the token owning the subscription, or NULL on failure.

Definition at line 182 of file session_lifecycle.c.

Macro Definitions​

APP_TAG​

#define APP_TAG   "main.session-lifecycle.examples.quaternar"

Definition at line 84 of file session_lifecycle.c.

File Listing​

The file content with the documentation metadata removed is:

1/** \file session_lifecycle.c
2 * \brief A local app that follows its session through a hub's new session,
3 * a join to another hub, and a reset that requires onboarding again.
4 * \example session_lifecycle.c
5 */
6
7/** \page qar_c_tutorial_session_lifecycle Session Lifecycle
8 * \tableofcontents
9 *
10 * \section session_lifecycle_intro What You Will Learn
11 * - Read the session's lifecycle state with
12 * qar_session_get_lifecycle_state()
13 * - Subscribe to its changes with qar_session_subscribe_lifecycle(), keep the
14 * callback short and react on the app's own thread
15 * - Notice a move into a new session by a change of the session id, not by
16 * one particular state, and get or create what the app owns again
17 * - Recover from QAR_SESSION_LIFECYCLE_STATE_RE_PAIR_REQUIRED: destroy the
18 * session, onboard again, recreate what the app owns
19 *
20 * \section session_lifecycle_when When It Matters
21 * A session follows its hub by itself. When the hub's operator starts a new
22 * session, or the hub joins another hub as its member, the same QarSession
23 * moves into the new session and keeps everything the app created or took
24 * over with a get_or_create call: its app volumes, GUI panels and rooms are
25 * proposed again, senders renegotiate and frames pause for a few seconds.
26 * Objects of other peers disappear until their owners have moved too. Only
27 * the session id changes.
28 *
29 * On each new session id the app still calls get_or_create again for every
30 * common name it owns. The calls are idempotent: each returns the same id,
31 * takes the object over when it is there (a previous run of the app may have
32 * left it) and re-creates it when it is missing. After onboarding again they
33 * are the only way the app's objects come back.
34 *
35 * A hub reset to a new install, a revoked approval, or a peer that missed too
36 * many rotations ends the session for good: RE_PAIR_REQUIRED. Nothing is
37 * retried, and the app must onboard again.
38 *
39 * \section session_lifecycle_run Build and Run
40 * \code{.bash}
41 * cmake --build --preset x64-windows-debug --target session_lifecycle
42 * ./build/x64-windows/Debug/session_lifecycle.exe [run-seconds]
43 * \endcode
44 * While it runs (default 300 s), start a new session in the QAROS Visualizer
45 * to see a move, or revoke the app's approval to see RE_PAIR_REQUIRED and the
46 * new approval prompt.
47 *
48 * \section session_lifecycle_callback Step 1: A Short Callback
49 * The callback runs on a library thread. It copies the state into a mailbox
50 * and returns; `info` and its `reason` are valid only during the call. Never
51 * destroy the session or the library from it.
52 * \snippet session_lifecycle.c lifecycle_callback
53 *
54 * \section session_lifecycle_subscribe Step 2: Subscribe
55 * The first callback carries the current state. The subscription belongs to
56 * its cancel token: destroying the token ends it and waits for a running
57 * callback.
58 * \snippet session_lifecycle.c lifecycle_subscribe
59 *
60 * \section session_lifecycle_react Step 3: React on the App's Thread
61 * - A move reports MIGRATING with the old id, then CONNECTED with the new id,
62 * the previous id and a cause. A move whose new session answers late reports
63 * HUB_UNREACHABLE with the new id first, and a callback that falls behind
64 * sees only the newest state. Compare the session id with the last one seen.
65 * - The cause (ROTATED, JOINED_PRIMARY) is for logs only: the move is the
66 * same, and a join can be reported as ROTATED.
67 * - HUB_UNREACHABLE alone needs nothing: the session keeps trying.
68 * - On a new session id, get or create what the app owns again.
69 * \snippet session_lifecycle.c lifecycle_react
70 *
71 * \section session_lifecycle_repair Step 4: Onboard Again After RE_PAIR_REQUIRED
72 * \snippet session_lifecycle.c lifecycle_repair
73 */
74
75#include "common.h"
76
77#ifndef QAR_ENABLE_DYNAMIC_LOADING
78#define QAR_ENABLE_DYNAMIC_LOADING
79#endif
80#include <qar_streaming.h>
81
83
84#define APP_TAG "main.session-lifecycle.examples.quaternar"
85
86//! [lifecycle_callback]
87/** \brief The newest lifecycle state, handed from the library thread to main. */
88typedef struct LifecycleMailbox
89{
90 CRITICAL_SECTION lock;
91 bool has_news;
92 QarSessionLifecycleInfo latest; /* `reason` is not kept: see below */
93 char reason[256];
95
96static void
97on_lifecycle(const QarSessionLifecycleInfo* info, void* user_state)
98{
99 LifecycleMailbox* mailbox = (LifecycleMailbox*)user_state;
100 EnterCriticalSection(&mailbox->lock);
101 mailbox->latest = *info;
102 mailbox->latest.reason = NULL; /* valid only during this callback */
103 snprintf(
104 mailbox->reason, sizeof(mailbox->reason), "%s", info->reason != NULL ? info->reason : ""
105 );
106 mailbox->has_news = true;
107 LeaveCriticalSection(&mailbox->lock);
108}
109//! [lifecycle_callback]
110
111static const char*
113{
114 switch(state)
115 {
117 return "CONNECTED";
119 return "MIGRATING";
121 return "RE_PAIR_REQUIRED";
122 default: /* a value this header does not name reads as unreachable */
123 return "HUB_UNREACHABLE";
124 }
125}
126
127static const char*
129{
130 switch(cause)
131 {
133 return "none";
135 return "the hub joined another hub";
136 default: /* a value this header does not name reads as ROTATED */
137 return "the hub started a new session";
138 }
139}
140
141static void
142print_session_id(const char* label, const QarSessionId* id)
143{
144 char text[64] = { 0 };
145 QarResult formatted = qar_uuid_to_string(id->data, text, sizeof(text));
146 if(qar_result_is_error(formatted))
147 {
148 log_result("qar_uuid_to_string", formatted);
149 return;
150 }
151 printf("%s %s\n", label, text);
152}
153
154/** \brief Get or create what this app owns in the session: here one app
155 * volume. Idempotent: the same id every time; an existing volume is taken
156 * over and carried into the hub's next session, a missing one is created.
157 * Called on every start, on every new session id and after onboarding
158 * again. */
159static bool
161{
163 volume_init.common_name = "lifecycle-volume.examples.qaros";
164 volume_init.display_name = "Lifecycle Tutorial Volume";
165 volume_init.pose = qar_pose_default();
166 volume_init.pose.position.z = -1.0f;
167 volume_init.size = qar_app_volume_size_default();
169 QarResult created = qar_app_volumes_get_or_create(session, &volume_init, &volume_id);
170 if(qar_result_is_error(created))
171 {
172 log_result("qar_app_volumes_get_or_create", created);
173 return false;
174 }
175 return true;
176}
177
178//! [lifecycle_subscribe]
179/** \brief Subscribe to the session's lifecycle. Returns the token owning the
180 * subscription, or NULL on failure. */
181static QarCancelToken*
183{
184 QarCancelToken* token = NULL;
185 QarResult created = qar_cancel_token_create(&token);
186 if(qar_result_is_error(created) || token == NULL)
187 {
188 log_result("qar_cancel_token_create", created);
189 return NULL;
190 }
191 QarResult subscribed = qar_session_subscribe_lifecycle(session, on_lifecycle, mailbox, token);
192 if(qar_result_is_error(subscribed))
193 {
194 log_result("qar_session_subscribe_lifecycle", subscribed);
196 return NULL;
197 }
198 return token;
199}
200//! [lifecycle_subscribe]
201
202/** \brief Join as a local app, create what the app owns and subscribe.
203 * Returns false, with nothing left open, on failure. */
204static bool
206 QarRuntime* runtime,
207 LifecycleMailbox* mailbox,
208 QarSession** out_session,
209 QarCancelToken** out_token,
210 QarSessionId* out_seen_id
211)
212{
213 *out_session = NULL;
214 *out_token = NULL;
215 if(example_join_as_local_app(runtime, "Session Lifecycle Example", APP_TAG, out_session) != 0
216 || *out_session == NULL)
217 {
218 return false;
219 }
220 /* The state now, without a subscription: the id to compare against. */
222 QarResult got_state = qar_session_get_lifecycle_state(*out_session, &now);
223 if(qar_result_is_error(got_state))
224 {
225 log_result("qar_session_get_lifecycle_state", got_state);
226 }
227 else
228 {
229 printf("State %s\n", state_name(now.state));
230 print_session_id("In session", &now.session_id);
231 *out_seen_id = now.session_id;
232 }
233 if(qar_result_is_success(got_state) && get_or_create_owned_objects(*out_session))
234 {
235 *out_token = subscribe_lifecycle(*out_session, mailbox);
236 }
237 if(*out_token == NULL)
238 {
239 qar_session_handle_destroy(*out_session);
240 *out_session = NULL;
241 return false;
242 }
243 return true;
244}
245
246/** \brief End the subscription before the session: the token waits for a
247 * running callback, so no callback touches the mailbox afterwards. A state
248 * the old session delivered since main last looked is dropped with it. */
249static void
251{
254 mailbox->has_news = false;
255}
256
257int
258main(int argc, char** argv)
259{
260 const int requested_seconds = argc > 1 ? atoi(argv[1]) : 0;
261 const int run_seconds = requested_seconds > 0 ? requested_seconds : 300;
262
263 if(!qar_library_load(NULL)) /* NULL: the default lookup, normally the installed QAROS */
264 {
265 fprintf(
266 stderr,
267 "Could not load the QAROS runtime (reason above): install QAROS, or fix "
268 "QAR_INSTANCE.\n"
269 );
270 return 2;
271 }
273 QarResult library_result = qar_library_init(&library_init);
274 if(qar_result_is_error(library_result))
275 {
276 log_result("qar_library_init", library_result);
278 return 3;
279 }
280 QarRuntime* runtime = NULL;
282 QarResult runtime_result = qar_runtime_create(&runtime_init, &runtime);
283 if(qar_result_is_error(runtime_result) || runtime == NULL)
284 {
285 log_result("qar_runtime_create", runtime_result);
286 log_result("qar_library_destroy", qar_library_destroy());
288 return 4;
289 }
290
291 LifecycleMailbox mailbox;
292 InitializeCriticalSection(&mailbox.lock);
293 mailbox.has_news = false;
295 mailbox.reason[0] = '\0';
296
297 QarSession* session = NULL;
298 QarCancelToken* token = NULL;
299 QarSessionId seen_id;
300 memset(&seen_id, 0, sizeof(seen_id));
301 int exit_code = 0;
302 int joins_again = 0;
303 if(!start_session(runtime, &mailbox, &session, &token, &seen_id))
304 {
305 exit_code = 5;
306 }
307
308 const ULONGLONG deadline = GetTickCount64() + (ULONGLONG)run_seconds * 1000;
309 while(session != NULL && GetTickCount64() < deadline)
310 {
311 Sleep(200);
312 //! [lifecycle_react]
313 EnterCriticalSection(&mailbox.lock);
314 const bool has_news = mailbox.has_news;
315 const QarSessionLifecycleInfo info = mailbox.latest;
316 char reason[sizeof(mailbox.reason)];
317 memcpy(reason, mailbox.reason, sizeof(reason));
318 mailbox.has_news = false;
319 LeaveCriticalSection(&mailbox.lock);
320 if(!has_news)
321 {
322 continue;
323 }
324 printf("State %s%s%s\n", state_name(info.state), reason[0] != '\0' ? ": " : "", reason);
325 if(memcmp(info.session_id.data, seen_id.data, sizeof(seen_id.data)) != 0)
326 {
327 /* A new session: re-read whatever the app keyed by the session id,
328 * and get or create what the app owns again (the same ids; a
329 * missing object comes back). Handles and senders are still there. */
330 seen_id = info.session_id;
331 print_session_id("Now in session", &info.session_id);
333 {
334 print_session_id(" moved from", &info.previous_session_id);
335 printf(" because %s\n", cause_name(info.cause));
336 }
338 && !get_or_create_owned_objects(session))
339 {
340 printf("Could not get or create the app's volume in the new session.\n");
341 }
342 }
343 //! [lifecycle_react]
344
345 //! [lifecycle_repair]
347 {
348 /* Terminal: nothing is retried. Leave the old session, join again
349 * (the user may be asked to approve the app again) and recreate
350 * what the app owns. Here, on the app's thread, never in the
351 * callback. A hub that keeps refusing is not asked forever. */
352 printf("The hub no longer admits this app. Joining again ...\n");
353 stop_session(session, token, &mailbox);
354 session = NULL;
355 if(++joins_again > 3)
356 {
357 printf("Still refused after 3 joins; giving up.\n");
358 exit_code = 6;
359 }
360 else if(!start_session(runtime, &mailbox, &session, &token, &seen_id))
361 {
362 exit_code = 6;
363 }
364 }
365 //! [lifecycle_repair]
366 }
367
368 if(session != NULL)
369 {
370 stop_session(session, token, &mailbox);
371 }
372 DeleteCriticalSection(&mailbox.lock);
374 log_result("qar_library_destroy", qar_library_destroy());
376 return exit_code;
377}

Generated via doxygen2docusaurus 2.2.1 by Doxygen 1.9.8.