ascii-chat 0.8.38
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
topology.c
Go to the documentation of this file.
1
6#include <ascii-chat/network/consensus/topology.h>
7#include <ascii-chat/common.h>
8#include <ascii-chat/asciichat_errno.h>
9#include <string.h>
10
22
23// Helper: find position of UUID in array
24static int find_uuid_position(const uint8_t uuids[64][16], int count, const uint8_t target[16]) {
25 for (int i = 0; i < count; i++) {
26 if (memcmp(uuids[i], target, 16) == 0) {
27 return i;
28 }
29 }
30 return -1;
31}
32
33// Comparison function for qsort
34static int qsort_uuid_compare(const void *a, const void *b) {
35 return memcmp(a, b, 16);
36}
37
38asciichat_error_t consensus_topology_create(const uint8_t participant_ids[64][16], int num_participants,
39 const uint8_t my_id[16], consensus_topology_t **out_topology) {
40 if (!participant_ids || !my_id || !out_topology || num_participants <= 0 || num_participants > 64) {
41 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid topology creation parameters");
42 }
43
44 consensus_topology_t *topology = SAFE_MALLOC(sizeof(*topology), consensus_topology_t *);
45 memset(topology, 0, sizeof(*topology));
46
47 // Copy and sort participant IDs lexicographically
48 memcpy(topology->participant_ids, participant_ids, num_participants * 16);
49 topology->num_participants = num_participants;
50
51 qsort(topology->participant_ids, num_participants, 16, qsort_uuid_compare);
52
53 // Find my position
54 topology->my_position = find_uuid_position(topology->participant_ids, num_participants, my_id);
55 if (topology->my_position < 0) {
56 SAFE_FREE(topology);
57 return SET_ERRNO(ERROR_INVALID_PARAM, "My UUID not found in participant list");
58 }
59
60 *out_topology = topology;
61 return ASCIICHAT_OK;
62}
63
65 if (topology) {
66 SAFE_FREE(topology);
67 }
68}
69
71 if (!topology)
72 return -1;
73 return topology->my_position;
74}
75
77 if (!topology)
78 return false;
79 return topology->my_position == (topology->num_participants - 1);
80}
81
82asciichat_error_t consensus_topology_get_next(const consensus_topology_t *topology, uint8_t out_id[16]) {
83 if (!topology || !out_id) {
84 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters");
85 }
86
87 int next_pos = (topology->my_position + 1) % topology->num_participants;
88 memcpy(out_id, topology->participant_ids[next_pos], 16);
89 return ASCIICHAT_OK;
90}
91
92asciichat_error_t consensus_topology_get_prev(const consensus_topology_t *topology, uint8_t out_id[16]) {
93 if (!topology || !out_id) {
94 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters");
95 }
96
97 int prev_pos = (topology->my_position - 1 + topology->num_participants) % topology->num_participants;
98 memcpy(out_id, topology->participant_ids[prev_pos], 16);
99 return ASCIICHAT_OK;
100}
101
102asciichat_error_t consensus_topology_get_leader(const consensus_topology_t *topology, uint8_t out_id[16]) {
103 if (!topology || !out_id) {
104 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters");
105 }
106
107 memcpy(out_id, topology->participant_ids[topology->num_participants - 1], 16);
108 return ASCIICHAT_OK;
109}
110
111asciichat_error_t consensus_topology_get_all(const consensus_topology_t *topology, uint8_t out_ids[64][16],
112 int *out_count) {
113 if (!topology || !out_ids || !out_count) {
114 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters");
115 }
116
117 memcpy(out_ids, topology->participant_ids, topology->num_participants * 16);
118 *out_count = topology->num_participants;
119 return ASCIICHAT_OK;
120}
Ring topology for consensus participants.
Definition topology.c:17
uint8_t participant_ids[64][16]
Participant UUIDs in sorted order.
Definition topology.c:18
int my_position
My index in the sorted list.
Definition topology.c:20
int num_participants
Total number of participants.
Definition topology.c:19
int consensus_topology_get_position(const consensus_topology_t *topology)
Definition topology.c:70
asciichat_error_t consensus_topology_get_prev(const consensus_topology_t *topology, uint8_t out_id[16])
Definition topology.c:92
void consensus_topology_destroy(consensus_topology_t *topology)
Definition topology.c:64
asciichat_error_t consensus_topology_get_leader(const consensus_topology_t *topology, uint8_t out_id[16])
Definition topology.c:102
bool consensus_topology_am_leader(const consensus_topology_t *topology)
Definition topology.c:76
asciichat_error_t consensus_topology_get_all(const consensus_topology_t *topology, uint8_t out_ids[64][16], int *out_count)
Definition topology.c:111
asciichat_error_t consensus_topology_get_next(const consensus_topology_t *topology, uint8_t out_id[16])
Definition topology.c:82
asciichat_error_t consensus_topology_create(const uint8_t participant_ids[64][16], int num_participants, const uint8_t my_id[16], consensus_topology_t **out_topology)
Definition topology.c:38
struct consensus_topology consensus_topology_t
Ring topology for consensus participants.