ascii-chat 0.8.38
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
topology.c File Reference

Ring topology implementation. More...

Go to the source code of this file.

Data Structures

struct  consensus_topology
 Ring topology for consensus participants. More...
 

Typedefs

typedef struct consensus_topology consensus_topology_t
 Ring topology for consensus participants.
 

Functions

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)
 
void consensus_topology_destroy (consensus_topology_t *topology)
 
int consensus_topology_get_position (const consensus_topology_t *topology)
 
bool consensus_topology_am_leader (const consensus_topology_t *topology)
 
asciichat_error_t consensus_topology_get_next (const consensus_topology_t *topology, uint8_t out_id[16])
 
asciichat_error_t consensus_topology_get_prev (const consensus_topology_t *topology, uint8_t out_id[16])
 
asciichat_error_t consensus_topology_get_leader (const consensus_topology_t *topology, uint8_t out_id[16])
 
asciichat_error_t consensus_topology_get_all (const consensus_topology_t *topology, uint8_t out_ids[64][16], int *out_count)
 

Detailed Description

Ring topology implementation.

Definition in file topology.c.

Typedef Documentation

◆ consensus_topology_t

Ring topology for consensus participants.

Maintains a sorted list of participant IDs for ring-based consensus algorithms and leader election.

Function Documentation

◆ consensus_topology_am_leader()

bool consensus_topology_am_leader ( const consensus_topology_t topology)

Definition at line 76 of file topology.c.

76 {
77 if (!topology)
78 return false;
79 return topology->my_position == (topology->num_participants - 1);
80}
int my_position
My index in the sorted list.
Definition topology.c:20
int num_participants
Total number of participants.
Definition topology.c:19

References consensus_topology::my_position, and consensus_topology::num_participants.

Referenced by consensus_coordinator_process(), consensus_state_collection_complete(), and consensus_state_is_leader().

◆ consensus_topology_create()

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 at line 38 of file topology.c.

39 {
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}
Ring topology for consensus participants.
Definition topology.c:17
uint8_t participant_ids[64][16]
Participant UUIDs in sorted order.
Definition topology.c:18

References consensus_topology::my_position, consensus_topology::num_participants, and consensus_topology::participant_ids.

Referenced by session_consensus_create(), and session_consensus_set_topology().

◆ consensus_topology_destroy()

void consensus_topology_destroy ( consensus_topology_t topology)

Definition at line 64 of file topology.c.

64 {
65 if (topology) {
66 SAFE_FREE(topology);
67 }
68}

Referenced by session_consensus_create(), session_consensus_destroy(), and session_consensus_set_topology().

◆ consensus_topology_get_all()

asciichat_error_t consensus_topology_get_all ( const consensus_topology_t topology,
uint8_t  out_ids[64][16],
int *  out_count 
)

Definition at line 111 of file topology.c.

112 {
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}

References consensus_topology::num_participants, and consensus_topology::participant_ids.

◆ consensus_topology_get_leader()

asciichat_error_t consensus_topology_get_leader ( const consensus_topology_t topology,
uint8_t  out_id[16] 
)

Definition at line 102 of file topology.c.

102 {
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}

References consensus_topology::num_participants, and consensus_topology::participant_ids.

◆ consensus_topology_get_next()

asciichat_error_t consensus_topology_get_next ( const consensus_topology_t topology,
uint8_t  out_id[16] 
)

Definition at line 82 of file topology.c.

82 {
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}

References consensus_topology::my_position, consensus_topology::num_participants, and consensus_topology::participant_ids.

◆ consensus_topology_get_position()

int consensus_topology_get_position ( const consensus_topology_t topology)

Definition at line 70 of file topology.c.

70 {
71 if (!topology)
72 return -1;
73 return topology->my_position;
74}

References consensus_topology::my_position.

◆ consensus_topology_get_prev()

asciichat_error_t consensus_topology_get_prev ( const consensus_topology_t topology,
uint8_t  out_id[16] 
)

Definition at line 92 of file topology.c.

92 {
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}

References consensus_topology::my_position, consensus_topology::num_participants, and consensus_topology::participant_ids.