ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
topology.h File Reference

Ring topology management. More...

Go to the source code of this file.

Functions

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.
void consensus_topology_destroy (consensus_topology_t *topology)
 Destroy topology.
int consensus_topology_get_position (const consensus_topology_t *topology)
 Get my position in ring (0 = first).
bool consensus_topology_am_leader (const consensus_topology_t *topology)
 Check if I am the ring leader (last position).
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.
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.
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).
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.

Detailed Description

Ring topology management.

Definition in file topology.h.

Function Documentation

◆ consensus_topology_am_leader()

bool consensus_topology_am_leader ( const consensus_topology_t * topology)

Check if I am the ring leader (last position).

Parameters
topologyTopology handle
Returns
true if I am the leader, false otherwise

Definition at line 85 of file topology.c.

85 {
86 if (!topology)
87 return false;
88 return topology->my_position == (topology->num_participants - 1);
89}
int my_position
My index in the sorted list.
Definition topology.c:25
int num_participants
Total number of participants.
Definition topology.c:24

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[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.

Sorts participants lexicographically and computes ring positions. Returns error if my_id not in participant list.

Parameters
participant_idsArray of 16-byte UUIDs (up to 128 participants)
num_participantsNumber of participants (1-128)
my_idMy 16-byte UUID (must be in participant_ids)
out_topologyOutput topology handle (caller must destroy)
Returns
ASCIICHAT_OK on success, error code otherwise

Definition at line 44 of file topology.c.

46 {
47 if (!participant_ids || !my_id || !out_topology || num_participants <= 0 ||
48 num_participants > MAX_SESSION_PARTICIPANTS) {
49 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid topology creation parameters");
50 }
51
52 consensus_topology_t *topology = SAFE_MALLOC(sizeof(*topology), consensus_topology_t *);
53 defer_named(release_topology, SAFE_FREE(topology));
54 memset(topology, 0, sizeof(*topology));
55
56 // Copy and sort participant IDs lexicographically
57 memcpy(topology->participant_ids, participant_ids, num_participants * 16);
58 topology->num_participants = num_participants;
59
60 qsort(topology->participant_ids, num_participants, 16, qsort_uuid_compare);
61
62 // Find my position
63 topology->my_position = find_uuid_position(topology->participant_ids, num_participants, my_id);
64 if (topology->my_position < 0) {
65 return SET_ERRNO(ERROR_INVALID_PARAM, "My UUID not found in participant list");
66 }
67
68 *out_topology = topology;
69 defer_cancel(release_topology);
70 return ASCIICHAT_OK;
71}
#define SET_ERRNO(code,...)
#define SAFE_FREE(ptr)
Definition common.h:377
#define SAFE_MALLOC(size, cast)
Definition common.h:265
@ ASCIICHAT_OK
Definition error_codes.h:46
@ ERROR_INVALID_PARAM
#define MAX_SESSION_PARTICIPANTS
uint8_t participant_ids[MAX_SESSION_PARTICIPANTS][SESSION_ID_SIZE]
Participant UUIDs in sorted order.
Definition topology.c:23
struct consensus_topology consensus_topology_t
Ring topology for consensus participants.

References ASCIICHAT_OK, ERROR_INVALID_PARAM, MAX_SESSION_PARTICIPANTS, consensus_topology::my_position, consensus_topology::num_participants, consensus_topology::participant_ids, SAFE_FREE, SAFE_MALLOC, SESSION_ID_SIZE, and SET_ERRNO.

Referenced by session_consensus_create(), and session_consensus_set_topology().

◆ consensus_topology_destroy()

void consensus_topology_destroy ( consensus_topology_t * topology)

Destroy topology.

Parameters
topologyTopology handle (safe to pass NULL)

Definition at line 73 of file topology.c.

73 {
74 if (topology) {
75 SAFE_FREE(topology);
76 }
77}

References SAFE_FREE.

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[MAX_SESSION_PARTICIPANTS][SESSION_ID_SIZE],
int * out_count )

Get all participant IDs in ring order.

Parameters
topologyTopology handle
out_idsArray to fill with 16-byte UUIDs (must hold 64*16 bytes)
out_countOutput number of participants
Returns
ASCIICHAT_OK on success, error code otherwise

Definition at line 120 of file topology.c.

122 {
123 if (!topology || !out_ids || !out_count) {
124 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters");
125 }
126
127 memcpy(out_ids, topology->participant_ids, topology->num_participants * 16);
128 *out_count = topology->num_participants;
129 return ASCIICHAT_OK;
130}

References ASCIICHAT_OK, ERROR_INVALID_PARAM, MAX_SESSION_PARTICIPANTS, consensus_topology::num_participants, consensus_topology::participant_ids, SESSION_ID_SIZE, and SET_ERRNO.

◆ consensus_topology_get_leader()

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).

Parameters
topologyTopology handle
out_idOutput 16-byte UUID of ring leader
Returns
ASCIICHAT_OK on success, error code otherwise

Definition at line 111 of file topology.c.

111 {
112 if (!topology || !out_id) {
113 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters");
114 }
115
116 memcpy(out_id, topology->participant_ids[topology->num_participants - 1], SESSION_ID_SIZE);
117 return ASCIICHAT_OK;
118}
#define SESSION_ID_SIZE

References ASCIICHAT_OK, ERROR_INVALID_PARAM, consensus_topology::num_participants, consensus_topology::participant_ids, SESSION_ID_SIZE, and SET_ERRNO.

◆ consensus_topology_get_next()

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.

Wraps around from last position to first.

Parameters
topologyTopology handle
out_idOutput 16-byte UUID of next participant
Returns
ASCIICHAT_OK on success, error code otherwise

Definition at line 91 of file topology.c.

91 {
92 if (!topology || !out_id) {
93 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters");
94 }
95
96 int next_pos = (topology->my_position + 1) % topology->num_participants;
97 memcpy(out_id, topology->participant_ids[next_pos], SESSION_ID_SIZE);
98 return ASCIICHAT_OK;
99}

References ASCIICHAT_OK, ERROR_INVALID_PARAM, consensus_topology::my_position, consensus_topology::num_participants, consensus_topology::participant_ids, SESSION_ID_SIZE, and SET_ERRNO.

◆ consensus_topology_get_position()

int consensus_topology_get_position ( const consensus_topology_t * topology)

Get my position in ring (0 = first).

Parameters
topologyTopology handle
Returns
My position in ring order, or -1 if invalid

Definition at line 79 of file topology.c.

79 {
80 if (!topology)
81 return -1;
82 return topology->my_position;
83}

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[SESSION_ID_SIZE] )

Get previous participant ID in ring.

Wraps around from first position to last.

Parameters
topologyTopology handle
out_idOutput 16-byte UUID of previous participant
Returns
ASCIICHAT_OK on success, error code otherwise

Definition at line 101 of file topology.c.

101 {
102 if (!topology || !out_id) {
103 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters");
104 }
105
106 int prev_pos = (topology->my_position - 1 + topology->num_participants) % topology->num_participants;
107 memcpy(out_id, topology->participant_ids[prev_pos], SESSION_ID_SIZE);
108 return ASCIICHAT_OK;
109}

References ASCIICHAT_OK, ERROR_INVALID_PARAM, consensus_topology::my_position, consensus_topology::num_participants, consensus_topology::participant_ids, SESSION_ID_SIZE, and SET_ERRNO.