Raw ↗
+ +
5797689 fix(windows): resolve MSVC test linkage, missing unistd, and export declarations
saifmukhtar · 2026-11-24
1 /*
2 * SPDX-License-Identifier: AGPL-3.0-or-later WITH Classpath-exception-2.0
3 * Copyright (c) 2026 Saif Mukhtar
4 *
5 * OpenAVX - Open Audio-Video eXchange
6 * test/openavx_discovery_server_test.c - Centralized Discovery Server Verification
7 */
8  
9 #define _GNU_SOURCE
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <assert.h>
14 #include <time.h>
15 #include "avx_platform.h"
16  
17 #include "openavx.h"
18 #include "avx_discovery_server.h"
19  
20 #define DS_TEST_PORT 7959
21  
22 int main(void) {
23 printf("\n=======================================================\n");
24 printf(" OpenAVX Centralized Discovery Server Test\n");
25 printf("=======================================================\n\n");
26  
27 /* 1. Start Centralized Discovery Server on 127.0.0.1:7959 */
28 printf("[*] 1. Launching Discovery Server daemon on 127.0.0.1:%d...\n", DS_TEST_PORT);
29 avx_discovery_server_t *srv = avx_discovery_server_create("127.0.0.1", DS_TEST_PORT);
30 assert(srv != NULL);
31 printf(" [+] Centralized Discovery Server listening on TCP port %d!\n", DS_TEST_PORT);
32  
33 /* 2. Initialize openavx */
34 printf("[*] 2. Initializing OpenAVX library...\n");
35 oavx_context_t *ctx = oavx_context_create();
36 assert(ctx != NULL);
37  
38 /* Set environment variable pointing to our discovery server */
39 setenv("OPENAVX_DISCOVERY_SERVER", "127.0.0.1:7959", 1);
40  
41 /* 3. Create Sender */
42 printf("[*] 3. Creating Sender \"ENTERPRISE_STUDIO_TX\"...\n");
43 oavx_stream_config_t send_create = {
44 .name = "ENTERPRISE_STUDIO_TX",
45 .groups = "corporate",
46 .direction = OAVX_DIR_TRANSMIT,
47 .clock_sync = true
48 };
49 oavx_stream_t *p_send = oavx_stream_open(ctx, &send_create);
50 assert(p_send != NULL);
51 printf(" [+] Sender registered automatically with Discovery Server!\n");
52  
53 /* Give server time to record registration */
54 avx_sleep_ms(50);
55  
56 /* 4. Create Finder configured with corporate group */
57 printf("[*] 4. Creating Finder targeting Discovery Server...\n");
58 oavx_discovery_t *p_find = oavx_discovery_create(ctx, "corporate");
59 assert(p_find != NULL);
60  
61 bool found = oavx_discovery_wait(p_find, 1000);
62 assert(found);
63  
64 oavx_endpoint_t sources[16];
65 size_t num_sources = oavx_discovery_get_sources(p_find, sources, 16);
66 assert(num_sources >= 1);
67  
68 printf(" [+] Discovery Server returned %zu source(s):\n", num_sources);
69 int match_idx = -1;
70 for (size_t i = 0; i < num_sources; ++i) {
71 printf(" [%zu] \"%s\" at %s\n", i, sources[i].name, sources[i].address);
72 if (strcmp(sources[i].name, "ENTERPRISE_STUDIO_TX") == 0) {
73 match_idx = (int)i;
74 }
75 }
76 assert(match_idx >= 0);
77  
78 /* 5. Connect Receiver via discovered URL */
79 printf("[*] 5. Connecting Receiver via Centralized Discovery catalog...\n");
80 oavx_stream_config_t recv_create = {
81 .name = "Enterprise_Client",
82 .endpoint = sources[match_idx].address,
83 .direction = OAVX_DIR_RECEIVE,
84 .color_format = OAVX_COLOR_FORMAT_UYVY_RGBA,
85 .bandwidth = OAVX_BANDWIDTH_HIGHEST,
86 .clock_sync = false
87 };
88 oavx_stream_t *p_recv = oavx_stream_open(ctx, &recv_create);
89 assert(p_recv != NULL);
90 printf(" [+] Receiver connected successfully!\n");
91  
92 /* 6. Verify Source Deregistration on Sender Shutdown */
93 printf("[*] 6. Verifying Discovery Server auto-purge on Sender disconnect...\n");
94 oavx_stream_close(p_recv);
95 oavx_stream_close(p_send);
96  
97 avx_sleep_ms(50);
98  
99 /* Direct client query to verify server purged the source */
100 char names[16][256];
101 char urls[16][256];
102 int count = avx_discovery_client_query("127.0.0.1", DS_TEST_PORT, names, urls, 16);
103 assert(count == 0);
104 printf(" [+] Server purged disconnected sender: remaining active sources = %d\n", count);
105  
106 /* 7. Cleanup */
107 printf("[*] 7. Tearing down Discovery Server...\n");
108 oavx_discovery_destroy(p_find);
109 oavx_context_destroy(ctx);
110 avx_discovery_server_destroy(srv);
111 unsetenv("OPENAVX_DISCOVERY_SERVER");
112  
113 printf("\n[PASS] openavx_discovery_server_test\n\n");
114 return 0;
115 }