]> git.ozlabs.org Git - ccan-lca-2011.git/blobdiff - ccan/oserver/oserver.c
lca2011: move types out to a separate header.
[ccan-lca-2011.git] / ccan / oserver / oserver.c
index 076ba70b4c15930eeb7cfde0ffb6e2eed1a38a57..317a01cc5e2f177bcf7b9337c88560958b85d035 100644 (file)
@@ -1,4 +1,5 @@
 #include <ccan/oserver/oserver.h>
+#include <ccan/oserver/oserver_types.h>
 #include <ccan/read_write_all/read_write_all.h>
 #include <ccan/opt/opt.h>
 #include <ccan/tevent/tevent.h>
 #include <string.h>
 #include <errno.h>
 #include <signal.h>
-
-enum state {
-       RECEIVING_USER_QUESTION,
-       SENDING_ANSWER,
-       FINISHED
-};
+#include <assert.h>
 
 static uint16_t state_flag_map[] = {
+       [SENDING_GREETING]              = TEVENT_FD_WRITE,
        [RECEIVING_USER_QUESTION]       = TEVENT_FD_READ,
+       [SENDING_OTHER_QUESTION_PREFIX] = TEVENT_FD_WRITE,
+       [SENDING_OTHER_QUESTION]        = TEVENT_FD_WRITE,
+       [RECEIVING_OTHER_ANSWER]        = TEVENT_FD_READ,
+       [SENDING_ANSWER_PREFIX]         = TEVENT_FD_WRITE,
        [SENDING_ANSWER]                = TEVENT_FD_WRITE,
        [FINISHED]                      = 0
 };
 
-struct client {
-       /* What are we doing today, brain? */
-       enum state state;
-       /* Our event info, and the file descriptor. */
-       struct tevent_fd *fde;
-       int fd;
-       /* The question we read from client. */
-       char *question;
-       /* How many bytes of the reply we sent so far. */
-       size_t bytes_sent;
-       /* Our server. */
-       struct oserver *oserver;
-};
-
-struct oserver {
-       /* 5 clients should be enough for anybody! */
-       struct client *clients[5];
-       int fd;
-       struct tevent_fd *fde;
-};
-
 static ssize_t write_string(int fd, const char *str)
 {
        return write(fd, str, strlen(str));
@@ -98,6 +78,45 @@ static bool send_string(struct client *c, const char *str)
        return true;
 }
 
+static bool get_subclient(struct client *me)
+{
+       unsigned int i;
+
+       for (i = 0; i < ARRAY_SIZE(me->oserver->clients); i++) {
+               struct client *c = me->oserver->clients[i];
+               if (!c || c == me)
+                       continue;
+               if (c->oracle == NULL && input_finished(c->question)) {
+                       me->subclient = c;
+                       c->oracle = me;
+                       return true;
+               }
+       }
+       return false;
+}
+
+static bool get_oracle(struct client *me)
+{
+       unsigned int i;
+
+       for (i = 0; i < ARRAY_SIZE(me->oserver->clients); i++) {
+               struct client *c = me->oserver->clients[i];
+               if (!c || c == me)
+                       continue;
+               if (c->subclient == NULL && input_finished(c->question)) {
+                       me->oracle = c;
+                       c->subclient = me;
+                       return true;
+               }
+       }
+       return false;
+}
+
+static void wakeup(struct client *c)
+{
+       tevent_fd_set_flags(c->fde, state_flag_map[c->state]);
+}
+
 static void service_client(struct tevent_context *ev,
                           struct tevent_fd *fde, uint16_t flags, void *_c)
 {
@@ -105,20 +124,49 @@ static void service_client(struct tevent_context *ev,
        ssize_t len;
 
        switch (c->state) {
+       case SENDING_GREETING:
+               if (!send_string(c, "Welcome.  Please ask your question.\n"))
+                       goto fail;
+               break;
        case RECEIVING_USER_QUESTION:
                len = read_string(c->fd, &c->question);
                if (len <= 0)
                        goto fail;
-               if (input_finished(c->question)) {
-                       unsigned int i;
-
-                       for (i = 0; c->question[i]; i++)
-                               c->question[i] = toupper(c->question[i]);
-                       set_state(c, SENDING_ANSWER);
+               if (input_finished(c->question))
+                       set_state(c, SENDING_OTHER_QUESTION_PREFIX);
+               break;
+       case SENDING_OTHER_QUESTION_PREFIX:
+               if (!c->subclient)
+                       goto need_subclient;
+               if (!send_string(c, "While the Oracle ponders,"
+                                " please answer the following question:\n"))
+                       goto fail;
+               break;
+       case SENDING_OTHER_QUESTION:
+               if (!c->subclient)
+                       goto need_subclient;
+               if (!send_string(c, c->subclient->question))
+                       goto fail;
+               break;
+       case RECEIVING_OTHER_ANSWER:
+               if (!c->subclient)
+                       goto need_subclient;
+               len = read_string(c->fd, &c->subclient->answer);
+               if (len <= 0)
+                       goto fail;
+               if (input_finished(c->subclient->answer)) {
+                       set_state(c, SENDING_ANSWER_PREFIX);
+                       wakeup(c->subclient);
                }
                break;
+       case SENDING_ANSWER_PREFIX:
+               if (!input_finished(c->answer))
+                       goto need_answer;
+               if (!send_string(c, "The Oracle spake thus:\n"))
+                       goto fail;
+               break;
        case SENDING_ANSWER:
-               if (!send_string(c, c->question))
+               if (!send_string(c, c->answer))
                        goto fail;
                break;
        default:
@@ -127,15 +175,43 @@ static void service_client(struct tevent_context *ev,
 
        if (c->state != FINISHED)
                return;
-
 fail:
        talloc_free(c);
+       return;
+
+need_subclient:
+       if (!get_subclient(c)) {
+               /* We can't get one: go to sleep until someone find_oracle() */
+               tevent_fd_set_flags(c->fde, 0);
+       } else
+               /* In case they are waiting... */
+               wakeup(c->subclient);
+       return;
+
+need_answer:
+       /* If we don't have an oracle and find one, that's OK. */
+       if (!c->oracle && get_oracle(c)) {
+               /* In case they are waiting... */
+               wakeup(c->oracle);
+               return;
+       }
+
+       /* Either our oracle is not finished, or we don't have one: sleep. */
+       tevent_fd_set_flags(c->fde, 0);
 }
 
 static int cleanup_client(struct client *client)
 {
        unsigned int i;
 
+       /* We were an oracle? */
+       if (client->subclient)
+               client->subclient->oracle = NULL;
+
+       /* We had an oracle? */
+       if (client->oracle)
+               client->oracle->subclient = NULL;
+
        for (i = 0; i < ARRAY_SIZE(client->oserver->clients); i++) {
                if (client->oserver->clients[i] == client) {
                        client->oserver->clients[i] = NULL;
@@ -159,10 +235,13 @@ static void add_client(struct tevent_context *ev,
        if (client->fd < 0)
                err(1, "Accepting client connection");
 
-       client->state = RECEIVING_USER_QUESTION;
+       client->state = SENDING_GREETING;
        client->bytes_sent = 0;
        client->question = talloc_strdup(client, "");
        client->oserver = oserver;
+       client->oracle = NULL;
+       client->subclient = NULL;
+       client->answer = talloc_strdup(client, "");
        client->fde = tevent_add_fd(ev, client, client->fd,
                                    state_flag_map[client->state],
                                    service_client, client);
@@ -190,6 +269,27 @@ static int destroy_oserver(struct oserver *oserver)
        return 0;
 }
 
+static void talloc_dump(struct tevent_context *ev,
+                       struct tevent_signal *se,
+                       int signum,
+                       int count,
+                       void *siginfo,
+                       void *_oserver)
+{
+       struct oserver *oserver = _oserver;
+       FILE *f;
+
+       /* Fork off a child for the report, so we aren't stopped. */
+       if (fork() == 0) {
+               f = fopen("/var/run/oserver/talloc.dump", "w");
+               if (f) {
+                       talloc_report_full(oserver, f);
+                       fclose(f);
+               }
+               _exit(0);
+       }
+}
+
 struct oserver *oserver_setup(struct tevent_context *ev, unsigned short port)
 {
        struct oserver *oserver;
@@ -206,7 +306,7 @@ struct oserver *oserver_setup(struct tevent_context *ev, unsigned short port)
                talloc_free(oserver);
                return NULL;
        }
-       
+
        talloc_set_destructor(oserver, destroy_oserver);
 
        if (setsockopt(oserver->fd, SOL_SOCKET, SO_REUSEADDR,
@@ -236,5 +336,9 @@ struct oserver *oserver_setup(struct tevent_context *ev, unsigned short port)
        /* Don't kill us if client dies. */
        signal(SIGPIPE, SIG_IGN);
 
+       /* Show talloc tree on SIGUSR1. */
+       tevent_add_signal(ev, oserver, SIGUSR1, SA_RESTART,
+                         talloc_dump, oserver);
+
        return oserver;
 }