more ping cleanup and friendly interface to streams

This commit is contained in:
John Jones 2017-02-23 15:15:33 -05:00
parent ae48e058dd
commit 7a6b138444
7 changed files with 198 additions and 83 deletions

View file

@ -8,43 +8,67 @@
#include "libp2p/record/message.h"
#include "libp2p/net/multistream.h"
#include "ipfs/core/daemon.h"
#include "ipfs/routing/routing.h"
#include "ipfs/core/ipfs_node.h"
#define BUF_SIZE 4096
/**
* We've received a connection. Find out what they want
*/
void *ipfs_null_connection (void *ptr)
{
struct null_connection_params *connection_param;
//char b[BUF_SIZE];
//int len;
struct null_connection_params *connection_param = NULL;
struct s_ipfs_routing* routing = NULL;
struct Stream* stream = NULL;
connection_param = (struct null_connection_params*) ptr;
// TODO: multistream + secio + message.
// TODO: when should we exit the for loop and disconnect?
stream = libp2p_net_multistream_stream_new(connection_param->socket);
fprintf(stderr, "Connection %d, count %d\n", connection_param->socket, *(connection_param->count));
for(;;) {
if (libp2p_net_multistream_negotiate(connection_param->socket)) {
// we negotiated, now find out what they want
libp2p_net_multistream_handle_message(connection_param->socket);
} else {
break;
}
/*
len = socket_read(connection_param->socket, b, sizeof(b)-1, 0);
if (len > 0) {
while (b[len-1] == '\r' || b[len-1] == '\n') len--;
b[len] = '\0';
fprintf(stderr, "Recv: '%s'\n", b);
if (strcmp (b, "ping") == 0) {
socket_write(connection_param->socket, "pong", 4, 0);
}
} else if(len < 0) {
break;
}
*/
}
if (libp2p_net_multistream_negotiate(stream)) {
routing = ipfs_routing_new_online(connection_param->local_node->repo, &connection_param->local_node->identity->private_key, stream);
close (connection_param->socket); // close socket.
for(;;) {
struct Libp2pMessage* msg = libp2p_net_multistream_get_message(stream);
if (msg != NULL) {
switch(msg->message_type) {
case (MESSAGE_TYPE_PING):
routing->Ping(routing, msg);
break;
case (MESSAGE_TYPE_GET_VALUE):
routing->GetValue(routing, msg->key, msg->key_size, NULL, NULL);
break;
default:
break;
}
} else {
break;
}
}
}
/*
len = socket_read(connection_param->socket, b, sizeof(b)-1, 0);
if (len > 0) {
while (b[len-1] == '\r' || b[len-1] == '\n') len--;
b[len] = '\0';
fprintf(stderr, "Recv: '%s'\n", b);
if (strcmp (b, "ping") == 0) {
socket_write(connection_param->socket, "pong", 4, 0);
}
} else if(len < 0) {
break;
}
*/
if (stream != NULL) {
stream->close(stream);
}
(*(connection_param->count))--; // update counter.
free (connection_param);
return (void*) 1;

View file

@ -12,7 +12,7 @@
int ipfs_ping (int argc, char **argv)
{
char* results = NULL;
unsigned char* results = NULL;
size_t results_size = 0;
//TODO: handle multiaddress
@ -20,8 +20,8 @@ int ipfs_ping (int argc, char **argv)
//TODO: Error checking
char* ip = argv[2];
int port = atoi(argv[3]);
int socket_fd = libp2p_net_multistream_connect(ip, port);
if (socket_fd < 0) {
struct Stream* stream = libp2p_net_multistream_connect(ip, port);
if (stream == NULL) {
fprintf(stderr, "Unable to connect to %s on port %s", ip, argv[3]);
}
@ -32,8 +32,8 @@ int ipfs_ping (int argc, char **argv)
size_t protobuf_size = libp2p_message_protobuf_encode_size(msg);
unsigned char protobuf[protobuf_size];
libp2p_message_protobuf_encode(msg, &protobuf[0], protobuf_size, &protobuf_size);
libp2p_net_multistream_send(socket_fd, protobuf, protobuf_size);
libp2p_net_multistream_receive(socket_fd, &results, &results_size);
libp2p_net_multistream_write(stream, protobuf, protobuf_size);
libp2p_net_multistream_read(stream, &results, &results_size);
if (results_size != protobuf_size) {
fprintf(stderr, "PING unsuccessful. Original size: %lu, returned size: %lu\n", protobuf_size, results_size);