aboutsummaryrefslogtreecommitdiff
path: root/tests/main.cpp
blob: 2d1d5055042102df932a2bee01c6b6c6509e6779 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "../include/sibs/BootstrapConnection.hpp"
#include "../include/sibs/BootstrapNode.hpp"
#include <thread>
#include <stdio.h>
#include <string.h>

const int PORT = 35231;

#define REQUIRE(expr) do { if(!(expr)) { fprintf(stderr, "Assert failed: %s\n", #expr); exit(1); } }while(0)

int main()
{
    const sibs::PubsubKey key("abcdefghijklmnopqrstuvxyz0123456789", 35);
    sibs::BootstrapNode boostrapNode(sibs::Ipv4(nullptr, PORT));

    sibs::BootstrapConnection connection1(sibs::Ipv4("127.0.0.1", PORT));
    bool gotData1 = false;
    bool gotAsdf1 = false;
    connection1.listen(key, [&gotData1, &gotAsdf1](const sibs::DirectConnectionPeer *peer, const void *data, const sibs::usize size)
    {
        if(size == 5 && strncmp((const char*)data, "hello", 5) == 0)
            gotData1 = true;
        if(size == 4 && strncmp((const char*)data, "asdf", 4) == 0)
            gotAsdf1 = true;
        return true;
    });

    sibs::BootstrapConnection connection2(sibs::Ipv4("127.0.0.1", PORT));
    bool gotData2 = false;
    bool gotAsdf2 = false;
    connection2.listen(key, [&gotData2, &gotAsdf2](const sibs::DirectConnectionPeer *peer, const void *data, const sibs::usize size)
    {
        if(size == 5 && strncmp((const char*)data, "hello", 5) == 0)
            gotData2 = true;
        if(size == 4 && strncmp((const char*)data, "asdf", 4) == 0)
            gotAsdf2 = true;
        return true;
    });
    // wait until connection1 and connection2 receive each other as peers from bootstrap node
    std::this_thread::sleep_for(std::chrono::seconds(3));

    connection1.put(key, "hello", 5);
    connection1.put(key, "asdf", 4);
    std::this_thread::sleep_for(std::chrono::seconds(3));

    REQUIRE(gotData1);
    REQUIRE(gotAsdf1);
    REQUIRE(gotData2);
    REQUIRE(gotAsdf2);

    return 0;
}