PUBLIC int r_sendq_face_send_queue_insert(struct ccnr_handle *h, struct fdholder *fdholder, struct content_entry *content) { int ans = -1; int delay; enum cq_delay_class c; struct content_queue *q; if (fdholder == NULL || content == NULL || (fdholder->flags & CCNR_FACE_NOSEND) != 0) return(-1); c = choose_content_delay_class(h, fdholder->filedesc, r_store_content_flags(content)); if (fdholder->q[c] == NULL) fdholder->q[c] = content_queue_create(h, fdholder, c); q = fdholder->q[c]; if (q == NULL) return(-1); ans = ccn_indexbuf_set_insert(q->send_queue, r_store_content_cookie(h, content)); if (q->sender == NULL) { delay = randomize_content_delay(h, q); q->ready = q->send_queue->n; q->sender = ccn_schedule_event(h->sched, delay, content_sender, q, fdholder->filedesc); if (CCNSHOULDLOG(h, LM_8, CCNL_FINER)) ccnr_msg(h, "fdholder %u q %d delay %d usec", fdholder->filedesc, c, delay); } return (ans); }
/** * Called by ccnr when a fdholder undergoes a substantive status change that * should be reported to interested parties. * * In the destroy case, this is called from the hash table finalizer, * so it shouldn't do much directly. Inspecting the fdholder is OK, though. */ void ccnr_face_status_change(struct ccnr_handle *ccnr, unsigned filedesc) { struct ccn_indexbuf *chface = ccnr->chface; if (chface != NULL) { ccn_indexbuf_set_insert(chface, filedesc); if (ccnr->notice_push == NULL) ccnr->notice_push = ccn_schedule_event(ccnr->sched, 2000, ccnr_notice_push, NULL, 0); } }
// start_round schedules a new comparison round, // cancelling any previously scheduled round static void start_round(struct ccns_handle *ch, int micros) { struct ccn_scheduled_event *ev = ch->ev; if (ev != NULL && ev->action != NULL && ev->evdata == ch) // get rid of the existing event ccn_schedule_cancel(ch->sync_plumbing->sched, ev); // start a new event ch->ev = ccn_schedule_event(ch->sync_plumbing->sched, micros, each_round, ch, 0); return; }
int ccnr_internal_client_start(struct ccnr_handle *ccnr) { if (ccnr->internal_client != NULL) return(-1); if (ccnr->face0 == NULL) abort(); ccnr->internal_client = ccn_create(); if (ccnr_init_repo_keystore(ccnr, ccnr->internal_client) < 0) { ccn_destroy(&ccnr->internal_client); return(-1); } ccnr->internal_client_refresh = ccn_schedule_event(ccnr->sched, 50000, ccnr_internal_client_refresh, NULL, CCN_INTEREST_LIFETIME_MICROSEC); return(0); }
PUBLIC void r_store_index_needs_cleaning(struct ccnr_handle *h) { int k; if (h->btree != NULL && h->btree->io != NULL && h->btree->cleanreq > 0) { if (h->index_cleaner == NULL) { h->index_cleaner = ccn_schedule_event(h->sched, CCN_BT_CLEAN_TICK_MICROS, r_store_index_cleaner, NULL, 0); if (CCNSHOULDLOG(h, sdfsdffd, CCNL_FINER)) ccnr_msg(h, "index cleaner started"); } /* If necessary, clean in a hurry. */ for (k = 30; /* Backstop to make sure we do not loop here */ k > 0 && h->index_cleaner != NULL && h->btree->io->openfds > CCN_BT_OPEN_NODES_LIMIT - 2; k--) r_store_index_cleaner(h->sched, h, h->index_cleaner, 0); if (k == 0) ccnr_msg(h, "index cleaner is in trouble"); } }
PUBLIC struct content_entry * r_store_lookup(struct ccnr_handle *h, const unsigned char *msg, const struct ccn_parsed_interest *pi, struct ccn_indexbuf *comps) { struct content_entry *content = NULL; struct ccn_btree_node *leaf = NULL; ccnr_cookie last_match = 0; ccnr_accession last_match_acc = CCNR_NULL_ACCESSION; struct ccn_charbuf *scratch = NULL; size_t size = pi->offset[CCN_PI_E]; int ndx; int res; int try; content = r_store_find_first_match_candidate(h, msg, pi); if (content != NULL && CCNSHOULDLOG(h, LM_8, CCNL_FINER)) ccnr_debug_content(h, __LINE__, "first_candidate", NULL, content); if (content != NULL && !r_store_content_matches_interest_prefix(h, content, msg, size)) { if (CCNSHOULDLOG(h, LM_8, CCNL_FINER)) ccnr_debug_ccnb(h, __LINE__, "prefix_mismatch", NULL, msg, size); content = NULL; } scratch = ccn_charbuf_create(); for (try = 0; content != NULL; try++) { res = ccn_btree_lookup(h->btree, content->flatname->buf, content->flatname->length, &leaf); if (CCN_BT_SRCH_FOUND(res) == 0) { ccnr_debug_content(h, __LINE__, "impossible", NULL, content); content = NULL; break; } ndx = CCN_BT_SRCH_INDEX(res); res = ccn_btree_match_interest(leaf, ndx, msg, pi, scratch); if (res == -1) { ccnr_debug_ccnb(h, __LINE__, "match_error", NULL, msg, size); content = NULL; break; } if (res == 1) { if ((pi->orderpref & 1) == 0) // XXX - should be symbolic break; last_match = content->cookie; last_match_acc = content->accession; content = r_store_next_child_at_level(h, content, comps->n - 1); } else content = r_store_content_next(h, content); if (content != NULL && !r_store_content_matches_interest_prefix(h, content, msg, size)) content = NULL; } if (last_match != 0) { content = r_store_content_from_cookie(h, last_match); if (content == NULL) content = r_store_content_from_accession(h, last_match_acc); } ccn_charbuf_destroy(&scratch); return(content); } /** * Find the first content handle that matches the prefix given by the namish, * which may be a Name, Interest, ContentObject, ... * * Does not check the other parts of namish, in particular, does not generate * the digest component of a ContentObject. */ PUBLIC struct content_entry * r_store_lookup_ccnb(struct ccnr_handle *h, const unsigned char *namish, size_t size) { struct content_entry *content = NULL; struct ccn_charbuf *flatname = NULL; int res; flatname = ccn_charbuf_create(); if (flatname == NULL) goto Bail; res = ccn_flatname_from_ccnb(flatname, namish, size); if (res < 0) goto Bail; content = r_store_look(h, flatname->buf, flatname->length); if (content != NULL) { res = ccn_flatname_charbuf_compare(flatname, content->flatname); if (res == 0 || res == -9999) { /* prefix matches */ } else content = NULL; } Bail: ccn_charbuf_destroy(&flatname); return(content); } /** * Mark content as stale */ PUBLIC void r_store_mark_stale(struct ccnr_handle *h, struct content_entry *content) { ccnr_cookie cookie = content->cookie; if ((content->flags & CCN_CONTENT_ENTRY_STALE) != 0) return; if (CCNSHOULDLOG(h, LM_4, CCNL_FINE)) ccnr_debug_content(h, __LINE__, "stale", NULL, content); content->flags |= CCN_CONTENT_ENTRY_STALE; h->n_stale++; if (cookie < h->min_stale) h->min_stale = cookie; if (cookie > h->max_stale) h->max_stale = cookie; } /** * Scheduled event that makes content stale when its FreshnessSeconds * has expired. */ static int expire_content(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags) { struct ccnr_handle *h = clienth; ccnr_cookie cookie = ev->evint; struct content_entry *content = NULL; if ((flags & CCN_SCHEDULE_CANCEL) != 0) return(0); content = r_store_content_from_cookie(h, cookie); if (content != NULL) r_store_mark_stale(h, content); return(0); } /** * Schedules content expiration based on its FreshnessSeconds. * */ PUBLIC void r_store_set_content_timer(struct ccnr_handle *h, struct content_entry *content, struct ccn_parsed_ContentObject *pco) { int seconds = 0; int microseconds = 0; size_t start = pco->offset[CCN_PCO_B_FreshnessSeconds]; size_t stop = pco->offset[CCN_PCO_E_FreshnessSeconds]; const unsigned char *content_msg = NULL; if (start == stop) return; content_msg = r_store_content_base(h, content); seconds = ccn_fetch_tagged_nonNegativeInteger( CCN_DTAG_FreshnessSeconds, content_msg, start, stop); if (seconds <= 0) return; if (seconds > ((1U<<31) / 1000000)) { ccnr_debug_content(h, __LINE__, "FreshnessSeconds_too_large", NULL, content); return; } microseconds = seconds * 1000000; ccn_schedule_event(h->sched, microseconds, &expire_content, NULL, content->cookie); }
PUBLIC void r_store_init(struct ccnr_handle *h) { struct ccn_btree *btree = NULL; struct ccn_btree_node *node = NULL; struct hashtb_param param = {0}; int i; int j; int res; struct ccn_charbuf *path = NULL; struct ccn_charbuf *msgs = NULL; off_t offset; path = ccn_charbuf_create(); param.finalize_data = h; param.finalize = 0; h->cob_limit = r_init_confval(h, "CCNR_CONTENT_CACHE", 16, 2000000, 4201); h->cookie_limit = choose_limit(h->cob_limit, (ccnr_cookie)(~0U)); h->content_by_cookie = calloc(h->cookie_limit, sizeof(h->content_by_cookie[0])); CHKPTR(h->content_by_cookie); h->content_by_accession_tab = hashtb_create(sizeof(struct content_by_accession_entry), NULL); CHKPTR(h->content_by_accession_tab); h->btree = btree = ccn_btree_create(); CHKPTR(btree); FAILIF(btree->nextnodeid != 1); ccn_charbuf_putf(path, "%s/index", h->directory); res = mkdir(ccn_charbuf_as_string(path), 0700); if (res != 0 && errno != EEXIST) r_init_fail(h, __LINE__, ccn_charbuf_as_string(path), errno); else { msgs = ccn_charbuf_create(); btree->io = ccn_btree_io_from_directory(ccn_charbuf_as_string(path), msgs); if (btree->io == NULL) res = errno; if (msgs->length != 0 && CCNSHOULDLOG(h, sffdsdf, CCNL_WARNING)) { ccnr_msg(h, "while initializing %s - %s", ccn_charbuf_as_string(path), ccn_charbuf_as_string(msgs)); } ccn_charbuf_destroy(&msgs); if (btree->io == NULL) r_init_fail(h, __LINE__, ccn_charbuf_as_string(path), res); } node = ccn_btree_getnode(btree, 1, 0); if (btree->io != NULL) btree->nextnodeid = btree->io->maxnodeid + 1; CHKPTR(node); if (node->buf->length == 0) { res = ccn_btree_init_node(node, 0, 'R', 0); CHKSYS(res); } ccn_charbuf_destroy(&path); if (h->running == -1) return; r_store_read_stable_point(h); h->active_in_fd = -1; h->active_out_fd = r_io_open_repo_data_file(h, "repoFile1", 1); /* output */ offset = lseek(h->active_out_fd, 0, SEEK_END); h->startupbytes = offset; if (offset != h->stable || node->corrupt != 0) { ccnr_msg(h, "Index not current - resetting"); ccn_btree_init_node(node, 0, 'R', 0); node = NULL; ccn_btree_destroy(&h->btree); path = ccn_charbuf_create(); /* Remove old index files to avoid confusion */ for (i = 1, j = 0; i > 0 && j < 3; i++) { path->length = 0; res = ccn_charbuf_putf(path, "%s/index/%d", h->directory, i); if (res >= 0) res = unlink(ccn_charbuf_as_string(path)); if (res < 0) j++; } h->btree = btree = ccn_btree_create(); path->length = 0; ccn_charbuf_putf(path, "%s/index", h->directory); btree->io = ccn_btree_io_from_directory(ccn_charbuf_as_string(path), msgs); CHKPTR(btree->io); btree->io->maxnodeid = 0; btree->nextnodeid = 1; node = ccn_btree_getnode(btree, 1, 0); btree->nextnodeid = btree->io->maxnodeid + 1; ccn_btree_init_node(node, 0, 'R', 0); h->stable = 0; h->active_in_fd = r_io_open_repo_data_file(h, "repoFile1", 0); /* input */ ccn_charbuf_destroy(&path); if (CCNSHOULDLOG(h, dfds, CCNL_INFO)) ccn_schedule_event(h->sched, 50000, r_store_reindexing, NULL, 0); } if (CCNSHOULDLOG(h, weuyg, CCNL_FINEST)) { FILE *dumpfile = NULL; path = ccn_charbuf_create(); ccn_charbuf_putf(path, "%s/index/btree_check.out", h->directory); dumpfile = fopen(ccn_charbuf_as_string(path), "w"); res = ccn_btree_check(btree, dumpfile); if (dumpfile != NULL) { fclose(dumpfile); dumpfile = NULL; } else path->length = 0; ccnr_msg(h, "ccn_btree_check returned %d (%s)", res, ccn_charbuf_as_string(path)); ccn_charbuf_destroy(&path); if (res < 0) r_init_fail(h, __LINE__, "index is corrupt", res); } btree->full = r_init_confval(h, "CCNR_BTREE_MAX_FANOUT", 4, 9999, 1999); btree->full0 = r_init_confval(h, "CCNR_BTREE_MAX_LEAF_ENTRIES", 4, 9999, 1999); btree->nodebytes = r_init_confval(h, "CCNR_BTREE_MAX_NODE_BYTES", 1024, 8388608, 2097152); btree->nodepool = r_init_confval(h, "CCNR_BTREE_NODE_POOL", 16, 2000000, 512); if (h->running != -1) r_store_index_needs_cleaning(h); }
int main(int argc, char *argv[]) { const char *progname = argv[0]; struct ccn_ping_client client = {.sent = 0, .received = 0, .total = -1, .number = -1, .interval = 1}; struct ccn_closure in_content = {.p = &incoming_content}; struct hashtb_param param = {0}; int res; struct sigaction sa; memset(&sa, 0, sizeof(sa)); sa.sa_handler = &handle_interrupt; sigaction(SIGINT, &sa, &osa); memset(&sta, 0, sizeof(sta)); gettimeofday(&sta.start, 0); sta.min = INT_MAX; while ((res = getopt(argc, argv, "hi:c:n:")) != -1) { switch (res) { case 'c': client.total = atol(optarg); if (client.total <= 0) usage(progname); break; case 'i': client.interval = atof(optarg); if (client.interval < PING_MIN_INTERVAL) usage(progname); break; case 'n': client.number = atol(optarg); if (client.number < 0) usage(progname); break; case 'h': default: usage(progname); break; } } if (client.number < 0) srandom(time(NULL)); argc -= optind; argv += optind; if (argv[0] == NULL) usage(progname); sta.prefix = argv[0]; client.original_prefix = argv[0]; client.prefix = ccn_charbuf_create(); res = ccn_name_from_uri(client.prefix, argv[0]); if (res < 0) { fprintf(stderr, "%s: bad ccn URI: %s\n", progname, argv[0]); exit(1); } if (argv[1] != NULL) fprintf(stderr, "%s warning: extra arguments ignored\n", progname); //append "/ping" to the given name prefix res = ccn_name_append_str(client.prefix, PING_COMPONENT); if (res < 0) { fprintf(stderr, "%s: error constructing ccn URI: %s/%s\n", progname, argv[0], PING_COMPONENT); exit(1); } /* Connect to ccnd */ client.h = ccn_create(); if (ccn_connect(client.h, NULL) == -1) { perror("Could not connect to ccnd"); exit(1); } client.closure = &in_content; in_content.data = &client; client.ccn_ping_table = hashtb_create(sizeof(struct ccn_ping_entry), ¶m); client.sched = ccn_schedule_create(&client, &ccn_ping_ticker); client.event = ccn_schedule_event(client.sched, 0, &do_ping, NULL, 0); printf("CCNPING %s\n", client.original_prefix); res = 0; while (res >= 0 && (client.total <= 0 || client.sent < client.total || hashtb_n(client.ccn_ping_table) > 0)) { if (client.total <= 0 || client.sent < client.total) ccn_schedule_run(client.sched); res = ccn_run(client.h, 10); } ccn_schedule_destroy(&client.sched); ccn_destroy(&client.h); ccn_charbuf_destroy(&client.prefix); print_statistics(); return 0; }