Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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)
- {
- memset(fds, 0, sizeof(&fds));
- fds[0].fd = *socket_fd;
- fds[0].events = POLLIN;
- *clock1 = timerfd_create(CLOCK_REALTIME, 0);
- if(*clock1 == -1){
- printf("Problem with clock.\n");
- exit(1);
- }
- value_to_add.it_value.tv_sec = (int)speed;
- value_to_add.it_value.tv_nsec = (speed - value_to_add.it_value.tv_sec) * 1000000000;
- value_to_add.it_interval.tv_sec = (int)speed;
- value_to_add.it_interval.tv_nsec = (speed - value_to_add.it_interval.tv_sec) * 1000000000;
- if(timerfd_settime(*clock1, 0, &value_to_add, NULL) == -1){
- printf("Problem with timerfd_settime - createClock\n");
- exit(1);
- }
- fds[1].fd = *clock1;
- fds[1].events = POLLIN;
- *clock2 = timerfd_create(CLOCK_REALTIME, 0);
- if(*clock2 == -1){
- printf("Problem with clock.\n");
- exit(1);
- }
- value_to_print.it_value.tv_sec = 5;
- value_to_print.it_value.tv_nsec = 0;
- value_to_print.it_interval.tv_sec = 5;
- value_to_print.it_interval.tv_nsec = 0;
- if(timerfd_settime(*clock2, 0, &value_to_print, NULL) == -1){
- printf("Problem with timerfd_settime - printClock\n");
- exit(1);
- }
- fds[2].fd = *clock2;
- fds[2].events = POLLIN;
- }
- int main(int argc, char* argv[])
- {
- double speed = 0;
- long port = 0;
- char address[20]="localhost";
- char* r_path = NULL;
- r_path = Parameters(argc, argv, &speed, address, &port);
- speed = speed * 60 / (96*100);
- struct buffer pBuf;
- createBuffer(&pBuf);
- struct queue* Q = createQueue(10000);
- int tmp, on = 1, socket_fd = -1, nsd = 1, end_server = 0, close_connection;
- char bufferX[buffer_gets], clientIP[25], rec_buff[5], server[25], c = 'a';
- long ts1 = 0, ts2 = 0;
- struct sockaddr_in addressX;
- struct sockaddr_in *clientX = (struct sockaddr_in*)&clientIP;
- int actualSize = 0, timeout, nfds = 3, current_size = 0, sent_size = 0, make_size = 0, data_size = buffer_size;
- struct pollfd fds[200];
- hostname_to_ip(address, server); // Zmiana z nazwy domeny na adres IP.
- sock_setsock_ioctl(&socket_fd, &on);
- memset(&addressX, 0, sizeof(addressX));
- addressX.sin_family = AF_INET;
- addressX.sin_addr.s_addr = inet_addr(server);
- addressX.sin_port = htons(port);
- if(bind(socket_fd, (struct sockaddr*)&addressX, sizeof(addressX)) < 0){
- printf("Problem with bind...\n");
- exit(-1);
- }
- if(listen(socket_fd, 32) < 0){
- printf("Problem with listen...\n");
- exit(-1);
- }
- int createClock, printClock;
- struct timespec ts;
- void setClocks(value_to_add, value_to_print, ts, socket_fd, fds, &createClock, &printClock, speed);
- /*
- struct timespec ts;
- memset(fds, 0, sizeof(fds));
- fds[0].fd = socket_fd;
- fds[0].events = POLLIN;
- int createClock = timerfd_create(CLOCK_REALTIME, 0);
- if(createClock == -1){
- printf("Problem with clock.\n");
- exit(1);
- }
- value_to_add.it_value.tv_sec = (int)speed;
- value_to_add.it_value.tv_nsec = (speed - value_to_add.it_value.tv_sec) * 1000000000;
- value_to_add.it_interval.tv_sec = (int)speed;
- value_to_add.it_interval.tv_nsec = (speed - value_to_add.it_interval.tv_sec) * 1000000000;
- if(timerfd_settime(createClock, 0, &value_to_add, NULL) == -1){
- printf("Problem with timerfd_settime - createClock\n");
- exit(1);
- }
- fds[1].fd = createClock;
- fds[1].events = POLLIN;
- int printClock = timerfd_create(CLOCK_REALTIME, 0);
- if(printClock == -1){
- printf("Problem with clock.\n");
- exit(1);
- }
- value_to_print.it_value.tv_sec = 5;
- value_to_print.it_value.tv_nsec = 0;
- value_to_print.it_interval.tv_sec = 5;
- value_to_print.it_interval.tv_nsec = 0;
- if(timerfd_settime(printClock, 0, &value_to_print, NULL) == -1){
- printf("Problem with timerfd_settime - printClock\n");
- exit(1);
- }
- fds[2].fd = printClock;
- fds[2].events = POLLIN;
- */
- timeout = (30000);
- 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);
- gettime(ts, &ts1, &ts2);
- 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
- //printf("%c", c);
- }
- 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);
- gettime(ts, &ts1, &ts2);
- printingFun(ts1, ts2, actualSize, buffer_size, make_size, sent_size, Q, r_path);
- 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){
- gettime(ts, &ts1, &ts2);
- 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);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement