ccnsyncslice.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <fcntl.h>
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #include <ctype.h>
00025 #include <unistd.h>
00026
00027 #include <ccn/ccn.h>
00028 #include <ccn/sync.h>
00029 #include <ccn/uri.h>
00030
00031 void
00032 usage(char *prog)
00033 {
00034 fprintf(stderr,
00035 "%s [-hv] (create|delete) topo-uri prefix-uri [filter-uri]...\n"
00036 " topo-uri, prefix-uri, and the optional filter-uris must be CCNx URIs.\n", prog);
00037 exit(1);
00038 }
00039
00040 int
00041 main(int argc, char **argv)
00042 {
00043 int opt;
00044 int res;
00045 char *prog = argv[0];
00046 struct ccn *h;
00047 struct ccns_slice *slice;
00048 struct ccn_charbuf *prefix = ccn_charbuf_create();
00049 struct ccn_charbuf *topo = ccn_charbuf_create();
00050 struct ccn_charbuf *clause = ccn_charbuf_create();
00051 struct ccn_charbuf *slice_name = ccn_charbuf_create();
00052 struct ccn_charbuf *slice_uri = ccn_charbuf_create();
00053 enum {
00054 CREATE = 0,
00055 DELETE = 1
00056 } cmd = CREATE;
00057 unsigned verbose = 0;
00058 unsigned i;
00059
00060 if (prefix == NULL || topo == NULL || clause == NULL ||
00061 slice_name == NULL || slice_uri == NULL) {
00062 fprintf(stderr, "Unable to allocate required memory.\n");
00063 exit(1);
00064 }
00065
00066 while ((opt = getopt(argc, argv, "vh")) != -1) {
00067 switch (opt) {
00068 case 'v':
00069 verbose = 1;
00070 break;
00071 default:
00072 case 'h':
00073 usage(prog);
00074 break;
00075 }
00076 }
00077 argc -= optind;
00078 argv += optind;
00079
00080 if (argc < 3)
00081 usage(prog);
00082 if (strcmp(argv[0], "create") == 0)
00083 cmd = CREATE;
00084 else if (strcmp(argv[0], "delete") == 0)
00085 cmd = DELETE;
00086 else
00087 usage(prog);
00088
00089 slice = ccns_slice_create();
00090
00091 ccn_charbuf_reset(topo);
00092 if (0 > ccn_name_from_uri(topo, argv[1])) usage(prog);
00093 ccn_charbuf_reset(prefix);
00094 if (0 > ccn_name_from_uri(prefix, argv[2])) usage(prog);
00095 if (0 > ccns_slice_set_topo_prefix(slice, topo, prefix)) usage(prog);
00096 for (i = 3; i < argc; i++) {
00097 ccn_charbuf_reset(clause);
00098 if (0 > ccn_name_from_uri(clause, argv[i])) usage(prog);
00099 else
00100 if (0 > ccns_slice_add_clause(slice, clause)) usage(prog);
00101 }
00102
00103 h = ccn_create();
00104 res = ccn_connect(h, NULL);
00105 if (0 > res) {
00106 fprintf(stderr, "Unable to connect to ccnd.\n");
00107 exit(1);
00108 }
00109 switch(cmd) {
00110 case CREATE:
00111 res = ccns_write_slice(h, slice, slice_name);
00112 break;
00113 case DELETE:
00114 ccns_slice_name(slice_name, slice);
00115 res = ccns_delete_slice(h, slice_name);
00116 break;
00117 }
00118 if (verbose || res < 0) {
00119 ccn_uri_append(slice_uri, slice_name->buf, slice_name->length, 1);
00120 printf("%s slice %s %s\n",
00121 cmd == CREATE ? "create" : "delete",
00122 ccn_charbuf_as_string(slice_uri),
00123 (res < 0) ? "failed" : "succeeded");
00124 }
00125 ccns_slice_destroy(&slice);
00126 ccn_destroy(&h);
00127 ccn_charbuf_destroy(&prefix);
00128 ccn_charbuf_destroy(&topo);
00129 ccn_charbuf_destroy(&clause);
00130 ccn_charbuf_destroy(&slice_name);
00131 ccn_charbuf_destroy(&slice_uri);
00132
00133 exit(res);
00134 }