ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
http_renderer_process.c File Reference

Go to the source code of this file.

Data Structures

struct  render_worker_t

Macros

#define MCP_RENDER_ENV_CAPACITY   16

Functions

void http_renderer_process (http_renderer_process_request_t *r, atomic_t *cancelled, http_renderer_process_result_t *result)
void http_renderer_process_result_destroy (http_renderer_process_result_t *result)

Macro Definition Documentation

◆ MCP_RENDER_ENV_CAPACITY

#define MCP_RENDER_ENV_CAPACITY   16

Definition at line 3 of file http_renderer_process.c.

Referenced by http_renderer_process().

Function Documentation

◆ http_renderer_process()

void http_renderer_process ( http_renderer_process_request_t * r,
atomic_t * cancelled,
http_renderer_process_result_t * result )

Definition at line 120 of file http_renderer_process.c.

122 {
123 memset(result, 0, sizeof(*result));
126 SAFE_STRNCPY(result->error, "Renderer output must select text or files",
127 sizeof(result->error));
128 return;
129 }
130 render_worker_t w = {.cancelled = cancelled,
131 .result = result,
132 .started = time_get_ns(),
133 .timeout = r->timeout_ms};
134 result->text = SAFE_CALLOC(HTTP_RENDERER_MAX_OUTPUT + 1, 1, char *);
135 if (uv_loop_init(&w.loop)) {
136 SAFE_STRNCPY(result->error, "Cannot initialize renderer event loop",
137 sizeof(result->error));
138 return;
139 }
140 uv_pipe_init(&w.loop, &w.output, 0);
141 uv_pipe_init(&w.loop, &w.errors, 0);
142 uv_timer_init(&w.loop, &w.timer);
143 w.output.data = w.errors.data = w.timer.data = w.process.data = &w;
144 uv_stdio_container_t streams[3] = {0};
145 streams[0].flags = UV_IGNORE;
146 streams[1].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
147 streams[1].data.stream = (uv_stream_t *)&w.output;
148 streams[2].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
149 streams[2].data.stream = (uv_stream_t *)&w.errors;
150 // Only runtime essentials are inherited; user config, ASCII_CHAT_* and
151 // credentials are excluded.
152 const char *keys[] = {
153 "PATH", "SystemRoot", "WINDIR", "TEMP",
154 "TMP", "TMPDIR", "LD_LIBRARY_PATH", "DYLD_LIBRARY_PATH",
155 "TESTING", NULL};
156 char *env[MCP_RENDER_ENV_CAPACITY] = {0};
157 size_t ne = 0;
158 for (size_t i = 0; keys[i]; ++i) {
159 const char *v = platform_getenv(keys[i]);
160 if (v) {
161 size_t n = strlen(keys[i]) + strlen(v) + 2;
162 env[ne] = SAFE_MALLOC(n, char *);
163 SAFE_SNPRINTF(env[ne], n, "%s=%s", keys[i], v);
164 ++ne;
165 }
166 }
167 // The private staging directory provides an isolated home for media
168 // libraries.
169 if (r->stage_dir[0]) {
170 size_t n = strlen(r->stage_dir) + sizeof("HOME=");
171 env[ne] = SAFE_MALLOC(n, char *);
172 SAFE_SNPRINTF(env[ne], n, "HOME=%s", r->stage_dir);
173 ++ne;
174 }
175 env[ne++] = platform_strdup("ASCII_CHAT_MCP_WORKER=1");
176 env[ne++] = platform_strdup("ASCII_CHAT_OTEL_ENABLED_TELEMETRY=none");
177 if (r->capture_webcam)
178 env[ne++] = platform_strdup("ASCII_CHAT_MCP_WEBCAM=1");
179 uv_process_options_t opts = {.exit_cb = exited,
180 .file = r->argv[0],
181 .args = r->argv,
182 .env = env,
183 .stdio_count = 3,
184 .stdio = streams};
185 int rc = uv_spawn(&w.loop, &w.process, &opts);
186 for (size_t i = 0; i < ne; ++i)
187 SAFE_FREE(env[i]);
188 if (rc) {
189 SAFE_SNPRINTF(result->error, sizeof(result->error),
190 "Cannot start renderer: %s", uv_strerror(rc));
191 uv_close((uv_handle_t *)&w.output, NULL);
192 uv_close((uv_handle_t *)&w.errors, NULL);
193 uv_close((uv_handle_t *)&w.timer, NULL);
194 } else {
195 uv_read_start((uv_stream_t *)&w.output, allocate, read_output);
196 uv_read_start((uv_stream_t *)&w.errors, allocate, read_output);
197 uv_timer_start(&w.timer, tick, 20, 20);
198 }
199 uv_run(&w.loop, UV_RUN_DEFAULT);
200 uv_loop_close(&w.loop);
201 result->elapsed_ms = (time_get_ns() - w.started) / 1000000;
202 if (rc || w.failed)
203 return;
204 result->error[0] = '\0';
205 if (!result->length && !r->stage_file[0]) {
206 SAFE_STRNCPY(result->error, "Renderer returned no ASCII art",
207 sizeof(result->error));
208 return;
209 }
210 if (r->stage_file[0]) {
211 uv_fs_t fs;
212 int e = uv_fs_stat(NULL, &fs, r->stage_file, NULL);
213 bool valid = e == 0 && fs.statbuf.st_size > 0;
214 uv_fs_req_cleanup(&fs);
215 if (!valid) {
216 SAFE_STRNCPY(result->error, "Renderer did not create an output file",
217 sizeof(result->error));
218 return;
219 }
220 // Hard-link publication is atomic and fails if any file/symlink already
221 // owns the destination. Staging beside the destination keeps this on the
222 // same filesystem.
223 e = uv_fs_link(NULL, &fs, r->stage_file, r->output_path, NULL);
224 uv_fs_req_cleanup(&fs);
225 if (e) {
226 SAFE_SNPRINTF(result->error, sizeof(result->error),
227 "Cannot save without replacing an existing file: %s",
228 uv_strerror(e));
229 return;
230 }
232 sizeof(result->output_path));
233 }
234 http_render_result_t frames = {0};
235 if (result->text && result->length)
236 (void)http_render_result_add_text_frame(&frames, result->text,
237 result->length, 0);
238 if (result->output_path[0])
239 (void)http_render_result_add_file_frame(&frames, result->output_path, 0);
240 result->frames = frames.frames;
241 result->frame_count = frames.frame_count;
242}
bool valid
Definition fft.c:29
#define SAFE_STRNCPY(dst, src, size)
#define SAFE_FREE(ptr)
#define SAFE_MALLOC(size, cast)
#define SAFE_SNPRINTF(buffer, buffer_size,...)
#define SAFE_CALLOC(count, size, cast)
asciichat_error_t http_render_result_add_file_frame(http_render_result_t *result, const char *path, uint64_t timestamp_ms)
asciichat_error_t http_render_result_add_text_frame(http_render_result_t *result, const char *text, size_t length, uint64_t timestamp_ms)
#define HTTP_RENDERER_MAX_OUTPUT
@ HTTP_RENDER_OUTPUT_FILE
@ HTTP_RENDER_OUTPUT_TEXT
#define MCP_RENDER_ENV_CAPACITY
char * platform_strdup(const char *s)
http_render_frame_t * frames
http_render_output_type_t output_type
char stage_file[PLATFORM_MAX_PATH_LENGTH]
char stage_dir[PLATFORM_MAX_PATH_LENGTH]
char output_path[PLATFORM_MAX_PATH_LENGTH]
char output_path[PLATFORM_MAX_PATH_LENGTH]
http_render_frame_t * frames
const char * platform_getenv(const char *name)
Definition system.c:45

