From b72f2479399464e0e10c3d126f26ec7fa2cb93f5 Mon Sep 17 00:00:00 2001 From: jmjatlanta Date: Mon, 23 Oct 2017 18:03:55 -0500 Subject: [PATCH] More implementation of dialer --- importer/resolver.c | 26 ++++++++++---------------- test/core/test_ping.h | 14 ++++++++------ 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/importer/resolver.c b/importer/resolver.c index bb50a59..f0cf1ba 100644 --- a/importer/resolver.c +++ b/importer/resolver.c @@ -2,6 +2,7 @@ #include #include #include +#include #include "ipfs/importer/resolver.h" #include "libp2p/utils/logger.h" @@ -15,6 +16,7 @@ #include "libp2p/record/message.h" #include "multiaddr/multiaddr.h" #include "libp2p/record/message.h" +#include "libp2p/conn/dialer.h" /** * return the next chunk of a path @@ -138,20 +140,18 @@ struct HashtableNode* ipfs_resolver_remote_get(const char* path, struct Hashtabl //TODO: We don't have the peer address. Ask the swarm for the data related to the hash return NULL; } - // connect to the peer - struct MultiAddress* address = peer->addr_head->item; - char* ip; - int port = multiaddress_get_ip_port(address); - multiaddress_get_ip_address(address, &ip); - struct Stream* stream = libp2p_net_multistream_connect(ip, port); - free(ip); + if (!libp2p_peer_connect(ipfs_node->dialer, peer, ipfs_node->peerstore, ipfs_node->repo->config->datastore, 10)) + return NULL; + struct Stream* kademlia_stream = libp2p_conn_dialer_get_stream(ipfs_node->dialer, peer, "kademlia"); + if (kademlia_stream == NULL) + return NULL; // build the request struct KademliaMessage* message = libp2p_message_new(); message->message_type = MESSAGE_TYPE_GET_VALUE; message->key = key; message->key_size = strlen(key); size_t b58size = 100; - uint8_t *b58key = (uint8_t *) malloc(b58size); + uint8_t *b58key = (uint8_t*) malloc(b58size); if (b58key == NULL) { libp2p_crypto_encoding_base58_encode((unsigned char*)message->key, message->key_size, (unsigned char**) &b58key, &b58size); libp2p_logger_debug("resolver", "Attempting to use kademlia to get key %s.\n", b58key); @@ -161,19 +161,13 @@ struct HashtableNode* ipfs_resolver_remote_get(const char* path, struct Hashtabl unsigned char message_protobuf[message_protobuf_size]; libp2p_message_protobuf_encode(message, message_protobuf, message_protobuf_size, &message_protobuf_size); libp2p_message_free(message); - struct SessionContext session_context; - session_context.insecure_stream = stream; - session_context.default_stream = stream; - // switch to kademlia - if (!libp2p_routing_dht_upgrade_stream(&session_context)) - return NULL; struct StreamMessage outgoing; outgoing.data = message_protobuf; outgoing.data_size = message_protobuf_size; - stream->write(&session_context, &outgoing); + kademlia_stream->write(kademlia_stream->stream_context, &outgoing); struct StreamMessage* response; // we should get back a protobuf'd record - stream->read(&session_context, &response, 5); + kademlia_stream->read(kademlia_stream->stream_context, &response, 5); if (response->data_size == 1) return NULL; // turn the protobuf into a Node diff --git a/test/core/test_ping.h b/test/core/test_ping.h index 3179021..9f54196 100644 --- a/test/core/test_ping.h +++ b/test/core/test_ping.h @@ -23,11 +23,9 @@ int test_ping() { //struct IpfsNode local_node; struct Libp2pPeer* remote_peer = NULL; struct Dialer* dialer = NULL; - struct Connection* conn = NULL; + struct Stream* conn = NULL; unsigned char* protobuf = NULL; size_t protobuf_size = 0; - unsigned char* response = NULL; - size_t response_size = 0; // act like this is a normal node drop_build_and_open_repo("/tmp/.ipfs", &fs_repo); @@ -63,9 +61,13 @@ int test_ping() { //TODO: Dialer should know the protocol // send the record - conn->write(conn, (char*)protobuf, protobuf_size); - conn->read(conn, (char**)&response, &response_size); - libp2p_message_protobuf_decode(response, response_size, &message); + struct StreamMessage msg; + msg.data = (uint8_t*)protobuf; + msg.data_size = protobuf_size; + conn->write(conn->stream_context, &msg); + struct StreamMessage* incoming_message = NULL; + conn->read(conn->stream_context, &incoming_message, 10); + libp2p_message_protobuf_decode(incoming_message->data, incoming_message->data_size, &message); // verify the response if (message->message_type != MESSAGE_TYPE_PING) goto exit;