ccnsyncwatch.c

Go to the documentation of this file.
00001 /**
00002  * @file ccnsyncwatch.c
00003  * Utility to use the Sync library protocol to watch changes in a repository's contents.
00004  *
00005  * A CCNx program.
00006  *
00007  * Copyright (C) 2012 Palo Alto Research Center, Inc.
00008  *
00009  * This work is free software; you can redistribute it and/or modify it under
00010  * the terms of the GNU General Public License version 2 as published by the
00011  * Free Software Foundation.
00012  * This work is distributed in the hope that it will be useful, but WITHOUT ANY
00013  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
00015  * for more details. You should have received a copy of the GNU General Public
00016  * License along with this program; if not, write to the
00017  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
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 char *
00032 hex_string(unsigned char *s, size_t l)
00033 {
00034     const char *hex_digits = "0123456789abcdef";
00035     char *r;
00036     int i;
00037     r = calloc(1, 1 + 2 * l);
00038     for (i = 0; i < l; i++) {
00039         r[2*i] = hex_digits[(s[i]>>4) & 0xf];
00040         r[1+2*i] = hex_digits[s[i] & 0xf];
00041     }
00042     return(r);
00043 }
00044 
00045 int hex_value(char c)
00046 {
00047     if (0 == isxdigit(c)) return (-1);
00048     if (c >= '0' && c <= '9') return (c - '0');
00049     return (10+tolower(c) - 'a');
00050 }
00051 
00052 void
00053 usage(char *prog)
00054 {
00055     fprintf(stderr,
00056             "%s [-h] [-t topo-uri] [-p prefix-uri] [-f filter-uri] [-r roothash-hex] [-w timeout-secs]\n"
00057             "   topo-uri, prefix-uri, and filter-uri must be CCNx URIs.\n"
00058             "   roothash-hex must be an even number of hex digits "
00059             "representing a valid starting root hash.\n"
00060             "   timeout-secs is the time, in seconds that the program "
00061             "should monitor sync activity.\n"
00062             "       or -1 to run until interrupted.\n", prog);
00063     exit(1);
00064 }
00065 
00066 int
00067 sync_cb(struct ccns_handle *h,
00068         struct ccn_charbuf *lhash,
00069         struct ccn_charbuf *rhash,
00070         struct ccn_charbuf *name)
00071 {
00072     char *hexL;
00073     char *hexR;
00074     struct ccn_charbuf *uri = ccn_charbuf_create();
00075     ccn_uri_append(uri, name->buf, name->length, 1);
00076     if (lhash == NULL || lhash->length == 0) {
00077         hexL = strdup("none");
00078     } else
00079         hexL = hex_string(lhash->buf, lhash->length);
00080     if (rhash == NULL || rhash->length == 0) {
00081         hexR = strdup("none");
00082     } else
00083         hexR = hex_string(rhash->buf, rhash->length);
00084     printf("%s %s %s\n", ccn_charbuf_as_string(uri), hexL, hexR);
00085     fflush(stdout);
00086     free(hexL);
00087     free(hexR);
00088     ccn_charbuf_destroy(&uri);
00089     return(0);
00090 }
00091 
00092 int
00093 main(int argc, char **argv)
00094 {
00095     int opt;
00096     int res;
00097     struct ccn *h;
00098     struct ccns_slice *slice;
00099     struct ccns_handle *ccns;
00100     struct ccn_charbuf *prefix = ccn_charbuf_create();
00101     struct ccn_charbuf *roothash = NULL;
00102     struct ccn_charbuf *topo = ccn_charbuf_create();
00103     struct ccn_charbuf *clause = ccn_charbuf_create();
00104     int timeout = 10*1000;
00105     unsigned i, j, n;
00106  
00107     slice = ccns_slice_create();
00108     ccn_charbuf_reset(prefix);
00109     ccn_charbuf_reset(topo);
00110     while ((opt = getopt(argc, argv, "hf:p:r:t:w:")) != -1) {
00111         switch (opt) {
00112             case 'f':
00113                 ccn_charbuf_reset(clause);
00114                 if (0 > ccn_name_from_uri(clause, optarg)) usage(argv[0]);
00115                 ccns_slice_add_clause(slice, clause);
00116                 break;
00117             case 'p':
00118                 ccn_charbuf_reset(prefix);
00119                 if (0 > ccn_name_from_uri(prefix, optarg)) usage(argv[0]);
00120                 break;
00121             case 'r':
00122                 n = strlen(optarg);
00123                 if (n == 0) {
00124                     roothash = ccn_charbuf_create();
00125                     break;
00126                 }
00127                 if ((n % 2) != 0)
00128                     usage(argv[0]);
00129                 roothash = ccn_charbuf_create_n(n / 2);
00130                 for (i = 0; i < (n / 2); i++) {
00131                     j = (hex_value(optarg[2*i]) << 4) | hex_value(optarg[1+2*i]);
00132                     ccn_charbuf_append_value(roothash, j, 1);
00133                 }
00134                 break;
00135             case 't':
00136                 ccn_charbuf_reset(topo);
00137                 if (0 > ccn_name_from_uri(topo, optarg)) usage(argv[0]);
00138                 break;
00139             case 'w':
00140                 timeout = atoi(optarg);
00141                 if (timeout < -1) usage(argv[0]);
00142                 timeout *= 1000;
00143                 break;
00144             default:
00145             case 'h':
00146                 usage(argv[0]);
00147         }
00148     }
00149 
00150     ccns_slice_set_topo_prefix(slice, topo, prefix);
00151     h = ccn_create();
00152     res = ccn_connect(h, NULL);
00153     ccns = ccns_open(h, slice, &sync_cb, roothash, NULL);
00154     ccn_run(h, timeout);
00155     ccns_close(&ccns, NULL, NULL);
00156     ccns_slice_destroy(&slice);
00157     ccn_destroy(&h);
00158     exit(res);
00159 }
Generated on Tue Aug 21 14:54:17 2012 for Content-Centric Networking in C by  doxygen 1.6.3