summaryrefslogtreecommitdiff
path: root/testing/DHT_sendfiletest.c
diff options
context:
space:
mode:
Diffstat (limited to 'testing/DHT_sendfiletest.c')
-rw-r--r--testing/DHT_sendfiletest.c60
1 files changed, 27 insertions, 33 deletions
diff --git a/testing/DHT_sendfiletest.c b/testing/DHT_sendfiletest.c
index 5b5d6c9f..873a73f8 100644
--- a/testing/DHT_sendfiletest.c
+++ b/testing/DHT_sendfiletest.c
@@ -96,78 +96,72 @@ int main(int argc, char *argv[])
96 uint8_t buffer2[128]; 96 uint8_t buffer2[128];
97 int read2 = 0; 97 int read2 = 0;
98 FILE *file1 = fopen(argv[4], "rb"); 98 FILE *file1 = fopen(argv[4], "rb");
99 if ( file1==NULL ){printf("Error opening file.\n");return 1;} 99 if (file1 == NULL) {
100 printf("Error opening file.\n");
101 return 1;
102 }
100 FILE *file2 = fopen("received.txt", "wb"); 103 FILE *file2 = fopen("received.txt", "wb");
101 if ( file2==NULL ){return 1;} 104 if (file2 == NULL)
105 return 1;
106
102 read1 = fread(buffer1, 1, 128, file1); 107 read1 = fread(buffer1, 1, 128, file1);
103 108
104 while(1) { 109 while (1) {
105 110 while (receivepacket(&ip_port, data, &length) != -1) {
106 while(receivepacket(&ip_port, data, &length) != -1) { 111 if (rand() % 3 != 1) { /* simulate packet loss */
107 if(rand() % 3 != 1) { /* simulate packet loss */ 112 if (DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port))
108 if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port)) { 113 printf("Received unhandled packet with length: %u\n", length); /* if packet is not recognized */
109 /* if packet is not recognized */ 114 else
110 printf("Received unhandled packet with length: %u\n", length);
111 } else {
112 printf("Received handled packet with length: %u\n", length); 115 printf("Received handled packet with length: %u\n", length);
113 }
114 } 116 }
115 } 117 }
116 friend_ip = DHT_getfriendip((uint8_t *)argv[3]); 118 friend_ip = DHT_getfriendip((uint8_t *)argv[3]);
117 if(friend_ip.ip.i != 0) { 119 if (friend_ip.ip.i != 0) {
118 if(connection == -1) { 120 if (connection == -1) {
119 printf("Started connecting to friend:"); 121 printf("Started connecting to friend:");
120 printip(friend_ip); 122 printip(friend_ip);
121 connection = new_connection(friend_ip); 123 connection = new_connection(friend_ip);
122 } 124 }
123 } 125 }
124 if(inconnection == -1) { 126 if (inconnection == -1) {
125 inconnection = incoming_connection(); 127 inconnection = incoming_connection();
126 if(inconnection != -1) { 128 if (inconnection != -1) {
127 printf("Someone connected to us:"); 129 printf("Someone connected to us:");
128 printip(connection_ip(inconnection)); 130 printip(connection_ip(inconnection));
129 } 131 }
130 } 132 }
131 /* if someone connected to us write what he sends to a file */ 133 /* if someone connected to us write what he sends to a file */
132 /* also send him our file. */ 134 /* also send him our file. */
133 if(inconnection != -1) { 135 if (inconnection != -1) {
134 if(write_packet(inconnection, buffer1, read1)) { 136 if (write_packet(inconnection, buffer1, read1)) {
135 printf("Wrote data.\n"); 137 printf("Wrote data.\n");
136 read1 = fread(buffer1, 1, 128, file1); 138 read1 = fread(buffer1, 1, 128, file1);
137 } 139 }
138 read2 = read_packet(inconnection, buffer2); 140 read2 = read_packet(inconnection, buffer2);
139 if(read2 != 0) { 141 if (read2 != 0) {
140 printf("Received data.\n"); 142 printf("Received data.\n");
141 if(!fwrite(buffer2, read2, 1, file2)) { 143 if (!fwrite(buffer2, read2, 1, file2))
142 printf("file write error\n"); 144 printf("file write error\n");
143 } 145 if (read2 < 128) {
144 if(read2 < 128) {
145 fclose(file2); 146 fclose(file2);
146 } 147 }
147 } 148 }
148 } 149 }
149 /* if we are connected to a friend send him data from the file. 150 /* if we are connected to a friend send him data from the file.
150 * also put what he sends us in a file. */ 151 * also put what he sends us in a file. */
151 if(is_connected(connection) == 3) 152 if (is_connected(connection) == 3) {
152 { 153 if (write_packet(0, buffer1, read1)) {
153 if(write_packet(0, buffer1, read1))
154 {
155 printf("Wrote data.\n"); 154 printf("Wrote data.\n");
156 read1 = fread(buffer1, 1, 128, file1); 155 read1 = fread(buffer1, 1, 128, file1);
157 } 156 }
158 read2 = read_packet(0, buffer2); 157 read2 = read_packet(0, buffer2);
159 if(read2 != 0) 158 if (read2 != 0) {
160 {
161 printf("Received data.\n"); 159 printf("Received data.\n");
162 if(!fwrite(buffer2, read2, 1, file2)) 160 if(!fwrite(buffer2, read2, 1, file2))
163 { 161 printf("file write error\n");
164 printf("file write error\n");
165 }
166 if(read2 < 128) 162 if(read2 < 128)
167 {
168 fclose(file2); 163 fclose(file2);
169 } 164 }
170 }
171 } 165 }
172 doDHT(); 166 doDHT();
173 doLossless_UDP(); 167 doLossless_UDP();