Fixes for remote file retrieval

yamux
jmjatlanta 2017-09-28 07:58:51 -05:00
parent f5250a71f3
commit 3d425bb30f
4 changed files with 27 additions and 11 deletions

View File

@ -150,10 +150,11 @@ int ipfs_repo_config_init(struct RepoConfig* config, unsigned int num_bits_for_k
config->addresses->swarm_head->next->item = malloc(strlen(addr1) + 1);
strcpy(config->addresses->swarm_head->next->item, addr1);
strcpy(addr1, "/ip4/127.0.0.1/tcp/5001");
int port_adder = swarm_port - 4001;
sprintf(addr1, "/ip4/127.0.0.1/tcp/%d", 5001 + port_adder);
config->addresses->api = malloc(strlen(addr1)+1);
strcpy(config->addresses->api, addr1);
strcpy(addr1, "ip4/127.0.0.1/tcp/8080");
sprintf(addr1, "/ip4/127.0.0.1/tcp/%d", 8080 + port_adder);
config->addresses->gateway = malloc(strlen(addr1+1));
strcpy(config->addresses->gateway, addr1);
free(addr1);

View File

@ -60,6 +60,8 @@ int ipfs_routing_generic_get_value (ipfs_routing* routing, const unsigned char *
return 0;
}
libp2p_logger_debug("offline", "Attempting to ask API for dht get %s.\n", buffer);
char* response;
struct HttpRequest* req = ipfs_core_http_request_new();
req->command = "dht";
@ -140,6 +142,8 @@ int ipfs_routing_offline_provide (ipfs_routing* offlineRouting, const unsigned c
}
fprintf(stdout, "%s", response);
return 1;
} else {
libp2p_logger_debug("offline", "Unable to announce that I can provide the hash, as API not available.\n");
}
return 0;

View File

@ -76,7 +76,7 @@ int ipfs_routing_online_find_remote_providers(struct IpfsRouting* routing, const
struct Libp2pPeer *current_peer = current_provider_peer_list_item->item;
// if we can find the peer in the peerstore, use that one instead
struct Libp2pPeer* peerstorePeer = libp2p_peerstore_get_peer(routing->local_node->peerstore, (unsigned char*)current_peer->id, current_peer->id_size);
if (peerstorePeer != NULL) {
if (peerstorePeer == NULL) {
// add it to the peerstore
libp2p_peerstore_add_peer(routing->local_node->peerstore, current_peer);
peerstorePeer = libp2p_peerstore_get_peer(routing->local_node->peerstore, (unsigned char*)current_peer->id, current_peer->id_size);
@ -118,19 +118,22 @@ int ipfs_routing_online_find_providers(struct IpfsRouting* routing, const unsign
// see if we can find the key, and retrieve the peer who has it
if (!libp2p_providerstore_get(routing->local_node->providerstore, key, key_size, &peer_id, &peer_id_size)) {
libp2p_logger_debug("online", "Unable to find provider locally... Asking network\n");
libp2p_logger_debug("online", "%s: Unable to find provider locally... Asking network\n", libp2p_peer_id_to_string(routing->local_node->identity->peer));
// we need to look remotely
return ipfs_routing_online_find_remote_providers(routing, key, key_size, peers);
}
libp2p_logger_debug("online", "FindProviders: Found provider locally. Searching for peer.\n");
libp2p_logger_debug("online", "%s: Found provider locally. Searching for peer.\n", libp2p_peer_id_to_string(routing->local_node->identity->peer));
// now translate the peer id into a peer to get the multiaddresses
peer = libp2p_peerstore_get_peer(routing->local_node->peerstore, peer_id, peer_id_size);
free(peer_id);
if (peer == NULL) {
libp2p_logger_error("online", "find_providers: We said we had the peer, but then we couldn't find it.\n");
return 0;
}
libp2p_logger_debug("online", "%s: Found peer %s.\n", libp2p_peer_id_to_string(routing->local_node->identity->peer), libp2p_peer_id_to_string(peer));
*peers = libp2p_utils_vector_new(1);
libp2p_utils_vector_add(*peers, peer);
return 1;
@ -383,11 +386,13 @@ int ipfs_routing_online_get_value (ipfs_routing* routing, const unsigned char *k
struct Libp2pPeer* current_peer = libp2p_peerstore_get_or_add_peer(routing->local_node->peerstore, libp2p_utils_vector_get(peers, i));
if (current_peer->is_local) {
// it's a local fetch. Retrieve it
libp2p_logger_debug("online", "It is a local fetch. Attempting get_value locally.\n");
if (ipfs_routing_generic_get_value(routing, key, key_size, buffer, buffer_size) == 0) {
retVal = 1;
break;
}
} else if (libp2p_peer_is_connected(current_peer)) {
libp2p_logger_debug("online", "It is a remote fetch to a peer that is already connected. Attemping get_peer_value for peer %s.\n", libp2p_peer_id_to_string(current_peer));
// ask a connected peer for the file. If unsuccessful, continue in the loop.
if (ipfs_routing_online_get_peer_value(routing, current_peer, key, key_size, buffer, buffer_size)) {
retVal = 1;

View File

@ -462,6 +462,7 @@ int test_routing_retrieve_file_third_party() {
char hash[256] = "";
libp2p_logger_add_class("online");
libp2p_logger_add_class("offline");
libp2p_logger_add_class("multistream");
libp2p_logger_add_class("null");
libp2p_logger_add_class("dht_protocol");
@ -470,6 +471,7 @@ int test_routing_retrieve_file_third_party() {
libp2p_logger_add_class("exporter");
libp2p_logger_add_class("peer");
libp2p_logger_add_class("test_routing");
libp2p_logger_add_class("api");
// clean out repository
@ -486,9 +488,6 @@ int test_routing_retrieve_file_third_party() {
}
thread1_started = 1;
// wait for everything to start up
sleep(3);
// create peer 2, that will host the file
// create a vector to hold peer1's multiaddress so we can connect as a peer
ma_vector2 = libp2p_utils_vector_new(1);
@ -504,6 +503,9 @@ int test_routing_retrieve_file_third_party() {
}
thread2_started = 1;
// wait for everything to start up
sleep(3);
//TODO: add a file to server 2
uint8_t *bytes = (unsigned char*)"hello, world!\n";
char* filename = "test1.txt";
@ -513,7 +515,7 @@ int test_routing_retrieve_file_third_party() {
ipfs_import_file(NULL, filename, &node, ipfs_node2, &bytes_written, 0);
memset(hash, 0, 256);
ipfs_cid_hash_to_base58(node->hash, node->hash_size, (unsigned char*)hash, 256);
libp2p_logger_debug("test_api", "Inserted file with hash %s.\n", hash);
libp2p_logger_debug("test_routing", "Inserted file with hash %s.\n", hash);
ipfs_node_free(ipfs_node2);
// wait for everything to start up
@ -525,18 +527,22 @@ int test_routing_retrieve_file_third_party() {
libp2p_utils_vector_add(ma_vector3, ma_peer1);
drop_and_build_repository(ipfs_path_3, 4003, ma_vector3, &peer_id_3);
multiaddress_free(ma_peer1);
libp2p_logger_debug("test_routing", "Firing up daemon 2.\n");
libp2p_logger_debug("test_routing", "Firing up daemon 3.\n");
if (pthread_create(&thread3, NULL, test_daemon_start, (void*)ipfs_path_3) < 0) {
libp2p_logger_error("test_routing", "Unable to start thread 3.\n");
goto exit;
}
thread3_started = 1;
sleep(3);
//now have peer 3 ask for a file that is on peer 2, but peer 3 only knows of peer 1
ipfs_node_offline_new(ipfs_path_3, &ipfs_node3);
libp2p_logger_debug("test_routing", "Attempting to look for %s.\n", hash);
if (!ipfs_exporter_get_node(ipfs_node3, node->hash, node->hash_size, &result_node)) {
fprintf(stderr, "Get_Node returned false\n");
libp2p_logger_error("test_routing", "Get_Node returned false\n");
goto exit;
}