Guest User

Untitled

a guest
Jan 9th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.34 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <unistd.h>
  8. #include <getopt.h>
  9. #include <errno.h>
  10. #include <string.h>
  11. #include <signal.h>
  12. #include <time.h>
  13.  
  14. void signal_handler(int signum, siginfo_t *info, void *data);
  15. int conf_open;
  16. volatile int isRhytmic = 0;
  17. volatile int isAsynchronic = 0;
  18. int new_fd;
  19. char rhytmCharacter = 'A';
  20. char new_rhytmCharacter;
  21. int fd;
  22. char* fifoPath[];
  23. struct stat status; //struktura do sprawdzenia czy plik jest FIFO
  24.  
  25. struct sigaction sa;
  26. float rate = -1;
  27. int findNewRhytm = 0;
  28.  
  29. int main(int argc, char* argv[]) {
  30.  
  31. int c;
  32. char* end;
  33. char Buf[3];
  34.  
  35. int option_index = 0;
  36. static struct option long_options[] ={
  37. {"delay", required_argument, 0, 'd'},
  38. {0 , 0 , 0 , 0}
  39. };
  40.  
  41.  
  42. while((c = getopt_long(argc, argv, "d:", long_options, &option_index)) != -1)
  43. {
  44. switch (c)
  45. {
  46. case 'd':
  47. if((rate = strtof( optarg, & end )) == 0.0F)
  48. {
  49. fprintf(stderr, "Wrong argument type\n");
  50. exit(EXIT_FAILURE);
  51. }
  52. printf("%f\n",rate );
  53. break;
  54. case '?':
  55. if (optopt == 'd')
  56. fprintf (stderr, "Option -%c requires an argument.\n", optopt);
  57. else if (isprint (optopt))
  58. fprintf (stderr, "Unknown option `-%c'.\n", optopt);
  59. else
  60. fprintf (stderr,
  61. "Unknown option character.\n");
  62. exit(EXIT_FAILURE);
  63. default:
  64. printf("Usage: rytmika -d <float> <fifo_path>\n");
  65. exit(EXIT_FAILURE);
  66. }
  67.  
  68. }
  69.  
  70. if(rate == -1)
  71. {
  72. printf("Usage: rytmika -d or --delay <float> <fifo_path>\n");
  73. exit(EXIT_FAILURE);
  74. }
  75.  
  76. if(optind >= argc)
  77. {
  78. fprintf (stderr, "Expected argument after options\n");
  79. exit(EXIT_FAILURE);
  80. }
  81.  
  82. int fSize = 0;
  83. while(optind < argc)
  84. {
  85. fifoPath[fSize++] = argv[optind++];
  86. }
  87.  
  88. for(int k = 0 ; k < fSize; k++)
  89. printf("%s\n", fifoPath[k]);
  90.  
  91.  
  92.  
  93. if((conf_open = open("rytm.conf", O_RDWR)) < 0)
  94. {
  95. perror("open conf file");
  96. return 1;
  97. }
  98.  
  99.  
  100. char noise = '.';
  101.  
  102.  
  103. memset (&sa, 0, sizeof (sa));
  104. sa.sa_sigaction = &signal_handler;
  105. sa.sa_flags = SA_SIGINFO;
  106.  
  107. if (sigaction (SIGUSR1, &sa, NULL) == -1) {
  108. perror("Error: cannot handle SIGUSR1");
  109. }
  110.  
  111. if (sigaction (SIGUSR2, &sa, NULL) == -1) {
  112. perror("Error: cannot handle SIGUSR2");
  113. }
  114.  
  115.  
  116. //printf("%c", znak);
  117.  
  118. //otwarcie udaje sie nawet jesli nikt nie otworzyl FIFO po stronie czytajacej
  119. //powrot wystepuje z bledem ENXIO jesli nie ma dostepnych czytnikow
  120. //w blokujacym trybie open() blokuje do czasu otwarcia FIFO do czytania
  121. while(1) {
  122. for (int i = 0; i < fSize; i++) {
  123.  
  124. fd = open(fifoPath[i], O_WRONLY | O_NONBLOCK);
  125. if (fd == -1 && errno == ENXIO) {
  126.  
  127. printf("%s nie jest otwarte do czytania\n", fifoPath[i]);
  128. close(fd);
  129. //return 1;
  130. //tutaj nie bede wychodzil calkiem z programu tylko szukal nastepnego dostepnego FIFO do pisania
  131. } else if (fd == -1) {
  132. perror("open fifo");
  133. return 1;
  134. }
  135.  
  136. fstat(fd, &status);
  137. if (!S_ISFIFO(status.st_mode)) //sprawdzam czy FIFO, zwraca true jesli gdzies jest otwarte FIFO
  138. {
  139. printf("%s is not a fifo!\n", fifoPath[i]);
  140. close(fd);
  141. //exit(EXIT_FAILURE);
  142. //tez nie moge konczyc programu
  143. }
  144. else
  145. {
  146. printf("plik %s jest FIFO\n", fifoPath[i]);
  147.  
  148.  
  149. while(1)
  150. {
  151. if(isAsynchronic)
  152. {
  153. char znak2;
  154. if(!findNewRhytm)
  155. {
  156. do {
  157. read(new_fd, &znak2, 1);
  158. } while (znak2 != '\n');
  159. findNewRhytm = 1;
  160. printf("Znalazlem nowy rytm!");
  161. }
  162. if(!read(new_fd, &znak2, 1))
  163. {
  164. printf("Koniec pliku\n");
  165. if( !lseek(conf_open, 0, 0))
  166. printf("Przesunalem sie");
  167. }
  168. if (znak2 == '*')
  169. {
  170. znak2 = new_rhytmCharacter;
  171. if(write(fd, &znak2, 1) == -1)
  172. {
  173. isAsynchronic = !isAsynchronic;
  174. break;
  175. }
  176. }
  177. else if (znak2 == '.')
  178. {
  179. sleep((unsigned int)rate);
  180. }
  181. else if(znak2 == '\n')
  182. {
  183. close(new_fd);
  184. isAsynchronic = !isAsynchronic;
  185. new_rhytmCharacter++;
  186. }
  187. //sleep((unsigned int)rate);
  188. }
  189.  
  190. if(isRhytmic)
  191. {
  192. char znak;
  193.  
  194. if(!read(conf_open, &znak, 1))
  195. {
  196. printf("Koniec pliku\n");
  197. if( !lseek(conf_open, 0, 0))
  198. printf("Przesunalem sie");
  199. }
  200. if (znak == '*')
  201. {
  202. znak = rhytmCharacter;
  203. if(write(fd, &znak, 1) == -1)
  204. {
  205. isRhytmic = !isRhytmic;
  206. break;
  207. }
  208. //write(fd, &znak, 1);
  209. //if (read(fd, &znak, 0) == -1 && errno == EINTR) break;
  210. // int zwracam = read(fd, &znak, 1);
  211. // printf("%d\n", zwracam);
  212. // printf("%s\n", strerror(errno));
  213. }
  214. else if (znak == '.')
  215. {
  216. sleep((unsigned int)rate);
  217. }
  218. else if(znak == '\n')
  219. {
  220. isRhytmic = !isRhytmic;
  221. rhytmCharacter++;
  222. }
  223.  
  224. }
  225. else if (write(fd, &noise, 1) == -1) {
  226. printf("%s\n", strerror(errno));
  227. break;
  228. }
  229. sleep((unsigned int)rate);
  230. }
  231.  
  232.  
  233.  
  234. }
  235. sleep(2);
  236. }
  237. }
  238.  
  239. // while(1)
  240. // {
  241. //while (read(conf_open, buff, 1))
  242. // //read(conf_open, buff, 1);
  243. // write(fd,buff,1);
  244. //}
  245. // write(fd, "Hi", sizeof("Hi"));
  246. //close(fd);
  247.  
  248.  
  249.  
  250. return 0;
  251. }
  252.  
  253.  
  254. void signal_handler(int signum, siginfo_t *info, void *data)
  255. {
  256.  
  257. if(info->si_signo == SIGUSR1)
  258. {
  259. //clock_gettime(CLOCK_MONOTONIC, &req);
  260. //req.tv_sec = 3;
  261. //req.tv_nsec = 500000000;
  262. //nanosleep(&req, NULL);
  263. //clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &req, NULL);
  264. isRhytmic = !isRhytmic;
  265.  
  266. }
  267.  
  268. else if(info->si_signo == SIGUSR2)
  269. {
  270. isAsynchronic = !isAsynchronic;
  271. new_fd = dup(conf_open);
  272. new_rhytmCharacter = rhytmCharacter + 1;
  273. }
  274.  
  275. }
Advertisement
Add Comment
Please, Sign In to add comment