diff --git a/examples/chat.capi.cpp b/examples/chat.capi.cpp index 90eef16..11f0705 100644 --- a/examples/chat.capi.cpp +++ b/examples/chat.capi.cpp @@ -73,7 +73,7 @@ int main(int argc, char* argv[]) { p2p_set_peer_connected_callback(p2p_initial_network(), peerJoined); // NOTE: these callbacks will only be called for peers directly connected... if you need to know about all peers in the network that will need to be done at a higher level! p2p_set_peer_disconnected_callback(p2p_initial_network(), peerLeft); // NOTE: these callbacks will only be called for peers directly connected... if you need to know about all peers in the network that will need to be done at a higher level! - auto network = p2p_initialize(p2p_initialize_args_from_strings("/ip4/0.0.0.0/udp/0/quic-v1", "simplep2p/examples/chat/capi/v1.0.0", key, 60, false)); + auto network = p2p_initialize(p2p_initialize_args_from_strings("/ip4/0.0.0.0/udp/0/quic-v1", "simplep2p/examples/chat/capi/v1.0.0", key, 60, false, false)); p2p_set_message_callback(network, print); p2p_set_connected_callback(network, connected); diff --git a/src/libp2p.go b/src/libp2p.go index 78613fa..e7cae2b 100755 --- a/src/libp2p.go +++ b/src/libp2p.go @@ -176,7 +176,7 @@ func generateKey() []byte { // initialize starts up a connection to the p2p network and initializes some library states // //export initialize -func initialize(listenAddress string, discoveryTopic string, keyString string, connectionTimeout float64, verbose bool) int { +func initialize(listenAddress string, discoveryTopic string, keyString string, connectionTimeout float64, fullyConnected bool, verbose bool) int { var nid = len(states) var localState State localState.verbose = verbose @@ -208,11 +208,20 @@ func initialize(listenAddress string, discoveryTopic string, keyString string, c go discoverPeers(nid, states[nid].ctx, states[nid].host, discoveryTopic) - ps, err := pubsub.NewGossipSub(localState.ctx, localState.host) - if err != nil { - panic(err) + if fullyConnected { + ps, err := pubsub.NewFloodSub(localState.ctx, localState.host) + if err != nil { + panic(err) + } + localState.ps = ps + } else { + ps, err := pubsub.NewGossipSub(localState.ctx, localState.host) + if err != nil { + panic(err) + } + localState.ps = ps } - localState.ps = ps + localState.topics = make(map[int]Topic) states[nid] = localState diff --git a/src/simplep2p.c b/src/simplep2p.c index c1e356c..dba2940 100644 --- a/src/simplep2p.c +++ b/src/simplep2p.c @@ -261,6 +261,7 @@ P2PInitializationArguments p2p_default_initialize_args() { out.discoveryTopicSize = strlen(out.discoveryTopic); out.identity = p2p_null_key(); out.connectionTimeout = 60 /*seconds*/; + out.fullyConnected = false; out.verbose = false; return out; } @@ -277,7 +278,7 @@ P2PInitializationArguments p2p_default_initialize_args() { * @param verbose The verbose flag. * @return The initialization arguments. */ -P2PInitializationArguments p2p_initialize_args_from_strings(const char* listenAddress, const char* discoveryTopic, P2PKey identity, double connectionTimeout, bool verbose) { +P2PInitializationArguments p2p_initialize_args_from_strings(const char* listenAddress, const char* discoveryTopic, P2PKey identity, double connectionTimeout, bool fullyConnected, bool verbose) { P2PInitializationArguments out; out.listenAddress = listenAddress; out.listenAddressSize = strlen(out.listenAddress); @@ -285,6 +286,7 @@ P2PInitializationArguments p2p_initialize_args_from_strings(const char* listenAd out.discoveryTopicSize = strlen(out.discoveryTopic); out.identity = identity; out.connectionTimeout = connectionTimeout; + out.fullyConnected = fullyConnected; out.verbose = verbose; return out; } @@ -307,7 +309,7 @@ P2PTopic p2p_initialize(P2PInitializationArguments args) { GoString key; key.p = args.identity.data; key.n = args.identity.size; - return initialize(listenAddress, discoveryTopic, key, args.connectionTimeout, args.verbose); + return initialize(listenAddress, discoveryTopic, key, args.connectionTimeout, args.fullyConnected, args.verbose); } /** diff --git a/src/simplep2p.h b/src/simplep2p.h index 4806337..5dee545 100644 --- a/src/simplep2p.h +++ b/src/simplep2p.h @@ -150,6 +150,7 @@ typedef struct { long long discoveryTopicSize; ///< The size of the discovery topic. P2PKey identity; ///< The P2P key identity. double connectionTimeout; ///< The time in seconds to try connecting before giving up + bool fullyConnected; ///< Weather every message should be sent to every peer, or if the network should be more intelligent bool verbose; ///< The verbose flag. } P2PInitializationArguments; @@ -174,7 +175,7 @@ P2PInitializationArguments p2p_default_initialize_args(); * @param verbose The verbose flag. * @return The initialization arguments. */ -P2PInitializationArguments p2p_initialize_args_from_strings(const char* listenAddress, const char* discoveryTopic, P2PKey identity, double connectionTimeout, bool verbose); +P2PInitializationArguments p2p_initialize_args_from_strings(const char* listenAddress, const char* discoveryTopic, P2PKey identity, double connectionTimeout, bool verbose, bool fullyConnected); /** * @brief Initializes P2P network with the provided arguments. diff --git a/src/simplep2p.hpp b/src/simplep2p.hpp index 92832eb..2fa6957 100644 --- a/src/simplep2p.hpp +++ b/src/simplep2p.hpp @@ -239,6 +239,7 @@ namespace p2p { * @param identityKey The identity key for network initialization. * @param do_on_connected Callback function to register in on_connected before initializing the connection * @param connectionTimeout The time to wait for a connection before giving up. + * @param fullyConnected Weather or not every message should be sent to every peer, or if the network should be more intelligent. * @param verbose Flag indicating if the GO library should spew some more verbose messages. */ Network( @@ -247,12 +248,13 @@ namespace p2p { const Key& identityKey = {}, delegate_function do_on_connected = nullptr, std::chrono::milliseconds connectionTimeout = std::chrono::seconds(60), + bool fullyConnected = false, bool verbose = false ) { if(do_on_connected != nullptr) on_connected = do_on_connected; - initialize(listenAddress, discoveryTopic, identityKey, connectionTimeout, verbose); + initialize(listenAddress, discoveryTopic, identityKey, connectionTimeout, fullyConnected, verbose); } /** @@ -290,6 +292,7 @@ namespace p2p { * @param discoveryTopic The discovery topic for network initialization. * @param identityKey The identity key for network initialization. * @param connectionTimeout The time to wait for a connection before giving up. + * @param fullyConnected Weather or not every message should be sent to every peer, or if the network should be more intelligent. * @param verbose Flag indicating if the GO library should spew some more verbose messages. */ void initialize( @@ -297,6 +300,7 @@ namespace p2p { std::string_view discoveryTopic = default_discovery_topic, const Key& identityKey = {}, std::chrono::milliseconds connectionTimeout = std::chrono::seconds(60), + bool fullyConnected = false, bool verbose = false ) { // Connect the connect delegate to its callback @@ -310,6 +314,7 @@ namespace p2p { .discoveryTopicSize = (long long)discoveryTopic.size(), .identity = identityKey, .connectionTimeout = std::chrono::duration_cast>(connectionTimeout).count(), + .fullyConnected = fullyConnected, .verbose = verbose });