ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
state.c
Go to the documentation of this file.
1
5
10#include <ascii-chat/common.h>
13#include <string.h>
14
25
42
44 if (!my_id || !topology || !out_state) {
45 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid state creation parameters");
46 }
47
48 consensus_state_t *state = SAFE_MALLOC(sizeof(*state), consensus_state_t *);
49 memset(state, 0, sizeof(*state));
50
52 state->topology = (consensus_topology_t *)topology; /* Cast away const for storage */
53 memcpy(state->my_id, my_id, SESSION_ID_SIZE);
54
55 /* Initialize metrics array with initial capacity of 10 */
56 state->metrics_capacity = 10;
57 state->metrics_count = 0;
58 state->metrics = SAFE_MALLOC(state->metrics_capacity * sizeof(participant_metrics_t), participant_metrics_t *);
59
60 memset(&state->election_result, 0, sizeof(election_result_t));
61
62 *out_state = state;
63 return ASCIICHAT_OK;
64}
65
67 if (state) {
68 if (state->metrics) {
69 SAFE_FREE(state->metrics);
70 }
71 SAFE_FREE(state);
72 }
73}
74
76 if (!state) {
77 return SET_ERRNO(ERROR_INVALID_PARAM, "State is NULL");
78 }
79
80 if (state->current_state != CONSENSUS_STATE_IDLE) {
81 return SET_ERRNO(ERROR_INVALID_STATE, "Invalid transition from state %d", state->current_state);
82 }
83
84 /* Reset metrics from previous round */
85 state->metrics_count = 0;
86 memset(&state->election_result, 0, sizeof(election_result_t));
87
89 return ASCIICHAT_OK;
90}
91
92asciichat_error_t consensus_state_add_metrics(consensus_state_t *state, const participant_metrics_t *metrics) {
93 if (!state || !metrics) {
94 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters");
95 }
96
98 return SET_ERRNO(ERROR_INVALID_STATE, "Invalid state for adding metrics: %d", state->current_state);
99 }
100
101 /* Grow array if needed */
102 if (state->metrics_count >= state->metrics_capacity) {
103 int new_capacity = state->metrics_capacity * 2;
104 participant_metrics_t *new_metrics = SAFE_REALLOC(state->metrics, new_capacity * sizeof(participant_metrics_t), participant_metrics_t *);
105 state->metrics = new_metrics;
106 state->metrics_capacity = new_capacity;
107 }
108
109 /* Copy metrics into array */
110 memcpy(&state->metrics[state->metrics_count], metrics, sizeof(participant_metrics_t));
111 state->metrics_count++;
112
113 return ASCIICHAT_OK;
114}
115
117 if (!state) {
118 return SET_ERRNO(ERROR_INVALID_PARAM, "State is NULL");
119 }
120
122 return SET_ERRNO(ERROR_INVALID_STATE, "Invalid transition from state %d", state->current_state);
123 }
124
125 /* Transition: if we're leader, go to ELECTION_START; otherwise go to IDLE */
128 } else {
130 }
131
132 return ASCIICHAT_OK;
133}
134
136 if (!state) {
137 return SET_ERRNO(ERROR_INVALID_PARAM, "State is NULL");
138 }
139
141 return SET_ERRNO(ERROR_INVALID_STATE, "Invalid transition from state %d", state->current_state);
142 }
143
144 int best_idx = -1, backup_idx = -1;
145 asciichat_error_t result = consensus_election_choose_hosts(state->metrics, state->metrics_count, &best_idx, &backup_idx);
146 if (result != ASCIICHAT_OK)
147 return result;
148 return consensus_state_store_election(state, best_idx, backup_idx);
149}
150
152 if (!state || state->current_state != CONSENSUS_STATE_ELECTION_START || best_idx < 0 || best_idx >= state->metrics_count || backup_idx < -1 || backup_idx >= state->metrics_count ||
153 backup_idx == best_idx)
154 return SET_ERRNO(ERROR_INVALID_STATE, "Invalid election result");
155 memcpy(state->election_result.host_id, state->metrics[best_idx].participant_id, SESSION_ID_SIZE);
156 memset(state->election_result.backup_id, 0, SESSION_ID_SIZE);
157 if (backup_idx >= 0)
158 memcpy(state->election_result.backup_id, state->metrics[backup_idx].participant_id, SESSION_ID_SIZE);
159 state->election_result.timestamp_ns = time_get_realtime_ns();
161 return ASCIICHAT_OK;
162}
163
165 if (!state) {
166 return SET_ERRNO(ERROR_INVALID_PARAM, "State is NULL");
167 }
168
170 return SET_ERRNO(ERROR_INVALID_STATE, "Invalid transition from state %d", state->current_state);
171 }
172
174 return ASCIICHAT_OK;
175}
176
178 if (!state) {
180 }
181 return state->current_state;
182}
183
185 if (!state || !out_host_id) {
186 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters");
187 }
188
190 return SET_ERRNO(ERROR_INVALID_STATE, "Election not complete, current state: %d", state->current_state);
191 }
192
193 memcpy(out_host_id, state->election_result.host_id, SESSION_ID_SIZE);
194 return ASCIICHAT_OK;
195}
196
198 if (!state || !out_backup_id) {
199 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters");
200 }
201
203 return SET_ERRNO(ERROR_INVALID_STATE, "Election not complete, current state: %d", state->current_state);
204 }
205
206 memcpy(out_backup_id, state->election_result.backup_id, SESSION_ID_SIZE);
207 return ASCIICHAT_OK;
208}
209
211 if (!state) {
212 return false;
213 }
215}
216
218 if (!state) {
219 return -1;
220 }
221 return state->metrics_count;
222}
223
224asciichat_error_t consensus_state_get_metric_at(const consensus_state_t *state, int index, participant_metrics_t *out_metrics) {
225 if (!state || !out_metrics || index < 0) {
226 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters");
227 }
228
229 if (index >= state->metrics_count) {
230 return SET_ERRNO(ERROR_INVALID_PARAM, "Index %d out of bounds (count: %d)", index, state->metrics_count);
231 }
232
233 /* Valid to retrieve metrics in COLLECTING, COLLECTION_COMPLETE, or ELECTION states */
236 return SET_ERRNO(ERROR_INVALID_STATE, "Cannot retrieve metrics in state %d", state->current_state);
237 }
238
239 memcpy(out_metrics, &state->metrics[index], sizeof(participant_metrics_t));
240 return ASCIICHAT_OK;
241}
Synchronized per-thread pending error stacks and value snapshots. SET_ERRNO pushes,...
#define SET_ERRNO(code,...)
Application limits and constraints.
asciichat_error_t consensus_election_choose_hosts(const participant_metrics_t *metrics, int num_metrics, int *out_best_index, int *out_backup_index)
Elect best and backup host from metrics.
Definition election.c:78
Deterministic host election algorithm.
#define SAFE_REALLOC(ptr, size, cast)
#define SAFE_FREE(ptr)
#define SAFE_MALLOC(size, cast)
unsigned long long uint64_t
unsigned char uint8_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_INVALID_STATE
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
#define SESSION_ID_SIZE
⚙️ Common definitions, error codes, macros, and types shared throughout the application
static bool size_t index
Definition queries.h:8
asciichat_error_t consensus_state_start_collection(consensus_state_t *state)
Transition to COLLECTION_START state.
Definition state.c:75
asciichat_error_t consensus_state_compute_election(consensus_state_t *state)
Run election algorithm and transition to ELECTION_COMPLETE.
Definition state.c:135
asciichat_error_t consensus_state_get_metric_at(const consensus_state_t *state, int index, participant_metrics_t *out_metrics)
Get collected metrics at index.
Definition state.c:224
asciichat_error_t consensus_state_create(const uint8_t my_id[SESSION_ID_SIZE], const consensus_topology_t *topology, consensus_state_t **out_state)
Initialize state machine with topology.
Definition state.c:43
bool consensus_state_is_leader(const consensus_state_t *state)
Check if we are the elected leader for this round.
Definition state.c:210
void consensus_state_destroy(consensus_state_t *state)
Destroy state machine and free resources.
Definition state.c:66
asciichat_error_t consensus_state_store_election(consensus_state_t *state, int best_idx, int backup_idx)
Definition state.c:151
asciichat_error_t consensus_state_get_elected_backup(const consensus_state_t *state, uint8_t out_backup_id[SESSION_ID_SIZE])
Get elected backup host ID from last election.
Definition state.c:197
asciichat_error_t consensus_state_get_elected_host(const consensus_state_t *state, uint8_t out_host_id[SESSION_ID_SIZE])
Get elected host ID from last election.
Definition state.c:184
asciichat_error_t consensus_state_reset_to_idle(consensus_state_t *state)
Transition back to IDLE from ELECTION_COMPLETE.
Definition state.c:164
asciichat_error_t consensus_state_collection_complete(consensus_state_t *state)
Transition to COLLECTION_COMPLETE state.
Definition state.c:116
struct consensus_state consensus_state_t
State machine instance.
int consensus_state_get_metrics_count(const consensus_state_t *state)
Get count of metrics collected in current round.
Definition state.c:217
consensus_state_machine_t consensus_state_get_current_state(const consensus_state_t *state)
Get current state machine state.
Definition state.c:177
asciichat_error_t consensus_state_add_metrics(consensus_state_t *state, const participant_metrics_t *metrics)
Add collected metrics from a participant.
Definition state.c:92
Ring consensus state machine.
consensus_state_machine_t
State machine lifecycle states.
Definition state.h:23
@ CONSENSUS_STATE_COLLECTION_COMPLETE
Definition state.h:27
@ CONSENSUS_STATE_ELECTION_COMPLETE
Definition state.h:30
@ CONSENSUS_STATE_FAILED
Definition state.h:31
@ CONSENSUS_STATE_ELECTING
Definition state.h:29
@ CONSENSUS_STATE_ELECTION_START
Definition state.h:28
@ CONSENSUS_STATE_IDLE
Definition state.h:24
@ CONSENSUS_STATE_COLLECTING
Definition state.h:26
🔤 String Manipulation and Shell Escaping Utilities
State machine instance.
Definition state.c:29
int metrics_capacity
Definition state.c:37
int metrics_count
Definition state.c:36
election_result_t election_result
Definition state.c:40
participant_metrics_t * metrics
Definition state.c:35
uint8_t my_id[SESSION_ID_SIZE]
Definition state.c:32
consensus_topology_t * topology
Definition state.c:31
consensus_state_machine_t current_state
Definition state.c:30
Election result storage.
Definition state.c:20
uint8_t backup_id[SESSION_ID_SIZE]
Definition state.c:22
uint8_t host_id[SESSION_ID_SIZE]
Definition state.c:21
uint64_t timestamp_ns
Definition state.c:23
⏱️ High-precision timing utilities using sokol_time.h and uthash
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.