diff options
author | irungentoo <irungentoo@gmail.com> | 2015-03-10 18:29:10 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2015-03-10 18:29:10 -0400 |
commit | c434d4857993668ed78668f2edbef29744740fd3 (patch) | |
tree | 1c314fb5f41d7684d5c95394df1f0655d1279434 | |
parent | 0207fcdfb0e43d425abb026b08c074a6822e0349 (diff) |
testing/tox_sync now fully works on the new api.
-rw-r--r-- | testing/tox_sync.c | 112 |
1 files changed, 63 insertions, 49 deletions
diff --git a/testing/tox_sync.c b/testing/tox_sync.c index 77f75c36..785fffeb 100644 --- a/testing/tox_sync.c +++ b/testing/tox_sync.c | |||
@@ -46,43 +46,39 @@ | |||
46 | #define NUM_FILE_SENDERS 256 | 46 | #define NUM_FILE_SENDERS 256 |
47 | typedef struct { | 47 | typedef struct { |
48 | FILE *file; | 48 | FILE *file; |
49 | uint16_t friendnum; | 49 | uint32_t friendnum; |
50 | uint8_t filenumber; | 50 | uint32_t filenumber; |
51 | uint8_t nextpiece[1400]; | ||
52 | uint16_t piecelength; | ||
53 | } File_t; | 51 | } File_t; |
54 | File_t file_senders[NUM_FILE_SENDERS]; | 52 | File_t file_senders[NUM_FILE_SENDERS]; |
55 | File_t file_recv[NUM_FILE_SENDERS]; | 53 | File_t file_recv[NUM_FILE_SENDERS]; |
56 | uint8_t numfilesenders; | 54 | uint8_t numfilesenders; |
57 | 55 | ||
58 | void send_filesenders(Tox *m) | 56 | void tox_file_request_chunk(Tox *tox, uint32_t friend_number, uint32_t file_number, uint64_t position, size_t length, |
57 | void *user_data) | ||
59 | { | 58 | { |
60 | uint32_t i; | 59 | unsigned int i; |
61 | 60 | ||
62 | for (i = 0; i < NUM_FILE_SENDERS; ++i) { | 61 | for (i = 0; i < NUM_FILE_SENDERS; ++i) { |
63 | if (file_senders[i].file == 0) | 62 | /* This is slow */ |
64 | continue; | 63 | if (file_senders[i].file && file_senders[i].friendnum == friend_number && file_senders[i].filenumber == file_number) { |
65 | 64 | if (length == 0) { | |
66 | while (1) { | ||
67 | if (tox_file_send_data(m, file_senders[i].friendnum, file_senders[i].filenumber, file_senders[i].nextpiece, | ||
68 | file_senders[i].piecelength) != 0) | ||
69 | break; | ||
70 | |||
71 | file_senders[i].piecelength = fread(file_senders[i].nextpiece, 1, tox_file_data_size(m, file_senders[i].friendnum), | ||
72 | file_senders[i].file); | ||
73 | |||
74 | if (file_senders[i].piecelength == 0) { | ||
75 | fclose(file_senders[i].file); | 65 | fclose(file_senders[i].file); |
76 | file_senders[i].file = 0; | 66 | file_senders[i].file = 0; |
77 | 67 | printf("[t] %u file transfer: %u completed\n", file_senders[i].friendnum, file_senders[i].filenumber); | |
78 | printf("[t] %u file transfer: %u completed %i\n", file_senders[i].friendnum, file_senders[i].filenumber, | ||
79 | tox_file_send_control(m, file_senders[i].friendnum, 0, file_senders[i].filenumber, TOX_FILECONTROL_FINISHED, 0, 0)); | ||
80 | break; | 68 | break; |
81 | } | 69 | } |
70 | |||
71 | fseek(file_senders[i].file, position, SEEK_SET); | ||
72 | uint8_t data[length]; | ||
73 | int len = fread(data, 1, length, file_senders[i].file); | ||
74 | tox_file_send_chunk(tox, friend_number, file_number, data, len, 0); | ||
75 | break; | ||
82 | } | 76 | } |
83 | } | 77 | } |
84 | } | 78 | } |
85 | int add_filesender(Tox *m, uint16_t friendnum, char *filename) | 79 | |
80 | |||
81 | uint32_t add_filesender(Tox *m, uint16_t friendnum, char *filename) | ||
86 | { | 82 | { |
87 | FILE *tempfile = fopen(filename, "rb"); | 83 | FILE *tempfile = fopen(filename, "rb"); |
88 | 84 | ||
@@ -92,22 +88,20 @@ int add_filesender(Tox *m, uint16_t friendnum, char *filename) | |||
92 | fseek(tempfile, 0, SEEK_END); | 88 | fseek(tempfile, 0, SEEK_END); |
93 | uint64_t filesize = ftell(tempfile); | 89 | uint64_t filesize = ftell(tempfile); |
94 | fseek(tempfile, 0, SEEK_SET); | 90 | fseek(tempfile, 0, SEEK_SET); |
95 | int filenum = tox_new_file_sender(m, friendnum, filesize, (uint8_t *)filename, strlen(filename) + 1); | 91 | uint32_t filenum = tox_file_send(m, friendnum, TOX_FILE_KIND_DATA, filesize, (uint8_t *)filename, strlen(filename) + 1, |
92 | 0); | ||
96 | 93 | ||
97 | if (filenum == -1) | 94 | if (filenum == -1) |
98 | return -1; | 95 | return -1; |
99 | 96 | ||
100 | file_senders[numfilesenders].file = tempfile; | 97 | file_senders[numfilesenders].file = tempfile; |
101 | file_senders[numfilesenders].piecelength = fread(file_senders[numfilesenders].nextpiece, 1, tox_file_data_size(m, | ||
102 | file_senders[numfilesenders].friendnum), | ||
103 | file_senders[numfilesenders].file); | ||
104 | file_senders[numfilesenders].friendnum = friendnum; | 98 | file_senders[numfilesenders].friendnum = friendnum; |
105 | file_senders[numfilesenders].filenumber = filenum; | 99 | file_senders[numfilesenders].filenumber = filenum; |
106 | ++numfilesenders; | 100 | ++numfilesenders; |
107 | return filenum; | 101 | return filenum; |
108 | } | 102 | } |
109 | 103 | ||
110 | void kill_filesender(Tox *m, uint8_t filenum) | 104 | void kill_filesender(Tox *m, uint32_t filenum) |
111 | { | 105 | { |
112 | uint32_t i; | 106 | uint32_t i; |
113 | 107 | ||
@@ -130,9 +124,15 @@ int not_sending() | |||
130 | 124 | ||
131 | static char path[1024]; | 125 | static char path[1024]; |
132 | 126 | ||
133 | void file_request_accept(Tox *m, int friendnumber, uint8_t filenumber, uint64_t filesize, const uint8_t *filename, | 127 | void file_request_accept(Tox *tox, uint32_t friend_number, uint32_t file_number, TOX_FILE_KIND type, |
134 | uint16_t filename_length, void *userdata) | 128 | uint64_t file_size, const uint8_t *filename, size_t filename_length, void *user_data) |
135 | { | 129 | { |
130 | if (type != TOX_FILE_KIND_DATA) { | ||
131 | printf("Refused invalid file type."); | ||
132 | tox_file_control(tox, friend_number, file_number, TOX_FILE_CONTROL_CANCEL, 0); | ||
133 | return; | ||
134 | } | ||
135 | |||
136 | char fullpath[1024]; | 136 | char fullpath[1024]; |
137 | uint32_t i; | 137 | uint32_t i; |
138 | uint16_t rm = 0; | 138 | uint16_t rm = 0; |
@@ -151,48 +151,62 @@ void file_request_accept(Tox *m, int friendnumber, uint8_t filenumber, uint64_t | |||
151 | 151 | ||
152 | if (tempfile != 0) { | 152 | if (tempfile != 0) { |
153 | fclose(tempfile); | 153 | fclose(tempfile); |
154 | tox_file_send_control(m, friendnumber, 1, filenumber, TOX_FILECONTROL_KILL, 0, 0); | 154 | tox_file_control(tox, friend_number, file_number, TOX_FILE_CONTROL_CANCEL, 0); |
155 | return; | 155 | return; |
156 | } | 156 | } |
157 | 157 | ||
158 | file_recv[filenumber].file = fopen(fullpath, "wb"); | 158 | uint8_t file_index = (file_number >> 16) - 1; |
159 | file_recv[file_index].file = fopen(fullpath, "wb"); | ||
159 | 160 | ||
160 | if (file_recv[filenumber].file == 0) { | 161 | if (file_recv[file_index].file == 0) { |
161 | tox_file_send_control(m, friendnumber, 1, filenumber, TOX_FILECONTROL_KILL, 0, 0); | 162 | tox_file_control(tox, friend_number, file_number, TOX_FILE_CONTROL_CANCEL, 0); |
162 | return; | 163 | return; |
163 | } | 164 | } |
164 | 165 | ||
165 | if (tox_file_send_control(m, friendnumber, 1, filenumber, TOX_FILECONTROL_ACCEPT, 0, 0) == 0) { | 166 | if (tox_file_control(tox, friend_number, file_number, TOX_FILE_CONTROL_RESUME, 0)) { |
166 | printf("Accepted file transfer. (file: %s)\n", fullpath); | 167 | printf("Accepted file transfer. (file: %s)\n", fullpath); |
167 | } | 168 | } |
168 | 169 | ||
169 | } | 170 | } |
170 | 171 | ||
171 | void file_print_control(Tox *m, int friendnumber, uint8_t recieve_send, uint8_t filenumber, uint8_t control_type, | 172 | void file_print_control(Tox *tox, uint32_t friend_number, uint32_t file_number, TOX_FILE_CONTROL control, |
172 | const uint8_t *data, | 173 | void *user_data) |
173 | uint16_t length, void *userdata) | ||
174 | { | 174 | { |
175 | if (recieve_send == 1 && (control_type == TOX_FILECONTROL_KILL || control_type == TOX_FILECONTROL_FINISHED)) { | 175 | if (file_number < (1 << 15) && (control == TOX_FILE_CONTROL_CANCEL)) { |
176 | kill_filesender(m, filenumber); | 176 | kill_filesender(tox, file_number); |
177 | return; | 177 | return; |
178 | } | 178 | } |
179 | 179 | ||
180 | if (recieve_send == 0 && (control_type == TOX_FILECONTROL_KILL || control_type == TOX_FILECONTROL_FINISHED)) { | 180 | if (file_number > (1 << 15) && (control == TOX_FILE_CONTROL_CANCEL)) { |
181 | fclose(file_recv[filenumber].file); | 181 | uint8_t file_index = (file_number >> 16) - 1; |
182 | fclose(file_recv[file_index].file); | ||
182 | printf("File closed\n"); | 183 | printf("File closed\n"); |
183 | file_recv[filenumber].file = 0; | 184 | file_recv[file_index].file = 0; |
184 | return; | 185 | return; |
185 | } | 186 | } |
186 | } | 187 | } |
187 | 188 | ||
188 | void write_file(Tox *m, int friendnumber, uint8_t filenumber, const uint8_t *data, uint16_t length, void *userdata) | 189 | void write_file(Tox *tox, uint32_t friendnumber, uint32_t filenumber, uint64_t position, const uint8_t *data, |
190 | size_t length, void *user_data) | ||
189 | { | 191 | { |
190 | if (file_recv[filenumber].file != 0) | 192 | uint8_t file_index = (filenumber >> 16) - 1; |
191 | if (fwrite(data, length, 1, file_recv[filenumber].file) != 1) | 193 | if (length == 0) { |
194 | fclose(file_recv[file_index].file); | ||
195 | printf("File closed\n"); | ||
196 | file_recv[file_index].file = 0; | ||
197 | printf("%u file transfer: %u completed\n", friendnumber, filenumber); | ||
198 | return; | ||
199 | } | ||
200 | |||
201 | if (file_recv[file_index].file != 0) { | ||
202 | fseek(file_recv[file_index].file, position, SEEK_SET); | ||
203 | |||
204 | if (fwrite(data, length, 1, file_recv[file_index].file) != 1) | ||
192 | printf("Error writing data\n"); | 205 | printf("Error writing data\n"); |
206 | } | ||
193 | } | 207 | } |
194 | 208 | ||
195 | void print_online(Tox *tox, uint32_t friendnumber, uint8_t status, void *userdata) | 209 | void print_online(Tox *tox, uint32_t friendnumber, TOX_CONNECTION status, void *userdata) |
196 | { | 210 | { |
197 | if (status) | 211 | if (status) |
198 | printf("\nOther went online.\n"); | 212 | printf("\nOther went online.\n"); |
@@ -215,9 +229,10 @@ int main(int argc, char *argv[]) | |||
215 | } | 229 | } |
216 | 230 | ||
217 | Tox *tox = tox_new(0, 0, 0, 0); | 231 | Tox *tox = tox_new(0, 0, 0, 0); |
218 | tox_callback_file_data(tox, write_file, NULL); | 232 | tox_callback_file_receive_chunk(tox, write_file, NULL); |
219 | tox_callback_file_control(tox, file_print_control, NULL); | 233 | tox_callback_file_control(tox, file_print_control, NULL); |
220 | tox_callback_file_send_request(tox, file_request_accept, NULL); | 234 | tox_callback_file_receive(tox, file_request_accept, NULL); |
235 | tox_callback_file_request_chunk(tox, tox_file_request_chunk, NULL); | ||
221 | tox_callback_friend_connection_status(tox, print_online, NULL); | 236 | tox_callback_friend_connection_status(tox, print_online, NULL); |
222 | 237 | ||
223 | uint16_t port = atoi(argv[argvoffset + 2]); | 238 | uint16_t port = atoi(argv[argvoffset + 2]); |
@@ -290,7 +305,6 @@ int main(int argc, char *argv[]) | |||
290 | } | 305 | } |
291 | } | 306 | } |
292 | 307 | ||
293 | send_filesenders(tox); | ||
294 | tox_iteration(tox); | 308 | tox_iteration(tox); |
295 | c_sleep(1); | 309 | c_sleep(1); |
296 | } | 310 | } |