Advertisement
Guest User

Untitled

a guest
Jun 8th, 2015
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.70 KB | None | 0 0
  1. #include <sys/socket.h>
  2. #include <sys/types.h>
  3. #include <netinet/in.h>
  4. #include <netdb.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <errno.h>
  9. #include <arpa/inet.h>
  10. #include <fcntl.h>
  11. #include <sys/stat.h>
  12. #include <ncurses.h> // Includes stdio.h unctrl.h stdarg.h stddef.h
  13. #include <semaphore.h>
  14. #include <sys/wait.h>
  15. #include <sched.h>
  16. void start_server(void);
  17. void draw_barbershop(void);
  18. void recv_connection(char* msg);
  19. void send_message(char* msg, int clientfd);
  20. void center(int row, char *title);
  21. void randwait(int secs);
  22. void console_write(char* message);
  23. void console_writef(char* message, va_list lst);
  24. void wrapper_console_writef(char* message,int first, ...);
  25. void move_client(void);
  26. void redraw_chairs(void);
  27. void redraw_clients_outside(void);
  28. void move_client_to_barber(void);
  29. void sit_client_barber(void);
  30. void exit_client(void);
  31. void barber_console_write(char* message);
  32. void move_client_to_chairs(void);
  33.  
  34. #define xluc 5 //X left up corner
  35. #define yluc 2 //Y left up corner
  36.  
  37. int width =40;
  38. int height =10; // width and height of the barbershop
  39. int sitting_client_count=0;
  40. int waiting_client_count=0;
  41. int barber_console_msg_count;
  42. int chair_number;
  43. int console_msg_count;
  44.  
  45. sem_t *console_control;
  46.  
  47. int main(void) {
  48.     console_msg_count=0;
  49.     barber_console_msg_count=0;
  50.     sem_unlink("/cctrl");
  51.     if((console_control=sem_open("/cctrl",O_CREAT,0655,1))==(sem_t *)-1)
  52.     {
  53.         perror("No se puede crear el semáforo");
  54.         exit(-1);
  55.     }
  56.     printf("Ingresa el número de sillas : ");
  57.     scanf("%d",&chair_number);
  58.     draw_barbershop();
  59.     start_server();
  60.    
  61.    
  62.     getch();
  63.  
  64.     endwin();
  65.     return 0;
  66. }
  67.  
  68. void start_server(void){
  69.     /*
  70.      * Starts server and puts it on listener state
  71.      */
  72.      
  73.      /** Server configuration **/
  74.      int connection=0,result=0,connection_client=0;
  75.      int recvs, sends;
  76.      long RandSeed;
  77.      char petition[1024];
  78.      
  79.      struct sockaddr_in own_dir, remote_dir;
  80.      socklen_t remote_dir_size;
  81.      if((connection=socket(AF_INET,SOCK_STREAM,0))<0){
  82.         //printf("Could not create socket");
  83.         exit(-1);
  84.     }
  85.      memset(&own_dir,0,sizeof(struct sockaddr_in));
  86.      own_dir.sin_family=AF_INET;
  87.      own_dir.sin_addr.s_addr=htonl(INADDR_ANY);
  88.      own_dir.sin_port=htons(5000);
  89.      
  90.      if((result=bind(connection,(struct sockaddr *)&own_dir,sizeof(own_dir)))<0){
  91.          //printf("Could not create socket");
  92.          exit(-1);
  93.      }
  94.      /** Semaphore configuration **/
  95.     sem_unlink("/wrx");
  96.     sem_unlink("/bcx");
  97.     sem_unlink("/bpx");
  98.     sem_unlink("/sbx");
  99.     sem_t *waitingRoom,*barberChair,*barberPillow,*seatBelt;
  100.     if((waitingRoom=sem_open("/wrx",O_CREAT,0655,chair_number))==(sem_t *)-1)
  101.     {
  102.         perror("No se puede crear el semáforo");
  103.         exit(-1);
  104.     }
  105.     if((barberChair=sem_open("/bcx",O_CREAT,0655,1))==(sem_t *)-1)
  106.     {
  107.         perror("No se puede crear el semáforo");
  108.         exit(-1);
  109.     }
  110.     if((barberPillow=sem_open("/bpx",O_CREAT,0655,0))==(sem_t *)-1)
  111.     {
  112.         perror("No se puede crear el semáforo");
  113.         exit(-1);
  114.     }
  115.     if((seatBelt=sem_open("/sbx",O_CREAT,0655,0))==(sem_t *)-1)
  116.     {
  117.         perror("No se puede crear el semáforo");
  118.         exit(-1);
  119.     }
  120.     //The client pid.
  121.     pid_t client;
  122.     int p;
  123.     RandSeed=8;
  124.      listen(connection,90);
  125.      while(1){
  126.          remote_dir_size = sizeof(remote_dir);
  127.         // console_write("Awaiting connection...");
  128.          if((connection_client=accept(connection,(struct sockaddr *)&remote_dir,&remote_dir_size))<0){
  129.              console_write("CONNECTION REJECTED!");
  130.              exit(-1);
  131.          }
  132.         // console_write("Connection accepted!");
  133.          client=fork();
  134.          switch(client)
  135.          {
  136.              case -1:
  137.                 console_write("Error Forking!!");
  138.                 exit(1);
  139.              case 0:
  140.                 close(connection); //So that another client can come.
  141.                 recvs = recv(connection_client,petition,sizeof(petition),0);
  142.                
  143.                  //console_write(petition);
  144.                 // console_write(" moviendose.");
  145.                  move_client();
  146.                
  147.                  //Checando si hay espacio
  148.                  //Espera afuera...
  149.                  //waiting_client_count++;
  150.                  sem_wait(waitingRoom); //Hasta que hay un lugar
  151.                  move_client_to_chairs();
  152.                  sitting_client_count++;
  153.                  
  154.                  //waiting_client_count--;
  155.                  //redraw_clients_outside(); //redibujamos los clientes esperando.
  156.                  redraw_chairs();  //redibujamos las sillas
  157.                  //Esperando al barbero.
  158.                  
  159.                 // console_write(petition);
  160.                 // console_write(" esperando al barbero...");
  161.                  
  162.                  sem_wait(barberChair);
  163.                  
  164.                  //El barbero está desocupado, liberamos la silla que ocupamos.
  165.                  sem_post(waitingRoom);
  166.                  
  167.                  sitting_client_count--;
  168.                  redraw_chairs();
  169.                  
  170.                  move_client_to_barber(); //Se mueve hacia el barbero
  171.                  sit_client_barber();
  172.                  //Levantamos al barbero
  173.                  sem_post(barberPillow);
  174.                  //console_write(" Barbero despierto!");
  175.                  //Esperamos a que el barbero termine de cortarnos
  176.                  //console_write(petition);
  177.                  //console_write(" siendo atentido.");
  178.                  sem_wait(seatBelt);
  179.                  //Nos paramos de la silla
  180.                  sem_post(barberChair);
  181.                  exit_client();
  182.                  //console_write(petition);
  183.                  //console_write(" servicio terminado.");
  184.                  exit(0);
  185.              default:
  186.                
  187.                 //duermo hasta que alguien me despierte
  188.                  
  189.                 //console_write("Barbero durmiendo");
  190.                  
  191.                 sem_wait(barberPillow);
  192.                 randwait(5);
  193.                 //console_write("Barbero cortando cabello");
  194.                 randwait(5);
  195.                 //console_write("Barbero terminó.");
  196.                 //Liberamos al cliente.
  197.                 sem_post(seatBelt);
  198.                 wait(&p);        
  199.          }
  200.     }
  201.     sem_close(waitingRoom);
  202.     sem_close(barberChair);
  203.     sem_close(barberPillow);
  204.     sem_close(seatBelt);
  205.     sem_close(console_control);
  206.     sem_unlink("/wrx");
  207.     sem_unlink("/bcx");
  208.     sem_unlink("/bpx");
  209.     sem_unlink("/sbx");
  210. }
  211. void exit_client(void) {
  212.     mvaddstr(xluc+3,yluc+width-8," ");
  213.     refresh();
  214. }
  215. void sit_client_barber(void){
  216.     mvaddstr(xluc+3,yluc+width-8,"P");
  217.     refresh();
  218. }
  219. void move_client_to_barber(void){
  220.     int x;
  221.     int y;
  222.     x=xluc+height-1;
  223.     y=yluc+6;
  224.     while(y<xluc+width-9){
  225.         mvaddch(x,y,'P');
  226.         refresh();
  227.         napms(150);
  228.         mvaddch(x,y,' ');
  229.         refresh();
  230.         y++;
  231.     }
  232.     int j=5;
  233.     x--;
  234.     while(j>1){
  235.         j--;
  236.         mvaddch(x,y,'P');
  237.         refresh();
  238.         napms(150);
  239.         mvaddch(x,y,' ');
  240.         refresh();
  241.         x--;
  242.     }
  243.     /*move(xluc+height-1,yluc+12);
  244.     addch('P');
  245.     refresh();
  246.     randwait(5);
  247.     move(xluc+height-1,yluc+12);
  248.     addch(' ');
  249.     refresh();
  250.     move(xluc+height-1,yluc+19);
  251.     addch('P');
  252.     refresh();
  253.     randwait(5);
  254.     move(xluc+height-1,yluc+19);
  255.     addch(' ');
  256.     refresh();
  257.     move(xluc+height-1,yluc+26);
  258.     addch('P');
  259.     refresh();
  260.     randwait(5);
  261.     move(xluc+height-1,yluc+26);
  262.     addch(' ');
  263.     refresh();
  264.     * */
  265. }
  266. void redraw_chairs(void){
  267.     //xluc+height,yluc+4+j
  268.     randwait(4);
  269.     move(xluc+height,yluc+4);
  270.     int i;
  271.     for(i=0;i<sitting_client_count;i++){
  272.         addch('P');
  273.     }
  274.     for(i=0;i<chair_number-sitting_client_count;i++){
  275.         addch('O');
  276.     }
  277.     refresh();
  278. }
  279.  
  280. void redraw_clients_outside(void){
  281.     if(waiting_client_count==0){
  282.     mvaddch(4,8,' ');
  283.     refresh();
  284. }
  285. }
  286. void draw_barbershop(void){
  287.     /*
  288.      * Draws the barbershop using ncurses.
  289.      * Draws the barber.
  290.      * Draws the chairs for the first time.
  291.      */
  292.  
  293.      initscr();
  294.      scrollok(stdscr,TRUE);
  295.      center(1,"Barbershop");
  296.      
  297.      mvaddstr(2,52,"===CONSOLE===");
  298.      move(5,52);
  299.    
  300.      //wrapper_console_writef("%d this is a test %d",a,b);
  301.      //Door stars here [(8,15),(8,19)]
  302.  
  303.  
  304.      move(xluc,yluc);
  305.      int j;
  306.      for(j=0;j<width;j++){
  307.          if(j>4 && j<10) addch(' ');
  308.          else addch('=');
  309.      }
  310.      move(xluc+height+1,yluc);
  311.      for(j=0;j<width;j++){
  312.          addch('=');
  313.      }
  314.      
  315.      for(j=0;j<height;j++)
  316.      mvaddstr(xluc+1+j,yluc,"||");
  317.      
  318.      
  319.      for(j=0;j<height;j++)
  320.      mvaddstr(xluc+1+j,(width+yluc-2),"||");
  321.      
  322.      for(j=0;j<height-2;j++)
  323.      mvaddstr(xluc+1+j,(yluc+width-15),"}");
  324.      
  325.      //This is the chair
  326.      //mvaddstr(xluc+3,(yluc+width-10),"{{");
  327.      mvaddstr(xluc+4,(yluc+width-10),"{{");
  328.      addstr("__");
  329.      //This is the barber
  330.      mvaddstr(xluc+3,(yluc+width-11),"B");
  331.      mvaddstr(xluc+4,(yluc+width-11),"|");
  332.      //Head of client in chair position will be xluc+3,yluc+width-7
  333.      //mvaddstr(xluc+3,yluc+width-7,"P");
  334.      //mvaddstr(xluc+4,yluc+width-7,"|");
  335.      
  336.      //These are the chairs.
  337.      for(j=0;j<chair_number;j++)
  338.      mvaddstr(xluc+height,yluc+4+j,"O");
  339.      refresh();
  340.      
  341. }
  342. void move_client(void){
  343.     int x,y;
  344.     y=4;
  345.     x=2;
  346.     while(x<8) {
  347.         mvaddch(y,x,'P');
  348.         refresh();
  349.         napms(150);
  350.         mvaddch(y,x,' ');
  351.         refresh();
  352.         x=x+1;
  353.     }
  354.     mvaddch(y,x,'P');
  355.     refresh();
  356.     /*
  357.     mvaddch(4,2,'P');
  358.     refresh();
  359.     randwait(10);
  360.     mvaddch(4,2,' ');
  361.     mvaddch(4,5,'P');
  362.     refresh();
  363.     randwait(10);
  364.     mvaddch(4,5,' ');
  365.     mvaddch(4,8,'P');
  366.     refresh();
  367.     * */
  368. }
  369. void move_client_to_chairs(void){
  370.     int y=4;
  371.     int x=8;
  372.     mvaddch(y,x,' ');
  373.     while(y<15){
  374.         mvaddch(y,x,'P');
  375.         refresh();
  376.         napms(150);
  377.         mvaddch(y,x,' ');
  378.         refresh();
  379.         y++;
  380.     }
  381. }
  382. void console_write(char* message){
  383.     //sem_wait(console_control);
  384.     //int x;
  385.     /*if(console_msg_count>=20){
  386.         //move(3,45);
  387.         for(x=0;x<20;x++){
  388.             mvaddstr(3+x,45,"                    ");
  389.        
  390.         }
  391.         console_msg_count=0;
  392.     }*/
  393.    
  394.     mvprintw(3+console_msg_count,45,"%s",message);
  395.     console_msg_count++;
  396.     refresh();
  397.     //sem_post(console_control);
  398. }
  399. void barber_console_write(char* message){
  400.     int x;
  401.         if(barber_console_msg_count>=20){
  402.         //move(6+height,5);
  403.         for(x=0;x<20;x++){
  404.             mvaddstr(10+x+height,5,"                    ");
  405.        
  406.         }
  407.         barber_console_msg_count=0;
  408.     }
  409.     mvprintw(7+barber_console_msg_count+height,5,"%s",message);
  410.     barber_console_msg_count++;
  411.     refresh();
  412. }
  413.  
  414. void center(int row, char *title)
  415. {
  416.     /*
  417.      * Paints a centered title in given row of the console
  418.      */
  419.     int len, indent,y,width;
  420.    
  421.     getmaxyx(stdscr,y,width);
  422.     len=strlen(title);
  423.     indent = width- len;
  424.     indent /=2;
  425.     mvprintw(99,99,"%d",y);
  426.     mvaddstr(row,indent,title);
  427.     refresh();
  428. }
  429.  
  430. void randwait(int secs) {
  431.     int len;
  432.    
  433.     // Generate a random number...
  434.     len = (int) ((drand48() * secs) + 1);
  435.     sleep(len);
  436. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement