xref: /netbsd-src/external/mit/libuv/dist/docs/code/multi-echo-server/main.c (revision 0e552da7216834a96e91ad098e59272b41087480)
1*0e552da7Schristos #include <inttypes.h>
2*0e552da7Schristos #include <stdio.h>
3*0e552da7Schristos #include <stdlib.h>
4*0e552da7Schristos #include <string.h>
5*0e552da7Schristos #include <uv.h>
6*0e552da7Schristos 
7*0e552da7Schristos uv_loop_t *loop;
8*0e552da7Schristos 
9*0e552da7Schristos struct child_worker {
10*0e552da7Schristos     uv_process_t req;
11*0e552da7Schristos     uv_process_options_t options;
12*0e552da7Schristos     uv_pipe_t pipe;
13*0e552da7Schristos } *workers;
14*0e552da7Schristos 
15*0e552da7Schristos int round_robin_counter;
16*0e552da7Schristos int child_worker_count;
17*0e552da7Schristos 
18*0e552da7Schristos uv_buf_t dummy_buf;
19*0e552da7Schristos char worker_path[500];
20*0e552da7Schristos 
close_process_handle(uv_process_t * req,int64_t exit_status,int term_signal)21*0e552da7Schristos void close_process_handle(uv_process_t *req, int64_t exit_status, int term_signal) {
22*0e552da7Schristos     fprintf(stderr, "Process exited with status %" PRId64 ", signal %d\n", exit_status, term_signal);
23*0e552da7Schristos     uv_close((uv_handle_t*) req, NULL);
24*0e552da7Schristos }
25*0e552da7Schristos 
alloc_buffer(uv_handle_t * handle,size_t suggested_size,uv_buf_t * buf)26*0e552da7Schristos void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) {
27*0e552da7Schristos   buf->base = malloc(suggested_size);
28*0e552da7Schristos   buf->len = suggested_size;
29*0e552da7Schristos }
30*0e552da7Schristos 
on_new_connection(uv_stream_t * server,int status)31*0e552da7Schristos void on_new_connection(uv_stream_t *server, int status) {
32*0e552da7Schristos     if (status == -1) {
33*0e552da7Schristos         // error!
34*0e552da7Schristos         return;
35*0e552da7Schristos     }
36*0e552da7Schristos 
37*0e552da7Schristos     uv_tcp_t *client = (uv_tcp_t*) malloc(sizeof(uv_tcp_t));
38*0e552da7Schristos     uv_tcp_init(loop, client);
39*0e552da7Schristos     if (uv_accept(server, (uv_stream_t*) client) == 0) {
40*0e552da7Schristos         uv_write_t *write_req = (uv_write_t*) malloc(sizeof(uv_write_t));
41*0e552da7Schristos         dummy_buf = uv_buf_init("a", 1);
42*0e552da7Schristos         struct child_worker *worker = &workers[round_robin_counter];
43*0e552da7Schristos         uv_write2(write_req, (uv_stream_t*) &worker->pipe, &dummy_buf, 1, (uv_stream_t*) client, NULL);
44*0e552da7Schristos         round_robin_counter = (round_robin_counter + 1) % child_worker_count;
45*0e552da7Schristos     }
46*0e552da7Schristos     else {
47*0e552da7Schristos         uv_close((uv_handle_t*) client, NULL);
48*0e552da7Schristos     }
49*0e552da7Schristos }
50*0e552da7Schristos 
setup_workers()51*0e552da7Schristos void setup_workers() {
52*0e552da7Schristos     size_t path_size = 500;
53*0e552da7Schristos     uv_exepath(worker_path, &path_size);
54*0e552da7Schristos     strcpy(worker_path + (strlen(worker_path) - strlen("multi-echo-server")), "worker");
55*0e552da7Schristos     fprintf(stderr, "Worker path: %s\n", worker_path);
56*0e552da7Schristos 
57*0e552da7Schristos     char* args[2];
58*0e552da7Schristos     args[0] = worker_path;
59*0e552da7Schristos     args[1] = NULL;
60*0e552da7Schristos 
61*0e552da7Schristos     round_robin_counter = 0;
62*0e552da7Schristos 
63*0e552da7Schristos     // ...
64*0e552da7Schristos 
65*0e552da7Schristos     // launch same number of workers as number of CPUs
66*0e552da7Schristos     uv_cpu_info_t *info;
67*0e552da7Schristos     int cpu_count;
68*0e552da7Schristos     uv_cpu_info(&info, &cpu_count);
69*0e552da7Schristos     uv_free_cpu_info(info, cpu_count);
70*0e552da7Schristos 
71*0e552da7Schristos     child_worker_count = cpu_count;
72*0e552da7Schristos 
73*0e552da7Schristos     workers = calloc(cpu_count, sizeof(struct child_worker));
74*0e552da7Schristos     while (cpu_count--) {
75*0e552da7Schristos         struct child_worker *worker = &workers[cpu_count];
76*0e552da7Schristos         uv_pipe_init(loop, &worker->pipe, 1);
77*0e552da7Schristos 
78*0e552da7Schristos         uv_stdio_container_t child_stdio[3];
79*0e552da7Schristos         child_stdio[0].flags = UV_CREATE_PIPE | UV_READABLE_PIPE;
80*0e552da7Schristos         child_stdio[0].data.stream = (uv_stream_t*) &worker->pipe;
81*0e552da7Schristos         child_stdio[1].flags = UV_IGNORE;
82*0e552da7Schristos         child_stdio[2].flags = UV_INHERIT_FD;
83*0e552da7Schristos         child_stdio[2].data.fd = 2;
84*0e552da7Schristos 
85*0e552da7Schristos         worker->options.stdio = child_stdio;
86*0e552da7Schristos         worker->options.stdio_count = 3;
87*0e552da7Schristos 
88*0e552da7Schristos         worker->options.exit_cb = close_process_handle;
89*0e552da7Schristos         worker->options.file = args[0];
90*0e552da7Schristos         worker->options.args = args;
91*0e552da7Schristos 
92*0e552da7Schristos         uv_spawn(loop, &worker->req, &worker->options);
93*0e552da7Schristos         fprintf(stderr, "Started worker %d\n", worker->req.pid);
94*0e552da7Schristos     }
95*0e552da7Schristos }
96*0e552da7Schristos 
main()97*0e552da7Schristos int main() {
98*0e552da7Schristos     loop = uv_default_loop();
99*0e552da7Schristos 
100*0e552da7Schristos     setup_workers();
101*0e552da7Schristos 
102*0e552da7Schristos     uv_tcp_t server;
103*0e552da7Schristos     uv_tcp_init(loop, &server);
104*0e552da7Schristos 
105*0e552da7Schristos     struct sockaddr_in bind_addr;
106*0e552da7Schristos     uv_ip4_addr("0.0.0.0", 7000, &bind_addr);
107*0e552da7Schristos     uv_tcp_bind(&server, (const struct sockaddr *)&bind_addr, 0);
108*0e552da7Schristos     int r;
109*0e552da7Schristos     if ((r = uv_listen((uv_stream_t*) &server, 128, on_new_connection))) {
110*0e552da7Schristos         fprintf(stderr, "Listen error %s\n", uv_err_name(r));
111*0e552da7Schristos         return 2;
112*0e552da7Schristos     }
113*0e552da7Schristos     return uv_run(loop, UV_RUN_DEFAULT);
114*0e552da7Schristos }
115