ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
mdns.h File Reference

mDNS service discovery UI with terminal_screen rendering More...

Go to the source code of this file.

Data Structures

struct  ui_mdns_server_t
 Discovered server information from mDNS. More...
 
struct  ui_mdns_config_t
 Configuration for TUI discovery. More...
 

Functions

void ui_mdns_log_init (void)
 Initialize log buffer for mDNS discovery UI.
 
void ui_mdns_log_destroy (void)
 Destroy mDNS discovery log buffer.
 
void ui_mdns_log_clear (void)
 Clear mDNS discovery log buffer.
 
void ui_mdns_log_append (const char *message)
 Append message to mDNS discovery log buffer.
 
ui_mdns_server_t * ui_mdns_query (const ui_mdns_config_t *config, int *out_count)
 Discover ascii-chat servers on the local network via mDNS.
 
void ui_mdns_free_results (ui_mdns_server_t *servers)
 Free results from TUI discovery query.
 
int ui_mdns_prompt_selection (const ui_mdns_server_t *servers, int count)
 Display discovered servers to user and prompt for selection.
 
int ui_mdns_select (const ui_mdns_server_t *servers, int count)
 TUI-based server selection with formatted display.
 
const char * ui_mdns_get_best_address (const ui_mdns_server_t *server)
 Get best address representation for a discovered server.
 

Detailed Description

mDNS service discovery UI with terminal_screen rendering

Implements interactive mDNS-based discovery of ascii-chat servers on the local network. Uses the shared terminal_screen infrastructure for consistent UI rendering with other modules.

Definition in file ui/mdns.h.

Function Documentation

◆ ui_mdns_free_results()

void ui_mdns_free_results ( ui_mdns_server_t *  servers)

Free results from TUI discovery query.

Releases memory allocated by ui_mdns_query().

Parameters
serversPointer to server array (safe to pass NULL)
Note
Safe to call multiple times or with NULL pointer

Free results from TUI discovery query.

Definition at line 77 of file ui/mdns.c.

77 {
79}
void discovery_mdns_destroy(ui_mdns_server_t *servers)
Free memory from mDNS discovery results.
Definition discovery.c:265

References discovery_mdns_destroy().

◆ ui_mdns_get_best_address()

const char * ui_mdns_get_best_address ( const ui_mdns_server_t *  server)

Get best address representation for a discovered server.

Returns the most suitable address representation (IPv4 > hostname > IPv6) for connecting to a discovered server.

Selection Priority:

  1. IPv4 address (most universal)
  2. Service name (if IPv4 not available)
  3. IPv6 address (last resort)
Parameters
serverDiscovered server
Returns
Pointer to best address string (points to field in server struct)

Get best address representation for a discovered server.

Definition at line 233 of file ui/mdns.c.

233 {
234 if (!server) {
235 return "";
236 }
237
238 // Prefer IPv4 > name > IPv6
239 if (server->ipv4[0] != '\0') {
240 return server->ipv4;
241 }
242 if (server->name[0] != '\0') {
243 return server->name;
244 }
245 if (server->ipv6[0] != '\0') {
246 return server->ipv6;
247 }
248
249 return server->address; // Fallback to address field
250}
char name[256]
Service instance name (e.g., "swift-river-canyon")
Definition ui/mdns.h:58
char ipv4[16]
IPv4 address (if available)
Definition ui/mdns.h:61
char ipv6[46]
IPv6 address (if available)
Definition ui/mdns.h:62
char address[256]
Server address (IPv4, IPv6, or hostname)
Definition ui/mdns.h:59

References ui_mdns_server_t::address, ui_mdns_server_t::ipv4, ui_mdns_server_t::ipv6, and ui_mdns_server_t::name.

Referenced by ui_mdns_prompt_selection(), and ui_mdns_select().

◆ ui_mdns_log_append()

void ui_mdns_log_append ( const char *  message)

Append message to mDNS discovery log buffer.

Delegates to the logging system which automatically captures to session log buffer.

Parameters
messageLog message to append

Definition at line 46 of file ui/mdns.c.

46 {
47 if (message) {
48 log_info("%s", message);
49 }
50}
#define log_info(...)
Log an INFO message.
Definition log/log.h:561

References log_info.

◆ ui_mdns_log_clear()

void ui_mdns_log_clear ( void  )

Clear mDNS discovery log buffer.

Delegates to terminal_screen log clear. Useful when starting fresh mDNS discovery screen.

Definition at line 42 of file ui/mdns.c.

42 {
44}
void terminal_screen_log_clear(void)
Clear buffered logs for terminal screens.

References terminal_screen_log_clear().

◆ ui_mdns_log_destroy()

void ui_mdns_log_destroy ( void  )

Destroy mDNS discovery log buffer.

Delegates to terminal_screen log cleanup.

Definition at line 37 of file ui/mdns.c.

