diff --git a/core/api.c b/core/api.c index 5597a23..ca6b96e 100644 --- a/core/api.c +++ b/core/api.c @@ -20,10 +20,10 @@ #include "ipfs/importer/exporter.h" #include "ipfs/core/http_request.h" -pthread_mutex_t conns_lock; -int conns_count; +//pthread_mutex_t conns_lock; +//int conns_count; -struct s_list api_list; +//struct ApiContext api_list; /** * Write two strings on one write. @@ -401,8 +401,8 @@ void *api_connection_thread (void *ptr) buf[MAX_READ] = '\0'; - s = api_list.conns[params->index]->socket; - timeout = api_list.timeout; + s = params->this_node->api_context->conns[params->index]->socket; + timeout = params->this_node->api_context->timeout; if (socket_read_select4(s, timeout) <= 0) { libp2p_logger_error("api", "Client connection timeout.\n"); @@ -592,15 +592,15 @@ void *api_connection_thread (void *ptr) quit: if (req.buf) free(req.buf); - if (inet_ntop(AF_INET, &(api_list.conns[params->index]->ipv4), client, INET_ADDRSTRLEN) == NULL) + if (inet_ntop(AF_INET, &( params->this_node->api_context->conns[params->index]->ipv4), client, INET_ADDRSTRLEN) == NULL) strcpy(client, "UNKNOW"); - libp2p_logger_error("api", "Closing client connection %s:%d (%d).\n", client, api_list.conns[params->index]->port, params->index+1); - pthread_mutex_lock(&conns_lock); + libp2p_logger_error("api", "Closing client connection %s:%d (%d).\n", client, params->this_node->api_context->conns[params->index]->port, params->index+1); + pthread_mutex_lock(¶ms->this_node->api_context->conns_lock); close(s); - free (api_list.conns[params->index]); - api_list.conns[params->index] = NULL; - conns_count--; - pthread_mutex_unlock(&conns_lock); + free ( params->this_node->api_context->conns[params->index]); + params->this_node->api_context->conns[params->index] = NULL; + params->this_node->api_context->conns_count--; + pthread_mutex_unlock(¶ms->this_node->api_context->conns_lock); free(params); return NULL; } @@ -608,27 +608,27 @@ quit: /** * Close all connections stopping respectives pthreads and free allocated memory. */ -void api_connections_cleanup (void) +void api_connections_cleanup (struct IpfsNode* local_node) { int i; - pthread_mutex_lock(&conns_lock); - if (conns_count > 0 && api_list.conns) { - for (i = 0 ; i < api_list.max_conns ; i++) { - if (api_list.conns[i]->pthread) { - pthread_cancel (api_list.conns[i]->pthread); - close (api_list.conns[i]->socket); - free (api_list.conns[i]); - api_list.conns[i] = NULL; + pthread_mutex_lock(&local_node->api_context->conns_lock); + if (local_node->api_context->conns_count > 0 && local_node->api_context->conns) { + for (i = 0 ; i < local_node->api_context->max_conns ; i++) { + if (local_node->api_context->conns[i]->pthread) { + pthread_cancel (local_node->api_context->conns[i]->pthread); + close (local_node->api_context->conns[i]->socket); + free (local_node->api_context->conns[i]); + local_node->api_context->conns[i] = NULL; } } - conns_count = 0; + local_node->api_context->conns_count = 0; } - if (api_list.conns) { - free (api_list.conns); - api_list.conns = NULL; + if (local_node->api_context->conns) { + free (local_node->api_context->conns); + local_node->api_context->conns = NULL; } - pthread_mutex_unlock(&conns_lock); + pthread_mutex_unlock(&local_node->api_context->conns_lock); } /** @@ -645,56 +645,56 @@ void *api_listen_thread (void *ptr) char client[INET_ADDRSTRLEN]; struct IpfsNode* local_node = (struct IpfsNode*)ptr; - conns_count = 0; + local_node->api_context->conns_count = 0; for (;;) { - s = socket_accept4(api_list.socket, &ipv4, &port); + s = socket_accept4(local_node->api_context->socket, &ipv4, &port); if (s <= 0) { break; } - if (conns_count >= api_list.max_conns) { // limit reached. - libp2p_logger_error("api", "Limit of connections reached (%d).\n", api_list.max_conns); + if (local_node->api_context->conns_count >= local_node->api_context->max_conns) { // limit reached. + libp2p_logger_error("api", "Limit of connections reached (%d).\n", local_node->api_context->max_conns); close (s); continue; } - pthread_mutex_lock(&conns_lock); - for (i = 0 ; i < api_list.max_conns && api_list.conns[i] ; i++); - api_list.conns[i] = malloc (sizeof (struct s_conns)); - if (!api_list.conns[i]) { + pthread_mutex_lock(&local_node->api_context->conns_lock); + for (i = 0 ; i < local_node->api_context->max_conns && local_node->api_context->conns[i] ; i++); + local_node->api_context->conns[i] = malloc (sizeof (struct s_conns)); + if (!local_node->api_context->conns[i]) { libp2p_logger_error("api", "Fail to allocate memory to accept connection.\n"); - pthread_mutex_unlock(&conns_lock); + pthread_mutex_unlock(&local_node->api_context->conns_lock); close (s); continue; } if (inet_ntop(AF_INET, &ipv4, client, INET_ADDRSTRLEN) == NULL) strcpy(client, "UNKNOW"); - api_list.conns[i]->socket = s; - api_list.conns[i]->ipv4 = ipv4; - api_list.conns[i]->port = port; + local_node->api_context->conns[i]->socket = s; + local_node->api_context->conns[i]->ipv4 = ipv4; + local_node->api_context->conns[i]->port = port; // create a struct, which the thread is responsible to destroy struct ApiConnectionParam* connection_param = (struct ApiConnectionParam*) malloc(sizeof(struct ApiConnectionParam)); if (connection_param == NULL) { libp2p_logger_error("api", "api_listen_thread: Unable to allocate memory.\n"); - pthread_mutex_unlock(&conns_lock); + pthread_mutex_unlock(&local_node->api_context->conns_lock); close (s); continue; } connection_param->index = i; connection_param->this_node = local_node; - if (pthread_create(&(api_list.conns[i]->pthread), NULL, api_connection_thread, (void*)connection_param)) { + if (pthread_create(&(local_node->api_context->conns[i]->pthread), NULL, api_connection_thread, (void*)connection_param)) { libp2p_logger_error("api", "Create pthread fail.\n"); - free (api_list.conns[i]); - api_list.conns[i] = NULL; - conns_count--; + free (local_node->api_context->conns[i]); + local_node->api_context->conns[i] = NULL; + local_node->api_context->conns_count--; close(s); } else { - conns_count++; + local_node->api_context->conns_count++; } - libp2p_logger_error("api", "Accept connection %s:%d (%d/%d), pthread %d.\n", client, port, conns_count, api_list.max_conns, i+1); - pthread_mutex_unlock(&conns_lock); + libp2p_logger_error("api", "API for %s: Accept connection %s:%d (%d/%d), pthread %d.\n", client, port, local_node->api_context->conns_count, local_node->api_context->max_conns, i+1); + pthread_mutex_unlock(&local_node->api_context->conns_lock); } - api_connections_cleanup (); + api_connections_cleanup (local_node); return NULL; } @@ -705,7 +705,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 (pthread_t *scope_pth, struct IpfsNode* local_node, int max_conns, int timeout) +int api_start (struct IpfsNode* local_node, int max_conns, int timeout) { int s; size_t alloc_size = sizeof(void*) * max_conns; @@ -716,35 +716,38 @@ int api_start (pthread_t *scope_pth, struct IpfsNode* local_node, int max_conns, multiaddress_get_ip_address(my_address, &ip); int port = multiaddress_get_ip_port(my_address); - api_list.ipv4 = hostname_to_ip(ip); // api is listening only on loopback. - api_list.port = port; + local_node->api_context->ipv4 = hostname_to_ip(ip); // api is listening only on loopback. + local_node->api_context->port = port; - if ((s = socket_listen(socket_tcp4(), &(api_list.ipv4), &(api_list.port))) <= 0) { + if ((s = socket_listen(socket_tcp4(), &(local_node->api_context->ipv4), &(local_node->api_context->port))) <= 0) { libp2p_logger_error("api", "Failed to init API. port: %d\n", port); return 0; } - api_list.socket = s; - api_list.max_conns = max_conns; - api_list.timeout = timeout; + local_node->api_context->socket = s; + local_node->api_context->max_conns = max_conns; + local_node->api_context->timeout = timeout; - api_list.conns = malloc (alloc_size); - if (!api_list.conns) { + local_node->api_context->conns = malloc (alloc_size); + if (!local_node->api_context->conns) { close (s); libp2p_logger_error("api", "Error allocating memory.\n"); return 0; } - memset(api_list.conns, 0, alloc_size); + memset(local_node->api_context->conns, 0, alloc_size); - if (pthread_create(scope_pth, NULL, api_listen_thread, (void*)local_node)) { + local_node->api_context = (struct ApiContext*) malloc(sizeof(struct ApiContext)); + + if (pthread_create(&local_node->api_context->api_thread, NULL, api_listen_thread, (void*)local_node)) { close (s); - free (api_list.conns); - api_list.conns = NULL; - *scope_pth = 0; + free (local_node->api_context->conns); + local_node->api_context->conns = NULL; + local_node->api_context->api_thread = 0; libp2p_logger_error("api", "Error creating thread for API.\n"); return 0; } + libp2p_logger_debug("api", "Started API on localhost port %d.\n", port); return 1; } @@ -752,14 +755,14 @@ int api_start (pthread_t *scope_pth, struct IpfsNode* local_node, int max_conns, * Stop API. * @returns 0 when failure or 1 if success. */ -int api_stop (pthread_t *scope_pth) +int api_stop (struct IpfsNode *local_node) { - if (*scope_pth == 0) return 0; - pthread_cancel(*scope_pth); + if (local_node->api_context->api_thread == 0) return 0; + pthread_cancel(local_node->api_context->api_thread); - api_connections_cleanup (); + api_connections_cleanup (local_node); - *scope_pth = 0; + local_node->api_context->api_thread = 0; return 1; } diff --git a/core/daemon.c b/core/daemon.c index b2554de..e64133d 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], api_pth = 0; + pthread_t work_pths[MAX]; 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(&api_pth, repo_path, &local_node)) + if (!ipfs_node_online_new(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(&api_pth, local_node); + ipfs_node_free(local_node); } return retVal; diff --git a/core/ipfs_node.c b/core/ipfs_node.c index 0380e4f..1124ecb 100644 --- a/core/ipfs_node.c +++ b/core/ipfs_node.c @@ -60,7 +60,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(pthread_t *pth_scope, const char* repo_path, struct IpfsNode** node) { +int ipfs_node_online_new(const char* repo_path, struct IpfsNode** node) { struct FSRepo* fs_repo = NULL; *node = ipfs_node_new(); @@ -71,13 +71,13 @@ int ipfs_node_online_new(pthread_t *pth_scope, const char* repo_path, struct Ipf // build the struct if (!ipfs_repo_fsrepo_new(repo_path, NULL, &fs_repo)) { - ipfs_node_free(pth_scope, local_node); + ipfs_node_free(local_node); *node = NULL; return 0; } // open the repo if (!ipfs_repo_fsrepo_open(fs_repo)) { - ipfs_node_free(pth_scope, local_node); + ipfs_node_free(local_node); *node = NULL; return 0; } @@ -94,7 +94,7 @@ int ipfs_node_online_new(pthread_t *pth_scope, const char* repo_path, struct Ipf local_node->exchange = ipfs_bitswap_new(local_node); // fire up the API - api_start(pth_scope, local_node, 10, 5); + api_start(local_node, 10, 5); return 1; } @@ -116,13 +116,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(NULL, local_node); + ipfs_node_free(local_node); *node = NULL; return 0; } // open the repo if (!ipfs_repo_fsrepo_open(fs_repo)) { - ipfs_node_free(NULL, local_node); + ipfs_node_free(local_node); *node = NULL; return 0; } @@ -149,10 +149,10 @@ 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(pthread_t *pth_scope, struct IpfsNode* node) { +int ipfs_node_free(struct IpfsNode* node) { if (node != NULL) { - if (pth_scope != NULL) - api_stop(pth_scope); + if (node->api_context->api_thread != NULL) + api_stop(node); if (node->exchange != NULL) { node->exchange->Close(node->exchange); } diff --git a/importer/exporter.c b/importer/exporter.c index 9b45b17..8d5d4b9 100644 --- a/importer/exporter.c +++ b/importer/exporter.c @@ -191,7 +191,6 @@ 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); @@ -199,13 +198,13 @@ int ipfs_exporter_object_get(int argc, char** argv) { } struct IpfsNode* local_node = NULL; - if (!ipfs_node_online_new(&api_pth, repo_path, &local_node)) + if (!ipfs_node_online_new(repo_path, &local_node)) return 0; // find hash int retVal = ipfs_exporter_to_console((unsigned char*)argv[3], local_node); - ipfs_node_free(&api_pth, local_node); + ipfs_node_free(local_node); return retVal; } diff --git a/importer/importer.c b/importer/importer.c index 273f121..bcd4945 100644 --- a/importer/importer.c +++ b/importer/importer.c @@ -379,7 +379,6 @@ 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); @@ -391,7 +390,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(&api_pth, repo_path, &local_node); + ipfs_node_online_new(repo_path, &local_node); // import the file(s) @@ -421,7 +420,7 @@ int ipfs_import_files(int argc, char** argv) { retVal = 1; exit: if (local_node != NULL) - ipfs_node_free(&api_pth, local_node); + ipfs_node_free(local_node); // free file list current = first; while (current != NULL) { diff --git a/include/ipfs/core/api.h b/include/ipfs/core/api.h index fd3f1ce..6575df6 100644 --- a/include/ipfs/core/api.h +++ b/include/ipfs/core/api.h @@ -11,7 +11,7 @@ #define MAX_READ (32*1024) // 32k -struct s_list { +struct ApiContext { int socket; uint32_t ipv4; uint16_t port; @@ -23,6 +23,9 @@ struct s_list { uint16_t port; pthread_t pthread; } **conns; + pthread_mutex_t conns_lock; + int conns_count; + pthread_t api_thread; }; struct s_request { @@ -84,7 +87,7 @@ struct s_request { #define strstart(a,b) (memcmp(a,b,strlen(b))==0) void *api_connection_thread (void *ptr); -void api_connections_cleanup (void); +void api_connections_cleanup (struct IpfsNode* node); void *api_listen_thread (void *ptr); -int api_start (pthread_t *scope_pth, struct IpfsNode* local_node, int max_conns, int timeout); -int api_stop (pthread_t *scope_pth); +int api_start (struct IpfsNode* local_node, int max_conns, int timeout); +int api_stop (struct IpfsNode* local_node); diff --git a/include/ipfs/core/ipfs_node.h b/include/ipfs/core/ipfs_node.h index 20cd01e..90cf9e8 100644 --- a/include/ipfs/core/ipfs_node.h +++ b/include/ipfs/core/ipfs_node.h @@ -37,6 +37,7 @@ struct IpfsNode { struct Blockstore* blockstore; struct Exchange* exchange; struct Libp2pVector* protocol_handlers; + struct ApiContext* api_context; //struct Pinner pinning; // an interface //struct Mount** mounts; // TODO: Add more here @@ -48,7 +49,7 @@ struct IpfsNode { * @param node the completed IpfsNode struct * @returns true(1) on success */ -int ipfs_node_online_new(pthread_t *pth_scope, const char* repo_path, struct IpfsNode** node); +int ipfs_node_online_new(const char* repo_path, struct IpfsNode** node); /*** * build an offline IpfsNode @@ -63,4 +64,4 @@ 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(pthread_t *pth_scope, struct IpfsNode* node); +int ipfs_node_free(struct IpfsNode* node); diff --git a/namesys/name.c b/namesys/name.c index 58178bb..03d3e0c 100644 --- a/namesys/name.c +++ b/namesys/name.c @@ -83,7 +83,7 @@ int ipfs_name(struct CliArguments* args) { exit: // shut everything down - ipfs_node_free(NULL, client_node); + ipfs_node_free(client_node); return retVal; } diff --git a/test/config.test1.wo_journal b/test/config.test1.wo_journal index b0ed4b6..44e442e 100644 --- a/test/config.test1.wo_journal +++ b/test/config.test1.wo_journal @@ -19,7 +19,7 @@ "/ip4/0.0.0.0/tcp/4001", "/ip6/::/tcp/4001" ], - "API": "/ip4/127.0.0.1/tcp/5002", + "API": "/ip4/127.0.0.1/tcp/5001", "Gateway": "(null)" }, "Mounts": { diff --git a/test/core/test_api.h b/test/core/test_api.h index 638ca94..45c5868 100644 --- a/test/core/test_api.h +++ b/test/core/test_api.h @@ -98,12 +98,11 @@ int test_core_api_object_cat() { struct HashtableNode* node; size_t bytes_written; struct IpfsNode *local_node = NULL; - pthread_t api_pth = 0; ipfs_node_offline_new(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(&api_pth, local_node); + ipfs_node_free(local_node); ipfs_hashtable_node_free(node); libp2p_logger_debug("test_api", "*** Firing up daemons ***\n"); @@ -146,6 +145,8 @@ int test_core_api_name_resolve() { char* resolve_args[] = {"ipfs", "--config", ipfs_path2, "name", "resolve", peer_id1 }; struct CliArguments* args = NULL; + libp2p_logger_add_class("api"); + // build 2 repos... repo 1 if (!drop_build_open_repo(ipfs_path1, &fs_repo, config_file1)) { ipfs_repo_fsrepo_free(fs_repo); @@ -170,20 +171,19 @@ int test_core_api_name_resolve() { struct HashtableNode* node; size_t bytes_written; struct IpfsNode *local_node = NULL; - pthread_t api_pth = 0; ipfs_node_offline_new(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(&api_pth, local_node); + ipfs_node_free(local_node); ipfs_hashtable_node_free(node); libp2p_logger_debug("test_api", "*** Firing up daemons ***\n"); pthread_create(&daemon_thread1, NULL, test_daemon_start, (void*)ipfs_path1); thread_started1 = 1; + sleep(3); pthread_create(&daemon_thread2, NULL, test_daemon_start, (void*)ipfs_path2); thread_started2 = 1; - sleep(3); // publish name on server 1 diff --git a/test/core/test_node.h b/test/core/test_node.h index aa3d65e..6d8dba5 100644 --- a/test/core/test_node.h +++ b/test/core/test_node.h @@ -10,12 +10,11 @@ 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(&api_pth, repo_path, &local_node)) + if (!ipfs_node_online_new(repo_path, &local_node)) goto exit; // add a peer to the peerstore @@ -49,7 +48,7 @@ int test_node_peerstore() { if (peer != NULL) libp2p_peer_free(peer); if (local_node != NULL) - ipfs_node_free(&api_pth, local_node); + ipfs_node_free(local_node); return retVal; } diff --git a/test/core/test_null.h b/test/core/test_null.h index ac2c237..b160613 100644 --- a/test/core/test_null.h +++ b/test/core/test_null.h @@ -32,11 +32,10 @@ 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; - pthread_t api_pth = 0; - ipfs_node_online_new(&api_pth, ipfs_path, &local_node2); + ipfs_node_online_new(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(&api_pth, local_node2); + ipfs_node_free(local_node2); // start the daemon in a separate thread if (pthread_create(&thread2, NULL, test_daemon_start, (void*)ipfs_path) < 0) goto exit; @@ -50,7 +49,7 @@ int test_null_add_provider() { retVal = 1; exit: if (local_node2 != NULL) - ipfs_node_free(&api_pth, local_node2); + ipfs_node_free(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 d00daae..9129963 100644 --- a/test/exchange/test_bitswap.h +++ b/test/exchange/test_bitswap.h @@ -125,12 +125,11 @@ 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(&api_pth, ipfs_path, &localNode); + ipfs_node_online_new(ipfs_path, &localNode); // add a file localNode->routing->Bootstrap(localNode->routing); @@ -155,7 +154,7 @@ int test_bitswap_retrieve_file() ipfs_cid_free(cid); if (node != NULL) ipfs_hashtable_node_free(node); - ipfs_node_free(&api_pth, localNode); + ipfs_node_free(localNode); return retVal; } @@ -188,7 +187,6 @@ 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"); @@ -198,7 +196,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(&api_pth1, ipfs_path, &ipfs_node1); + ipfs_node_online_new(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) { @@ -217,7 +215,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(&api_pth2, ipfs_path, &ipfs_node2); + ipfs_node_online_new(ipfs_path, &ipfs_node2); ipfs_node2->routing->Bootstrap(ipfs_node2->routing); @@ -301,7 +299,6 @@ 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]; @@ -315,7 +312,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(&api_pth, ipfs_path, &ipfs_node2); + ipfs_node_online_new(ipfs_path, &ipfs_node2); if (!ipfs_cid_decode_hash_from_base58((unsigned char*)hello_world_hash, strlen(hello_world_hash), &cid)) goto exit; @@ -357,7 +354,7 @@ int test_bitswap_retrieve_file_known_remote() { // ipfs_block_free(result); if (cid != NULL) ipfs_cid_free(cid); - ipfs_node_free(&api_pth, ipfs_node2); + ipfs_node_free(ipfs_node2); return retVal; } @@ -393,7 +390,6 @@ 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"); @@ -422,11 +418,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(&api_pth1, ipfs_path, &ipfs_node2)) + if (!ipfs_node_online_new(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(&api_pth1, ipfs_node2); + ipfs_node_free(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"); @@ -444,7 +440,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(&api_pth2, ipfs_path, &ipfs_node3); + ipfs_node_online_new(ipfs_path, &ipfs_node3); ipfs_node3->routing->Bootstrap(ipfs_node3->routing); @@ -473,7 +469,7 @@ int test_bitswap_retrieve_file_third_party() { if (thread2_started) pthread_join(thread2, NULL); if (ipfs_node3 != NULL) - ipfs_node_free(&api_pth2, ipfs_node3); + ipfs_node_free(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 a6a00b8..3005785 100644 --- a/test/journal/test_journal.h +++ b/test/journal/test_journal.h @@ -119,10 +119,9 @@ int test_journal_server_1() { struct HashtableNode* node; size_t bytes_written; struct IpfsNode *local_node = NULL; - pthread_t api_pth = 0; ipfs_node_offline_new(ipfs_path, &local_node); ipfs_import_file(NULL, filename, &node, local_node, &bytes_written, 0); - ipfs_node_free(&api_pth, local_node); + ipfs_node_free(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_namesys.h b/test/namesys/test_namesys.h index cf2053d..a6cb880 100644 --- a/test/namesys/test_namesys.h +++ b/test/namesys/test_namesys.h @@ -27,7 +27,7 @@ int test_namesys_publisher_publish() { retVal = 1; exit: - ipfs_node_free(NULL, local_node); + ipfs_node_free(local_node); return retVal; } @@ -72,7 +72,7 @@ int test_namesys_resolver_resolve() { retVal = 1; exit: - ipfs_node_free(NULL, local_node); + ipfs_node_free(local_node); if (result != NULL) free(result); return retVal; diff --git a/test/node/test_importer.h b/test/node/test_importer.h index 6f2e0db..4b8e6a7 100644 --- a/test/node/test_importer.h +++ b/test/node/test_importer.h @@ -30,7 +30,6 @@ 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); @@ -42,7 +41,7 @@ int test_import_large_file() { goto exit; } - if (!ipfs_node_online_new(&api_pth, repo_dir, &local_node)) { + if (!ipfs_node_online_new(repo_dir, &local_node)) { fprintf(stderr, "Unable to create new IpfsNode\n"); goto exit; } @@ -130,7 +129,7 @@ int test_import_large_file() { exit: if (local_node != NULL) - ipfs_node_free(&api_pth, local_node); + ipfs_node_free(local_node); if (write_node != NULL) ipfs_hashtable_node_free(write_node); if (read_node != NULL) @@ -148,7 +147,6 @@ 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); @@ -163,7 +161,7 @@ int test_import_small_file() { 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(&api_pth, local_node); + ipfs_node_free(local_node); return 0; } @@ -174,7 +172,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(&api_pth, local_node); + ipfs_node_free(local_node); ipfs_hashtable_node_free(write_node); return 0; } @@ -183,7 +181,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(&api_pth, local_node); + ipfs_node_free(local_node); ipfs_hashtable_node_free(write_node); return 0; } @@ -191,7 +189,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(&api_pth, local_node); + ipfs_node_free(local_node); ipfs_hashtable_node_free(write_node); ipfs_hashtable_node_free(read_node); return 0; @@ -200,7 +198,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(&api_pth, local_node); + ipfs_node_free(local_node); ipfs_hashtable_node_free(write_node); ipfs_hashtable_node_free(read_node); return 0; @@ -220,7 +218,7 @@ int test_import_small_file() { fprintf(stderr, "Unable to find any records in the database.\n"); } - ipfs_node_free(&api_pth, local_node); + ipfs_node_free(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 277a28b..dbfe338 100644 --- a/test/routing/test_routing.h +++ b/test/routing/test_routing.h @@ -101,7 +101,6 @@ 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"); @@ -131,10 +130,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(&api_pth, ipfs_path, &local_node2); + ipfs_node_online_new(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(&api_pth, local_node2); + ipfs_node_free(local_node2); // start the daemon in a separate thread if (pthread_create(&thread2, NULL, test_daemon_start, (void*)ipfs_path) < 0) goto exit; @@ -225,7 +224,6 @@ 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); @@ -250,9 +248,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(&api_pth, ipfs_path, &local_node2); + ipfs_node_online_new(ipfs_path, &local_node2); ipfs_import_file(NULL, "/home/parallels/ipfstest/hello_world.txt", &node, local_node2, &bytes_written, 0); - ipfs_node_free(&api_pth, local_node2); + ipfs_node_free(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"); @@ -378,7 +376,6 @@ 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"); @@ -406,9 +403,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(&api_pth, ipfs_path, &local_node2); + ipfs_node_online_new(ipfs_path, &local_node2); ipfs_import_file(NULL, "/home/parallels/ipfstest/hello_world.txt", &node, local_node2, &bytes_written, 0); - ipfs_node_free(&api_pth, local_node2); + ipfs_node_free(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"); @@ -469,8 +466,6 @@ 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); @@ -500,11 +495,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(&api_pth1, ipfs_path, &ipfs_node2)) + if (!ipfs_node_online_new(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(&api_pth1, ipfs_node2); + ipfs_node_free(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) { @@ -525,7 +520,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(&api_pth2, ipfs_path, &ipfs_node3); + ipfs_node_online_new(ipfs_path, &ipfs_node3); ipfs_node3->routing->Bootstrap(ipfs_node3->routing); @@ -552,7 +547,7 @@ int test_routing_retrieve_file_third_party() { if (thread2_started) pthread_join(thread2, NULL); if (ipfs_node3 != NULL) - ipfs_node_free(&api_pth2, ipfs_node3); + ipfs_node_free(ipfs_node3); if (peer_id_1 != NULL) free(peer_id_1); if (peer_id_2 != NULL) @@ -634,12 +629,11 @@ 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; - pthread_t api_pth1 = 0; - if (!ipfs_node_online_new(&api_pth1, ipfs_path, &ipfs_node2)) + if (!ipfs_node_online_new(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(&api_pth1, ipfs_node2); + ipfs_node_free(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) { @@ -661,8 +655,7 @@ 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); - pthread_t api_pth2 = 0; - ipfs_node_online_new(&api_pth2, ipfs_path, &ipfs_node3); + ipfs_node_online_new(ipfs_path, &ipfs_node3); ipfs_node3->routing->Bootstrap(ipfs_node3->routing); @@ -687,7 +680,7 @@ int test_routing_retrieve_large_file() { if (thread2_started) pthread_join(thread2, NULL); if (ipfs_node3 != NULL) - ipfs_node_free(&api_pth2, ipfs_node3); + ipfs_node_free(ipfs_node3); if (peer_id_1 != NULL) free(peer_id_1); if (peer_id_2 != NULL)