References http_renderer_process_request_t::argv, http_renderer_process_request_t::capture_webcam, http_renderer_process_result_t::elapsed_ms, http_renderer_process_result_t::error, render_worker_t::errors, render_worker_t::failed, http_render_result_t::frame_count, http_renderer_process_result_t::frame_count, http_render_result_t::frames, http_renderer_process_result_t::frames, HTTP_RENDER_OUTPUT_FILE, HTTP_RENDER_OUTPUT_TEXT, http_render_result_add_file_frame(), http_render_result_add_text_frame(), HTTP_RENDERER_MAX_OUTPUT, http_renderer_process_result_t::length, render_worker_t::loop, MCP_RENDER_ENV_CAPACITY, render_worker_t::output, http_renderer_process_request_t::output_path, http_renderer_process_result_t::output_path, http_renderer_process_request_t::output_type, platform_getenv(), platform_strdup(), render_worker_t::process, SAFE_CALLOC, SAFE_FREE, SAFE_MALLOC, SAFE_SNPRINTF, SAFE_STRNCPY, http_renderer_process_request_t::stage_dir, http_renderer_process_request_t::stage_file, render_worker_t::started, http_renderer_process_result_t::text, http_renderer_process_request_t::timeout_ms, render_worker_t::timer, and valid.

◆ http_renderer_process_result_destroy()

void http_renderer_process_result_destroy ( http_renderer_process_result_t * result)

Definition at line 244 of file http_renderer_process.c.

245 {
246 if (!result)
247 return;
248 for (size_t i = 0; i < result->frame_count; ++i)
249 SAFE_FREE(result->frames[i].text);
250 SAFE_FREE(result->frames);
251 SAFE_FREE(result->text);
252 memset(result, 0, sizeof(*result));
253}

References http_renderer_process_result_t::frame_count, http_renderer_process_result_t::frames, SAFE_FREE, http_render_frame_t::text, and http_renderer_process_result_t::text.