ccnseqwriter.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 <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();
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 }