http_send: Send http OK message earlier.
[paraslash.git] / client.c
1 /*
2  * Copyright (C) 1997-2009 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file client.c the client program used to connect to para_server */
8
9 #include "para.h"
10 #include "list.h"
11 #include "sched.h"
12 #include "client.cmdline.h"
13 #include "string.h"
14 #include "stdin.h"
15 #include "stdout.h"
16 #include "client.h"
17 #include "error.h"
18
19 INIT_CLIENT_ERRLISTS;
20
21 static struct client_task *ct;
22 static struct stdin_task sit;
23 static struct stdout_task sot;
24
25 static void supervisor_post_select(__a_unused struct sched *s, struct task *t)
26 {
27         if (ct->task.error < 0) {
28                 t->error = ct->task.error;
29                 return;
30         }
31         if (ct->status == CL_SENDING) {
32                 stdin_set_defaults(&sit);
33                 sit.buf = para_malloc(sit.bufsize),
34                 register_task(&sit.task);
35                 ct->inbuf = sit.buf;
36                 ct->in_loaded = &sit.loaded;
37                 ct->in_error = &sit.task.error;
38                 t->error = -E_TASK_STARTED;
39                 return;
40         }
41         if (ct->status == CL_RECEIVING) {
42                 stdout_set_defaults(&sot);
43                 sot.bufp = &ct->buf;
44                 sot.loaded = &ct->loaded;
45                 sot.input_error = &ct->task.error;
46                 register_task(&sot.task);
47                 t->error = -E_TASK_STARTED;
48                 return;
49         }
50 }
51
52 static struct task svt = {
53         .post_select = supervisor_post_select,
54         .status = "supervisor task"
55 };
56
57 static int client_loglevel; /* loglevel */
58 INIT_STDERR_LOGGING(client_loglevel);
59
60
61 /**
62  * The client program to connect to para_server.
63  *
64  * \param argc Usual argument count.
65  * \param argv Usual argument vector.
66  *
67  * It registers two tasks: The client task that communicates with para_server
68  * and the standard out task that writes any output produced by the client task
69  * to standard out.
70  *
71  * \return EXIT_SUCCESS or EXIT_FAILURE
72  *
73  * \sa client_open(), stdout.c, stdout.h, para_client(1), para_server(1)
74  */
75 int main(int argc, char *argv[])
76 {
77
78         int ret;
79         static struct sched s;
80
81         s.default_timeout.tv_sec = 1;
82         s.default_timeout.tv_usec = 0;
83         ret = client_open(argc, argv, &ct, &client_loglevel);
84         if (ret < 0) /* can not use PARA_LOG here because ct is NULL */
85                 exit(EXIT_FAILURE);
86         register_task(&svt);
87         ret = schedule(&s);
88         if (ret < 0)
89                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
90         client_close(ct);
91         return ret >= 0? EXIT_SUCCESS: EXIT_FAILURE;
92 }