sched: Allow more than one running scheduler instance.
[paraslash.git] / recv.c
1 /*
2 * Copyright (C) 2005-2011 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file recv.c the stand-alone audio stream receiver */
8
9 #include <regex.h>
10 #include <sys/types.h>
11 #include <stdbool.h>
12
13 #include "para.h"
14 #include "list.h"
15 #include "sched.h"
16 #include "ggo.h"
17 #include "recv.h"
18 #include "recv.cmdline.h"
19 #include "fd.h"
20 #include "string.h"
21 #include "error.h"
22 #include "stdout.h"
23 #include "buffer_tree.h"
24 #include "version.h"
25
26 /** The gengetopt args info struct. */
27 static struct recv_args_info conf;
28
29 static int loglevel;
30 /** Always log to stderr. */
31 INIT_STDERR_LOGGING(loglevel);
32
33 /** init array of error codes used by para_recv */
34 INIT_RECV_ERRLISTS;
35
36 __noreturn static void print_help_and_die(void)
37 {
38 int d = conf.detailed_help_given;
39 const char **p = d? recv_args_info_detailed_help
40 : recv_args_info_help;
41
42 printf_or_die("%s\n\n", RECV_CMDLINE_PARSER_PACKAGE "-"
43 RECV_CMDLINE_PARSER_VERSION);
44 printf_or_die("%s\n\n", recv_args_info_usage);
45 for (; *p; p++)
46 printf_or_die("%s\n", *p);
47 print_receiver_helps(d);
48 exit(0);
49 }
50
51 static void *parse_config(int argc, char *argv[], int *receiver_num)
52 {
53 if (recv_cmdline_parser(argc, argv, &conf))
54 return NULL;
55 HANDLE_VERSION_FLAG("recv", conf);
56 if (conf.help_given || conf.detailed_help_given)
57 print_help_and_die();
58 loglevel = get_loglevel_by_name(conf.loglevel_arg);
59 return check_receiver_arg(conf.receiver_arg, receiver_num);
60 }
61
62 /**
63 * The main function of para_recv.
64 *
65 * \param argc number of arguments
66 * \param argv vector of arguments
67 *
68 * para_recv uses the specified receiver to receive an audio stream sent by
69 * para_server. The received data is written to stdout.
70 *
71 * \return \a EXIT_SUCCESS on success, \a EXIT_FAILURE on errors.
72 */
73 int main(int argc, char *argv[])
74 {
75 int ret, r_opened = 0, receiver_num;
76 struct receiver *r = NULL;
77 struct receiver_node rn;
78 struct stdout_task sot;
79 static struct sched s;
80
81 s.default_timeout.tv_sec = 1;
82 s.default_timeout.tv_usec = 0;
83
84 memset(&sot, 0, sizeof(struct stdout_task));
85 memset(&rn, 0, sizeof(struct receiver_node));
86 recv_init();
87 ret = -E_RECV_SYNTAX;
88 rn.conf = parse_config(argc, argv, &receiver_num);
89 if (!rn.conf) {
90 PARA_EMERG_LOG("parse failed\n");
91 goto out;
92 }
93 r = &receivers[receiver_num];
94 rn.receiver = r;
95 rn.btrn = btr_new_node(&(struct btr_node_description)
96 EMBRACE(.name = r->name));
97 ret = r->open(&rn);
98 if (ret < 0)
99 goto out;
100 r_opened = 1;
101
102 sot.btrn = btr_new_node(&(struct btr_node_description)
103 EMBRACE(.parent = rn.btrn, .name = "stdout"));
104 stdout_set_defaults(&sot);
105 register_task(&s, &sot.task);
106
107 rn.task.pre_select = r->pre_select;
108 rn.task.post_select = r->post_select;
109 sprintf(rn.task.status, "%s", r->name);
110 register_task(&s, &rn.task);
111
112 ret = schedule(&s);
113 out:
114 if (r_opened)
115 r->close(&rn);
116 btr_free_node(rn.btrn);
117 btr_free_node(sot.btrn);
118 if (rn.conf)
119 r->free_config(rn.conf);
120 if (ret < 0)
121 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
122 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
123 }