Make it compile on Solaris.
[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 <sys/types.h>
10 #include <dirent.h>
11
12 #include "para.h"
13 #include "list.h"
14 #include "sched.h"
15 #include "recv.h"
16 #include "recv.cmdline.h"
17 #include "fd.h"
18 #include "error.h"
19 #include "stdout.h"
20
21 /** the gengetopt args info struct */
22 struct recv_args_info conf;
23
24 /** always log to stderr */
25 INIT_STDERR_LOGGING(conf.loglevel_arg);
26
27 /** init array of error codes used by para_recv */
28 INIT_RECV_ERRLISTS;
29
30 static void *parse_config(int argc, char *argv[], int *receiver_num)
31 {
32         int i;
33
34         if (recv_cmdline_parser(argc, argv, &conf))
35                 return NULL;
36         HANDLE_VERSION_FLAG("recv", conf);
37         if (conf.list_receivers_given) {
38                 printf("available receivers: ");
39                 for (i = 0; receivers[i].name; i++)
40                         printf("%s%s", i? " " : "", receivers[i].name);
41                 printf("\nTry\n\tpara_recv -r '<receivername> -h'\n"
42                         "for help on <receivername>.\n");
43                 exit(EXIT_SUCCESS);
44         }
45         return check_receiver_arg(conf.receiver_arg, receiver_num);
46 }
47
48 static void rn_event_handler(struct task *t)
49 {
50         struct receiver_node *rn = t->private_data;
51         PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t->ret));
52         rn->eof = 1;
53         unregister_task(t);
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         struct sched s;
74
75         s.default_timeout.tv_sec = 1;
76         s.default_timeout.tv_usec = 0;
77
78         memset(&rn, 0, sizeof(struct receiver_node));
79         for (ret = 0; receivers[ret].name; ret++)
80                 receivers[ret].init(&receivers[ret]);
81         ret = -E_RECV_SYNTAX;
82         rn.conf = parse_config(argc, argv, &receiver_num);
83         if (!rn.conf) {
84                 PARA_EMERG_LOG("%s", "parse failed\n");
85                 goto out;
86         }
87         r = &receivers[receiver_num];
88         rn.receiver = r;
89         ret = r->open(&rn);
90         if (ret < 0)
91                 goto out;
92         r_opened = 1;
93
94         stdout_set_defaults(&sot);
95         sot.buf = rn.buf;
96         sot.loaded = &rn.loaded;
97         sot.input_eof = &rn.eof;
98         register_task(&sot.task);
99
100         rn.task.private_data = &rn;
101         rn.task.pre_select = r->pre_select;
102         rn.task.post_select = r->post_select;
103         rn.task.event_handler = rn_event_handler;
104         sprintf(rn.task.status, "receiver node");
105         register_task(&rn.task);
106
107         ret = sched(&s);
108 out:
109         if (r_opened)
110                 r->close(&rn);
111         if (r)
112                 r->shutdown();
113         if (ret < 0)
114                 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
115         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
116 }