37 {
40}
void log_clear_session_log_buffer(void)
Unregister the session log buffer.
Definition log/log.c:1847
void terminal_screen_log_destroy(void)
Standard log cleanup for terminal screens.

References log_clear_session_log_buffer(), and terminal_screen_log_destroy().

◆ ui_mdns_log_init()

void ui_mdns_log_init ( void  )

Initialize log buffer for mDNS discovery UI.

Delegates to terminal_screen log initialization. Call once at startup before rendering mDNS discovery screen.

Definition at line 30 of file ui/mdns.c.

30 {
32 if (buf) {
34 }
35}
void log_set_session_log_buffer(session_log_buffer_t *buf)
Register a session log buffer with the logger.
Definition log/log.c:1838
Internal circular buffer structure.
session_log_buffer_t * terminal_screen_log_init(void)
Initialize session log buffer for terminal screens.

References log_set_session_log_buffer(), and terminal_screen_log_init().

◆ ui_mdns_prompt_selection()

int ui_mdns_prompt_selection ( const ui_mdns_server_t *  servers,
int  count 
)

Display discovered servers to user and prompt for selection.

Shows a numbered list of discovered servers and prompts user to select one. Handles invalid input with re-prompting.

User Experience:

Available ascii-chat servers on LAN:
1. swift-river-canyon (192.168.1.100:27224)
2. quiet-mountain-lake (192.168.1.101:27224)
3. gentle-forest-breeze (192.168.1.102:27224)
Select server (1-3) or press Enter to cancel: _

Behavior:

  • Displays each server with instance name and best-guess address
  • Shows IPv4 address if available, falls back to IPv6 or hostname
  • Handles non-numeric input with error message and re-prompt
  • Returns -1 if user presses Enter/Ctrl+C to cancel
  • Returns 0-based index of selected server
Parameters
serversArray of discovered servers
countNumber of servers in array
Returns
Index of selected server (0 to count-1), or -1 to cancel
Note
This function performs interactive I/O - may not be suitable for automated contexts
For automated selection, use servers[0] directly instead

Display discovered servers to user and prompt for selection.

Definition at line 84 of file ui/mdns.c.

84 {
85 if (!servers || count <= 0) {
86 return -1;
87 }
88
89 // Display available servers
90 printf("\nAvailable ascii-chat servers on LAN:\n");
91 for (int i = 0; i < count; i++) {
92 const ui_mdns_server_t *srv = &servers[i];
93 const char *addr = ui_mdns_get_best_address(srv);
94 printf(" %d. %s (%s:%u)\n", i + 1, srv->name, addr, srv->port);
95 }
96
97 // Prompt for selection
98 printf("\nSelect server (1-%d) or press Enter to cancel: ", count);
99 fflush(stdout);
100
101 // Read user input
102 char input[32];
103 if (fgets(input, sizeof(input), stdin) == NULL) {
104 printf("\n");
105 return -1; // EOF or error
106 }
107
108 // Check for empty input (Enter pressed)
109 if (input[0] == '\n' || input[0] == '\r' || input[0] == '\0') {
110 return -1; // User cancelled
111 }
112
113 // Parse input as number
114 char *endptr;
115 long selection = strtol(input, &endptr, 10);
116
117 // Validate input
118 if (selection < 1 || selection > count) {
119 printf("⚠️ Invalid selection. Please enter a number between 1 and %d\n", count);
120 return ui_mdns_prompt_selection(servers, count); // Re-prompt
121 }
122
123 return (int)(selection - 1); // Convert to 0-based index
124}
Discovered server information from mDNS.
Definition ui/mdns.h:57
uint16_t port
Server port number.
Definition ui/mdns.h:60
const char * ui_mdns_get_best_address(const ui_mdns_server_t *server)
Get best address for a server.
Definition ui/mdns.c:233
int ui_mdns_prompt_selection(const ui_mdns_server_t *servers, int count)
Interactive server selection.
Definition ui/mdns.c:84

References ui_mdns_server_t::name, ui_mdns_server_t::port, ui_mdns_get_best_address(), and ui_mdns_prompt_selection().

Referenced by ui_mdns_prompt_selection().

◆ ui_mdns_query()

ui_mdns_server_t * ui_mdns_query ( const ui_mdns_config_t *  config,
int *  out_count 
)

Discover ascii-chat servers on the local network via mDNS.

Performs an mDNS query for _ascii-chat._tcp services on the local network. Collects responses and returns discovered servers to the caller.

Behavior:

  • Sends multicast mDNS query for _ascii-chat._tcp.local services
  • Waits for responses for the specified timeout period
  • Collects all discovered servers with their addresses and ports
  • Returns array of discovered servers

Memory Management:

Error Handling:

  • Returns NULL and sets errno on initialization failure
  • Partial results returned if mDNS init fails but discovery was attempted
  • Network errors logged but don't prevent continuation

