setting up when the remote requests the yamux protocol

This commit is contained in:
John Jones 2017-11-06 17:31:30 -05:00
parent 18b0139b81
commit f4860d3ed4
5 changed files with 56 additions and 7 deletions

View file

@ -83,8 +83,11 @@ int libp2p_identify_receive_protocol(struct IdentifyContext* context) {
int libp2p_identify_handle_message(const struct StreamMessage* msg, struct SessionContext* context, void* protocol_context) { int libp2p_identify_handle_message(const struct StreamMessage* msg, struct SessionContext* context, void* protocol_context) {
if (protocol_context == NULL) if (protocol_context == NULL)
return -1; return -1;
//struct IdentifyContext* ctx = (struct IdentifyContext*) protocol_context; // attempt to create a new Identify connection with them.
// TODO: Do something with the incoming msg // send the protocol id back, and set up the channel
struct IdentifyContext* ctx = (struct IdentifyContext*)protocol_context;
libp2p_identify_send_protocol(ctx);
//TODO: now add this "channel"
return 1; return 1;
} }

View file

@ -42,4 +42,11 @@ struct Libp2pProtocolHandler {
*/ */
struct Libp2pProtocolHandler* libp2p_protocol_handler_new(); struct Libp2pProtocolHandler* libp2p_protocol_handler_new();
int libp2p_protocol_marshal(struct StreamMessage* msg, struct SessionContext* context, struct Libp2pVector* protocol_handlers); /***
* Handle an incoming message
* @param message the incoming message
* @param session the SessionContext of the incoming connection
* @param handlers a Vector of protocol handlers
* @returns -1 on error, 0 if everything was okay, but the daemon should no longer handle this connection, 1 on success
*/
int libp2p_protocol_marshal(struct StreamMessage* message, struct SessionContext* context, struct Libp2pVector* protocol_handlers);

View file

@ -41,8 +41,7 @@ struct Libp2pProtocolHandler* libp2p_protocol_handler_new() {
/*** /***
* Handle an incoming message * Handle an incoming message
* @param incoming the incoming data * @param message the incoming message
* @param incoming_size the size of the incoming data buffer
* @param session the SessionContext of the incoming connection * @param session the SessionContext of the incoming connection
* @param handlers a Vector of protocol handlers * @param handlers a Vector of protocol handlers
* @returns -1 on error, 0 if everything was okay, but the daemon should no longer handle this connection, 1 on success * @returns -1 on error, 0 if everything was okay, but the daemon should no longer handle this connection, 1 on success
@ -65,6 +64,5 @@ int libp2p_protocol_marshal(struct StreamMessage* msg, struct SessionContext* se
return -1; return -1;
} }
//TODO: strip off the protocol?
return handler->HandleMessage(msg, session, handler->context); return handler->HandleMessage(msg, session, handler->context);
} }

View file

@ -2,6 +2,7 @@
#include "libp2p/yamux/yamux.h" #include "libp2p/yamux/yamux.h"
#include "libp2p/identify/identify.h" #include "libp2p/identify/identify.h"
#include "mock_stream.h" #include "mock_stream.h"
#include "libp2p/utils/logger.h"
/*** /***
* Helpers * Helpers
@ -68,7 +69,7 @@ int test_yamux_identify() {
struct Stream* yamux_stream = libp2p_yamux_stream_new(mock_stream); struct Stream* yamux_stream = libp2p_yamux_stream_new(mock_stream);
if (yamux_stream == NULL) if (yamux_stream == NULL)
goto exit; goto exit;
// TODO: Now add in another protocol // Now add in another protocol
mock_stream->read = mock_identify_read_protocol; mock_stream->read = mock_identify_read_protocol;
if (!libp2p_yamux_stream_add(yamux_stream->stream_context, libp2p_identify_stream_new(libp2p_yamux_channel_new(yamux_stream)))) { if (!libp2p_yamux_stream_add(yamux_stream->stream_context, libp2p_identify_stream_new(libp2p_yamux_channel_new(yamux_stream)))) {
goto exit; goto exit;
@ -81,3 +82,42 @@ int test_yamux_identify() {
mock_stream->close(mock_stream->stream_context); mock_stream->close(mock_stream->stream_context);
return retVal; return retVal;
} }
int test_yamux_incoming_protocol_request() {
int retVal = 0;
// setup
struct Stream* mock_stream = mock_stream_new();
mock_stream->read = mock_yamux_read_protocol;
struct Stream* yamux_stream = libp2p_yamux_stream_new(mock_stream);
if (yamux_stream == NULL)
goto exit;
// build the protocol handler that can handle identify protocol
struct Libp2pVector* protocol_handlers = libp2p_utils_vector_new(1);
struct Libp2pProtocolHandler* handler = libp2p_identify_build_protocol_handler(protocol_handlers);
libp2p_utils_vector_add(protocol_handlers, handler);
struct SessionContext session_context;
session_context.default_stream = yamux_stream;
// Someone is requesting the identity protocol
mock_stream->read = mock_identify_read_protocol;
struct StreamMessage* result_message;
if (!yamux_stream->read(yamux_stream->stream_context, &result_message, 10)) {
libp2p_logger_error("test_yamux", "Unable to read identify protocol.\n");
goto exit;
}
// handle the marshaling of the protocol
libp2p_protocol_marshal(result_message, &session_context, protocol_handlers);
// now verify the results
struct YamuxContext* yamux_context = (struct YamuxContext*)yamux_stream->stream_context;
if (yamux_context->channels->total != 1) {
libp2p_logger_error("test_yamux", "Identify protocol was not found.\n");
goto exit;
}
// tear down
retVal = 1;
exit:
if (yamux_stream != NULL)
yamux_stream->close(yamux_stream->stream_context);
mock_stream->close(mock_stream->stream_context);
return retVal;
}

View file

@ -116,6 +116,7 @@ int build_test_collection() {
add_test("test_aes", test_aes, 1); add_test("test_aes", test_aes, 1);
add_test("test_yamux_stream_new", test_yamux_stream_new, 1); add_test("test_yamux_stream_new", test_yamux_stream_new, 1);
add_test("test_yamux_identify", test_yamux_identify, 1); add_test("test_yamux_identify", test_yamux_identify, 1);
add_test("test_yamux_incoming_protocol_request", test_yamux_incoming_protocol_request, 1);
return 1; return 1;
}; };