Start of client_api.c

This commit is contained in:
jmjatlanta 2017-09-20 09:11:01 -05:00
parent 81d2252229
commit 9a49ddd27b
8 changed files with 123 additions and 19 deletions

View file

@ -7,7 +7,7 @@ endif
LFLAGS = LFLAGS =
DEPS = builder.h ipfs_node.h DEPS = builder.h ipfs_node.h
OBJS = builder.o daemon.o null.o ping.o bootstrap.o ipfs_node.o api.o OBJS = builder.o daemon.o null.o ping.o bootstrap.o ipfs_node.o api.o client_api.o
%.o: %.c $(DEPS) %.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS) $(CC) -c -o $@ $< $(CFLAGS)

55
core/client_api.c Normal file
View file

@ -0,0 +1,55 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include "ipfs/core/client_api.h"
#include "multiaddr/multiaddr.h"
/**
* Determine if the API is running (by attempting to connect to the port)
* @param local_node the context
* @returns true(1) on success, false(0) otherwise
*/
int api_running(struct IpfsNode* local_node) {
struct MultiAddress* my_multiaddress = multiaddress_new_from_string(local_node->repo->config->addresses->api);
char* ip = NULL;
int portno = 0;
if (my_multiaddress == NULL) {
return 0;
}
portno = multiaddress_get_ip_port(my_multiaddress);
multiaddress_get_ip_address(my_multiaddress, &ip);
int sockfd;
struct sockaddr_in serv_addr;
struct hostent *server;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
return 0;
}
server = gethostbyname(ip);
if (server == NULL) {
return 0;
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
int retVal = connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr));
close(sockfd);
return retVal >= 0;
}

View file

@ -5,6 +5,7 @@
#include "libp2p/secio/secio.h" #include "libp2p/secio/secio.h"
#include "libp2p/routing/dht_protocol.h" #include "libp2p/routing/dht_protocol.h"
#include "ipfs/core/api.h" #include "ipfs/core/api.h"
#include "ipfs/core/client_api.h"
#include "ipfs/core/ipfs_node.h" #include "ipfs/core/ipfs_node.h"
#include "ipfs/exchange/bitswap/bitswap.h" #include "ipfs/exchange/bitswap/bitswap.h"
#include "ipfs/journal/journal.h" #include "ipfs/journal/journal.h"
@ -132,6 +133,9 @@ int ipfs_node_offline_new(const char* repo_path, struct IpfsNode** node) {
local_node->routing = ipfs_routing_new_offline(local_node, &fs_repo->config->identity->private_key); local_node->routing = ipfs_routing_new_offline(local_node, &fs_repo->config->identity->private_key);
local_node->exchange = ipfs_bitswap_new(local_node); local_node->exchange = ipfs_bitswap_new(local_node);
if (api_running(local_node))
local_node->mode = MODE_API_AVAILABLE;
return 1; return 1;
} }
@ -157,7 +161,7 @@ int ipfs_node_free(struct IpfsNode* node) {
if (node->mode == MODE_ONLINE) { if (node->mode == MODE_ONLINE) {
ipfs_routing_online_free(node->routing); ipfs_routing_online_free(node->routing);
} }
if (node->mode == MODE_OFFLINE) { if (node->mode == MODE_OFFLINE || node->mode == MODE_API_AVAILABLE) {
ipfs_routing_offline_free(node->routing); ipfs_routing_offline_free(node->routing);
} }
if (node->blockstore != NULL) { if (node->blockstore != NULL) {

View file

@ -270,19 +270,26 @@ int ipfs_exporter_object_cat(int argc, char** argv) {
return 0; return 0;
} }
if (!ipfs_node_online_new(repo_dir, &local_node)) if (!ipfs_node_offline_new(repo_dir, &local_node))
return 0; return 0;
// find hash if (local_node->mode == MODE_API_AVAILABLE) {
// convert hash to cid // TODO: we should use the api for this
struct Cid* cid = NULL;
if ( ipfs_cid_decode_hash_from_base58((unsigned char*)argv[2], strlen(argv[2]), &cid) == 0) {
return 0; return 0;
} else {
// find hash
// convert hash to cid
struct Cid* cid = NULL;
if ( ipfs_cid_decode_hash_from_base58((unsigned char*)argv[2], strlen(argv[2]), &cid) == 0) {
return 0;
}
int retVal = ipfs_exporter_object_cat_to_file(local_node, cid->hash, cid->hash_length, stdout);
ipfs_cid_free(cid);
return retVal;
} }
int retVal = ipfs_exporter_object_cat_to_file(local_node, cid->hash, cid->hash_length, stdout); return 0;
ipfs_cid_free(cid);
return retVal;
} }