Threading:

  • Blocking operation - waits for full timeout period
  • Safe to call from main thread
  • Does not spawn background threads
Parameters
configDiscovery configuration (NULL uses defaults)
out_countOutput parameter: number of discovered servers
Returns
Array of discovered servers, or NULL on error Must be freed with ui_mdns_free_results()
Note
Timeout includes network round-trip time, so 2000ms allows ~1.5s of actual waiting
Returns empty array (non-NULL with count=0) if no servers found, not NULL

Example:

ui_mdns_config_t config = {.timeout_ms = 2000, .max_servers = 20};
int count = 0;
ui_mdns_server_t *servers = ui_mdns_query(&config, &count);
if (servers && count > 0) {
for (int i = 0; i < count; i++) {
printf("%d: %s (%s:%d)\n", i+1, servers[i].name, servers[i].address, servers[i].port);
}
// User selects server...
}
void ui_mdns_free_results(mdns_result_t *results)
Definition misc.c:155
mdns_query_t * ui_mdns_query(const char *service)
Definition misc.c:139
Configuration for TUI discovery.
Definition ui/mdns.h:69
int timeout_ms
Maximum time to wait for responses (default: 2000)
Definition ui/mdns.h:70

Discover ascii-chat servers on the local network via mDNS.

Calls discovery_mdns_query() from discovery.c with TUI-friendly configuration.

Definition at line 57 of file ui/mdns.c.

57 {
58 if (!out_count) {
59 SET_ERRNO(ERROR_INVALID_PARAM, "out_count pointer is NULL");
60 return NULL;
61 }
62
63 *out_count = 0;
64
65 // Apply defaults if needed
66 int timeout_ms = (config && config->timeout_ms > 0) ? config->timeout_ms : 2000;
67 int max_servers = (config && config->max_servers > 0) ? config->max_servers : 20;
68 bool quiet = (config && config->quiet);
69
70 // Call the core mDNS discovery function from discovery.c
71 return discovery_mdns_query(timeout_ms, max_servers, quiet, out_count);
72}
ui_mdns_server_t * discovery_mdns_query(int timeout_ms, int max_servers, bool quiet, int *out_count)
Public mDNS query function used by both parallel discovery and TUI wrapper.
Definition discovery.c:179
#define SET_ERRNO(code, context_msg,...)
Set error code with custom context message and log it, returning the error code.
@ ERROR_INVALID_PARAM
int max_servers
Maximum servers to collect (default: 20)
Definition ui/mdns.h:71
bool quiet
Suppress discovery messages (default: false)
Definition ui/mdns.h:72

References discovery_mdns_query(), ERROR_INVALID_PARAM, ui_mdns_config_t::max_servers, ui_mdns_config_t::quiet, SET_ERRNO, and ui_mdns_config_t::timeout_ms.

◆ ui_mdns_select()

int ui_mdns_select ( const ui_mdns_server_t *  servers,
int  count 
)

TUI-based server selection with formatted display.

Displays discovered servers in a formatted terminal UI with:

  • Clear screen and boxed display
  • Server list with numbering and addresses
  • "No results" message if no servers available
  • Interactive numeric selection prompt

Display Example (3 servers):

╭─ 🔍 ascii-chat Server Discovery ────────────╮
│
│ Found 3 servers on your local network:
│
│ [1] swift-river-canyon 192.168.1.100:27224
│ [2] quiet-mountain-lake 192.168.1.101:27224
│ [3] gentle-forest-breeze 192.168.1.102:27224
│
╰────────────────────────────────────────────╯
Enter server number (1-3) or press Enter to cancel:

No Results Display:

╭─ 🔍 ascii-chat Server Discovery ─╮
│
│ No servers found on local network
│
│ Make sure an ascii-chat server is running on your LAN
│ Or provide a server address manually: ascii-chat client <address>
│
╰─────────────────────────────────────────╯
Parameters
serversArray of discovered servers
countNumber of servers (0 for "no results")
Returns
0-based index of selected server, or -1 to cancel

Displays discovered servers in a terminal UI with the following features:

  • Clears terminal and displays formatted server list
  • Shows "No results" message if no servers available
  • Allows numeric input for selection
  • Shows helpful prompts and icons
Parameters
serversArray of discovered servers
countNumber of servers
Returns
0-based index of selected server, or -1 to cancel

Definition at line 149 of file misc.c.

149 {
150 (void)servers;
151 (void)count;
152 return -1;
153}

References colored_string(), LOG_COLOR_DEBUG, LOG_COLOR_INFO, LOG_COLOR_WARN, log_lock_terminal(), log_plain, log_unlock_terminal(), ui_mdns_server_t::name, platform_sleep_ms(), ui_mdns_server_t::port, ui_mdns_get_best_address(), and ui_mdns_select().

Referenced by client_main(), and ui_mdns_select().