|
| 1 | +#include <amongoc/amongoc.h> // Make all APIs visible |
| 2 | + |
| 3 | +#include <stdio.h> |
| 4 | +#include <stdlib.h> |
| 5 | + |
| 6 | +amongoc_box on_connect(amongoc_box userdata, amongoc_status *status, amongoc_box result); |
| 7 | + |
| 8 | +int main(void) |
| 9 | +{ |
| 10 | + amongoc_loop loop; |
| 11 | + amongoc_status status = amongoc_default_loop_init(&loop); |
| 12 | + amongoc_if_error(status, msg) |
| 13 | + { |
| 14 | + fprintf(stderr, "Failed to prepare the event loop: %s\n", msg); |
| 15 | + return 2; |
| 16 | + } |
| 17 | + |
| 18 | + // Initiate a connection |
| 19 | + amongoc_emitter em = amongoc_client_new(&loop, "mongodb://localhost:27017"); |
| 20 | + // Set the continuation |
| 21 | + em = amongoc_then(em, &on_connect); |
| 22 | + // Run the program |
| 23 | + amongoc_detach_start(em); |
| 24 | + amongoc_default_loop_run(&loop); |
| 25 | + // Clean up |
| 26 | + amongoc_default_loop_destroy(&loop); |
| 27 | + return 0; |
| 28 | +} |
| 29 | + |
| 30 | +amongoc_box on_connect(amongoc_box userdata, amongoc_status *status, amongoc_box result) |
| 31 | +{ |
| 32 | + // We don't use the userdata |
| 33 | + (void)userdata; |
| 34 | + // Check for an error |
| 35 | + amongoc_if_error(*status, msg) |
| 36 | + { |
| 37 | + fprintf(stderr, "Error while connecting to server: %s\n", msg); |
| 38 | + } |
| 39 | + else |
| 40 | + { |
| 41 | + printf("Successfully connected!\n"); |
| 42 | + amongoc_client *client; |
| 43 | + amongoc_box_take(client, result); |
| 44 | + // `client` now stores a valid client. We don't do anything else, so just delete it: |
| 45 | + amongoc_client_delete(client); |
| 46 | + } |
| 47 | + amongoc_box_destroy(result); |
| 48 | + return amongoc_nil; |
| 49 | +} |
0 commit comments