AndreiS

Untitled

Mar 31st, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.63 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) + 1;
  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) + 1;
  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) + 1;
  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) + 1;
  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. printf("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA %d", filesize);
  126.  
  127. /* trimite filesize */
  128. sprintf(r.payload, "%d", filesize);
  129. r.len=strlen(r.payload) + 1;
  130. res = send_message(&r);
  131. if (res < 0)
  132. {
  133. perror("[RECEIVER] Send error. Exiting.\n");
  134. return -1;
  135. }
  136. char buffer[1400];
  137.  
  138. /* asteapta */
  139. res = recv_message(&r);
  140. if (res < 0)
  141. {
  142. perror("[RECEIVER] Receive error. Exiting.\n");
  143. return -1;
  144. }
  145.  
  146. int bytesRead = 0;
  147. while((bytesRead = fread(r.payload, sizeof(char), 1400, fp)) != 0)
  148. {
  149. //memcpy(r.payload, buffer, bytesRead);
  150. r.len = bytesRead;
  151. res = send_message(&r);
  152. if (res < 0)
  153. {
  154. perror("[RECEIVER] Send error. Exiting.\n");
  155. return -1;
  156. }
  157. /* asteapta */
  158. res = recv_message(&r);
  159. if (res < 0)
  160. {
  161. perror("[RECEIVER] Receive error. Exiting.\n");
  162. return -1;
  163. }
  164. }
  165. fclose(fp);
  166. }
  167. //end cp
  168.  
  169. //sn
  170. if(strcmp(token,"sn")==0)
  171. {
  172. token=strtok(NULL," ");
  173.  
  174. /* primeste filesize */
  175. res = recv_message(&r);
  176. if (res < 0)
  177. {
  178. perror("[RECEIVER] Receive error. Exiting.\n");
  179. return -1;
  180. }
  181.  
  182. /* numarul de pachete in care va fi impartit fisierul */
  183. filesize = atoi(r.payload);
  184. int packs = filesize/1400+1;
  185. char * filename = malloc((5+strlen(token))*sizeof(char));
  186. strcpy(filename, "new_");
  187. strcat(filename, token);
  188.  
  189. fp = fopen(filename, "w");
  190. for(i=0; i<packs; i++)
  191. {
  192. /* primeste pachet fisier */
  193. res=recv_message(&r);
  194. if (res < 0)
  195. {
  196. perror("[RECEIVER] Receive error. Exiting.\n");
  197. return -1;
  198. }
  199. /* pune pachetul in fisier */
  200. fputs(r.payload, fp);
  201. }
  202. fclose(fp);
  203. }
  204. //end sn
  205.  
  206.  
  207. // exit
  208. if(strcmp(token, "exit") == 0)
  209. {
  210. token = strtok(NULL, " ");
  211. if(strcmp(token, "exit") == 0);
  212. exit(0);
  213. }
  214. //end exit
  215.  
  216.  
  217. /* send dummy ACK */
  218. res = send_message(&r);
  219. if (res < 0) {
  220. perror("[RECEIVER] Send ACK error. Exiting.\n");
  221. return -1;
  222. }
  223. }
  224.  
  225. printf("[RECEIVER] Finished receiving..\n");
  226. return 0;
  227. }
Advertisement
Add Comment
Please, Sign In to add comment