AndreiS

Untitled

Mar 31st, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.51 KB | None | 0 0
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <fcntl.h>
  6.  
  7. #include "lib.h"
  8. #include <stdlib.h>
  9. #include <dirent.h>
  10. #include <string.h>
  11.  
  12. #define HOST "127.0.0.1"
  13. #define PORT 10001
  14.  
  15. int main()
  16. {
  17. printf("[RECEIVER] Starting.\n");
  18. msg r;
  19. int i, res;
  20. char inc_msg[MSGSIZE];
  21. char* token;
  22. DIR* dirp;
  23. struct dirent * dp;
  24. int filecount, filesize;
  25. FILE * fp;
  26.  
  27. init(HOST, PORT);
  28.  
  29. for (i = 0; i < COUNT; i++) {
  30. /* wait for message */
  31. res = recv_message(&r);
  32. if (res < 0) {
  33. perror("[RECEIVER] Receive error. Exiting.\n");
  34. return -1;
  35. }
  36.  
  37. strcpy(inc_msg, r.payload);
  38. token=strtok(inc_msg," ");
  39. //cd
  40. if(strcmp(token,"cd")==0)
  41. {
  42. token = strtok(NULL, " ");
  43. chdir(token);
  44. }
  45. //end cd
  46.  
  47. //ls
  48. if(strcmp(token,"ls")==0)
  49. {
  50. token = strtok(NULL, " "); //extrage argumentul
  51. dirp = opendir(token);
  52. filecount=0;
  53. while ( (dp = readdir(dirp)) != NULL)
  54. {
  55. filecount++;
  56. }
  57. /* trimtie ACK */
  58. strcpy(r.payload, "ACK");
  59. r.len=strlen(r.payload);
  60. res = send_message(&r);
  61. if (res < 0)
  62. {
  63. perror("[RECEIVER] Send ACK error. Exiting.\n");
  64. return -1;
  65. }
  66. /* trimite filecount */
  67. sprintf(r.payload, "%d", filecount);
  68. r.len=strlen(r.payload);
  69. res = send_message(&r);
  70. if (res < 0)
  71. {
  72. perror("[RECEIVER] Send filecount error. Exiting.\n");
  73. return -1;
  74. }
  75. dirp=NULL;
  76. dirp = opendir(token);
  77. /* asteapta */
  78. res = recv_message(&r);
  79. if (res < 0)
  80. {
  81. perror("[RECEIVER] Receive error. Exiting.\n");
  82. return -1;
  83. }
  84.  
  85. while ( (dp = readdir(dirp)) != NULL)
  86. {
  87. /* trimite nume fisier */
  88. strcpy(r.payload, dp->d_name);
  89. r.len=strlen(r.payload);
  90. res = send_message(&r);
  91. if (res < 0)
  92. {
  93. perror("[RECEIVER] Receive error. Exiting.\n");
  94. return -1;
  95. }
  96. /* asteapta */
  97. res = recv_message(&r);
  98. if (res < 0)
  99. {
  100. perror("[RECEIVER] Receive error. Exiting.\n");
  101. return -1;
  102. }
  103.  
  104. }
  105. (void)closedir(dirp);
  106. }
  107. //end ls
  108.  
  109. //cp
  110. if(strcmp(token, "cp") == 0)
  111. {
  112. token = strtok(NULL, " ");
  113. fp = fopen(token, "rb");
  114.  
  115. /* trimtie ACK */
  116. strcpy(r.payload, "ACK");
  117. r.len=strlen(r.payload);
  118. res = send_message(&r);
  119.  
  120. /* afla filesize */
  121. fseek(fp, 0, SEEK_END);
  122. filesize = ftell(fp);
  123. fseek(fp, 0, SEEK_SET);
  124.  
  125. /* trimite filesize */
  126. sprintf(r.payload, "%d", filesize);
  127. r.len=strlen(r.payload) + 1;
  128. res = send_message(&r);
  129. if (res < 0)
  130. {
  131. perror("[RECEIVER] Send error. Exiting.\n");
  132. return -1;
  133. }
  134. char buffer[1400];
  135.  
  136. /* asteapta */
  137. res = recv_message(&r);
  138. if (res < 0)
  139. {
  140. perror("[RECEIVER] Receive error. Exiting.\n");
  141. return -1;
  142. }
  143.  
  144. int bytesRead = 0;
  145. while((bytesRead = fread(r.payload, sizeof(char), 1400, fp)) != 0)
  146. {
  147. memcpy(r.payload, buffer, bytesRead);
  148. r.len = bytesRead;
  149. res = send_message(&r);
  150. if (res < 0)
  151. {
  152. perror("[RECEIVER] Send error. Exiting.\n");
  153. return -1;
  154. }
  155. /* asteapta */
  156. res = recv_message(&r);
  157. if (res < 0)
  158. {
  159. perror("[RECEIVER] Receive error. Exiting.\n");
  160. return -1;
  161. }
  162. }
  163. fclose(fp);
  164. }
  165. //end cp
  166.  
  167. //sn
  168. if(strcmp(token,"sn")==0)
  169. {
  170. token=strtok(NULL," ");
  171.  
  172. /* primeste filesize */
  173. res = recv_message(&r);
  174. if (res < 0)
  175. {
  176. perror("[RECEIVER] Receive error. Exiting.\n");
  177. return -1;
  178. }
  179.  
  180. /* numarul de pachete in care va fi impartit fisierul */
  181. filesize = atoi(r.payload);
  182. int packs = filesize/1400+1;
  183. char * filename = malloc((5+strlen(token))*sizeof(char));
  184. strcpy(filename, "new_");
  185. strcat(filename, token);
  186.  
  187. fp = fopen(filename, "w");
  188. for(i=0; i<packs; i++)
  189. {
  190. /* primeste pachet fisier */
  191. res=recv_message(&r);
  192. if (res < 0)
  193. {
  194. perror("[RECEIVER] Receive error. Exiting.\n");
  195. return -1;
  196. }
  197. /* pune pachetul in fisier */
  198. fputs(r.payload, fp);
  199. }
  200. fclose(fp);
  201. }
  202. //end sn
  203.  
  204.  
  205. // exit
  206. if(strcmp(token, "exit") == 0)
  207. {
  208. token = strtok(NULL, " ");
  209. if(strcmp(token, "exit") == 0);
  210. exit(0);
  211. }
  212. //end exit
  213.  
  214.  
  215. /* send dummy ACK */
  216. res = send_message(&r);
  217. if (res < 0) {
  218. perror("[RECEIVER] Send ACK error. Exiting.\n");
  219. return -1;
  220. }
  221. }
  222.  
  223. printf("[RECEIVER] Finished receiving..\n");
  224. return 0;
  225. }
Advertisement
Add Comment
Please, Sign In to add comment