Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.70 KB | None | 0 0
  1. void setClocks(struct itimerspec value_to_add, struct itimerspec value_to_print, struct timespec ts, int* socket_fd, struct pollfd* fds, int* clock1, int* clock2, double speed)
  2. {
  3. memset(fds, 0, sizeof(&fds));
  4. fds[0].fd = *socket_fd;
  5. fds[0].events = POLLIN;
  6.  
  7. *clock1 = timerfd_create(CLOCK_REALTIME, 0);
  8. if(*clock1 == -1){
  9. printf("Problem with clock.\n");
  10. exit(1);
  11. }
  12. value_to_add.it_value.tv_sec = (int)speed;
  13. value_to_add.it_value.tv_nsec = (speed - value_to_add.it_value.tv_sec) * 1000000000;
  14. value_to_add.it_interval.tv_sec = (int)speed;
  15. value_to_add.it_interval.tv_nsec = (speed - value_to_add.it_interval.tv_sec) * 1000000000;
  16. if(timerfd_settime(*clock1, 0, &value_to_add, NULL) == -1){
  17. printf("Problem with timerfd_settime - createClock\n");
  18. exit(1);
  19. }
  20. fds[1].fd = *clock1;
  21. fds[1].events = POLLIN;
  22.  
  23. *clock2 = timerfd_create(CLOCK_REALTIME, 0);
  24. if(*clock2 == -1){
  25. printf("Problem with clock.\n");
  26. exit(1);
  27. }
  28. value_to_print.it_value.tv_sec = 5;
  29. value_to_print.it_value.tv_nsec = 0;
  30. value_to_print.it_interval.tv_sec = 5;
  31. value_to_print.it_interval.tv_nsec = 0;
  32. if(timerfd_settime(*clock2, 0, &value_to_print, NULL) == -1){
  33. printf("Problem with timerfd_settime - printClock\n");
  34. exit(1);
  35. }
  36. fds[2].fd = *clock2;
  37. fds[2].events = POLLIN;
  38. }
  39.  
  40. int main(int argc, char* argv[])
  41. {
  42. double speed = 0;
  43. long port = 0;
  44. char address[20]="localhost";
  45. char* r_path = NULL;
  46. r_path = Parameters(argc, argv, &speed, address, &port);
  47. speed = speed * 60 / (96*100);
  48. struct buffer pBuf;
  49. createBuffer(&pBuf);
  50. struct queue* Q = createQueue(10000);
  51. int tmp, on = 1, socket_fd = -1, nsd = 1, end_server = 0, close_connection;
  52. char bufferX[buffer_gets], clientIP[25], rec_buff[5], server[25], c = 'a';
  53. long ts1 = 0, ts2 = 0;
  54. struct sockaddr_in addressX;
  55. struct sockaddr_in *clientX = (struct sockaddr_in*)&clientIP;
  56. int actualSize = 0, timeout, nfds = 3, current_size = 0, sent_size = 0, make_size = 0, data_size = buffer_size;
  57. struct pollfd fds[200];
  58.  
  59. hostname_to_ip(address, server); // Zmiana z nazwy domeny na adres IP.
  60. sock_setsock_ioctl(&socket_fd, &on);
  61.  
  62. memset(&addressX, 0, sizeof(addressX));
  63. addressX.sin_family = AF_INET;
  64. addressX.sin_addr.s_addr = inet_addr(server);
  65. addressX.sin_port = htons(port);
  66. if(bind(socket_fd, (struct sockaddr*)&addressX, sizeof(addressX)) < 0){
  67. printf("Problem with bind...\n");
  68. exit(-1);
  69. }
  70. if(listen(socket_fd, 32) < 0){
  71. printf("Problem with listen...\n");
  72. exit(-1);
  73. }
  74. int createClock, printClock;
  75. struct timespec ts;
  76. void setClocks(value_to_add, value_to_print, ts, socket_fd, fds, &createClock, &printClock, speed);
  77. /*
  78. struct timespec ts;
  79. memset(fds, 0, sizeof(fds));
  80. fds[0].fd = socket_fd;
  81. fds[0].events = POLLIN;
  82.  
  83. int createClock = timerfd_create(CLOCK_REALTIME, 0);
  84. if(createClock == -1){
  85. printf("Problem with clock.\n");
  86. exit(1);
  87. }
  88. value_to_add.it_value.tv_sec = (int)speed;
  89. value_to_add.it_value.tv_nsec = (speed - value_to_add.it_value.tv_sec) * 1000000000;
  90. value_to_add.it_interval.tv_sec = (int)speed;
  91. value_to_add.it_interval.tv_nsec = (speed - value_to_add.it_interval.tv_sec) * 1000000000;
  92. if(timerfd_settime(createClock, 0, &value_to_add, NULL) == -1){
  93. printf("Problem with timerfd_settime - createClock\n");
  94. exit(1);
  95. }
  96. fds[1].fd = createClock;
  97. fds[1].events = POLLIN;
  98.  
  99. int printClock = timerfd_create(CLOCK_REALTIME, 0);
  100. if(printClock == -1){
  101. printf("Problem with clock.\n");
  102. exit(1);
  103. }
  104. value_to_print.it_value.tv_sec = 5;
  105. value_to_print.it_value.tv_nsec = 0;
  106. value_to_print.it_interval.tv_sec = 5;
  107. value_to_print.it_interval.tv_nsec = 0;
  108. if(timerfd_settime(printClock, 0, &value_to_print, NULL) == -1){
  109. printf("Problem with timerfd_settime - printClock\n");
  110. exit(1);
  111. }
  112. fds[2].fd = printClock;
  113. fds[2].events = POLLIN;
  114. */
  115. timeout = (30000);
  116.  
  117. while(1){
  118. do{
  119. tmp = poll(fds, nfds, timeout);
  120. if(tmp < 0){
  121. printf("Problem with poll\n");
  122. break;
  123. }
  124. else if(tmp == 0){
  125. printf("End a program.\n");
  126. break;
  127. }
  128. current_size = nfds;
  129. while(actualSize > buffer_gets && currentsizeQueue(Q) > 0)
  130. {
  131. for(int i = 0; i < buffer_gets; i++)
  132. getfromBuffer(&pBuf, &bufferX[i]);
  133. data_size += buffer_gets;
  134. actualSize -= buffer_gets;
  135. sent_size += buffer_gets;
  136. if((tmp = send(getfromQueue(Q), &bufferX, sizeof(bufferX), 0)) < 0){
  137. printf("Problem with send()\n");
  138. close_connection = 1;
  139. break;
  140. }
  141. }
  142. for(int i = 0; i < current_size; i++){
  143. if(fds[i].revents == 0)
  144. continue;
  145. if(fds[i].fd == socket_fd){
  146. do{
  147. nsd = accept(socket_fd, NULL, NULL);
  148. if(nsd < 0){
  149. if(errno != EWOULDBLOCK){
  150. printf("Problem with accept()\n");
  151. end_server = 1;
  152. }
  153. break;
  154. }
  155. unsigned ex = sizeof(clientX);
  156. getpeername(socket_fd, (struct sockaddr*)&clientX, &ex);
  157. inet_ntop(AF_INET, &clientX->sin_addr, clientIP, INET6_ADDRSTRLEN);
  158. gettime(ts, &ts1, &ts2);
  159. fd = fopen(r_path, "a");
  160. fprintf(fd, "After connection:\t1st clock:%ld\t2nd clock:%ld\tClient IP: %s\n", ts1, ts2, clientIP);
  161. fclose(fd);
  162. fds[nfds].fd = nsd;
  163. fds[nfds].events = POLLIN;
  164. nfds++;
  165. }while(nsd != -1);
  166. }
  167. else if(fds[i].fd == createClock){
  168. char buf[8];
  169. read(createClock, buf, 8);
  170. // *********************************************** Składowanie zamówienia
  171. if(data_size >= buffer_puts){
  172. if(c == 'Z' + 1)
  173. c = 'a';
  174. else if(c == 'z' + 1)
  175. c = 'A';
  176. for(int i = 0; i < buffer_puts; i++)
  177. {
  178. addtoBuffer(&pBuf, c); // Dodawanie wartości do bufora cyklicznego
  179. //printf("%c", c);
  180. }
  181. data_size -= buffer_puts;
  182. actualSize += buffer_puts;
  183. make_size += buffer_puts;
  184. c++;
  185. if(data_size == 0)
  186. break;
  187. }
  188. }
  189. else if(fds[i].fd == printClock){
  190. char buf[8];
  191. read(printClock, buf, 8);
  192. gettime(ts, &ts1, &ts2);
  193. printingFun(ts1, ts2, actualSize, buffer_size, make_size, sent_size, Q, r_path);
  194. make_size = 0;
  195. sent_size = 0;
  196. }
  197. else{
  198. close_connection = 0;
  199. tmp = recv(fds[i].fd, rec_buff, sizeof(rec_buff), 0);
  200. CheckRcv(tmp, &close_connection);
  201. addtoQueue(Q, fds[i].fd);
  202. if(close_connection){
  203. gettime(ts, &ts1, &ts2);
  204. fd = fopen(r_path, "a");
  205. fprintf(fd, "After disconnection:\t1st clock: %ld\t2nd clock:%ld\tClient IP: %s\n", ts1, ts2, clientIP);
  206. fclose(fd);
  207. close(fds[i].fd);
  208. fds[i].fd = -1;
  209. }
  210. }
  211. }
  212. }while(end_server == 0);
  213. for(int i = 0; i < nfds; i++){
  214. if(fds[i].fd >= 0)
  215. close(fds[i].fd);
  216. }
  217. }
  218. return 0;
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement