1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
#define AL_LOG_SECTION "cmsrv"
#include <al/log.h>
#include <nnwt/common.h>
#ifndef NAUNET_ON_WINDOWS
#define CMSRV_LOCAL_SOCKET
#endif
#ifdef CMSRV_LOCAL_SOCKET
#include <nnwt/line_processor.h>
#endif
#include <nnwt/timer.h>
#include "../../server/server.h"
#include "../../server/common.h"
#include "../../codec/ffmpeg/common.h"
#if CACHE_HAVE_CDIO
#include "../../cache/handlers/cdio.h"
#endif
#ifdef CMSRV_USE_UI
#include "ui.h"
#endif
struct cmsrv {
struct nn_event_loop loop;
struct nn_signal quit_signal;
struct camu_server server;
#ifdef CMSRV_LOCAL_SOCKET
struct {
struct nn_socket sock;
struct nn_line_processor cli;
} local;
#endif
bool use_ui;
#ifdef CMSRV_USE_UI
struct cmsrv_ui ui;
struct nn_poll input_poll;
struct nn_timer render_timer;
#endif
};
static void close_cmsrv(struct cmsrv *s)
{
#ifdef CMSRV_LOCAL_SOCKET
nn_line_processor_stop(&s->local.cli);
#endif
#ifdef CMSRV_USE_UI
if (s->use_ui) {
nn_poll_stop(&s->input_poll);
nn_timer_stop(&s->render_timer);
}
#endif
camu_server_close(&s->server);
nn_signal_stop(&s->quit_signal);
}
#ifdef CMSRV_LOCAL_SOCKET
static inline void try_parse_arg_from_line(str *line, u32 offset, s32 *n)
{
str arg = al_str_substr(line, offset, line->length);
bool error;
s64 i = al_str_to_long(&arg, 10, &error);
if (!error) {
if (*n < 0) i = -i;
*n = CLAMP(i, (s64)INT32_MIN, (s64)INT32_MAX);
}
}
static u8 server_line_callback(void *userdata, str *line)
{
struct cmsrv *s = (struct cmsrv *)userdata;
struct lia_list *list = al_array_at(s->server.lists, 0);
if (al_str_eq(line, &al_str_c(";PAUSE"))) {
lia_list_toggle_pause(list, LIANA_SEQUENCE_ANY, -1.0);
} else if (al_str_cmp(line, &al_str_c(";NEXT"), 0, 5) == 0) {
s32 n = 1;
if (line->length > 6) {
try_parse_arg_from_line(line, 6, &n);
}
lia_list_skip(list, LIANA_SEQUENCE_ANY, n);
} else if (al_str_cmp(line, &al_str_c(";PREV"), 0, 5) == 0) {
s32 n = -1;
if (line->length > 6) {
try_parse_arg_from_line(line, 6, &n);
}
lia_list_skip(list, LIANA_SEQUENCE_ANY, n);
} else if (al_str_cmp(line, &al_str_c(";SEEK"), 0, 5) == 0) {
if (line->length > 6) {
str arg = al_str_substr(line, 6, line->length);
bool error;
s64 i = al_str_to_long(&arg, 10, &error);
if (!error) {
i *= 1000000;
lia_list_seek(list, LIANA_SEQUENCE_ANY, 0, CLAMP(i, (s64)0, (s64)INT64_MAX));
}
}
} else if (al_str_eq(line, &al_str_c(";SHUFFLE"))) {
lia_list_shuffle(list);
} else if (al_str_eq(line, &al_str_c(";SORT"))) {
lia_list_sort(list);
} else if (al_str_eq(line, &al_str_c(";REVERSE"))) {
lia_list_reverse(list);
} else if (al_str_eq(line, &al_str_c(";CLEAR"))) {
lia_list_clear(list);
} else if (al_str_cmp(line, &al_str_c(";START_AT"), 0, 9) == 0) {
if (line->length > 6) {
s32 n = 0;
try_parse_arg_from_line(line, 10, &n);
if (n > 0) {
camu_server_local_start_at(&s->server, n);
}
}
} else if (al_str_eq(line, &al_str_c(";DISCONNECT"))) {
lia_server_force_disconnect_nodes(&s->server.data.server);
} else if (al_str_eq(line, &al_str_c(";QUIT"))) {
close_cmsrv(s);
} else {
camu_server_local_add(&s->server, line);
}
return NNWT_LINE_PROCESSOR_CONTINUE;
}
#endif
#ifdef CMSRV_USE_UI
static s32 log_callback(void *userdata, u8 level, char *message)
{
struct cmsrv *s = (struct cmsrv *)userdata;
(void)level;
cmsrv_ui_push_message(&s->ui, al_strndup(message, AL_LOG_MESSAGE_SIZE));
return al_strnlen(message, AL_LOG_MESSAGE_SIZE);
}
static void input_poll_callback(void *userdata, s32 revents)
{
struct cmsrv *s = (struct cmsrv *)userdata;
(void)revents;
struct ncinput input;
for (;;) {
if (!cmsrv_ui_read_input(&s->ui, &input)) break;
if (input.evtype == NCTYPE_PRESS || input.evtype == NCTYPE_UNKNOWN) {
switch (input.id) {
case 'q':
close_cmsrv(s);
break;
case '1':
cmsrv_ui_set_pane(&s->ui, CMSRV_UI_LISTS);
break;
case '2':
cmsrv_ui_set_pane(&s->ui, CMSRV_UI_RESOURCES);
break;
case '3':
cmsrv_ui_set_pane(&s->ui, CMSRV_UI_NODES);
break;
}
}
}
}
static void render_timer_callback(void *userdata, struct nn_timer *timer)
{
struct cmsrv *s = (struct cmsrv *)userdata;
(void)timer;
cmsrv_ui_render(&s->ui);
}
#endif
static void quit_signal_callback(void *userdata)
{
struct cmsrv *s = (struct cmsrv *)userdata;
close_cmsrv(s);
}
static struct cmsrv s = { 0 };
static bool sigint_force = false;
static void sigint_handler(s32 signum)
{
(void)signum;
if (sigint_force) {
exit(128 + SIGINT);
} else {
nn_signal_send(&s.quit_signal);
log_warn("Attempting graceful exit. ^C again to force exit.");
sigint_force = true;
}
}
#ifdef NAUNET_ON_WINDOWS
s32 wmain(s32 argc, wchar_t **argv)
#else
s32 main(s32 argc, char *argv[])
#endif
{
if (!nn_common_init("cmsrv_main")) {
return EXIT_FAILURE;
}
#ifdef CAMU_DIRECT_MODE
log_error("Can't run cmsrv in DIRECT_MODE.");
return EXIT_FAILURE;
#endif
s.use_ui = false;
#ifdef CMSRV_USE_UI
char *CMSRV_UI = getenv("CMSRV_UI");
if (CMSRV_UI) s.use_ui = al_strscmp(CMSRV_UI, "1") == 0;
if (s.use_ui) {
if (!cmsrv_ui_init(&s.ui, &s.server)) {
return EXIT_FAILURE;
}
al_set_print(log_callback, &s);
}
#endif
#ifdef CAMU_HAVE_FFMPEG
camu_ff_common_init();
#endif
nn_event_loop_init(&s.loop);
nn_signal_init(&s.quit_signal, &s.loop, quit_signal_callback, &s);
nn_signal_start(&s.quit_signal);
signal(SIGINT, sigint_handler);
str addr = al_str_c("127.0.0.1");
char *CMSRV_IP = getenv("CMSRV_IP");
if (CMSRV_IP) addr = al_str_cr(CMSRV_IP);
log_info("Serving from %.*s.", al_str_x(&addr));
camu_server_init(&s.server, &s.loop, false);
if (!camu_server_listen(&s.server, NNWT_SOCKET_TCP, &addr, CAMU_PORT)) {
return EXIT_FAILURE;
}
#ifdef CMSRV_LOCAL_SOCKET
s.local.sock.type = NNWT_SOCKET_UNIX;
nn_socket_init(&s.local.sock, NNWT_SOCKET_NONBLOCKING);
s.local.cli.callback = server_line_callback;
s.local.cli.userdata = &s;
nn_line_processor_init(&s.local.cli, &al_str_c("\n"));
nn_line_processor_open_socket(&s.local.cli, &s.local.sock);
if (nn_socket_bind(&s.local.sock, &al_str_c("/tmp/camu_control"), 0) && nn_socket_listen(&s.local.sock)) {
nn_line_processor_run(&s.local.cli, &s.loop);
}
#endif
#ifdef CMSRV_USE_UI
if (s.use_ui) {
nn_poll_init(&s.input_poll, input_poll_callback, &s);
nn_poll_set(&s.input_poll, cmsrv_ui_get_input_fd(&s.ui), NNWT_POLL_READ);
nn_poll_start(&s.input_poll, &s.loop);
nn_timer_init(&s.render_timer, &s.loop, render_timer_callback, &s);
nn_timer_set_repeat(&s.render_timer, NNWT_TS_FROM_USEC(100000));
nn_timer_again(&s.render_timer);
}
#endif
nn_event_loop_run(&s.loop);
#ifdef CMSRV_LOCAL_SOCKET
nn_socket_close(&s.local.sock);
#endif
#ifdef CMSRV_USE_UI
if (s.use_ui) cmsrv_ui_close(&s.ui);
#endif
camu_server_free(&s.server);
nn_event_loop_destroy(&s.loop);
nn_common_close();
(void)argc;
(void)argv;
return EXIT_SUCCESS;
}
|