ccnseqwriter.c

Go to the documentation of this file.
00001 /**
00002  * @file ccnseqwriter.c
00003  * Streams data from stdin into ccn
00004  *
00005  * A CCNx command-line utility.
00006  *
00007  * Copyright (C) 2010 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 <errno.h>
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #include <unistd.h>
00025 #include <ccn/ccn.h>
00026 #include <ccn/uri.h>
00027 #include <ccn/seqwriter.h>
00028 
00029 static void
00030 usage(const char *progname)
00031 {
00032         fprintf(stderr,
00033                 "%s [-h] [-b blocksize] ccnx:/some/uri\n"
00034                 " Reads stdin, sending data under the given URI"
00035                 " using ccn versioning and segmentation.\n", progname);
00036         exit(1);
00037 }
00038 
00039 int
00040 main(int argc, char **argv)
00041 {
00042     const char *progname = argv[0];
00043     struct ccn *ccn = NULL;
00044     struct ccn_charbuf *name = NULL;
00045     struct ccn_seqwriter *w = NULL;
00046     long blocksize = 1024;
00047     int i;
00048     int status = 0;
00049     int res;
00050     ssize_t read_res;
00051     unsigned char *buf = NULL;
00052     while ((res = getopt(argc, argv, "hb:")) != -1) {
00053         switch (res) {
00054             case 'b':
00055                 blocksize = atol(optarg);
00056                 if (blocksize <= 0 || blocksize > 4096)
00057                     usage(progname);
00058                 break;
00059             default:
00060             case 'h':
00061                 usage(progname);
00062                 break;
00063         }
00064     }
00065     argc -= optind;
00066     argv += optind;
00067     if (argv[0] == NULL)
00068         usage(progname);
00069     name = ccn_charbuf_create();
00070     res = ccn_name_from_uri(name, argv[0]);
00071     if (res < 0) {
00072         fprintf(stderr, "%s: bad ccnx URI: %s\n", progname, argv[0]);
00073         exit(1);
00074     }
00075     if (argv[1] != NULL)
00076         fprintf(stderr, "%s warning: extra arguments ignored\n", progname);
00077 
00078     ccn = ccn_create();
00079     if (ccn_connect(ccn, NULL) == -1) {
00080         perror("Could not connect to ccnd");
00081         exit(1);
00082     }
00083     
00084     buf = calloc(1, blocksize);
00085     
00086     w = ccn_seqw_create(ccn, name);
00087     if (w == NULL) {
00088         fprintf(stderr, "ccn_seqw_create failed\n");
00089         exit(1);
00090     }
00091     for (i = 0;; i++) {
00092         ccn_run(ccn, 1);
00093         read_res = read(0, buf, blocksize);
00094         if (read_res < 0) {
00095             perror("read");
00096             read_res = 0;
00097             status = 1;
00098         }
00099         if (read_res == 0) {
00100             ccn_seqw_close(w);
00101             w = NULL;
00102             status = 0;
00103             break;
00104         }
00105         res = ccn_seqw_write(w, buf, read_res);
00106         while (res == -1) {
00107             ccn_run(ccn, 100);
00108             res = ccn_seqw_write(w, buf, read_res);
00109         }
00110         if (res != read_res)
00111             abort(); /* hmm, ccn_seqw_write did a short write or something */
00112     }
00113     ccn_run(ccn, 1);
00114     free(buf);
00115     buf = NULL;
00116     ccn_charbuf_destroy(&name);
00117     ccn_destroy(&ccn);
00118     exit(status);
00119 }
Generated on Fri May 13 16:27:01 2011 for Content-Centric Networking in C by  doxygen 1.6.3