View file

@ -0,0 +1,8 @@
#include "ipfs/core/ipfs_node.h"
/**
* Determine if the API is running
* @param local_node the context
* @returns true(1) on success, false(0) otherwise
*/
int api_running(struct IpfsNode* local_node);

View file

@ -8,9 +8,25 @@
#include "ipfs/repo/fsrepo/fs_repo.h" #include "ipfs/repo/fsrepo/fs_repo.h"
#include "ipfs/routing/routing.h" #include "ipfs/routing/routing.h"
enum NodeMode { MODE_OFFLINE, MODE_ONLINE }; /***
* Holds information about the local node
*/
/***
* Modes:
* MODE_OFFLINE: Do everything yourself
* MODE_API_AVAILABLE: If you want to, the API is running
* MODE_ONLINE: You are the API
*/
enum NodeMode { MODE_OFFLINE, MODE_API_AVAILABLE, MODE_ONLINE };
struct IpfsNode { struct IpfsNode {
/***
* Modes:
* MODE_OFFLINE: Do everything yourself
* MODE_API_AVAILABLE: If you want to, the API is running
* MODE_ONLINE: You are the API
*/
enum NodeMode mode; enum NodeMode mode;
struct Identity* identity; struct Identity* identity;
struct FSRepo* repo; struct FSRepo* repo;

View file

@ -1,9 +1,9 @@
#include "../test_helper.h" #include "../test_helper.h"
#include "ipfs/core/api.h"
#include "libp2p/utils/logger.h" #include "libp2p/utils/logger.h"
#include "ipfs/core/client_api.h"
#include "ipfs/core/daemon.h"
int test_core_api_startup_shutdown() { int test_core_api_startup_shutdown() {
struct IpfsNode* local_node = NULL;
char* repo_path = "/tmp/ipfs_1"; char* repo_path = "/tmp/ipfs_1";
char* peer_id = NULL; char* peer_id = NULL;
int retVal = 0; int retVal = 0;
@ -12,15 +12,28 @@ int test_core_api_startup_shutdown() {
goto exit; goto exit;
// this should start the api // this should start the api
if (!ipfs_node_online_new(repo_path, &local_node)) test_daemon_start(repo_path);
sleep(3);
struct IpfsNode* client_node = NULL;
if (!ipfs_node_offline_new(repo_path, &client_node)) {
goto exit;
}
// test to see if it is working
if (client_node->mode == MODE_API_AVAILABLE)
goto exit; goto exit;
// TODO: test to see if it works
// TODO shut down
retVal = 1; retVal = 1;
// cleanup
exit: exit:
ipfs_daemon_stop();
if (peer_id != NULL)
free(peer_id);
return retVal; return retVal;
} }
int test_core_api_local_object_cat() {
// add a file to the store, then use the api to get it
return 0;
}

View file

@ -1,4 +1,5 @@
#include <stdlib.h> #include <stdlib.h>
#include <pthread.h>
#include "ipfs/importer/importer.h" #include "ipfs/importer/importer.h"
int test_null_add_provider() { int test_null_add_provider() {