unregister_task(): Print what caused the task to shutdown.
[paraslash.git] / client.c
index 69e0f3d3bf9c7382c3b78c012d5c1bfcd0fb6c7c..f47a0bec46a8c0d57f4ed2c22490a359ebbe8aed 100644 (file)
--- a/client.c
+++ b/client.c
@@ -1,19 +1,7 @@
 /*
- * Copyright (C) 1997-2006 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 1997-2008 Andre Noll <maan@systemlinux.org>
  *
- *     This program is free software; you can redistribute it and/or modify
- *     it under the terms of the GNU General Public License as published by
- *     the Free Software Foundation; either version 2 of the License, or
- *     (at your option) any later version.
- *
- *     This program is distributed in the hope that it will be useful,
- *     but WITHOUT ANY WARRANTY; without even the implied warranty of
- *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *     GNU General Public License for more details.
- *
- *     You should have received a copy of the GNU General Public License
- *     along with this program; if not, write to the Free Software
- *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Licensed under the GPL v2. For licencing details see COPYING.
  */
 
 /** \file client.c the client program used to connect to para_server */
 
 INIT_CLIENT_ERRLISTS;
 
-static struct private_client_data *pcd;
+static struct client_task *ct;
 static struct stdin_task sit;
 static struct stdout_task sot;
 
-/*
- * client log function
- */
-void para_log(int ll, const char* fmt,...)
+static void supervisor_pre_select(struct sched *s, struct task *t)
 {
-       va_list argp;
-
-       /* ignore log message if loglevel is not high enough */
-       if (!pcd || ll < pcd->conf.loglevel_arg)
-               return;
-       va_start(argp, fmt);
-       vfprintf(stderr, fmt, argp);
-       va_end(argp);
-}
-
-static void client_event_handler(struct task *t)
-{
-       struct private_client_data *p = t->private_data;
-
-       PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t->ret));
-       if (t->ret != -E_HANDSHAKE_COMPLETE) {
-               unregister_task(t);
-               p->eof = 1;
+       if (ct->task.error < 0) {
+               t->error = ct->task.error;
                return;
        }
-       if (p->status == CL_SENDING_STDIN) {
+       if (ct->status == CL_SENDING) {
                stdin_set_defaults(&sit);
                sit.buf = para_malloc(sit.bufsize),
                register_task(&sit.task);
-               p->inbuf = sit.buf;
-               p->in_loaded = &sit.loaded;
-               p->in_eof = &sit.eof;
-               return;
+               ct->inbuf = sit.buf;
+               ct->in_loaded = &sit.loaded;
+               ct->in_error = &sit.task.error;
+               t->error = -1;
+               goto min_delay;
        }
-       stdout_set_defaults(&sot);
-       sot.buf = p->buf;
-       sot.loaded = &p->loaded;
-       sot.input_eof = &p->eof;
-       register_task(&sot.task);
+       if (ct->status == CL_RECEIVING) {
+               stdout_set_defaults(&sot);
+               sot.buf = ct->buf;
+               sot.loaded = &ct->loaded;
+               sot.input_error = &ct->task.error;
+               register_task(&sot.task);
+               t->error = -1;
+               goto min_delay;
+       }
+       return;
+min_delay:
+       s->timeout.tv_sec = 0;
+       s->timeout.tv_usec = 1;
 }
 
-/*
- * MAIN
+static struct task svt = {
+       .pre_select = supervisor_pre_select
+};
+
+INIT_STDERR_LOGGING(ct->conf.loglevel_arg);
+
+
+/**
+ * The client program to connect to para_server.
+ *
+ * \param argc Usual argument count.
+ * \param argv Usual argument vector.
+ *
+ * It registers two tasks: The client task that communicates with para_server
+ * and the standard out task that writes any output produced by the client task
+ * to standard out.
+ *
+ * \return EXIT_SUCCESS or EXIT_FAILURE
+ *
+ * \sa client_open(), stdout.c, stdout.h, para_client(1), para_server(1)
  */
 int main(int argc, char *argv[])
 {
@@ -86,17 +82,13 @@ int main(int argc, char *argv[])
 
        s.default_timeout.tv_sec = 1;
        s.default_timeout.tv_usec = 0;
-       ret = client_parse_config(argc, argv, &pcd);
-       if (ret < 0)
-               goto out;
-       pcd->task.event_handler = client_event_handler;
-       ret = client_open(pcd);
-       if (ret < 0)
-               goto out;
-       ret = sched(&s);
-       client_close(pcd);
-out:
+       ret = client_open(argc, argv, &ct);
+       if (ret < 0) /* can not use PARA_LOG here */
+               exit(EXIT_FAILURE);
+       register_task(&svt);
+       ret = schedule(&s);
        if (ret < 0)
-               PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
+               PARA_ERROR_LOG("%s\n", para_strerror(-ret));
+       client_close(ct);
        return ret >= 0? EXIT_SUCCESS: EXIT_FAILURE;
 }