be76a491b3e1db0ce15efd194611cde12f719ccf
[paraslash.git] / client.c
1 /*
2  * Copyright (C) 1997-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 client.c the client program used to connect to para_server */
20
21 #include "para.h"
22 #include "list.h"
23 #include "sched.h"
24 #include "client.cmdline.h"
25 #include "string.h"
26 #include "stdin.h"
27 #include "stdout.h"
28 #include "client.h"
29 #include "error.h"
30
31 INIT_CLIENT_ERRLISTS;
32
33 static struct private_client_data *pcd;
34 static struct stdin_task sit;
35 static struct stdout_task sot;
36
37
38 INIT_STDERR_LOGGING(pcd->conf.loglevel_arg);
39
40 static void client_event_handler(struct task *t)
41 {
42         struct private_client_data *p = t->private_data;
43
44         PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t->ret));
45         if (t->ret != -E_HANDSHAKE_COMPLETE) {
46                 unregister_task(t);
47                 p->eof = 1;
48                 return;
49         }
50         if (p->status == CL_SENDING) {
51                 stdin_set_defaults(&sit);
52                 sit.buf = para_malloc(sit.bufsize),
53                 register_task(&sit.task);
54                 p->inbuf = sit.buf;
55                 p->in_loaded = &sit.loaded;
56                 p->in_eof = &sit.eof;
57                 return;
58         }
59         stdout_set_defaults(&sot);
60         sot.buf = p->buf;
61         sot.loaded = &p->loaded;
62         sot.input_eof = &p->eof;
63         register_task(&sot.task);
64 }
65
66 /*
67  * MAIN
68  */
69 int main(int argc, char *argv[])
70 {
71
72         int ret;
73         struct sched s;
74
75         s.default_timeout.tv_sec = 1;
76         s.default_timeout.tv_usec = 0;
77         ret = client_parse_config(argc, argv, &pcd);
78         if (ret < 0)
79                 goto out;
80         pcd->task.event_handler = client_event_handler;
81         ret = client_open(pcd);
82         if (ret < 0)
83                 goto out;
84         ret = sched(&s);
85         client_close(pcd);
86 out:
87         if (ret < 0)
88                 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
89         return ret >= 0? EXIT_SUCCESS: EXIT_FAILURE;
90 }