diff options
Diffstat (limited to 'testing/DHT_sendfiletest.c')
-rw-r--r-- | testing/DHT_sendfiletest.c | 78 |
1 files changed, 41 insertions, 37 deletions
diff --git a/testing/DHT_sendfiletest.c b/testing/DHT_sendfiletest.c index 52ee7400..5b5d6c9f 100644 --- a/testing/DHT_sendfiletest.c +++ b/testing/DHT_sendfiletest.c | |||
@@ -14,7 +14,26 @@ | |||
14 | * Saves all received data to: received.txt | 14 | * Saves all received data to: received.txt |
15 | * | 15 | * |
16 | * EX: ./test 127.0.0.1 33445 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef filename.txt ABCDEFGHIJKLMNOPQRSTUVWXYZabcdeg | 16 | * EX: ./test 127.0.0.1 33445 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef filename.txt ABCDEFGHIJKLMNOPQRSTUVWXYZabcdeg |
17 | * | ||
18 | * Copyright (C) 2013 Tox project All Rights Reserved. | ||
19 | * | ||
20 | * This file is part of Tox. | ||
21 | * | ||
22 | * Tox is free software: you can redistribute it and/or modify | ||
23 | * it under the terms of the GNU General Public License as published by | ||
24 | * the Free Software Foundation, either version 3 of the License, or | ||
25 | * (at your option) any later version. | ||
26 | * | ||
27 | * Tox is distributed in the hope that it will be useful, | ||
28 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
29 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
30 | * GNU General Public License for more details. | ||
31 | * | ||
32 | * You should have received a copy of the GNU General Public License | ||
33 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
34 | * | ||
17 | */ | 35 | */ |
36 | |||
18 | #include "../core/network.h" | 37 | #include "../core/network.h" |
19 | #include "../core/DHT.h" | 38 | #include "../core/DHT.h" |
20 | #include "../core/Lossless_UDP.h" | 39 | #include "../core/Lossless_UDP.h" |
@@ -82,68 +101,53 @@ int main(int argc, char *argv[]) | |||
82 | if ( file2==NULL ){return 1;} | 101 | if ( file2==NULL ){return 1;} |
83 | read1 = fread(buffer1, 1, 128, file1); | 102 | read1 = fread(buffer1, 1, 128, file1); |
84 | 103 | ||
85 | while(1) | 104 | while(1) { |
86 | { | ||
87 | 105 | ||
88 | while(receivepacket(&ip_port, data, &length) != -1) | 106 | while(receivepacket(&ip_port, data, &length) != -1) { |
89 | { | 107 | if(rand() % 3 != 1) { /* simulate packet loss */ |
90 | if(rand() % 3 != 1)//simulate packet loss | 108 | if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port)) { |
91 | { | 109 | /* if packet is not recognized */ |
92 | if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port)) | ||
93 | { | ||
94 | //if packet is not recognized | ||
95 | printf("Received unhandled packet with length: %u\n", length); | 110 | printf("Received unhandled packet with length: %u\n", length); |
96 | } | 111 | } else { |
97 | else | ||
98 | { | ||
99 | printf("Received handled packet with length: %u\n", length); | 112 | printf("Received handled packet with length: %u\n", length); |
100 | } | 113 | } |
101 | } | 114 | } |
102 | } | 115 | } |
103 | friend_ip = DHT_getfriendip((uint8_t *)argv[3]); | 116 | friend_ip = DHT_getfriendip((uint8_t *)argv[3]); |
104 | if(friend_ip.ip.i != 0) | 117 | if(friend_ip.ip.i != 0) { |
105 | { | 118 | if(connection == -1) { |
106 | if(connection == -1) | ||
107 | { | ||
108 | printf("Started connecting to friend:"); | 119 | printf("Started connecting to friend:"); |
109 | printip(friend_ip); | 120 | printip(friend_ip); |
110 | connection = new_connection(friend_ip); | 121 | connection = new_connection(friend_ip); |
111 | } | 122 | } |
112 | } | 123 | } |
113 | if(inconnection == -1) | 124 | if(inconnection == -1) { |
114 | { | ||
115 | inconnection = incoming_connection(); | 125 | inconnection = incoming_connection(); |
116 | if(inconnection != -1) | 126 | if(inconnection != -1) { |
117 | { | ||
118 | printf("Someone connected to us:"); | 127 | printf("Someone connected to us:"); |
119 | printip(connection_ip(inconnection)); | 128 | printip(connection_ip(inconnection)); |
120 | } | 129 | } |
121 | } | 130 | } |
122 | //if someone connected to us write what he sends to a file | 131 | /* if someone connected to us write what he sends to a file */ |
123 | //also send him our file. | 132 | /* also send him our file. */ |
124 | if(inconnection != -1) | 133 | if(inconnection != -1) { |
125 | { | 134 | if(write_packet(inconnection, buffer1, read1)) { |
126 | if(write_packet(inconnection, buffer1, read1)) | ||
127 | { | ||
128 | printf("Wrote data.\n"); | 135 | printf("Wrote data.\n"); |
129 | read1 = fread(buffer1, 1, 128, file1); | 136 | read1 = fread(buffer1, 1, 128, file1); |
130 | } | 137 | } |
131 | read2 = read_packet(inconnection, buffer2); | 138 | read2 = read_packet(inconnection, buffer2); |
132 | if(read2 != 0) | 139 | if(read2 != 0) { |
133 | { | ||
134 | printf("Received data.\n"); | 140 | printf("Received data.\n"); |
135 | if(!fwrite(buffer2, read2, 1, file2)) | 141 | if(!fwrite(buffer2, read2, 1, file2)) { |
136 | { | ||
137 | printf("file write error\n"); | 142 | printf("file write error\n"); |
138 | } | 143 | } |
139 | if(read2 < 128) | 144 | if(read2 < 128) { |
140 | { | ||
141 | fclose(file2); | 145 | fclose(file2); |
142 | } | 146 | } |
143 | } | 147 | } |
144 | } | 148 | } |
145 | //if we are connected to a friend send him data from the file. | 149 | /* if we are connected to a friend send him data from the file. |
146 | //also put what he sends us in a file. | 150 | * also put what he sends us in a file. */ |
147 | if(is_connected(connection) == 3) | 151 | if(is_connected(connection) == 3) |
148 | { | 152 | { |
149 | if(write_packet(0, buffer1, read1)) | 153 | if(write_packet(0, buffer1, read1)) |
@@ -167,9 +171,9 @@ int main(int argc, char *argv[]) | |||
167 | } | 171 | } |
168 | doDHT(); | 172 | doDHT(); |
169 | doLossless_UDP(); | 173 | doLossless_UDP(); |
170 | //print_clientlist(); | 174 | /* print_clientlist(); |
171 | //print_friendlist(); | 175 | * print_friendlist(); |
172 | //c_sleep(300); | 176 | * c_sleep(300); */ |
173 | c_sleep(1); | 177 | c_sleep(1); |
174 | } | 178 | } |
175 | 179 | ||