summaryrefslogtreecommitdiff
path: root/testing/experiment/group_chats_test.c
blob: 900da79cec8ade67d824123e62e06d4e21b09c38 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "../../toxcore/group_chats.h"
#define NUM_CHATS 8

#ifdef WIN32
#define c_sleep(x) Sleep(1*x)
#else
#define c_sleep(x) usleep(1000*x)
#endif
Group_Chat *chats[NUM_CHATS];

void print_close(Group_Close *close)
{
    uint32_t i, j;
    IP_Port p_ip;
    printf("___________________CLOSE________________________________\n");

    for (i = 0; i < GROUP_CLOSE_CONNECTIONS; i++) {
        printf("ClientID: ");

        for (j = 0; j < CLIENT_ID_SIZE; j++) {
            printf("%02hhX", close[i].client_id[j]);
        }

        p_ip = close[i].ip_port;
        printf("\nIP: %u.%u.%u.%u Port: %u", p_ip.ip.uint8[0], p_ip.ip.uint8[1], p_ip.ip.uint8[2], p_ip.ip.uint8[3],
               ntohs(p_ip.port));
        printf("\nTimestamp: %llu", (long long unsigned int) close[i].last_recv);
        printf("\n");
    }
}

void print_group(Group_Chat *chat)
{
    uint32_t i, j;
    printf("-----------------\nClientID: ");

    for (j = 0; j < CLIENT_ID_SIZE; j++) {
        printf("%02hhX", chat->self_public_key[j]);
    }

    printf("\n___________________GROUP________________________________\n");

    for (i = 0; i < chat->numpeers; i++) {
        printf("ClientID: ");

        for (j = 0; j < CLIENT_ID_SIZE; j++) {
            printf("%02hhX", chat->group[i].client_id[j]);
        }

        printf("\nTimestamp: %llu", (long long unsigned int) chat->group[i].last_recv);
        printf("\nlast_pinged: %llu", (long long unsigned int) chat->group[i].last_pinged);
        printf("\npingid: %llu", (long long unsigned int) chat->group[i].pingid);
        printf("\n");
    }
}

void print_message(Group_Chat *chat, int peer_number, uint8_t *message, uint16_t length, void *userdata)
{
    printf("%u: %s | %u\n", peer_number, message, length);
}

int main()
{
    IP ip;
    ip.uint32 = 0;
    uint32_t i;


    for (i = 0; i < NUM_CHATS; ++i) {
        chats[i] = new_groupchat(new_networking(ip, 12745));

        if (chats[i] == 0)
            exit(1);

        networking_registerhandler(chats[i]->net, 48, &handle_groupchatpacket, chats[i]);
        callback_groupmessage(chats[i], &print_message, 0);
    }

    printf("ok\n");
    IP_Port ip_port;
    ip_port.ip.uint32 = 0;
    ip_port.ip.uint8[0] = 127;
    ip_port.ip.uint8[3] = 1;
    ip_port.port = htons(12745);

    for (i = 0; i < NUM_CHATS; ++i) {
        group_newpeer(chats[0], chats[i]->self_public_key);
        chat_bootstrap(chats[i], ip_port, chats[0]->self_public_key);
        printf("%u\n", i);
    }

    while (1) {
        for (i = 0; i < NUM_CHATS; ++i) {
            networking_poll(chats[i]->net);
            do_groupchat(chats[i]);
            printf("%u\n", chats[i]->numpeers);
            print_close(chats[i]->close);
            print_group(chats[i]);
        }

        c_sleep(100);
    }

    return 0;
}