Advertisement
Guest User

Untitled

a guest
Feb 16th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. while(1){
  2. do{
  3. tmp = poll(fds, nfds, timeout);
  4. if(tmp < 0){
  5. printf("Problem with poll\n");
  6. break;
  7. }
  8. else if(tmp == 0){
  9. printf("End a program.\n");
  10. break;
  11. }
  12. current_size = nfds;
  13. while(actualSize > buffer_gets && currentsizeQueue(Q) > 0)
  14. {
  15. for(int i = 0; i < buffer_gets; i++)
  16. getfromBuffer(&pBuf, &bufferX[i]);
  17. data_size += buffer_gets;
  18. actualSize -= buffer_gets;
  19. sent_size += buffer_gets;
  20. if((tmp = send(getfromQueue(Q), &bufferX, sizeof(bufferX), 0)) < 0){
  21. printf("Problem with send()\n");
  22. close_connection = 1;
  23. break;
  24. }
  25. }
  26. for(int i = 0; i < current_size; i++){
  27. if(fds[i].revents == 0)
  28. continue;
  29. if(fds[i].fd == socket_fd){
  30. do{
  31. nsd = accept(socket_fd, NULL, NULL);
  32. if(nsd < 0){
  33. if(errno != EWOULDBLOCK){
  34. printf("Problem with accept()\n");
  35. end_server = 1;
  36. }
  37. break;
  38. }
  39. unsigned ex = sizeof(clientX);
  40. getpeername(socket_fd, (struct sockaddr*)&clientX, &ex);
  41. inet_ntop(AF_INET, &clientX->sin_addr, clientIP, INET6_ADDRSTRLEN);
  42. clock_gettime(CLOCK_REALTIME, &ts);
  43. long ts1 = ts.tv_sec;
  44. clock_gettime(CLOCK_MONOTONIC, &ts);
  45. long ts2 = ts.tv_sec;
  46. fd = fopen(r_path, "a");
  47. fprintf(fd, "After connection:\t1st clock:%ld\t2nd clock:%ld\tClient IP: %s\n", ts1, ts2, clientIP);
  48. fclose(fd);
  49. fds[nfds].fd = nsd;
  50. fds[nfds].events = POLLIN;
  51. nfds++;
  52. }while(nsd != -1);
  53. }
  54. else if(fds[i].fd == createClock){
  55. char buf[8];
  56. read(createClock, buf, 8);
  57. // *********************************************** Składowanie zamówienia
  58. if(data_size >= buffer_puts){
  59. if(c == 'Z' + 1)
  60. c = 'a';
  61. else if(c == 'z' + 1)
  62. c = 'A';
  63. for(int i = 0; i < buffer_puts; i++)
  64. addtoBuffer(&pBuf, c); // Dodawanie wartości do bufora cyklicznego
  65. data_size -= buffer_puts;
  66. actualSize += buffer_puts;
  67. make_size += buffer_puts;
  68. c++;
  69. if(data_size == 0)
  70. break;
  71. }
  72. }
  73. else if(fds[i].fd == printClock){
  74. char buf[8];
  75. read(printClock, buf, 8);
  76. clock_gettime(CLOCK_REALTIME, &ts);
  77. long ts1 = ts.tv_sec;
  78. clock_gettime(CLOCK_MONOTONIC, &ts);
  79. long ts2 = ts.tv_sec;
  80.  
  81. fd = fopen(r_path, "a");
  82. fprintf(fd, "1st clock: %ld\t 2nd clock: %ld\n", ts1, ts2);
  83. fprintf(fd, "Numer of clients: %d\n", currentsizeQueue(Q));
  84. fprintf(fd, "Actual size: %d of %d\n", actualSize, buffer_size);
  85. fprintf(fd, "Actual size in %% is %f\n", (double)actualSize/buffer_size*100);
  86. fprintf(fd, "Material flow: %d\n", make_size - sent_size);
  87. fclose(fd);
  88. make_size = 0;
  89. sent_size = 0;
  90. }
  91. else{
  92. close_connection = 0;
  93. tmp = recv(fds[i].fd, rec_buff, sizeof(rec_buff), 0);
  94. CheckRcv(tmp, &close_connection);
  95. addtoQueue(Q, fds[i].fd);
  96. if(close_connection){
  97. clock_gettime(CLOCK_REALTIME, &ts);
  98. long ts1 = ts.tv_sec;
  99. clock_gettime(CLOCK_MONOTONIC, &ts);
  100. long ts2 = ts.tv_sec;
  101.  
  102. fd = fopen(r_path, "a");
  103. fprintf(fd, "After disconnection:\t1st clock: %ld\t2nd clock:%ld\tClient IP: %s\n", ts1, ts2, clientIP);
  104. fclose(fd);
  105. close(fds[i].fd);
  106. fds[i].fd = -1;
  107. }
  108. }
  109. }
  110. }while(end_server == 0);
  111. for(int i = 0; i < nfds; i++){
  112. if(fds[i].fd >= 0)
  113. close(fds[i].fd);
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement