]> git.ozlabs.org Git - ccan-lca-2011.git/commitdiff
lca2011: add greeting and prefix states.
authorRusty Russell <rusty@rustcorp.com.au>
Tue, 25 Jan 2011 00:28:10 +0000 (10:58 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Tue, 25 Jan 2011 00:28:10 +0000 (10:58 +1030)
ccan/oserver/oserver.c
ccan/oserver/test/run.c

index 076ba70b4c15930eeb7cfde0ffb6e2eed1a38a57..b8959035e1cadc8056f879892f541a74ea7c55d3 100644 (file)
 #include <signal.h>
 
 enum state {
+       SENDING_GREETING,
        RECEIVING_USER_QUESTION,
+       SENDING_ANSWER_PREFIX,
        SENDING_ANSWER,
        FINISHED
 };
 
 static uint16_t state_flag_map[] = {
+       [SENDING_GREETING]              = TEVENT_FD_WRITE,
        [RECEIVING_USER_QUESTION]       = TEVENT_FD_READ,
+       [SENDING_ANSWER_PREFIX]         = TEVENT_FD_WRITE,
        [SENDING_ANSWER]                = TEVENT_FD_WRITE,
        [FINISHED]                      = 0
 };
@@ -105,6 +109,10 @@ 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)
@@ -114,9 +122,13 @@ static void service_client(struct tevent_context *ev,
 
                        for (i = 0; c->question[i]; i++)
                                c->question[i] = toupper(c->question[i]);
-                       set_state(c, SENDING_ANSWER);
+                       set_state(c, SENDING_ANSWER_PREFIX);
                }
                break;
+       case SENDING_ANSWER_PREFIX:
+               if (!send_string(c, "Our answer is:\n"))
+                       goto fail;
+               break;
        case SENDING_ANSWER:
                if (!send_string(c, c->question))
                        goto fail;
@@ -159,7 +171,7 @@ 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;
index f99e40bde103e57d511fd4830d916b24115e548a..b093a560c56fa273745134d4e360799328c57ec6 100644 (file)
@@ -75,7 +75,7 @@ int main(int argc, char *argv[])
        char c;
 
        /* This is how many tests you plan to run */
-       plan_tests(13);
+       plan_tests(15);
 
        pipe(readyfd);
        pipe(exitfd);
@@ -103,6 +103,9 @@ int main(int argc, char *argv[])
        ok1(connect(sfd1, &u.addr, sizeof(u.in)) == 0);
        ok1(connect(sfd2, &u.addr, sizeof(u.in)) == 0);
 
+       ok1(input_is(sfd1, "Welcome.  Please ask your question.\n"));
+       ok1(input_is(sfd2, "Welcome.  Please ask your question.\n"));
+
        ok1(write_sall(sfd1, "question"));
        ok1(write_sall(sfd2, "question"));
        /* It shouldn't say anything until we've finished! */
@@ -112,8 +115,8 @@ int main(int argc, char *argv[])
        ok1(write_sall(sfd1, " 1\n"));
        ok1(write_sall(sfd2, " 2\n"));
 
-       ok1(input_is(sfd1, "QUESTION 1\n"));
-       ok1(input_is(sfd2, "QUESTION 2\n"));
+       ok1(input_is(sfd1, "Our answer is:\nQUESTION 1\n"));
+       ok1(input_is(sfd2, "Our answer is:\nQUESTION 2\n"));
 
        /* Sockets should be dead now. */
        ok1(read(sfd1, &c, 1) == 0);