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

Definition in file topology.c.

Typedef Documentation

◆ consensus_topology_t

Ring topology for consensus participants.

Opaque ring topology handle.

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)

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

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

Definition at line 82 of file topology.c.

82 {
83 if (!topology)
84 return false;
85 return topology->my_position == (topology->num_participants - 1);
86}
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 43 of file topology.c.

44 {
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}
#define SET_ERRNO(code,...)
#define SAFE_FREE(ptr)
#define SAFE_MALLOC(size, cast)
@ ASCIICHAT_OK
Definition error_codes.h:50
@ 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 70 of file topology.c.

70 {
71 if (topology) {
72 SAFE_FREE(topology);
73 }
74}

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 117 of file topology.c.

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

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 108 of file topology.c.

108 {
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}
#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 88 of file topology.c.

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

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 76 of file topology.c.

76 {
77 if (!topology)
78 return -1;
79 return topology->my_position;
80}

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 98 of file topology.c.

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

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