From a3f893b6ac5fd0040a0610711a7eded786681d2b Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 21 Jan 2011 15:59:42 +1030 Subject: [PATCH] lca2011: make our server parrot the last answer it got. Include my own fairly lame attempt to match up with the woodchuck question. --- ccan/oserver/oserver.c | 19 ++++++++++++------- ccan/oserver/test/run.c | 8 +++++--- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/ccan/oserver/oserver.c b/ccan/oserver/oserver.c index b895903..9e1f2b0 100644 --- a/ccan/oserver/oserver.c +++ b/ccan/oserver/oserver.c @@ -40,6 +40,8 @@ struct client { int fd; /* The question we read from client. */ char *question; + /* The answer to the client. */ + const char *answer; /* How many bytes of the reply we sent so far. */ size_t bytes_sent; /* Our server. */ @@ -51,6 +53,7 @@ struct oserver { struct client *clients[5]; int fd; struct tevent_fd *fde; + const char *last_answer; }; static ssize_t write_string(int fd, const char *str) @@ -118,19 +121,18 @@ static void service_client(struct tevent_context *ev, 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]); + c->answer = talloc_steal(c, c->oserver->last_answer); + c->oserver->last_answer + = talloc_steal(c->oserver, c->question); set_state(c, SENDING_ANSWER_PREFIX); } break; case SENDING_ANSWER_PREFIX: - if (!send_string(c, "Our answer is:\n")) + if (!send_string(c, "I believe a better question is ")) goto fail; break; case SENDING_ANSWER: - if (!send_string(c, c->question)) + if (!send_string(c, c->answer)) goto fail; break; default: @@ -218,7 +220,10 @@ struct oserver *oserver_setup(struct tevent_context *ev, unsigned short port) talloc_free(oserver); return NULL; } - + oserver->last_answer = talloc_strdup(oserver, + "how many manly men mendaciously" + " mention mending mansions?\n"); + talloc_set_destructor(oserver, destroy_oserver); if (setsockopt(oserver->fd, SOL_SOCKET, SO_REUSEADDR, diff --git a/ccan/oserver/test/run.c b/ccan/oserver/test/run.c index b093a56..e553f3d 100644 --- a/ccan/oserver/test/run.c +++ b/ccan/oserver/test/run.c @@ -22,10 +22,10 @@ static void run_server(int readyfd, int exitfd) if (oserver_setup(ev, OSERVER_PORT) == NULL) exit(1); + tevent_add_fd(ev, ev, exitfd, TEVENT_FD_READ, exit_quietly, NULL); /* Tell parent we are ready to go. */ write(readyfd, "", 1); - tevent_add_fd(ev, ev, exitfd, TEVENT_FD_READ, exit_quietly, NULL); while (tevent_loop_wait(ev) == 0); } @@ -113,10 +113,12 @@ int main(int argc, char *argv[]) ok1(no_input(sfd2)); ok1(write_sall(sfd1, " 1\n")); + /* Make sure that arrives first! */ + sleep(1); ok1(write_sall(sfd2, " 2\n")); - ok1(input_is(sfd1, "Our answer is:\nQUESTION 1\n")); - ok1(input_is(sfd2, "Our answer is:\nQUESTION 2\n")); + ok1(input_is(sfd1, "I believe a better question is how many manly men mendaciously mention mending mansions?\n")); + ok1(input_is(sfd2, "I believe a better question is question 1\n")); /* Sockets should be dead now. */ ok1(read(sfd1, &c, 1) == 0); -- 2.39.2