Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- while(1){
- do{
- tmp = poll(fds, nfds, timeout);
- if(tmp < 0){
- printf("Problem with poll\n");
- break;
- }
- else if(tmp == 0){
- printf("End a program.\n");
- break;
- }
- current_size = nfds;
- while(actualSize > buffer_gets && currentsizeQueue(Q) > 0)
- {
- for(int i = 0; i < buffer_gets; i++)
- getfromBuffer(&pBuf, &bufferX[i]);
- data_size += buffer_gets;
- actualSize -= buffer_gets;
- sent_size += buffer_gets;
- if((tmp = send(getfromQueue(Q), &bufferX, sizeof(bufferX), 0)) < 0){
- printf("Problem with send()\n");
- close_connection = 1;
- break;
- }
- }
- for(int i = 0; i < current_size; i++){
- if(fds[i].revents == 0)
- continue;
- if(fds[i].fd == socket_fd){
- do{
- nsd = accept(socket_fd, NULL, NULL);
- if(nsd < 0){
- if(errno != EWOULDBLOCK){
- printf("Problem with accept()\n");
- end_server = 1;
- }
- break;
- }
- unsigned ex = sizeof(clientX);
- getpeername(socket_fd, (struct sockaddr*)&clientX, &ex);
- inet_ntop(AF_INET, &clientX->sin_addr, clientIP, INET6_ADDRSTRLEN);
- clock_gettime(CLOCK_REALTIME, &ts);
- long ts1 = ts.tv_sec;
- clock_gettime(CLOCK_MONOTONIC, &ts);
- long ts2 = ts.tv_sec;
- fd = fopen(r_path, "a");
- fprintf(fd, "After connection:\t1st clock:%ld\t2nd clock:%ld\tClient IP: %s\n", ts1, ts2, clientIP);
- fclose(fd);
- fds[nfds].fd = nsd;
- fds[nfds].events = POLLIN;
- nfds++;
- }while(nsd != -1);
- }
- else if(fds[i].fd == createClock){
- char buf[8];
- read(createClock, buf, 8);
- // *********************************************** Składowanie zamówienia
- if(data_size >= buffer_puts){
- if(c == 'Z' + 1)
- c = 'a';
- else if(c == 'z' + 1)
- c = 'A';
- for(int i = 0; i < buffer_puts; i++)
- addtoBuffer(&pBuf, c); // Dodawanie wartości do bufora cyklicznego
- data_size -= buffer_puts;
- actualSize += buffer_puts;
- make_size += buffer_puts;
- c++;
- if(data_size == 0)
- break;
- }
- }
- else if(fds[i].fd == printClock){
- char buf[8];
- read(printClock, buf, 8);
- clock_gettime(CLOCK_REALTIME, &ts);
- long ts1 = ts.tv_sec;
- clock_gettime(CLOCK_MONOTONIC, &ts);
- long ts2 = ts.tv_sec;
- fd = fopen(r_path, "a");
- fprintf(fd, "1st clock: %ld\t 2nd clock: %ld\n", ts1, ts2);
- fprintf(fd, "Numer of clients: %d\n", currentsizeQueue(Q));
- fprintf(fd, "Actual size: %d of %d\n", actualSize, buffer_size);
- fprintf(fd, "Actual size in %% is %f\n", (double)actualSize/buffer_size*100);
- fprintf(fd, "Material flow: %d\n", make_size - sent_size);
- fclose(fd);
- make_size = 0;
- sent_size = 0;
- }
- else{
- close_connection = 0;
- tmp = recv(fds[i].fd, rec_buff, sizeof(rec_buff), 0);
- CheckRcv(tmp, &close_connection);
- addtoQueue(Q, fds[i].fd);
- if(close_connection){
- clock_gettime(CLOCK_REALTIME, &ts);
- long ts1 = ts.tv_sec;
- clock_gettime(CLOCK_MONOTONIC, &ts);
- long ts2 = ts.tv_sec;
- fd = fopen(r_path, "a");
- fprintf(fd, "After disconnection:\t1st clock: %ld\t2nd clock:%ld\tClient IP: %s\n", ts1, ts2, clientIP);
- fclose(fd);
- close(fds[i].fd);
- fds[i].fd = -1;
- }
- }
- }
- }while(end_server == 0);
- for(int i = 0; i < nfds; i++){
- if(fds[i].fd >= 0)
- close(fds[i].fd);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement