2019-10-28 19:49:25 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <ncurses.h>
|
|
|
|
//declaraao de funcoes
|
|
|
|
#include "dec.h"
|
|
|
|
//estructuras e outras coisas uteis
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]){
|
2019-11-11 17:58:27 +00:00
|
|
|
int sair=0, mopt=-1, n;
|
2019-10-28 19:49:25 +00:00
|
|
|
|
2019-11-11 17:58:27 +00:00
|
|
|
msg mensagem;
|
|
|
|
|
|
|
|
srand(time(NULL));
|
2019-10-28 19:49:25 +00:00
|
|
|
int fd_servidor;
|
|
|
|
/* VERIFICAR SE EXISTE "CP" DO SERVIDOR (access) -- APENAS UM!!!*/
|
|
|
|
if(access("CPservidor", F_OK)==0){
|
|
|
|
printf("[SERVIDOR] Ja existe um servidor!\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
/* CRIAR "CP" DO SERVIDOR - MINHA (mkfifo) */
|
|
|
|
mkfifo("CPservidor", 0600);
|
|
|
|
/* ABRIR "CP" DO SERVIDOR - MINHA (open - O_RDONLY) */
|
|
|
|
fd_servidor = open("CPservidor", O_RDWR);
|
|
|
|
|
|
|
|
printf("[SERVER] Servidor Iniciado!\n");
|
|
|
|
|
|
|
|
/* Fazer coisas aqui! */
|
|
|
|
do{
|
|
|
|
|
2019-11-11 17:58:27 +00:00
|
|
|
n = read(fd_servidor,&mensagem, sizeof(mensagem));
|
|
|
|
|
|
|
|
if(n == 0){
|
|
|
|
printf("[SERVER] deu merda!\n");
|
|
|
|
sleep(1);
|
|
|
|
continue;
|
|
|
|
}else if(strcmp(mensagem.titulo,"send")==0){
|
|
|
|
printf("funciona!\n");
|
|
|
|
printf("%s\n",mensagem.corpo);
|
|
|
|
}
|
|
|
|
|
2019-10-28 19:49:25 +00:00
|
|
|
}while(true);
|
|
|
|
|
2019-11-11 17:58:27 +00:00
|
|
|
printf("[SERVER] SERVIDOR DESLIGADO\n");
|
2019-10-28 19:49:25 +00:00
|
|
|
|
|
|
|
/* FECHAR "CP" DO SERVIDOR - MINHA (close) */
|
|
|
|
close(fd_servidor);
|
|
|
|
/* REMOVER "CP" DO SERVIDOR- MINHA (UNLINK) */
|
|
|
|
unlink("CPservidor");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2019-11-11 17:58:27 +00:00
|
|
|
void printMenu(void){
|
|
|
|
printf("[SERVER] Menu:\n");
|
|
|
|
printf("\t1 - coisas\n");
|
|
|
|
printf("\t0 - SAIR\n");
|
|
|
|
|
|
|
|
printf("> ");
|
|
|
|
}
|
|
|
|
|
2019-10-28 19:49:25 +00:00
|
|
|
int obtem_rand(int min, int max){
|
|
|
|
int random;
|
|
|
|
random = min + (rand() % (max - min + 1));
|
|
|
|
return random;
|
|
|
|
}
|