The video discusses implementing a generic Notification Chain system with two core functionalities: Subscription and Notification Invocation.
-
Purpose: Allows a subscriber to register with the notification chain for event notifications.
-
How it works:
- The subscriber fills in the parameters for a notification chain element.
- Calls
NFC_register_notification_chain()to register its element.
-
Function Signature:
void NFC_register_notification_chain(notif_chain_t *nfc, notif_chain_elem_t *nfce);
-
Arguments:
- First: Pointer to Notification Chain (
notif_chain_t *nfc) - Second: Notification Chain Element (
notif_chain_elem_t *nfce)
- First: Pointer to Notification Chain (
-
Implementation Steps:
- Create a copy of the passed-in
notif_chain_elem_t. - Insert the copy into the linked list represented by
notif_chain_t.
- Create a copy of the passed-in
-
Purpose: Triggered by the publisher to notify the subscribers.
-
How it works:
- Publisher specifies the chain to invoke and data to send.
- Calls
NFC_invoke_notification_chain().
-
Function Signature:
void NFC_invoke_notification_chain(notif_chain_t *nfc, void *arg, size_t arg_size, char *key, size_t key_size);
-
Arguments:
- First: Pointer to Notification Chain
- Second and Third: Data publisher wants to send (arg, arg_size)
- Fourth and Fifth: Key (key, key_size)
-
Implementation Steps:
- Iterate over the linked list.
- For each element, compare the key against the provided key.
- If a match or wildcard is found, invoke the callback.
- If the key is not set in the chain element, it is considered a "wildcard" entry.
- A wildcard subscriber will receive notifications for any key.
- The current setup is designed as a generic notification chain and can be extended.
- For example, adding a function to "unsubscribe" from the notification chain, although not implemented in the video.
A 'Subscription Request' is an action where a subscriber registers itself to a notification chain for receiving event notifications. This is done via the function NFC_register_notification_chain().
Upon registration, the subscriber's details are encapsulated in a notif_chain_elem_t. This element is then added to the linked list of the notification chain (notif_chain_t).
The 'Invoke Request' is triggered by the publisher to notify all subscribers in a notification chain. The function responsible for this is NFC_invoke_notification_chain().
The publisher specifies the chain to invoke and the data to send as arguments to the function. The function then iterates through the chain, matching keys and invoking callbacks for matching or wildcard entries.
Wildcard subscriptions are entries in the notification chain that don't specify a key. These entries will receive notifications for any key.
One way to extend it would be to add a function to allow subscribers to unsubscribe from the notification chain. Another way might be to introduce priority levels for the subscribers.
This should help you revise and prepare for your interviews! Good luck! 🍀