Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <string.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <errno.h>
- #include <arpa/inet.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <ncurses.h> // Includes stdio.h unctrl.h stdarg.h stddef.h
- #include <semaphore.h>
- #include <sys/wait.h>
- #include <sched.h>
- void start_server(void);
- void draw_barbershop(void);
- void recv_connection(char* msg);
- void send_message(char* msg, int clientfd);
- void center(int row, char *title);
- void randwait(int secs);
- void console_write(char* message);
- void console_writef(char* message, va_list lst);
- void wrapper_console_writef(char* message,int first, ...);
- void move_client(void);
- void redraw_chairs(void);
- void redraw_clients_outside(void);
- void move_client_to_barber(void);
- void sit_client_barber(void);
- void exit_client(void);
- void barber_console_write(char* message);
- void move_client_to_chairs(void);
- #define xluc 5 //X left up corner
- #define yluc 2 //Y left up corner
- int width =40;
- int height =10; // width and height of the barbershop
- int sitting_client_count=0;
- int waiting_client_count=0;
- int barber_console_msg_count;
- int chair_number;
- int console_msg_count;
- sem_t *console_control;
- int main(void) {
- console_msg_count=0;
- barber_console_msg_count=0;
- sem_unlink("/cctrl");
- if((console_control=sem_open("/cctrl",O_CREAT,0655,1))==(sem_t *)-1)
- {
- perror("No se puede crear el semáforo");
- exit(-1);
- }
- printf("Ingresa el número de sillas : ");
- scanf("%d",&chair_number);
- draw_barbershop();
- start_server();
- getch();
- endwin();
- return 0;
- }
- void start_server(void){
- /*
- * Starts server and puts it on listener state
- */
- /** Server configuration **/
- int connection=0,result=0,connection_client=0;
- int recvs, sends;
- long RandSeed;
- char petition[1024];
- struct sockaddr_in own_dir, remote_dir;
- socklen_t remote_dir_size;
- if((connection=socket(AF_INET,SOCK_STREAM,0))<0){
- //printf("Could not create socket");
- exit(-1);
- }
- memset(&own_dir,0,sizeof(struct sockaddr_in));
- own_dir.sin_family=AF_INET;
- own_dir.sin_addr.s_addr=htonl(INADDR_ANY);
- own_dir.sin_port=htons(5000);
- if((result=bind(connection,(struct sockaddr *)&own_dir,sizeof(own_dir)))<0){
- //printf("Could not create socket");
- exit(-1);
- }
- /** Semaphore configuration **/
- sem_unlink("/wrx");
- sem_unlink("/bcx");
- sem_unlink("/bpx");
- sem_unlink("/sbx");
- sem_t *waitingRoom,*barberChair,*barberPillow,*seatBelt;
- if((waitingRoom=sem_open("/wrx",O_CREAT,0655,chair_number))==(sem_t *)-1)
- {
- perror("No se puede crear el semáforo");
- exit(-1);
- }
- if((barberChair=sem_open("/bcx",O_CREAT,0655,1))==(sem_t *)-1)
- {
- perror("No se puede crear el semáforo");
- exit(-1);
- }
- if((barberPillow=sem_open("/bpx",O_CREAT,0655,0))==(sem_t *)-1)
- {
- perror("No se puede crear el semáforo");
- exit(-1);
- }
- if((seatBelt=sem_open("/sbx",O_CREAT,0655,0))==(sem_t *)-1)
- {
- perror("No se puede crear el semáforo");
- exit(-1);
- }
- //The client pid.
- pid_t client;
- int p;
- RandSeed=8;
- listen(connection,90);
- while(1){
- remote_dir_size = sizeof(remote_dir);
- // console_write("Awaiting connection...");
- if((connection_client=accept(connection,(struct sockaddr *)&remote_dir,&remote_dir_size))<0){
- console_write("CONNECTION REJECTED!");
- exit(-1);
- }
- // console_write("Connection accepted!");
- client=fork();
- switch(client)
- {
- case -1:
- console_write("Error Forking!!");
- exit(1);
- case 0:
- close(connection); //So that another client can come.
- recvs = recv(connection_client,petition,sizeof(petition),0);
- //console_write(petition);
- // console_write(" moviendose.");
- move_client();
- //Checando si hay espacio
- //Espera afuera...
- //waiting_client_count++;
- sem_wait(waitingRoom); //Hasta que hay un lugar
- move_client_to_chairs();
- sitting_client_count++;
- //waiting_client_count--;
- //redraw_clients_outside(); //redibujamos los clientes esperando.
- redraw_chairs(); //redibujamos las sillas
- //Esperando al barbero.
- // console_write(petition);
- // console_write(" esperando al barbero...");
- sem_wait(barberChair);
- //El barbero está desocupado, liberamos la silla que ocupamos.
- sem_post(waitingRoom);
- sitting_client_count--;
- redraw_chairs();
- move_client_to_barber(); //Se mueve hacia el barbero
- sit_client_barber();
- //Levantamos al barbero
- sem_post(barberPillow);
- //console_write(" Barbero despierto!");
- //Esperamos a que el barbero termine de cortarnos
- //console_write(petition);
- //console_write(" siendo atentido.");
- sem_wait(seatBelt);
- //Nos paramos de la silla
- sem_post(barberChair);
- exit_client();
- //console_write(petition);
- //console_write(" servicio terminado.");
- exit(0);
- default:
- //duermo hasta que alguien me despierte
- //console_write("Barbero durmiendo");
- sem_wait(barberPillow);
- randwait(5);
- //console_write("Barbero cortando cabello");
- randwait(5);
- //console_write("Barbero terminó.");
- //Liberamos al cliente.
- sem_post(seatBelt);
- wait(&p);
- }
- }
- sem_close(waitingRoom);
- sem_close(barberChair);
- sem_close(barberPillow);
- sem_close(seatBelt);
- sem_close(console_control);
- sem_unlink("/wrx");
- sem_unlink("/bcx");
- sem_unlink("/bpx");
- sem_unlink("/sbx");
- }
- void exit_client(void) {
- mvaddstr(xluc+3,yluc+width-8," ");
- refresh();
- }
- void sit_client_barber(void){
- mvaddstr(xluc+3,yluc+width-8,"P");
- refresh();
- }
- void move_client_to_barber(void){
- int x;
- int y;
- x=xluc+height-1;
- y=yluc+6;
- while(y<xluc+width-9){
- mvaddch(x,y,'P');
- refresh();
- napms(150);
- mvaddch(x,y,' ');
- refresh();
- y++;
- }
- int j=5;
- x--;
- while(j>1){
- j--;
- mvaddch(x,y,'P');
- refresh();
- napms(150);
- mvaddch(x,y,' ');
- refresh();
- x--;
- }
- /*move(xluc+height-1,yluc+12);
- addch('P');
- refresh();
- randwait(5);
- move(xluc+height-1,yluc+12);
- addch(' ');
- refresh();
- move(xluc+height-1,yluc+19);
- addch('P');
- refresh();
- randwait(5);
- move(xluc+height-1,yluc+19);
- addch(' ');
- refresh();
- move(xluc+height-1,yluc+26);
- addch('P');
- refresh();
- randwait(5);
- move(xluc+height-1,yluc+26);
- addch(' ');
- refresh();
- * */
- }
- void redraw_chairs(void){
- //xluc+height,yluc+4+j
- randwait(4);
- move(xluc+height,yluc+4);
- int i;
- for(i=0;i<sitting_client_count;i++){
- addch('P');
- }
- for(i=0;i<chair_number-sitting_client_count;i++){
- addch('O');
- }
- refresh();
- }
- void redraw_clients_outside(void){
- if(waiting_client_count==0){
- mvaddch(4,8,' ');
- refresh();
- }
- }
- void draw_barbershop(void){
- /*
- * Draws the barbershop using ncurses.
- * Draws the barber.
- * Draws the chairs for the first time.
- */
- initscr();
- scrollok(stdscr,TRUE);
- center(1,"Barbershop");
- mvaddstr(2,52,"===CONSOLE===");
- move(5,52);
- //wrapper_console_writef("%d this is a test %d",a,b);
- //Door stars here [(8,15),(8,19)]
- move(xluc,yluc);
- int j;
- for(j=0;j<width;j++){
- if(j>4 && j<10) addch(' ');
- else addch('=');
- }
- move(xluc+height+1,yluc);
- for(j=0;j<width;j++){
- addch('=');
- }
- for(j=0;j<height;j++)
- mvaddstr(xluc+1+j,yluc,"||");
- for(j=0;j<height;j++)
- mvaddstr(xluc+1+j,(width+yluc-2),"||");
- for(j=0;j<height-2;j++)
- mvaddstr(xluc+1+j,(yluc+width-15),"}");
- //This is the chair
- //mvaddstr(xluc+3,(yluc+width-10),"{{");
- mvaddstr(xluc+4,(yluc+width-10),"{{");
- addstr("__");
- //This is the barber
- mvaddstr(xluc+3,(yluc+width-11),"B");
- mvaddstr(xluc+4,(yluc+width-11),"|");
- //Head of client in chair position will be xluc+3,yluc+width-7
- //mvaddstr(xluc+3,yluc+width-7,"P");
- //mvaddstr(xluc+4,yluc+width-7,"|");
- //These are the chairs.
- for(j=0;j<chair_number;j++)
- mvaddstr(xluc+height,yluc+4+j,"O");
- refresh();
- }
- void move_client(void){
- int x,y;
- y=4;
- x=2;
- while(x<8) {
- mvaddch(y,x,'P');
- refresh();
- napms(150);
- mvaddch(y,x,' ');
- refresh();
- x=x+1;
- }
- mvaddch(y,x,'P');
- refresh();
- /*
- mvaddch(4,2,'P');
- refresh();
- randwait(10);
- mvaddch(4,2,' ');
- mvaddch(4,5,'P');
- refresh();
- randwait(10);
- mvaddch(4,5,' ');
- mvaddch(4,8,'P');
- refresh();
- * */
- }
- void move_client_to_chairs(void){
- int y=4;
- int x=8;
- mvaddch(y,x,' ');
- while(y<15){
- mvaddch(y,x,'P');
- refresh();
- napms(150);
- mvaddch(y,x,' ');
- refresh();
- y++;
- }
- }
- void console_write(char* message){
- //sem_wait(console_control);
- //int x;
- /*if(console_msg_count>=20){
- //move(3,45);
- for(x=0;x<20;x++){
- mvaddstr(3+x,45," ");
- }
- console_msg_count=0;
- }*/
- mvprintw(3+console_msg_count,45,"%s",message);
- console_msg_count++;
- refresh();
- //sem_post(console_control);
- }
- void barber_console_write(char* message){
- int x;
- if(barber_console_msg_count>=20){
- //move(6+height,5);
- for(x=0;x<20;x++){
- mvaddstr(10+x+height,5," ");
- }
- barber_console_msg_count=0;
- }
- mvprintw(7+barber_console_msg_count+height,5,"%s",message);
- barber_console_msg_count++;
- refresh();
- }
- void center(int row, char *title)
- {
- /*
- * Paints a centered title in given row of the console
- */
- int len, indent,y,width;
- getmaxyx(stdscr,y,width);
- len=strlen(title);
- indent = width- len;
- indent /=2;
- mvprintw(99,99,"%d",y);
- mvaddstr(row,indent,title);
- refresh();
- }
- void randwait(int secs) {
- int len;
- // Generate a random number...
- len = (int) ((drand48() * secs) + 1);
- sleep(len);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement