From a907f1dd2dd7fcd6b998109f065d2fec668ba7a9 Mon Sep 17 00:00:00 2001 From: Jose Marcial Vieira Bisneto Date: Wed, 20 Sep 2017 14:39:26 -0300 Subject: [PATCH] The API changed pthread to scope, so it can load multiple instances. --- cmd/ipfs/init.c | 1 + core/api.c | 21 +++++++------------- core/bootstrap.c | 1 + core/builder.c | 1 + core/client_api.c | 1 + core/daemon.c | 6 +++--- core/ipfs_node.c | 19 +++++++++--------- core/ping.c | 1 + exchange/bitswap/bitswap.c | 1 + exchange/bitswap/engine.c | 2 ++ exchange/bitswap/network.c | 2 ++ exchange/bitswap/want_manager.c | 2 ++ importer/exporter.c | 9 ++++++--- importer/importer.c | 6 ++++-- importer/resolver.c | 1 + include/ipfs/core/api.h | 4 ++-- include/ipfs/core/ipfs_node.h | 6 +++--- journal/journal.c | 2 ++ namesys/publisher.c | 1 + routing/k_routing.c | 2 ++ routing/offline.c | 1 + routing/online.c | 1 + test/cmd/ipfs/test_init.h | 1 + test/core/test_api.h | 8 +++++--- test/core/test_node.h | 6 ++++-- test/core/test_null.h | 7 ++++--- test/exchange/test_bitswap.h | 24 ++++++++++++---------- test/journal/test_journal.h | 6 ++++-- test/namesys/test_publisher.h | 6 ++++-- test/node/test_importer.h | 21 +++++++++++--------- test/routing/test_routing.h | 35 ++++++++++++++++++++------------- test/test_helper.c | 1 + test/testit.c | 2 ++ 33 files changed, 127 insertions(+), 81 deletions(-) diff --git a/cmd/ipfs/init.c b/cmd/ipfs/init.c index 439b24d..d9a695a 100644 --- a/cmd/ipfs/init.c +++ b/cmd/ipfs/init.c @@ -1,5 +1,6 @@ #include #include +#include #include "ipfs/cmd/ipfs/init.h" #include "ipfs/commands/request.h" diff --git a/core/api.c b/core/api.c index 5ef1c08..fa99402 100644 --- a/core/api.c +++ b/core/api.c @@ -22,8 +22,6 @@ pthread_mutex_t conns_lock; int conns_count; -pthread_t listen_thread = 0; - struct s_list api_list; /** @@ -624,7 +622,7 @@ void *api_listen_thread (void *ptr) * @param timeout time out of client connection. * @returns 0 when failure or 1 if success. */ -int api_start (struct IpfsNode* local_node, int max_conns, int timeout) +int api_start (pthread_t *scope_pth, struct IpfsNode* local_node, int max_conns, int timeout) { int s; size_t alloc_size = sizeof(void*) * max_conns; @@ -638,11 +636,6 @@ int api_start (struct IpfsNode* local_node, int max_conns, int timeout) api_list.ipv4 = hostname_to_ip(ip); // api is listening only on loopback. api_list.port = port; - if (listen_thread != 0) { - libp2p_logger_error("api", "API already running.\n"); - return 0; - } - if ((s = socket_listen(socket_tcp4(), &(api_list.ipv4), &(api_list.port))) <= 0) { libp2p_logger_error("api", "Failed to init API. port: %d\n", port); return 0; @@ -660,11 +653,11 @@ int api_start (struct IpfsNode* local_node, int max_conns, int timeout) } memset(api_list.conns, 0, alloc_size); - if (pthread_create(&listen_thread, NULL, api_listen_thread, (void*)local_node)) { + if (pthread_create(scope_pth, NULL, api_listen_thread, (void*)local_node)) { close (s); free (api_list.conns); api_list.conns = NULL; - listen_thread = 0; + *scope_pth = 0; libp2p_logger_error("api", "Error creating thread for API.\n"); return 0; } @@ -676,14 +669,14 @@ int api_start (struct IpfsNode* local_node, int max_conns, int timeout) * Stop API. * @returns 0 when failure or 1 if success. */ -int api_stop (void) +int api_stop (pthread_t *scope_pth) { - if (!listen_thread) return 0; - pthread_cancel(listen_thread); + if (*scope_pth == 0) return 0; + pthread_cancel(*scope_pth); api_connections_cleanup (); - listen_thread = 0; + *scope_pth = 0; return 1; } diff --git a/core/bootstrap.c b/core/bootstrap.c index 5e09b8f..79200bd 100644 --- a/core/bootstrap.c +++ b/core/bootstrap.c @@ -1,3 +1,4 @@ +#include #include "libp2p/peer/peer.h" #include "libp2p/utils/logger.h" diff --git a/core/builder.c b/core/builder.c index 6d7db79..2824121 100644 --- a/core/builder.c +++ b/core/builder.c @@ -1,3 +1,4 @@ +#include #include "ipfs/core/builder.h" int ipfs_core_builder_new_node(struct Context* context, struct BuildCfg* build_cfg, struct IpfsNode* buildConfig) { diff --git a/core/client_api.c b/core/client_api.c index a80f4e0..29acae6 100644 --- a/core/client_api.c +++ b/core/client_api.c @@ -6,6 +6,7 @@ #include #include #include +#include #include "ipfs/core/client_api.h" #include "multiaddr/multiaddr.h" diff --git a/core/daemon.c b/core/daemon.c index e64133d..b2554de 100644 --- a/core/daemon.c +++ b/core/daemon.c @@ -15,14 +15,14 @@ int ipfs_daemon_start(char* repo_path) { int count_pths = 0, retVal = 0; - pthread_t work_pths[MAX]; + pthread_t work_pths[MAX], api_pth = 0; struct IpfsNodeListenParams listen_param; struct MultiAddress* ma = NULL; libp2p_logger_info("daemon", "Initializing daemon for %s...\n", repo_path); struct IpfsNode* local_node = NULL; - if (!ipfs_node_online_new(repo_path, &local_node)) + if (!ipfs_node_online_new(&api_pth, repo_path, &local_node)) goto exit; // Set null router param @@ -56,7 +56,7 @@ int ipfs_daemon_start(char* repo_path) { if (ma != NULL) multiaddress_free(ma); if (local_node != NULL) { - ipfs_node_free(local_node); + ipfs_node_free(&api_pth, local_node); } return retVal; diff --git a/core/ipfs_node.c b/core/ipfs_node.c index be42e82..dc62b98 100644 --- a/core/ipfs_node.c +++ b/core/ipfs_node.c @@ -1,4 +1,5 @@ #include +#include #include "libp2p/net/multistream.h" #include "libp2p/utils/vector.h" @@ -43,7 +44,7 @@ int ipfs_node_online_protocol_handlers_free(struct Libp2pVector* handlers) { * @param node the completed IpfsNode struct * @returns true(1) on success */ -int ipfs_node_online_new(const char* repo_path, struct IpfsNode** node) { +int ipfs_node_online_new(pthread_t *pth_scope, const char* repo_path, struct IpfsNode** node) { struct FSRepo* fs_repo = NULL; *node = (struct IpfsNode*)malloc(sizeof(struct IpfsNode)); @@ -60,13 +61,13 @@ int ipfs_node_online_new(const char* repo_path, struct IpfsNode** node) { // build the struct if (!ipfs_repo_fsrepo_new(repo_path, NULL, &fs_repo)) { - ipfs_node_free(local_node); + ipfs_node_free(pth_scope, local_node); *node = NULL; return 0; } // open the repo if (!ipfs_repo_fsrepo_open(fs_repo)) { - ipfs_node_free(local_node); + ipfs_node_free(pth_scope, local_node); *node = NULL; return 0; } @@ -83,7 +84,7 @@ int ipfs_node_online_new(const char* repo_path, struct IpfsNode** node) { local_node->exchange = ipfs_bitswap_new(local_node); // fire up the API - api_start(local_node, 10, 5); + api_start(pth_scope, local_node, 10, 5); return 1; } @@ -94,7 +95,7 @@ int ipfs_node_online_new(const char* repo_path, struct IpfsNode** node) { * @param node the completed IpfsNode struct * @returns true(1) on success */ -int ipfs_node_offline_new(const char* repo_path, struct IpfsNode** node) { +int ipfs_node_offline_new(pthread_t *pth_scope, const char* repo_path, struct IpfsNode** node) { struct FSRepo* fs_repo = NULL; *node = (struct IpfsNode*)malloc(sizeof(struct IpfsNode)); @@ -111,13 +112,13 @@ int ipfs_node_offline_new(const char* repo_path, struct IpfsNode** node) { // build the struct if (!ipfs_repo_fsrepo_new(repo_path, NULL, &fs_repo)) { - ipfs_node_free(local_node); + ipfs_node_free(pth_scope, local_node); *node = NULL; return 0; } // open the repo if (!ipfs_repo_fsrepo_open(fs_repo)) { - ipfs_node_free(local_node); + ipfs_node_free(pth_scope, local_node); *node = NULL; return 0; } @@ -144,9 +145,9 @@ int ipfs_node_offline_new(const char* repo_path, struct IpfsNode** node) { * @param node the node to free * @returns true(1) */ -int ipfs_node_free(struct IpfsNode* node) { +int ipfs_node_free(pthread_t *pth_scope, struct IpfsNode* node) { if (node != NULL) { - api_stop(); + api_stop(pth_scope); if (node->exchange != NULL) { node->exchange->Close(node->exchange); } diff --git a/core/ping.c b/core/ping.c index b15e3d0..33c1b72 100644 --- a/core/ping.c +++ b/core/ping.c @@ -4,6 +4,7 @@ #include #include #include +#include #include "libp2p/net/p2pnet.h" #include "libp2p/net/multistream.h" #include "libp2p/record/message.h" diff --git a/exchange/bitswap/bitswap.c b/exchange/bitswap/bitswap.c index d479291..8f9de75 100644 --- a/exchange/bitswap/bitswap.c +++ b/exchange/bitswap/bitswap.c @@ -3,6 +3,7 @@ */ #include #include // for sleep() +#include #include "libp2p/os/utils.h" #include "libp2p/utils/logger.h" #include "ipfs/core/ipfs_node.h" diff --git a/exchange/bitswap/engine.c b/exchange/bitswap/engine.c index 3d56390..d130f49 100644 --- a/exchange/bitswap/engine.c +++ b/exchange/bitswap/engine.c @@ -1,4 +1,6 @@ #include +#include + #include "libp2p/utils/logger.h" #include "ipfs/core/null.h" #include "ipfs/exchange/bitswap/engine.h" diff --git a/exchange/bitswap/network.c b/exchange/bitswap/network.c index a1d9dff..a5d24f2 100644 --- a/exchange/bitswap/network.c +++ b/exchange/bitswap/network.c @@ -5,6 +5,8 @@ * For a somewhat accurate diagram of how this may work, @see https://github.com/ipfs/js-ipfs-bitswap */ +#include + #include "libp2p/utils/logger.h" #include "ipfs/exchange/bitswap/network.h" #include "ipfs/exchange/bitswap/peer_request_queue.h" diff --git a/exchange/bitswap/want_manager.c b/exchange/bitswap/want_manager.c index 29f32d9..f55f4da 100644 --- a/exchange/bitswap/want_manager.c +++ b/exchange/bitswap/want_manager.c @@ -1,3 +1,5 @@ +#include + #include "ipfs/exchange/bitswap/want_manager.h" #include "ipfs/exchange/bitswap/wantlist_queue.h" diff --git a/importer/exporter.c b/importer/exporter.c index 3b7f011..c883fe0 100644 --- a/importer/exporter.c +++ b/importer/exporter.c @@ -1,5 +1,6 @@ #include #include +#include #include "ipfs/cid/cid.h" #include "ipfs/merkledag/merkledag.h" @@ -190,6 +191,7 @@ int ipfs_exporter_to_console(const unsigned char* hash, struct IpfsNode *local_n */ int ipfs_exporter_object_get(int argc, char** argv) { char* repo_path = NULL; + pthread_t api_pth = 0; if (!ipfs_repo_get_directory(argc, argv, &repo_path)) { fprintf(stderr, "Unable to open repository: %s\n", repo_path); @@ -197,13 +199,13 @@ int ipfs_exporter_object_get(int argc, char** argv) { } struct IpfsNode* local_node = NULL; - if (!ipfs_node_online_new(repo_path, &local_node)) + if (!ipfs_node_online_new(&api_pth, repo_path, &local_node)) return 0; // find hash int retVal = ipfs_exporter_to_console((unsigned char*)argv[3], local_node); - ipfs_node_free(local_node); + ipfs_node_free(&api_pth, local_node); return retVal; } @@ -264,13 +266,14 @@ int ipfs_exporter_object_cat_to_file(struct IpfsNode *local_node, unsigned char* int ipfs_exporter_object_cat(int argc, char** argv) { struct IpfsNode *local_node = NULL; char* repo_dir = NULL; + pthread_t api_pth = 0; if (!ipfs_repo_get_directory(argc, argv, &repo_dir)) { fprintf(stderr, "Unable to open repo: %s\n", repo_dir); return 0; } - if (!ipfs_node_offline_new(repo_dir, &local_node)) + if (!ipfs_node_offline_new(&api_pth, repo_dir, &local_node)) return 0; if (local_node->mode == MODE_API_AVAILABLE) { diff --git a/importer/importer.c b/importer/importer.c index 412f62c..273f121 100644 --- a/importer/importer.c +++ b/importer/importer.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "ipfs/importer/importer.h" #include "ipfs/merkledag/merkledag.h" @@ -378,6 +379,7 @@ int ipfs_import_files(int argc, char** argv) { char* path = NULL; char* filename = NULL; struct HashtableNode* directory_entry = NULL; + pthread_t api_pth = 0; int recursive = ipfs_import_is_recursive(argc, argv); @@ -389,7 +391,7 @@ int ipfs_import_files(int argc, char** argv) { fprintf(stderr, "Repo does not exist: %s\n", repo_path); goto exit; } - ipfs_node_online_new(repo_path, &local_node); + ipfs_node_online_new(&api_pth, repo_path, &local_node); // import the file(s) @@ -419,7 +421,7 @@ int ipfs_import_files(int argc, char** argv) { retVal = 1; exit: if (local_node != NULL) - ipfs_node_free(local_node); + ipfs_node_free(&api_pth, local_node); // free file list current = first; while (current != NULL) { diff --git a/importer/resolver.c b/importer/resolver.c index f931387..9ede07e 100644 --- a/importer/resolver.c +++ b/importer/resolver.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "ipfs/importer/resolver.h" #include "libp2p/utils/logger.h" diff --git a/include/ipfs/core/api.h b/include/ipfs/core/api.h index 33ac85c..e6bc12a 100644 --- a/include/ipfs/core/api.h +++ b/include/ipfs/core/api.h @@ -68,5 +68,5 @@ struct s_request { void *api_connection_thread (void *ptr); void api_connections_cleanup (void); void *api_listen_thread (void *ptr); -int api_start (struct IpfsNode* local_node, int max_conns, int timeout); -int api_stop (void); +int api_start (pthread_t *scope_pth, struct IpfsNode* local_node, int max_conns, int timeout); +int api_stop (pthread_t *scope_pth); diff --git a/include/ipfs/core/ipfs_node.h b/include/ipfs/core/ipfs_node.h index ba2c829..7d99d53 100644 --- a/include/ipfs/core/ipfs_node.h +++ b/include/ipfs/core/ipfs_node.h @@ -47,7 +47,7 @@ struct IpfsNode { * @param node the completed IpfsNode struct * @returns true(1) on success */ -int ipfs_node_online_new(const char* repo_path, struct IpfsNode** node); +int ipfs_node_online_new(pthread_t *pth_scope, const char* repo_path, struct IpfsNode** node); /*** * build an offline IpfsNode @@ -55,11 +55,11 @@ int ipfs_node_online_new(const char* repo_path, struct IpfsNode** node); * @param node the completed IpfsNode struct * @returns true(1) on success */ -int ipfs_node_offline_new(const char* repo_path, struct IpfsNode** node); +int ipfs_node_offline_new(pthread_t *pth_scope, const char* repo_path, struct IpfsNode** node); /*** * Free resources from the creation of an IpfsNode * @param node the node to free * @returns true(1) */ -int ipfs_node_free(struct IpfsNode* node); +int ipfs_node_free(pthread_t *pth_scope, struct IpfsNode* node); diff --git a/journal/journal.c b/journal/journal.c index 2fdabfb..1bafe8f 100644 --- a/journal/journal.c +++ b/journal/journal.c @@ -1,6 +1,8 @@ /** * The journal protocol attempts to keep a journal in sync with other (approved) nodes */ +#include + #include "libp2p/crypto/encoding/base58.h" #include "libp2p/os/utils.h" #include "libp2p/utils/logger.h" diff --git a/namesys/publisher.c b/namesys/publisher.c index fadd07c..aa80345 100644 --- a/namesys/publisher.c +++ b/namesys/publisher.c @@ -1,5 +1,6 @@ #include #include +#include #include "libp2p/routing/dht_protocol.h" #include "ipfs/util/errs.h" #include "ipfs/util/time.h" diff --git a/routing/k_routing.c b/routing/k_routing.c index 90a6744..084f1e3 100644 --- a/routing/k_routing.c +++ b/routing/k_routing.c @@ -1,3 +1,5 @@ +#include + #include "ipfs/routing/routing.h" #include "libp2p/routing/kademlia.h" #include "libp2p/peer/providerstore.h" diff --git a/routing/offline.c b/routing/offline.c index 4b79930..e738e35 100644 --- a/routing/offline.c +++ b/routing/offline.c @@ -1,4 +1,5 @@ #include +#include #include #include #include "libp2p/crypto/rsa.h" diff --git a/routing/online.c b/routing/online.c index 65cee37..80cb108 100644 --- a/routing/online.c +++ b/routing/online.c @@ -1,5 +1,6 @@ #include #include +#include #include "libp2p/crypto/encoding/base58.h" #include "ipfs/routing/routing.h" diff --git a/test/cmd/ipfs/test_init.h b/test/cmd/ipfs/test_init.h index 9cb0c81..579c1e2 100644 --- a/test/cmd/ipfs/test_init.h +++ b/test/cmd/ipfs/test_init.h @@ -8,6 +8,7 @@ #include #include //#include +#include #include "ipfs/cmd/ipfs/init.h" #include "ipfs/commands/argument.h" diff --git a/test/core/test_api.h b/test/core/test_api.h index 5c2a8a1..43442e9 100644 --- a/test/core/test_api.h +++ b/test/core/test_api.h @@ -11,6 +11,7 @@ int test_core_api_startup_shutdown() { char* repo_path = "/tmp/ipfs_1/.ipfs"; char* peer_id = NULL; int retVal = 0; + pthread_t api_pth; if (!drop_and_build_repository(repo_path, 4001, NULL, &peer_id)) goto exit; @@ -20,7 +21,7 @@ int test_core_api_startup_shutdown() { sleep(3); struct IpfsNode* client_node = NULL; - if (!ipfs_node_offline_new(repo_path, &client_node)) { + if (!ipfs_node_offline_new(&api_pth, repo_path, &client_node)) { goto exit; } // test to see if it is working @@ -97,11 +98,12 @@ int test_core_api_object_cat() { struct HashtableNode* node; size_t bytes_written; struct IpfsNode *local_node = NULL; - ipfs_node_offline_new(ipfs_path1, &local_node); + pthread_t api_pth = 0; + ipfs_node_offline_new(&api_pth, ipfs_path1, &local_node); ipfs_import_file(NULL, filename, &node, local_node, &bytes_written, 0); memset(hash, 0, 256); ipfs_cid_hash_to_base58(node->hash, node->hash_size, (unsigned char*)hash, 256); - ipfs_node_free(local_node); + ipfs_node_free(&api_pth, local_node); ipfs_hashtable_node_free(node); libp2p_logger_debug("test_api", "*** Firing up daemons ***\n"); diff --git a/test/core/test_node.h b/test/core/test_node.h index 3d272b7..aa3d65e 100644 --- a/test/core/test_node.h +++ b/test/core/test_node.h @@ -1,4 +1,5 @@ #include +#include #include "../test_helper.h" #include "ipfs/core/ipfs_node.h" @@ -9,11 +10,12 @@ int test_node_peerstore() { char* peer_id = NULL; struct IpfsNode *local_node = NULL; struct Libp2pPeer* peer = NULL; + pthread_t api_pth = 0; if (!drop_and_build_repository(repo_path, 4001, NULL, &peer_id)) goto exit; - if (!ipfs_node_online_new(repo_path, &local_node)) + if (!ipfs_node_online_new(&api_pth, repo_path, &local_node)) goto exit; // add a peer to the peerstore @@ -47,7 +49,7 @@ int test_node_peerstore() { if (peer != NULL) libp2p_peer_free(peer); if (local_node != NULL) - ipfs_node_free(local_node); + ipfs_node_free(&api_pth, local_node); return retVal; } diff --git a/test/core/test_null.h b/test/core/test_null.h index b160613..ac2c237 100644 --- a/test/core/test_null.h +++ b/test/core/test_null.h @@ -32,10 +32,11 @@ int test_null_add_provider() { // add a file, to prime the connection to peer 1 //TODO: Find a better way to do this... size_t bytes_written = 0; - ipfs_node_online_new(ipfs_path, &local_node2); + pthread_t api_pth = 0; + ipfs_node_online_new(&api_pth, ipfs_path, &local_node2); struct HashtableNode* node = NULL; ipfs_import_file(NULL, "/home/parallels/ipfstest/hello_world.txt", &node, local_node2, &bytes_written, 0); - ipfs_node_free(local_node2); + ipfs_node_free(&api_pth, local_node2); // start the daemon in a separate thread if (pthread_create(&thread2, NULL, test_daemon_start, (void*)ipfs_path) < 0) goto exit; @@ -49,7 +50,7 @@ int test_null_add_provider() { retVal = 1; exit: if (local_node2 != NULL) - ipfs_node_free(local_node2); + ipfs_node_free(&api_pth, local_node2); if (ma_peer1 != NULL) multiaddress_free(ma_peer1); ipfs_daemon_stop(); diff --git a/test/exchange/test_bitswap.h b/test/exchange/test_bitswap.h index 9129963..d00daae 100644 --- a/test/exchange/test_bitswap.h +++ b/test/exchange/test_bitswap.h @@ -125,11 +125,12 @@ int test_bitswap_retrieve_file() size_t bytes_written = 0; struct Block* block = NULL; struct Cid* cid = NULL; + pthread_t api_pth = 0; // build and open the new IPFS repository with no bootstrap peers os_utils_setenv("IPFS_PATH", ipfs_path, 1); drop_and_build_repository(ipfs_path, 4001, NULL, NULL); - ipfs_node_online_new(ipfs_path, &localNode); + ipfs_node_online_new(&api_pth, ipfs_path, &localNode); // add a file localNode->routing->Bootstrap(localNode->routing); @@ -154,7 +155,7 @@ int test_bitswap_retrieve_file() ipfs_cid_free(cid); if (node != NULL) ipfs_hashtable_node_free(node); - ipfs_node_free(localNode); + ipfs_node_free(&api_pth, localNode); return retVal; } @@ -187,6 +188,7 @@ int test_bitswap_retrieve_file_remote() { struct HashtableNode* node = NULL; struct Block* result = NULL; struct Cid* cid = NULL; + pthread_t api_pth1 = 0, api_pth2 = 0; // create peer 1 libp2p_logger_debug("test_bitswap", "Firing up daemon 1.\n"); @@ -196,7 +198,7 @@ int test_bitswap_retrieve_file_remote() { ma_peer1 = multiaddress_new_from_string(multiaddress_string); // add a file size_t bytes_written = 0; - ipfs_node_online_new(ipfs_path, &ipfs_node1); + ipfs_node_online_new(&api_pth1, ipfs_path, &ipfs_node1); ipfs_import_file(NULL, "/home/parallels/ipfstest/hello_world.txt", &node, ipfs_node1, &bytes_written, 0); // start the daemon in a separate thread if (pthread_create(&thread1, NULL, test_daemon_start, (void*)ipfs_path) < 0) { @@ -215,7 +217,7 @@ int test_bitswap_retrieve_file_remote() { libp2p_utils_vector_add(ma_vector2, ma_peer1); drop_and_build_repository(ipfs_path, 4002, ma_vector2, &peer_id_2); multiaddress_free(ma_peer1); - ipfs_node_online_new(ipfs_path, &ipfs_node2); + ipfs_node_online_new(&api_pth2, ipfs_path, &ipfs_node2); ipfs_node2->routing->Bootstrap(ipfs_node2->routing); @@ -299,6 +301,7 @@ int test_bitswap_retrieve_file_known_remote() { struct Libp2pVector* ma_vector2 = NULL; struct Block* result = NULL; struct Cid* cid = NULL; + pthread_t api_pth = 0; // create peer 1 char multiaddress_string[255]; @@ -312,7 +315,7 @@ int test_bitswap_retrieve_file_known_remote() { libp2p_utils_vector_add(ma_vector2, ma_peer1); drop_and_build_repository(ipfs_path, 4002, ma_vector2, &peer_id_2); multiaddress_free(ma_peer1); - ipfs_node_online_new(ipfs_path, &ipfs_node2); + ipfs_node_online_new(&api_pth, ipfs_path, &ipfs_node2); if (!ipfs_cid_decode_hash_from_base58((unsigned char*)hello_world_hash, strlen(hello_world_hash), &cid)) goto exit; @@ -354,7 +357,7 @@ int test_bitswap_retrieve_file_known_remote() { // ipfs_block_free(result); if (cid != NULL) ipfs_cid_free(cid); - ipfs_node_free(ipfs_node2); + ipfs_node_free(&api_pth, ipfs_node2); return retVal; } @@ -390,6 +393,7 @@ int test_bitswap_retrieve_file_third_party() { struct HashtableNode* node = NULL; struct Block* result = NULL; struct Cid* cid = NULL; + pthread_t api_pth1 = 0, api_pth2 = 0; // create peer 1 libp2p_logger_debug("test_bitswap", "Firing up daemon 1.\n"); @@ -418,11 +422,11 @@ int test_bitswap_retrieve_file_third_party() { // add a file, to prime the connection to peer 1 //TODO: Find a better way to do this... size_t bytes_written = 0; - if (!ipfs_node_online_new(ipfs_path, &ipfs_node2)) + if (!ipfs_node_online_new(&api_pth1, ipfs_path, &ipfs_node2)) goto exit; ipfs_node2->routing->Bootstrap(ipfs_node2->routing); ipfs_import_file(NULL, "/home/parallels/ipfstest/hello_world.txt", &node, ipfs_node2, &bytes_written, 0); - ipfs_node_free(ipfs_node2); + ipfs_node_free(&api_pth1, ipfs_node2); // start the daemon in a separate thread if (pthread_create(&thread2, NULL, test_daemon_start, (void*)ipfs_path) < 0) { libp2p_logger_error("test_bitswap", "Unable to start thread 2\n"); @@ -440,7 +444,7 @@ int test_bitswap_retrieve_file_third_party() { libp2p_utils_vector_add(ma_vector3, ma_peer1); drop_and_build_repository(ipfs_path, 4003, ma_vector3, &peer_id_3); multiaddress_free(ma_peer1); - ipfs_node_online_new(ipfs_path, &ipfs_node3); + ipfs_node_online_new(&api_pth2, ipfs_path, &ipfs_node3); ipfs_node3->routing->Bootstrap(ipfs_node3->routing); @@ -469,7 +473,7 @@ int test_bitswap_retrieve_file_third_party() { if (thread2_started) pthread_join(thread2, NULL); if (ipfs_node3 != NULL) - ipfs_node_free(ipfs_node3); + ipfs_node_free(&api_pth2, ipfs_node3); if (peer_id_1 != NULL) free(peer_id_1); if (peer_id_2 != NULL) diff --git a/test/journal/test_journal.h b/test/journal/test_journal.h index 493f3df..2a0d5a1 100644 --- a/test/journal/test_journal.h +++ b/test/journal/test_journal.h @@ -1,4 +1,5 @@ #include +#include #include "ipfs/journal/journal_entry.h" #include "ipfs/journal/journal_message.h" @@ -118,9 +119,10 @@ int test_journal_server_1() { struct HashtableNode* node; size_t bytes_written; struct IpfsNode *local_node = NULL; - ipfs_node_offline_new(ipfs_path, &local_node); + pthread_t api_pth = 0; + ipfs_node_offline_new(&api_pth, ipfs_path, &local_node); ipfs_import_file(NULL, filename, &node, local_node, &bytes_written, 0); - ipfs_node_free(local_node); + ipfs_node_free(&api_pth, local_node); ipfs_hashtable_node_free(node); libp2p_logger_debug("test_journal", "*** Firing up daemon for server 1 ***\n"); diff --git a/test/namesys/test_publisher.h b/test/namesys/test_publisher.h index a409744..98c4f50 100644 --- a/test/namesys/test_publisher.h +++ b/test/namesys/test_publisher.h @@ -1,4 +1,5 @@ #include +#include #include "ipfs/cid/cid.h" #include "ipfs/core/ipfs_node.h" @@ -10,9 +11,10 @@ int test_namesys_publisher_publish() { struct Cid* cid = NULL; char* hash_text = "QmZtAEqmnXMZkwVPKdyMGxUoo35cQMzNhmq6CN3DvgRwAD"; char* repo_path = "/tmp/ipfs_1/.ipfs"; + pthread_t api_pth = 0; // get a local node - if (!ipfs_node_offline_new(repo_path, &local_node)) { + if (!ipfs_node_offline_new(&api_pth, repo_path, &local_node)) { libp2p_logger_error("test_publisher", "publish: Unable to open ipfs repository.\n"); goto exit; } @@ -31,7 +33,7 @@ int test_namesys_publisher_publish() { retVal = 1; exit: - ipfs_node_free(local_node); + ipfs_node_free(&api_pth, local_node); ipfs_cid_free(cid); return retVal; } diff --git a/test/node/test_importer.h b/test/node/test_importer.h index 15e7a6e..66ef62a 100644 --- a/test/node/test_importer.h +++ b/test/node/test_importer.h @@ -1,4 +1,5 @@ #include +#include #include "../test_helper.h" #include "ipfs/importer/importer.h" @@ -29,6 +30,7 @@ int test_import_large_file() { size_t bytes_read2 = 1; unsigned char buf1[100]; unsigned char buf2[100]; + pthread_t api_pth = 0; // create the necessary file create_bytes(file_bytes, bytes_size); @@ -40,7 +42,7 @@ int test_import_large_file() { goto exit; } - if (!ipfs_node_online_new(repo_dir, &local_node)) { + if (!ipfs_node_online_new(&api_pth, repo_dir, &local_node)) { fprintf(stderr, "Unable to create new IpfsNode\n"); goto exit; } @@ -128,7 +130,7 @@ int test_import_large_file() { exit: if (local_node != NULL) - ipfs_node_free(local_node); + ipfs_node_free(&api_pth, local_node); if (write_node != NULL) ipfs_hashtable_node_free(write_node); if (read_node != NULL) @@ -146,6 +148,7 @@ int test_import_small_file() { const char* fileName = "/tmp/test_import_small.tmp"; const char* repo_path = "/tmp/.ipfs"; struct IpfsNode *local_node = NULL; + pthread_t api_pth = 0; // create the necessary file create_bytes(file_bytes, bytes_size); @@ -154,13 +157,13 @@ int test_import_small_file() { // get the repo drop_and_build_repository(repo_path, 4001, NULL, NULL); - ipfs_node_offline_new(repo_path, &local_node); + ipfs_node_offline_new(&api_pth, repo_path, &local_node); // write to ipfs struct HashtableNode* write_node; size_t bytes_written; if (ipfs_import_file("/tmp", fileName, &write_node, local_node, &bytes_written, 1) == 0) { - ipfs_node_free(local_node); + ipfs_node_free(&api_pth, local_node); return 0; } @@ -171,7 +174,7 @@ int test_import_small_file() { for(int i = 0; i < 10; i++) { if (write_node->hash[i] != cid_test[i]) { printf("Hashes do not match at position %d, should be %02x but is %02x\n", i, cid_test[i], write_node->hash[i]); - ipfs_node_free(local_node); + ipfs_node_free(&api_pth, local_node); ipfs_hashtable_node_free(write_node); return 0; } @@ -180,7 +183,7 @@ int test_import_small_file() { // make sure all went okay struct HashtableNode* read_node; if (ipfs_merkledag_get(write_node->hash, write_node->hash_size, &read_node, local_node->repo) == 0) { - ipfs_node_free(local_node); + ipfs_node_free(&api_pth, local_node); ipfs_hashtable_node_free(write_node); return 0; } @@ -188,7 +191,7 @@ int test_import_small_file() { // compare data if (write_node->data_size != bytes_size + 8 || write_node->data_size != read_node->data_size) { printf("Data size of nodes are not equal or are incorrect. Should be %lu but are %lu\n", write_node->data_size, read_node->data_size); - ipfs_node_free(local_node); + ipfs_node_free(&api_pth, local_node); ipfs_hashtable_node_free(write_node); ipfs_hashtable_node_free(read_node); return 0; @@ -197,7 +200,7 @@ int test_import_small_file() { for(int i = 0; i < bytes_size; i++) { if (write_node->data[i] != read_node->data[i]) { printf("Data within node is different at position %d\n", i); - ipfs_node_free(local_node); + ipfs_node_free(&api_pth, local_node); ipfs_hashtable_node_free(write_node); ipfs_hashtable_node_free(read_node); return 0; @@ -217,7 +220,7 @@ int test_import_small_file() { fprintf(stderr, "Unable to find any records in the database.\n"); } - ipfs_node_free(local_node); + ipfs_node_free(&api_pth, local_node); ipfs_hashtable_node_free(write_node); ipfs_hashtable_node_free(read_node); diff --git a/test/routing/test_routing.h b/test/routing/test_routing.h index 7c9472e..e992ca7 100644 --- a/test/routing/test_routing.h +++ b/test/routing/test_routing.h @@ -101,6 +101,7 @@ int test_routing_find_peer() { struct Libp2pVector *ma_vector = NULL; struct Libp2pPeer* result = NULL; struct HashtableNode *node = NULL; + pthread_t api_pth = 0; //libp2p_logger_add_class("online"); //libp2p_logger_add_class("null"); @@ -130,10 +131,10 @@ int test_routing_find_peer() { // add a file, to prime the connection to peer 1 //TODO: Find a better way to do this... size_t bytes_written = 0; - ipfs_node_online_new(ipfs_path, &local_node2); + ipfs_node_online_new(&api_pth, ipfs_path, &local_node2); local_node2->routing->Bootstrap(local_node2->routing); ipfs_import_file(NULL, "/home/parallels/ipfstest/hello_world.txt", &node, local_node2, &bytes_written, 0); - ipfs_node_free(local_node2); + ipfs_node_free(&api_pth, local_node2); // start the daemon in a separate thread if (pthread_create(&thread2, NULL, test_daemon_start, (void*)ipfs_path) < 0) goto exit; @@ -224,6 +225,7 @@ int test_routing_find_providers() { struct FSRepo* fs_repo = NULL; struct HashtableNode* node = NULL; struct Libp2pVector* result = NULL; + pthread_t api_pth = 0; // create peer 1 drop_and_build_repository(ipfs_path, 4001, NULL, &peer_id_1); @@ -248,9 +250,9 @@ int test_routing_find_providers() { // add a file, to prime the connection to peer 1 //TODO: Find a better way to do this... size_t bytes_written = 0; - ipfs_node_online_new(ipfs_path, &local_node2); + ipfs_node_online_new(&api_pth, ipfs_path, &local_node2); ipfs_import_file(NULL, "/home/parallels/ipfstest/hello_world.txt", &node, local_node2, &bytes_written, 0); - ipfs_node_free(local_node2); + ipfs_node_free(&api_pth, local_node2); // start the daemon in a separate thread if (pthread_create(&thread2, NULL, test_daemon_start, (void*)ipfs_path) < 0) { fprintf(stderr, "Unable to start thread 2\n"); @@ -376,6 +378,7 @@ int test_routing_provide() { struct MultiAddress* ma_peer1 = NULL; struct Libp2pVector* ma_vector2 = NULL; struct HashtableNode* node = NULL; + pthread_t api_pth = 0; libp2p_logger_add_class("daemon"); libp2p_logger_add_class("null"); @@ -403,9 +406,9 @@ int test_routing_provide() { // add a file, to prime the connection to peer 1 //TODO: Find a better way to do this... size_t bytes_written = 0; - ipfs_node_online_new(ipfs_path, &local_node2); + ipfs_node_online_new(&api_pth, ipfs_path, &local_node2); ipfs_import_file(NULL, "/home/parallels/ipfstest/hello_world.txt", &node, local_node2, &bytes_written, 0); - ipfs_node_free(local_node2); + ipfs_node_free(&api_pth, local_node2); // start the daemon in a separate thread if (pthread_create(&thread2, NULL, test_daemon_start, (void*)ipfs_path) < 0) { fprintf(stderr, "Unable to start thread 2\n"); @@ -466,6 +469,8 @@ int test_routing_retrieve_file_third_party() { struct MultiAddress* ma_peer1 = NULL; struct Libp2pVector* ma_vector2 = NULL, *ma_vector3 = NULL; struct HashtableNode* node = NULL, *result_node = NULL; + pthread_t api_pth1 = 0; + pthread_t api_pth2 = 0; // create peer 1 drop_and_build_repository(ipfs_path, 4001, NULL, &peer_id_1); @@ -495,11 +500,11 @@ int test_routing_retrieve_file_third_party() { // add a file, to prime the connection to peer 1 //TODO: Find a better way to do this... size_t bytes_written = 0; - if (!ipfs_node_online_new(ipfs_path, &ipfs_node2)) + if (!ipfs_node_online_new(&api_pth1, ipfs_path, &ipfs_node2)) goto exit; ipfs_node2->routing->Bootstrap(ipfs_node2->routing); ipfs_import_file(NULL, "/home/parallels/ipfstest/hello_world.txt", &node, ipfs_node2, &bytes_written, 0); - ipfs_node_free(ipfs_node2); + ipfs_node_free(&api_pth1, ipfs_node2); // start the daemon in a separate thread libp2p_logger_debug("test_routing", "Firing up daemon 2.\n"); if (pthread_create(&thread2, NULL, test_daemon_start, (void*)ipfs_path) < 0) { @@ -520,7 +525,7 @@ int test_routing_retrieve_file_third_party() { libp2p_utils_vector_add(ma_vector3, ma_peer1); drop_and_build_repository(ipfs_path, 4003, ma_vector3, &peer_id_3); multiaddress_free(ma_peer1); - ipfs_node_online_new(ipfs_path, &ipfs_node3); + ipfs_node_online_new(&api_pth2, ipfs_path, &ipfs_node3); ipfs_node3->routing->Bootstrap(ipfs_node3->routing); @@ -547,7 +552,7 @@ int test_routing_retrieve_file_third_party() { if (thread2_started) pthread_join(thread2, NULL); if (ipfs_node3 != NULL) - ipfs_node_free(ipfs_node3); + ipfs_node_free(&api_pth2, ipfs_node3); if (peer_id_1 != NULL) free(peer_id_1); if (peer_id_2 != NULL) @@ -629,11 +634,12 @@ int test_routing_retrieve_large_file() { // add a file, to prime the connection to peer 1 //TODO: Find a better way to do this... size_t bytes_written = 0; - if (!ipfs_node_online_new(ipfs_path, &ipfs_node2)) + pthread_t api_pth1 = 0; + if (!ipfs_node_online_new(&api_pth1, ipfs_path, &ipfs_node2)) goto exit; ipfs_node2->routing->Bootstrap(ipfs_node2->routing); ipfs_import_file(NULL, "/home/parallels/ipfstest/test_import_large.tmp", &node, ipfs_node2, &bytes_written, 0); - ipfs_node_free(ipfs_node2); + ipfs_node_free(&api_pth1, ipfs_node2); // start the daemon in a separate thread libp2p_logger_debug("test_routing", "Firing up daemon 2.\n"); if (pthread_create(&thread2, NULL, test_daemon_start, (void*)ipfs_path) < 0) { @@ -655,7 +661,8 @@ int test_routing_retrieve_large_file() { libp2p_utils_vector_add(ma_vector3, ma_peer1); drop_and_build_repository(ipfs_path, 4003, ma_vector3, &peer_id_3); multiaddress_free(ma_peer1); - ipfs_node_online_new(ipfs_path, &ipfs_node3); + pthread_t api_pth2 = 0; + ipfs_node_online_new(&api_pth2, ipfs_path, &ipfs_node3); ipfs_node3->routing->Bootstrap(ipfs_node3->routing); @@ -680,7 +687,7 @@ int test_routing_retrieve_large_file() { if (thread2_started) pthread_join(thread2, NULL); if (ipfs_node3 != NULL) - ipfs_node_free(ipfs_node3); + ipfs_node_free(&api_pth2, ipfs_node3); if (peer_id_1 != NULL) free(peer_id_1); if (peer_id_2 != NULL) diff --git a/test/test_helper.c b/test/test_helper.c index 3c7173f..b56a3e0 100644 --- a/test/test_helper.c +++ b/test/test_helper.c @@ -4,6 +4,7 @@ #include #include #include +#include #include "ipfs/repo/init.h" #include "ipfs/repo/fsrepo/fs_repo.h" diff --git a/test/testit.c b/test/testit.c index 429b396..cfb6378 100644 --- a/test/testit.c +++ b/test/testit.c @@ -1,3 +1,5 @@ +#include + #include "cid/test_cid.h" #include "cmd/ipfs/test_init.h" #include "core/test_api.h"