diff options
author | irungentoo <irungentoo@gmail.com> | 2013-07-08 20:50:25 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-07-08 20:50:25 -0400 |
commit | db37eca44bca126c08dff4353c9d1dab824f8030 (patch) | |
tree | 00d7ce1a9de318bb185380adc259f4b7c2ac30a0 /testing | |
parent | 3df906c2db3cebf38eca974f58050d6066250b10 (diff) |
Some work done on the messenger part.
Diffstat (limited to 'testing')
-rw-r--r-- | testing/Messenger_test.c | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/testing/Messenger_test.c b/testing/Messenger_test.c new file mode 100644 index 00000000..4290d0fa --- /dev/null +++ b/testing/Messenger_test.c | |||
@@ -0,0 +1,92 @@ | |||
1 | |||
2 | |||
3 | #include "../core/Messenger.h" | ||
4 | |||
5 | #ifdef WIN32 | ||
6 | |||
7 | #define c_sleep(x) Sleep(1*x) | ||
8 | |||
9 | #else | ||
10 | #include <unistd.h> | ||
11 | #include <arpa/inet.h> | ||
12 | #define c_sleep(x) usleep(1000*x) | ||
13 | |||
14 | #endif | ||
15 | |||
16 | //horrible function from one of my first C programs. | ||
17 | //only here because I was too lazy to write a proper one. | ||
18 | unsigned char * hex_string_to_bin(char hex_string[]) | ||
19 | { | ||
20 | unsigned char * val = malloc(strlen(hex_string)); | ||
21 | char * pos = hex_string; | ||
22 | int i=0; | ||
23 | while(i < strlen(hex_string)) | ||
24 | { | ||
25 | sscanf(pos,"%2hhx",&val[i]); | ||
26 | pos+=2; | ||
27 | i++; | ||
28 | } | ||
29 | return val; | ||
30 | } | ||
31 | |||
32 | void print_request(uint8_t * public_key, uint8_t * data, uint16_t length) | ||
33 | { | ||
34 | printf("Friend request recieved from: \n"); | ||
35 | printf("ClientID: "); | ||
36 | uint32_t j; | ||
37 | for(j = 0; j < 32; j++) | ||
38 | { | ||
39 | if(public_key[j] < 16) | ||
40 | printf("0"); | ||
41 | printf("%hhX", public_key[j]); | ||
42 | } | ||
43 | printf("\nOf length: %u with data: %s \n", length, data); | ||
44 | |||
45 | } | ||
46 | |||
47 | void print_message(int friendnumber, uint8_t * string, uint16_t length) | ||
48 | { | ||
49 | printf("Message with length %u recieved from %u: %s \n", length, friendnumber, string); | ||
50 | |||
51 | } | ||
52 | |||
53 | int main(int argc, char *argv[]) | ||
54 | { | ||
55 | if (argc < 3) { | ||
56 | printf("usage %s ip port (of the DHT bootstrap node)\n", argv[0]); | ||
57 | exit(0); | ||
58 | } | ||
59 | initMessenger(); | ||
60 | m_callback_friendrequest(print_request); | ||
61 | m_callback_friendmessage(print_message); | ||
62 | |||
63 | m_setinfo("Install Gentoo", sizeof("Install Gentoo"));//The message we send is a message of peace | ||
64 | |||
65 | printf("OUR ID: "); | ||
66 | uint32_t i; | ||
67 | for(i = 0; i < 32; i++) | ||
68 | { | ||
69 | if(self_public_key[i] < 16) | ||
70 | printf("0"); | ||
71 | printf("%hhX",self_public_key[i]); | ||
72 | } | ||
73 | |||
74 | char temp_id[128]; | ||
75 | printf("\nEnter the client_id of the friend you wish to add (32 bytes HEX format):\n"); | ||
76 | scanf("%s", temp_id); | ||
77 | int num = m_addfriend(hex_string_to_bin(temp_id)); | ||
78 | |||
79 | perror("Initialization"); | ||
80 | IP_Port bootstrap_ip_port; | ||
81 | bootstrap_ip_port.port = htons(atoi(argv[2])); | ||
82 | bootstrap_ip_port.ip.i = inet_addr(argv[1]); | ||
83 | DHT_bootstrap(bootstrap_ip_port); | ||
84 | |||
85 | while(1) | ||
86 | { | ||
87 | m_sendmessage(num, "Test", 5); | ||
88 | doMessenger(); | ||
89 | c_sleep(1); | ||
90 | } | ||
91 | |||
92 | } | ||