Practice ยท 1 of 1
Two-Phase Init, Provable
Implement a two-phase connection object. The boilerplate
declares:
``c
typedef enum { C_NEW, C_READY, C_CONNECTED, C_FAILED } CState;
typedef struct {
CState state;
char target[32];
int attempts;
} Conn;
int conn_init(Conn *c); /* -> C_READY; 0 ok, -1 NULL */
int conn_connect(Conn *c, const char *target);/* 0 ok, -1 bad; -> C_CONNECTED
or C_FAILED (attempts counted) */
void conn_close(Conn *c); /* -> C_READY from CONNECTED/FAILED;
no-op from NEW/READY */
int conn_destroy(Conn *c); /* safe from EVERY state; 0 ok, -1 NULL */
``
connect fails (state C_FAILED) iff target is NULL/empty or longer than
31 chars. destroy must succeed from every reachable state.
Difficulty: intermediate
Back to lesson: Practice: Layered Errors Gym