ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
topology.c
Go to the documentation of this file.
3#include <ascii-chat/common.h>
4#include <recast/defer.h>
9
11#include <ascii-chat/common.h>
13#include <string.h>
14
27
28// Helper: find position of UUID in array
29static int find_uuid_position(const uint8_t uuids[MAX_SESSION_PARTICIPANTS][SESSION_ID_SIZE], int count, const uint8_t target[SESSION_ID_SIZE]) {
30 for (int i = 0; i < count; i++) {
31 if (memcmp(uuids[i], target, SESSION_ID_SIZE) == 0) {
32 return i;
33 }
34 }
35 return -1;
36}
37
38// Comparison function for qsort
39static int qsort_uuid_compare(const void *a, const void *b) {
40 return memcmp(a, b, SESSION_ID_SIZE);
41}
42
44 consensus_topology_t **out_topology) {
45 if (!participant_ids || !my_id || !out_topology || num_participants <= 0 || num_participants > MAX_SESSION_PARTICIPANTS) {
46 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid topology creation parameters");
47 }
48
49 consensus_topology_t *topology = SAFE_MALLOC(sizeof(*topology), consensus_topology_t *);
50 defer_named(release_topology, SAFE_FREE(topology));
51 memset(topology, 0, sizeof(*topology));
52
53 // Copy and sort participant IDs lexicographically
54 memcpy(topology->participant_ids, participant_ids, num_participants * 16);
55 topology->num_participants = num_participants;
56
57 qsort(topology->participant_ids, num_participants, 16, qsort_uuid_compare);
58
59 // Find my position
60 topology->my_position = find_uuid_position(topology->participant_ids, num_participants, my_id);
61 if (topology->my_position < 0) {
62 return SET_ERRNO(ERROR_INVALID_PARAM, "My UUID not found in participant list");
63 }
64
65 *out_topology = topology;
66 defer_cancel(release_topology);
67 return ASCIICHAT_OK;
68}
69
71 if (topology) {
72 SAFE_FREE(topology);
73 }
74}
75
77 if (!topology)
78 return -1;
79 return topology->my_position;
80}
81
83 if (!topology)
84 return false;
85 return topology->my_position == (topology->num_participants - 1);
86}
87
89 if (!topology || !out_id) {
90 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters");
91 }
92
93 int next_pos = (topology->my_position + 1) % topology->num_participants;
94 memcpy(out_id, topology->participant_ids[next_pos], SESSION_ID_SIZE);
95 return ASCIICHAT_OK;
96}
97
99 if (!topology || !out_id) {
100 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters");
101 }
102
103 int prev_pos = (topology->my_position - 1 + topology->num_participants) % topology->num_participants;
104 memcpy(out_id, topology->participant_ids[prev_pos], SESSION_ID_SIZE);
105 return ASCIICHAT_OK;
106}
107
109 if (!topology || !out_id) {
110 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters");
111 }
112
113 memcpy(out_id, topology->participant_ids[topology->num_participants - 1], SESSION_ID_SIZE);
114 return ASCIICHAT_OK;
115}
116
118 if (!topology || !out_ids || !out_count) {
119 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters");
120 }
121
122 memcpy(out_ids, topology->participant_ids, topology->num_participants * 16);
123 *out_count = topology->num_participants;
124 return ASCIICHAT_OK;
125}
Synchronized per-thread pending error stacks and value snapshots. SET_ERRNO pushes,...
#define SET_ERRNO(code,...)
Application limits and constraints.
#define SAFE_FREE(ptr)
#define SAFE_MALLOC(size, cast)
unsigned char uint8_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
#define MAX_SESSION_PARTICIPANTS
#define SESSION_ID_SIZE
⚙️ Common definitions, error codes, macros, and types shared throughout the application
🔤 String Manipulation and Shell Escaping Utilities
Ring topology for consensus participants.
Definition topology.c:22
uint8_t participant_ids[MAX_SESSION_PARTICIPANTS][SESSION_ID_SIZE]
Participant UUIDs in sorted order.
Definition topology.c:23
int my_position
My index in the sorted list.
Definition topology.c:25
int num_participants
Total number of participants.
Definition topology.c:24
int consensus_topology_get_position(const consensus_topology_t *topology)
Get my position in ring (0 = first).
Definition topology.c:76
asciichat_error_t consensus_topology_get_next(const consensus_topology_t *topology, uint8_t out_id[SESSION_ID_SIZE])
Get next participant ID in ring.
Definition topology.c:88
asciichat_error_t consensus_topology_get_prev(const consensus_topology_t *topology, uint8_t out_id[SESSION_ID_SIZE])
Get previous participant ID in ring.
Definition topology.c:98
asciichat_error_t consensus_topology_get_all(const consensus_topology_t *topology, uint8_t out_ids[MAX_SESSION_PARTICIPANTS][SESSION_ID_SIZE], int *out_count)
Get all participant IDs in ring order.
Definition topology.c:117
void consensus_topology_destroy(consensus_topology_t *topology)
Destroy topology.
Definition topology.c:70
asciichat_error_t consensus_topology_create(const uint8_t participant_ids[MAX_SESSION_PARTICIPANTS][SESSION_ID_SIZE], int num_participants, const uint8_t my_id[SESSION_ID_SIZE], consensus_topology_t **out_topology)
Create new ring topology from participant list.
Definition topology.c:43
bool consensus_topology_am_leader(const consensus_topology_t *topology)
Check if I am the ring leader (last position).
Definition topology.c:82
struct consensus_topology consensus_topology_t
Ring topology for consensus participants.
asciichat_error_t consensus_topology_get_leader(const consensus_topology_t *topology, uint8_t out_id[SESSION_ID_SIZE])
Get ring leader ID (last participant).
Definition topology.c:108
Ring topology management.