Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static int execute_receive(int connfd, operation_t * o) {
- char * sname = o - > sname;
- size_t m = (size_t) - 1;
- if (o - > n > 0) m = (ssize_t) o - > n;
- size_t c = 0;
- for (size_t i = 0; i < msgcur; ++i) {
- if (MSGS[i].data.buf != NULL) {
- if (MSGS[i].hdr.op == FILE_MESSAGE) {
- char * filename = MSGS[i].data.buf;
- printf("[%s vuole inviare il file '%s']\n", MSGS[i].hdr.sender, filename);
- if (downloadFile(connfd, filename, sname) == -1) {
- fprintf(stderr, "ERRORE: cercando di scaricare il file %s\n", filename);
- return -1;
- }
- printf("[Il file '%s' e' stato scaricato correttamente]\n", filename);
- } else
- printf("[%s:] %s\n", MSGS[i].hdr.sender, (char * ) MSGS[i].data.buf);
- if (++c == m) break;
- }
- }
- for (size_t i = c; i < m; ++i) {
- message_t msg;
- // leggo header e data
- if (readMsg(connfd, & msg) == -1) {
- perror("reply data");
- return -1;
- }
- switch (msg.hdr.op) {
- case TXT_MESSAGE:
- {
- printf("[%s:] %s\n", msg.hdr.sender, (char * ) msg.data.buf);
- }
- break;
- case FILE_MESSAGE:
- {
- char * filename = strdup(msg.data.buf);
- free(msg.data.buf);
- printf("[%s vuole inviare il file '%s']\n", msg.hdr.sender, filename);
- if (downloadFile(connfd, filename, sname) == -1) {
- fprintf(stderr, "ERRORE: cercando di scaricare il file %s\n", filename);
- return -1;
- }
- printf("[Il file '%s' e' stato scaricato correttamente]\n", filename);
- }
- break;
- default:
- {
- fprintf(stderr, "ERRORE: ricevuto messaggio non valido\n");
- return -1;
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement