Merge commit 'meins/master'
[paraslash.git] / recv.c
1 /*
2  * Copyright (C) 2005-2008 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 <sys/types.h>
10 #include <dirent.h>
11
12 #include "para.h"
13 #include "list.h"
14 #include "sched.h"
15 #include "ggo.h"
16 #include "recv.h"
17 #include "recv.cmdline.h"
18 #include "fd.h"
19 #include "error.h"
20 #include "stdout.h"
21
22 /** the gengetopt args info struct */
23 struct recv_args_info conf;
24
25 /** always log to stderr */
26 INIT_STDERR_LOGGING(conf.loglevel_arg);
27
28 /** init array of error codes used by para_recv */
29 INIT_RECV_ERRLISTS;
30
31 __noreturn static void print_help_and_die(void)
32 {
33         int d = conf.detailed_help_given;
34         const char **p = d? recv_args_info_detailed_help
35                 : recv_args_info_help;
36
37         printf_or_die("%s\n\n", RECV_CMDLINE_PARSER_PACKAGE "-"
38                 RECV_CMDLINE_PARSER_VERSION);
39         printf_or_die("%s\n\n", recv_args_info_usage);
40         for (; *p; p++)
41                 printf_or_die("%s\n", *p);
42         print_receiver_helps(d);
43         exit(0);
44 }
45
46 static void *parse_config(int argc, char *argv[], int *receiver_num)
47 {
48         if (recv_cmdline_parser(argc, argv, &conf))
49                 return NULL;
50         HANDLE_VERSION_FLAG("recv", conf);
51         if (conf.help_given || conf.detailed_help_given)
52                 print_help_and_die();
53         return check_receiver_arg(conf.receiver_arg, receiver_num);
54 }
55
56 /**
57  * the main function of para_recv
58  *
59  * \param argc number of arguments
60  * \param argv vector of arguments
61  *
62  * para_recv uses the specified receiver to receive an audio stream sent by
63  * para_server. The received data is written to stdout.
64  *
65  * \return \a EXIT_SUCCESS on success, \a EXIT_FAILURE on errors.
66  */
67 int main(int argc, char *argv[])
68 {
69         int ret, r_opened = 0, receiver_num;
70         struct receiver *r = NULL;
71         struct receiver_node rn;
72         struct stdout_task sot;
73         static struct sched s;
74
75         s.default_timeout.tv_sec = 1;
76         s.default_timeout.tv_usec = 0;
77
78         memset(&sot, 0, sizeof(struct stdout_task));
79         memset(&rn, 0, sizeof(struct receiver_node));
80         FOR_EACH_RECEIVER(ret)
81                 receivers[ret].init(&receivers[ret]);
82         ret = -E_RECV_SYNTAX;
83         rn.conf = parse_config(argc, argv, &receiver_num);
84         if (!rn.conf) {
85                 PARA_EMERG_LOG("parse failed\n");
86                 goto out;
87         }
88         r = &receivers[receiver_num];
89         rn.receiver = r;
90         ret = r->open(&rn);
91         if (ret < 0)
92                 goto out;
93         r_opened = 1;
94
95         stdout_set_defaults(&sot);
96         sot.buf = rn.buf;
97         sot.loaded = &rn.loaded;
98         sot.input_error = &rn.task.error;
99         register_task(&sot.task);
100
101         rn.task.pre_select = r->pre_select;
102         rn.task.post_select = r->post_select;
103         sprintf(rn.task.status, "receiver node");
104         register_task(&rn.task);
105
106         ret = schedule(&s);
107 out:
108         if (r_opened)
109                 r->close(&rn);
110         if (r)
111                 r->shutdown();
112         if (ret < 0)
113                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
114         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
115 }