/** Ordered local closest node lookup @return k neighbors in a list */ struct ilist* KDA_Closest(KDA_ID object_id) { NOTE_FX(object_id); struct ilist* result = ilist_create(); int d = XOR(self->id, object_id); ilist_add(result, d, self); for (int i = 0; i < KDA_SPACE_SIZE; i++) { for (struct list_item* item = k_bucket[i]->head; item; item = item->next) { KDA_Neighbor* neighbor = (KDA_Neighbor*) item->data; d = XOR(neighbor->id, object_id); if (result->size < k) { ilist_ordered_insert(result, d, neighbor); } else if (d < result->tail->key) { ilist_ordered_insert(result, d, neighbor); ilist_pop(result); } } } return result; }
static void server_io (void) { for (;;) { int client = accept(server, NULL, 0); if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) break; check(client, "accept()"); setup_io(client); clients = ilist_add(clients, client); report("connect (%d)\n", client); } }
xmpp_ibb_session_t *xmpp_ibb_open(xmpp_conn_t * const conn, char * const peer, char * const sid) { xmpp_ibb_session_t *sess; xmpp_stanza_t *iq, *open; xmpp_ctx_t *ctx; const char *jid = xmpp_conn_get_bound_jid(conn); char sizetemp[6] = ""; int size; if (peer == NULL || strlen(peer) == 0) { return NULL; } sess = _ibb_session_init(conn, peer, sid); if (sess == NULL) { return NULL; } size = snprintf(sizetemp, sizeof(sizetemp), "%d", sess->block_size); if (size < sizeof(sizetemp)) { sizetemp[size] = '\0'; } nmtoken_generate(sess->id, 8); ctx = xmpp_conn_get_context(conn); iq = xmpp_stanza_new(ctx); xmpp_stanza_set_name(iq, "iq"); xmpp_stanza_set_type(iq, "set"); xmpp_stanza_set_id(iq, sess->id); xmpp_stanza_set_attribute(iq, "from", jid); xmpp_stanza_set_attribute(iq, "to", sess->peer); open = xmpp_stanza_new(ctx); xmpp_stanza_set_name(open, "open"); xmpp_stanza_set_ns(open, XMLNS_IBB); xmpp_stanza_set_attribute(open, "block-size", sizetemp); xmpp_stanza_set_attribute(open, "sid", sess->sid); xmpp_stanza_set_attribute(open, "stanza", "iq"); xmpp_stanza_add_child(iq, open); xmpp_send(conn, iq); xmpp_stanza_release(open); xmpp_stanza_release(iq); ilist_add(g_list, sess); return sess; }
static int _ibb_set_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata) { xmpp_stanza_t *child; xmpp_ibb_userdata_t * udata = (xmpp_ibb_userdata_t *) userdata; xmpp_ibb_session_t * sess; char *from, *id, *type; id = xmpp_stanza_get_id(stanza); from = xmpp_stanza_get_attribute(stanza, "from"); type = xmpp_stanza_get_type(stanza); if ((child = xmpp_stanza_get_child_by_name(stanza, "open")) != NULL) { char *sid = xmpp_stanza_get_attribute(child, "sid"); char *bsize = xmpp_stanza_get_attribute(child, "block-size"); if (sid == NULL || bsize == NULL) { xmpp_iq_ack_error(conn, id, from, "cancel", "not-acceptable"); return 1; } else { xmpp_iq_ack_result(conn, id, from); } sess = _ibb_session_init(conn, from, sid); strncpy(sess->id, id, sizeof(sess->id)); strncpy(sess->peer, from, sizeof(sess->peer)); sess->state = STATE_READY; sess->block_size = atoi(bsize); if (udata != NULL && udata->open_cb != NULL) udata->open_cb(sess, type); ilist_add(g_list, sess); } else if ((child = xmpp_stanza_get_child_by_name(stanza, "data")) != NULL) { char *sid = xmpp_stanza_get_attribute(child, "sid"); sess = ilist_finditem_func(g_list, _find_sid, sid); if (sess != NULL) { xmppdata_t xdata; int seq = 0; char *intext = xmpp_stanza_get_text(child); xmpp_iq_ack_result(conn, id, from); xdata.from = from; strncpy(sess->id, id, sizeof(sess->id)); seq = atoi(xmpp_stanza_get_attribute(child, "seq")); if (seq != (sess->recv_seq + 1)) { //printf("sequence number is not continue. new seq %d last seq %d\n", seq, sess->recv_seq); } sess->recv_seq = seq; xmpp_b64decode(intext, (char **) &xdata.data, (size_t *) &xdata.size); if (udata != NULL && udata->recv_cb != NULL) udata->recv_cb(sess, &xdata); xmpp_b64free(sess->recv_data); sess->recv_data = NULL; } else { //printf("unknown session is not in handle.\n"); xmpp_iq_ack_error(conn, id, from, "cancel", "item-not-found"); } } else if ((child = xmpp_stanza_get_child_by_name(stanza, "close")) != NULL) { char *sid = xmpp_stanza_get_attribute(child, "sid"); sess = ilist_finditem_func(g_list, _find_sid, sid); if (sess != NULL) { xmpp_iq_ack_result(conn, id, from); strncpy(sess->id, id, sizeof(sess->id)); sess->state = STATE_NONE; if (udata != NULL && udata->close_cb != NULL) udata->close_cb(sess, type); _ibb_session_release(sess); } } time(&glast_ping_time); return 1; }
/*! \brief Add a new quest item * * Add a Quest info item to the current set * \sa ILIST * \param key The title of the item * \param text The text to associate with it * \author PH * \date 20050429 */ void add_questinfo(const char *key, const char *text) { ilist_add(&quest_list, key, text); }