]> git.ozlabs.org Git - ccan-lca-2011.git/blob - ccan/oserver/_info
Add slides images.
[ccan-lca-2011.git] / ccan / oserver / _info
1 #include <string.h>
2 #include "config.h"
3
4 /**
5  * oserver - a demonstration module for LCA2011
6  *
7  * This code shows off the features of CCAN and various C cantrips.
8  *
9  * License: GPL
10  *
11  * Example:
12  *      #include <sys/types.h>
13  *      #include <sys/socket.h>
14  *      #include <ccan/oserver/oserver.h>
15  *      #include <ccan/opt/opt.h>
16  *      #include <err.h>
17  *      #include <stdlib.h>
18  *
19  *      #define STATE_FILE "/var/run/oserver/dump"
20  *
21  *      int main(int argc, char *argv[])
22  *      {
23  *              struct tevent_context *ev = tevent_context_init(NULL);
24  *              unsigned int port = OSERVER_PORT;
25  *              bool restore = false;
26  *              char **restore_argv;
27  *
28  *              // Save args before opt_parse mangles them.
29  *              restore_argv = talloc_array(NULL, char *, argc+2);
30  *              restore_argv[0] = argv[0];
31  *              restore_argv[1] = "--restore";
32  *              memcpy(restore_argv+2, argv+1, sizeof(argv[0])*argc);
33  *
34  *              opt_register_noarg("--help|--usage|-h", opt_usage_and_exit,
35  *                                 "\nA oserver program", "Usage information");
36  *              opt_register_arg("--port", opt_set_uintval, opt_show_uintval,
37  *                               &port, "Server port number");
38  *              opt_register_noarg("--restore", opt_set_bool,
39  *                                 &restore, opt_hidden);
40  *
41  *              opt_parse(&argc, argv, opt_log_stderr_exit);
42  *              if (argc != 1)
43  *                      opt_log_stderr_exit("Unknown extra arguments");
44  *
45  *              if (restore) {
46  *                      if (!oserver_restore(ev, STATE_FILE))
47  *                              err(1, "Restoring server");
48  *              } else if (!oserver_setup(ev, port, STATE_FILE, restore_argv))
49  *                      err(1, "Failed to set up server");
50  *
51  *              while (tevent_loop_wait(ev) == 0);
52  *              err(1, "Serving client");
53  *      }
54  */
55 int main(int argc, char *argv[])
56 {
57         /* Expect exactly one argument */
58         if (argc != 2)
59                 return 1;
60
61         if (strcmp(argv[1], "depends") == 0) {
62                 printf("ccan/read_write_all\n");
63                 printf("ccan/str\n");
64                 printf("ccan/foreach\n");
65                 printf("ccan/failtest\n");
66                 printf("ccan/opt\n");
67                 printf("ccan/array_size\n");
68                 printf("ccan/tevent\n");
69                 printf("ccan/cdump\n");
70                 printf("ccan/grab_file\n");
71                 return 0;
72         }
73
74         return 1;
75 }