summaryrefslogtreecommitdiffstats
path: root/bindings/samples/tic-tac-toe.c
diff options
context:
space:
mode:
authorJose Bollo <jose.bollo@iot.bzh>2018-08-01 10:24:53 +0200
committerJosé Bollo <jose.bollo@iot.bzh>2018-08-02 15:49:43 +0200
commitdf67c029363f6d42a3e91cd62774c74b3ef66670 (patch)
tree8f29c9cf3462eacf203baf941f8e92102a49ac14 /bindings/samples/tic-tac-toe.c
parentb4455a71edcf9030c3b64c8bc13f9d50f1624aa4 (diff)
tic-tac-toe: improve event handling
Change-Id: Ifba67b4b83650f9b32f78d077d0bbd3d3882238b Signed-off-by: Jose Bollo <jose.bollo@iot.bzh>
Diffstat (limited to 'bindings/samples/tic-tac-toe.c')
-rw-r--r--bindings/samples/tic-tac-toe.c32
1 files changed, 17 insertions, 15 deletions
diff --git a/bindings/samples/tic-tac-toe.c b/bindings/samples/tic-tac-toe.c
index 2ee251cb..c6acb7c3 100644
--- a/bindings/samples/tic-tac-toe.c
+++ b/bindings/samples/tic-tac-toe.c
@@ -64,7 +64,7 @@ static struct board *search_board(int id)
/*
* Creates a new board and returns it.
*/
-static struct board *get_new_board(afb_req_t req)
+static struct board *get_new_board()
{
/* allocation */
struct board *board = calloc(1, sizeof *board);
@@ -78,7 +78,6 @@ static struct board *get_new_board(afb_req_t req)
board->id = (rand() >> 2) % 1000;
} while(board->id == 0 || search_board(board->id) != NULL);
board->event = afb_daemon_make_event("board");
- afb_req_subscribe(req, board->event);
/* link */
board->next = all_boards;
@@ -86,12 +85,6 @@ static struct board *get_new_board(afb_req_t req)
return board;
}
-static void *get_new_board_cb(void *closure)
-{
- afb_req_t req = closure;
- return get_new_board(req);
-}
-
/*
* Release a board
*/
@@ -112,12 +105,6 @@ static void release_board(struct board *board)
}
}
-static void release_board_cb(void *closure)
-{
- struct board *board = closure;
- return release_board(board);
-}
-
/*
* Checks who wins
* Returns zero if there is no winner
@@ -317,6 +304,20 @@ static void changed(struct board *board, const char *reason)
afb_event_push(board->event, json_object_new_string(reason));
}
+static void *get_new_board_cb(void *closure)
+{
+ afb_req_t req = closure;
+ struct board *board = get_new_board();
+ afb_req_subscribe(req, board->event);
+ return board;
+}
+
+static void release_board_cb(void *closure)
+{
+ struct board *board = closure;
+ return release_board(board);
+}
+
/*
* retrieves the board of the request
*/
@@ -491,8 +492,9 @@ static void join(afb_req_t req)
new_board->use_count++;
setctx:
/* set the new board (and leaves the previous one) */
- afb_req_context(req, 1, NULL, release_board_cb, new_board);
afb_req_unsubscribe(req, board->event);
+ afb_req_context(req, 1, NULL, release_board_cb, new_board);
+ afb_req_subscribe(req, new_board->event);
success:
/* replies */