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