client: combine client_open() and client_parse_config()
[paraslash.git] / recv.c
1 /*
2 * Copyright (C) 2005-2007 Andre Noll <maan@systemlinux.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
17 */
18
19 /** \file recv.c the stand-alone audio stream receiver */
20
21 #include "para.h"
22
23 #include "list.h"
24 #include "sched.h"
25 #include "recv.h"
26 #include "recv.cmdline.h"
27 #include "fd.h"
28 #include "error.h"
29 #include "stdout.h"
30
31 /** the gengetopt args info struct */
32 struct recv_args_info conf;
33
34 /** always log to stderr */
35 INIT_STDERR_LOGGING(conf.loglevel_arg);
36
37 /** init array of error codes used by para_recv */
38 INIT_RECV_ERRLISTS;
39
40 static void *parse_config(int argc, char *argv[], int *receiver_num)
41 {
42 int i;
43
44 if (recv_cmdline_parser(argc, argv, &conf))
45 return NULL;
46 if (conf.list_receivers_given) {
47 printf("available receivers: ");
48 for (i = 0; receivers[i].name; i++)
49 printf("%s%s", i? " " : "", receivers[i].name);
50 printf("\nTry\n\tpara_recv -r '<receivername> -h'\n"
51 "for help on <receivername>.\n");
52 exit(EXIT_SUCCESS);
53 }
54 return check_receiver_arg(conf.receiver_arg, receiver_num);
55 }
56
57 static void rn_event_handler(struct task *t)
58 {
59 struct receiver_node *rn = t->private_data;
60 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t->ret));
61 rn->eof = 1;
62 unregister_task(t);
63 }
64
65 /**
66 * the main function of para_recv
67 *
68 * \param argc number of arguments
69 * \param argv vector of arguments
70 *
71 * para_recv uses the specified receiver to receive an audio stream sent by
72 * para_server. The received data is written to stdout.
73 *
74 * \return \a EXIT_SUCCESS on success, \a EXIT_FAILURE on errors.
75 */
76 int main(int argc, char *argv[])
77 {
78 int ret, r_opened = 0, receiver_num;
79 struct receiver *r = NULL;
80 struct receiver_node rn;
81 struct stdout_task sot;
82 struct sched s;
83
84 s.default_timeout.tv_sec = 1;
85 s.default_timeout.tv_usec = 0;
86
87 memset(&rn, 0, sizeof(struct receiver_node));
88 for (ret = 0; receivers[ret].name; ret++)
89 receivers[ret].init(&receivers[ret]);
90 ret = -E_RECV_SYNTAX;
91 rn.conf = parse_config(argc, argv, &receiver_num);
92 if (!rn.conf) {
93 PARA_EMERG_LOG("%s", "parse failed\n");
94 goto out;
95 }
96 r = &receivers[receiver_num];
97 rn.receiver = r;
98 ret = r->open(&rn);
99 if (ret < 0)
100 goto out;
101 r_opened = 1;
102
103 stdout_set_defaults(&sot);
104 sot.buf = rn.buf;
105 sot.loaded = &rn.loaded;
106 sot.input_eof = &rn.eof;
107 register_task(&sot.task);
108
109 rn.task.private_data = &rn;
110 rn.task.pre_select = r->pre_select;
111 rn.task.post_select = r->post_select;
112 rn.task.event_handler = rn_event_handler;
113 sprintf(rn.task.status, "receiver node");
114 register_task(&rn.task);
115
116 ret = sched(&s);
117 out:
118 if (r_opened)
119 r->close(&rn);
120 if (r)
121 r->shutdown();
122 if (ret < 0)
123 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
124 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
125 }