Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <getopt.h>
- #include <errno.h>
- #include <string.h>
- #include <signal.h>
- #include <time.h>
- void signal_handler(int signum, siginfo_t *info, void *data);
- int conf_open;
- volatile int isRhytmic = 0;
- volatile int isAsynchronic = 0;
- int new_fd;
- char rhytmCharacter = 'A';
- char new_rhytmCharacter;
- int fd;
- char* fifoPath[];
- struct stat status; //struktura do sprawdzenia czy plik jest FIFO
- struct sigaction sa;
- float rate = -1;
- int findNewRhytm = 0;
- int main(int argc, char* argv[]) {
- int c;
- char* end;
- char Buf[3];
- int option_index = 0;
- static struct option long_options[] ={
- {"delay", required_argument, 0, 'd'},
- {0 , 0 , 0 , 0}
- };
- while((c = getopt_long(argc, argv, "d:", long_options, &option_index)) != -1)
- {
- switch (c)
- {
- case 'd':
- if((rate = strtof( optarg, & end )) == 0.0F)
- {
- fprintf(stderr, "Wrong argument type\n");
- exit(EXIT_FAILURE);
- }
- printf("%f\n",rate );
- break;
- case '?':
- if (optopt == 'd')
- fprintf (stderr, "Option -%c requires an argument.\n", optopt);
- else if (isprint (optopt))
- fprintf (stderr, "Unknown option `-%c'.\n", optopt);
- else
- fprintf (stderr,
- "Unknown option character.\n");
- exit(EXIT_FAILURE);
- default:
- printf("Usage: rytmika -d <float> <fifo_path>\n");
- exit(EXIT_FAILURE);
- }
- }
- if(rate == -1)
- {
- printf("Usage: rytmika -d or --delay <float> <fifo_path>\n");
- exit(EXIT_FAILURE);
- }
- if(optind >= argc)
- {
- fprintf (stderr, "Expected argument after options\n");
- exit(EXIT_FAILURE);
- }
- int fSize = 0;
- while(optind < argc)
- {
- fifoPath[fSize++] = argv[optind++];
- }
- for(int k = 0 ; k < fSize; k++)
- printf("%s\n", fifoPath[k]);
- if((conf_open = open("rytm.conf", O_RDWR)) < 0)
- {
- perror("open conf file");
- return 1;
- }
- char noise = '.';
- memset (&sa, 0, sizeof (sa));
- sa.sa_sigaction = &signal_handler;
- sa.sa_flags = SA_SIGINFO;
- if (sigaction (SIGUSR1, &sa, NULL) == -1) {
- perror("Error: cannot handle SIGUSR1");
- }
- if (sigaction (SIGUSR2, &sa, NULL) == -1) {
- perror("Error: cannot handle SIGUSR2");
- }
- //printf("%c", znak);
- //otwarcie udaje sie nawet jesli nikt nie otworzyl FIFO po stronie czytajacej
- //powrot wystepuje z bledem ENXIO jesli nie ma dostepnych czytnikow
- //w blokujacym trybie open() blokuje do czasu otwarcia FIFO do czytania
- while(1) {
- for (int i = 0; i < fSize; i++) {
- fd = open(fifoPath[i], O_WRONLY | O_NONBLOCK);
- if (fd == -1 && errno == ENXIO) {
- printf("%s nie jest otwarte do czytania\n", fifoPath[i]);
- close(fd);
- //return 1;
- //tutaj nie bede wychodzil calkiem z programu tylko szukal nastepnego dostepnego FIFO do pisania
- } else if (fd == -1) {
- perror("open fifo");
- return 1;
- }
- fstat(fd, &status);
- if (!S_ISFIFO(status.st_mode)) //sprawdzam czy FIFO, zwraca true jesli gdzies jest otwarte FIFO
- {
- printf("%s is not a fifo!\n", fifoPath[i]);
- close(fd);
- //exit(EXIT_FAILURE);
- //tez nie moge konczyc programu
- }
- else
- {
- printf("plik %s jest FIFO\n", fifoPath[i]);
- while(1)
- {
- if(isAsynchronic)
- {
- char znak2;
- if(!findNewRhytm)
- {
- do {
- read(new_fd, &znak2, 1);
- } while (znak2 != '\n');
- findNewRhytm = 1;
- printf("Znalazlem nowy rytm!");
- }
- if(!read(new_fd, &znak2, 1))
- {
- printf("Koniec pliku\n");
- if( !lseek(conf_open, 0, 0))
- printf("Przesunalem sie");
- }
- if (znak2 == '*')
- {
- znak2 = new_rhytmCharacter;
- if(write(fd, &znak2, 1) == -1)
- {
- isAsynchronic = !isAsynchronic;
- break;
- }
- }
- else if (znak2 == '.')
- {
- sleep((unsigned int)rate);
- }
- else if(znak2 == '\n')
- {
- close(new_fd);
- isAsynchronic = !isAsynchronic;
- new_rhytmCharacter++;
- }
- //sleep((unsigned int)rate);
- }
- if(isRhytmic)
- {
- char znak;
- if(!read(conf_open, &znak, 1))
- {
- printf("Koniec pliku\n");
- if( !lseek(conf_open, 0, 0))
- printf("Przesunalem sie");
- }
- if (znak == '*')
- {
- znak = rhytmCharacter;
- if(write(fd, &znak, 1) == -1)
- {
- isRhytmic = !isRhytmic;
- break;
- }
- //write(fd, &znak, 1);
- //if (read(fd, &znak, 0) == -1 && errno == EINTR) break;
- // int zwracam = read(fd, &znak, 1);
- // printf("%d\n", zwracam);
- // printf("%s\n", strerror(errno));
- }
- else if (znak == '.')
- {
- sleep((unsigned int)rate);
- }
- else if(znak == '\n')
- {
- isRhytmic = !isRhytmic;
- rhytmCharacter++;
- }
- }
- else if (write(fd, &noise, 1) == -1) {
- printf("%s\n", strerror(errno));
- break;
- }
- sleep((unsigned int)rate);
- }
- }
- sleep(2);
- }
- }
- // while(1)
- // {
- //while (read(conf_open, buff, 1))
- // //read(conf_open, buff, 1);
- // write(fd,buff,1);
- //}
- // write(fd, "Hi", sizeof("Hi"));
- //close(fd);
- return 0;
- }
- void signal_handler(int signum, siginfo_t *info, void *data)
- {
- if(info->si_signo == SIGUSR1)
- {
- //clock_gettime(CLOCK_MONOTONIC, &req);
- //req.tv_sec = 3;
- //req.tv_nsec = 500000000;
- //nanosleep(&req, NULL);
- //clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &req, NULL);
- isRhytmic = !isRhytmic;
- }
- else if(info->si_signo == SIGUSR2)
- {
- isAsynchronic = !isAsynchronic;
- new_fd = dup(conf_open);
- new_rhytmCharacter = rhytmCharacter + 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment