void map() { lista* l = lista_crear(); jugador* j1 = crear_jugador("Ginobili", "Argentina", 5, 198); jugador* j2 = crear_jugador("Scola", "Argentina", 6, 198); jugador* j3 = crear_jugador("Prigioni", "Argentina", 7, 198); jugador* j4 = crear_jugador("Ginobili", "Argentina", 9, 199); jugador* j5 = crear_jugador("Ginobili", "Argentina", 10, 199); insertar_ordenado(l, (void*)j1, (tipo_funcion_cmp)&menor_jugador); lista* lmap=mapear(l, (tipo_funcion_mapear)&i); assert(((jugador*)lmap->primero->datos)->altura==j1->altura); lista_imprimir(lmap, "hola.out",(tipo_funcion_imprimir)&imprimir_jugador); lista_borrar(lmap,(tipo_funcion_borrar)&borrar_jugador); insertar_ordenado(l, (void*)j2, (tipo_funcion_cmp)&menor_jugador); insertar_ordenado(l, (void*)j3, (tipo_funcion_cmp)&menor_jugador); insertar_ordenado(l, (void*)j4, (tipo_funcion_cmp)&menor_jugador); insertar_ordenado(l, (void*)j5, (tipo_funcion_cmp)&menor_jugador); lista* lmap2=mapear(l, (tipo_funcion_mapear)&normalizar_jugador); lista_imprimir(lmap2, "hola.out",(tipo_funcion_imprimir)&imprimir_jugador); jugador* n1 = normalizar_jugador(j1); assert(((jugador*)lmap2->primero->datos)->altura==n1->altura); borrar_jugador(n1); lista_borrar(lmap2,(tipo_funcion_borrar)&borrar_jugador); lista_borrar(l,(tipo_funcion_borrar)&borrar_jugador); }
void SpecificWorker::update() { float value_x, value_y, value_z; value_x=sliderX->value(); value_y=sliderY->value(); value_z=sliderZ->value(); x=value_x/300; X_pre=X; X=mapear(value_x,-65537,65537, -39,39); y=value_y/300; Y_pre=Y; Y=mapear(value_y,-65537,65537, 47,-47); z=value_z/300; Z_pre=Z; Z=mapear(value_z,65537,-65537, -47,47); vel=sliderVel->value(); angles_pre.q1=angles.q1; angles_pre.q2=angles.q2; angles_pre.q3=angles.q3; float aux=sliderq1->value(); angles.q1=(aux/35537.0); aux=sliderq2->value(); angles.q2=(aux/35537.0); aux=sliderq3->value(); angles.q3=(aux/35537.0); valueX->setText(QString::number(X)); valueY->setText(QString::number(Y)); valueZ->setText(QString::number(Z)); valueq1->setText(QString::number(angles.q1)); valueq2->setText(QString::number(angles.q2)); valueq3->setText(QString::number(angles.q3)); valuevel->setText(QString::number(vel)); }
int testLista() { char *nombre = "aaa"; char *pais = "pepe"; jugador *pepe = crear_jugador(nombre ,pais,'4',196); char *nombre2 = "ccc"; char *pais2 = "pepe"; jugador *pepe2 = crear_jugador(nombre2 ,pais2,'4',196); char *nombre3 = "bbb"; char *pais3 = "pepe"; jugador *pepe3 = crear_jugador(nombre3 ,pais3,'4',196); lista *miLista; printf("Creando Lista...\n"); miLista = lista_crear(); printf("Insertando jugador 1\n"); insertar_ordenado(miLista,(void*)pepe, (tipo_funcion_cmp)&menor_jugador); printf("Insertando jugador 2\n"); insertar_ordenado(miLista,(void*)pepe2, (tipo_funcion_cmp)&menor_jugador); printf("Insertando jugador 3\n"); insertar_ordenado(miLista,(void*)pepe3, (tipo_funcion_cmp)&menor_jugador); lista *backUp = miLista; miLista = ordenar_lista_jugadores(backUp); lista_borrar(backUp, (tipo_funcion_borrar)&borrar_jugador); nodo *unNodo = miLista->primero; jugador *unj = unNodo->datos; printf("Nombre: %s \n",unj->nombre); unNodo = miLista->primero->sig; unj = unNodo->datos; printf("Nombre: %s \n",unj->nombre); unNodo = miLista->primero->sig->sig; unj = unNodo->datos; printf("Nombre: %s \n",unj->nombre); lista* B = mapear(miLista, (tipo_funcion_mapear)&normalizar_jugador); printf("Borrando Lista...\n"); lista_borrar(B, (tipo_funcion_borrar)&borrar_jugador); lista_borrar(miLista, (tipo_funcion_borrar)&borrar_jugador); printf("Ok!\n"); return 0; }
int p5(){ char *nombre = "aaa"; char *pais = "pepe"; jugador *pepe = crear_jugador(nombre ,pais, 4 ,30); char *nombre2 = "aab"; char *pais2 = "pepe"; jugador *pepe2 = crear_jugador(nombre2 ,pais2, 4 ,30); char *nombre3 = "bbb"; char *pais3 = "pepe2"; jugador *pepe3 = crear_jugador(nombre3 ,pais3, 4 ,30); lista *l = lista_crear(); insertar_ordenado(l,pepe,(tipo_funcion_cmp)&menor_jugador); insertar_ordenado(l,pepe2,(tipo_funcion_cmp)&menor_jugador); insertar_ordenado(l,pepe3,(tipo_funcion_cmp)&menor_jugador); lista *pop = mapear(l, (tipo_funcion_mapear)&normalizar_jugador); lista_imprimir(pop,"ultimoTest.txt",(tipo_funcion_imprimir)&imprimir_jugador); lista *l2 = filtrar_jugadores(l, (tipo_funcion_cmp)&pais_jugador,(void*) l->primero); assert(menor_jugador(l->primero->datos,l2->primero->datos) && menor_jugador(l2->primero->datos,l->primero->datos)); lista_imprimir(l2,"swapsds",(tipo_funcion_imprimir)&imprimir_jugador); lista_borrar(pop,(tipo_funcion_borrar) &borrar_jugador); lista_borrar(l2,(tipo_funcion_borrar) &borrar_jugador); lista_borrar(l,(tipo_funcion_borrar) &borrar_jugador); return 0; }
int main( int argc , char *argv[]) { signal(SIGINT, ctrlHandler); n_linhas = linhas("localdns.txt"); #ifdef DEBUG printf("Verificacao dos ficheiros presentes.\n"); #endif if(verifica_ficheiros("localdns.txt") == 0) { printf("Ficheiro localdns.txt inexistente!\n"); exit(1); } if(verifica_ficheiros("config.txt") == 0) { printf("Ficheiro config.txt inexistente!\n"); exit(1); } #ifdef DEBUG printf("Inicializacoes.\n"); #endif unsigned char buf[65536], *reader, *reader2, qname; int sockfd, stop; struct DNS_HEADER *dns = NULL; struct stats stat; struct sockaddr_in servaddr, dest; socklen_t len; int i, np, r; cfg_process = 1; stats_process = 1; pthread_mutex_init(&mutex, NULL); pthread_cond_init(&cond, NULL); #ifdef DEBUG printf("Criacao das Filas Normal e Prioridade e da lista de Domains locais.\n"); #endif Fila fila_normal = cria_fila(); Fila fila_prioridade = cria_fila(); Local l_domains = cria_lista(); // Hora de arranque do Servidor time_t clk = time(NULL); strcpy(stat.data_hora_arranque, ctime(&clk)); // Inicializaco Memoria Partilhada init_shm(); ptr_config->flag = 0; printf("[PAI - MAIN] PID Main = %d PID DO MEU PAI = %d\n", getpid(), getppid()); char* data; #ifdef DEBUG printf("Criacao processo de configuracoes.\n"); #endif if ((cfg_process = fork()) == 0) { signal(SIGUSR1, handler_CFG); #ifdef DEBUG printf("RELEASE CFG.\n"); printf("Mapeamento de localdns.txt.\n"); #endif mapear("localdns.txt"); #ifdef DEBUG printf("Leitura de config.txt para estrutura em memoria partilhada.\n"); #endif file2memory("config.txt"); if(signal(SIGALRM, SIG_IGN) == SIG_ERR) { perror("SIGALRM Ignore error"); exit(1); } } else { usleep(200000); if(signal(SIGUSR1, SIG_IGN) == SIG_ERR) { perror("Signal ignore error"); exit(1); } #ifdef DEBUG printf("RELEASE MAIN.\n"); #endif #ifdef DEBUG printf("Extrair enderecos e ip's locais do mmap\n"); #endif token_mmap(l_domains, ptr_config->data); // Check arguments if(argc <= 1) { printf("Usage: %s <port>\n", argv[0]); exit(1); } // Get server UDP port number int port = atoi(argv[1]); if(port <= 0) { printf("Usage: %s <port>\n", argv[0]); exit(1); } // **************************************** // Create socket & bind // **************************************** // Create UDP socket sockfd = socket(AF_INET , SOCK_DGRAM , IPPROTO_UDP); //UDP packet for DNS queries if (sockfd < 0) { printf("ERROR opening socket.\n"); exit(1); } // Prepare UDP to bind port bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr=htonl(INADDR_ANY); servaddr.sin_port=htons(port); // **************************************** // Receive questions // **************************************** // Bind application to UDP port int res = bind(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr)); if(res < 0) { printf("Error binding to port %d.\n", servaddr.sin_port); if(servaddr.sin_port <= 1024) { printf("To use ports below 1024 you may need additional permitions. Try to use a port higher than 1024.\n"); } else { printf("Please make sure this UDP port is not being used.\n"); } exit(1); } #ifdef DEBUG printf("Criacao named pipe para as estatisticas\n."); #endif char* n_pipe = ptr_config->Named_pipe; if(verifica_ficheiros(n_pipe) == 1) { printf("Named pipe ja existente! A eliminar...\n"); unlink(n_pipe); } if(((np = mkfifo(n_pipe, O_CREAT|O_EXCL|0600)<0)) && (errno != EEXIST)) { perror("mkfifo"); exit(1); } if (np != 0) { fprintf(stderr, "Impossivel criar fifo %s\n", n_pipe); return 1; } if((np = open(n_pipe, O_RDWR)) < 0) { perror("Opening Named Pipe"); exit(1); } } #ifdef DEBUG printf("A sair = %d\n", getpid()); #endif #ifdef DEBUG printf("Criacao Processo de estatisticas\n"); #endif if(cfg_process != 0) { stats_process = fork(); if(stats_process == 0) { if(signal(SIGUSR1, SIG_IGN) == SIG_ERR) { perror("Signal ignore error"); exit(1); } #ifdef DEBUG printf("RELEASE STATS.\n"); #endif } else { usleep(200000); if(signal(SIGALRM, SIG_IGN) == SIG_ERR) { perror("SIGALRM Ignore error"); exit(1); } } } #ifdef DEBUG printf("Quem chega primeiro = %d\n", getpid()); #endif if(cfg_process != 0 && stats_process != 0) { #ifdef DEBUG printf("A preencher struct para argumento da funcao do thread.\n"); #endif struct thread_data t_data; t_data.normal = fila_normal; t_data.prioridade = fila_prioridade; t_data.domains = l_domains; int N_THREADS = ptr_config->N_threads; p_threads = malloc(sizeof(*p_threads) * N_THREADS); #ifdef DEBUG printf("A criar a pool de threads.\n"); #endif for(i=0;i < N_THREADS;i++) { pthread_create(p_threads+i, NULL, requester, (void*)&t_data); } usleep(50000); printf("\n\n-- Waiting for DNS message --\n\n"); } while(1) { if((cfg_process == 0) && (ptr_config->saida == 1)) { // MODO MANUNTENCAO file2memory("config.txt"); ptr_config->saida = 0; usleep(100000); printf("\n-- Waiting for DNS message --\n"); } if(cfg_process != 0 && stats_process != 0) { // GESTOR PEDIDOS usleep(200000); len = sizeof(dest); // Thread blocking! if(recvfrom (sockfd, (unsigned char*)buf, 65536 , 0 , (struct sockaddr*)&dest , &len) < 0) { perror("Error in recvfrom"); exit(1); } printf("DNS message received\n"); // Process received message dns = (struct DNS_HEADER*) buf; qname = (unsigned char)buf[sizeof(struct DNS_HEADER)]; reader = &buf[sizeof(struct DNS_HEADER)]; // We only need to process the questions // We only process DNS messages with one question // Get the query fields according to the RFC specification struct QUERY query; if(ntohs(dns->q_count) == 1) { // Get NAME query.name = convertRFC2Name(reader,buf,&stop); reader = reader + stop; // Get QUESTION structure query.ques = (struct QUESTION*)(reader); reader = reader + sizeof(struct QUESTION); // Check question type. We only need to process A records. if(ntohs(query.ques->qtype) == 1) { printf("A record request.\n\n"); } else { printf("NOT A record request!! Ignoring DNS message!\n"); continue; } } else { printf("\n\nDNS message must contain one question!! Ignoring DNS message!\n\n"); continue; } #ifdef DEBUG printf("ID PEDIDO = %d\n", dns->id); #endif /***** VALIDAR PEDIDO *****/ valido = 0; // 1. Verificar se e' localDomain (Se for, vai para a lista prioritaria) // TRABALHO DOS THREADS... if(strstr(query.name, ptr_config->localDomain) != NULL) { valido = 1; stat.pedidos_local += 1; insere_pedido(fila_prioridade, receber_info(query.name, &dest, dns->id, sockfd)); } else if(ptr_config->flag % 2 == 0) { // 2. Verificar se é um dos dominios autorizados (ptr_config->domains[]) for(i=0; i < ptr_config->n_domains; i++) { if(strstr(query.name, ptr_config->domains[i]) != NULL) { valido = 1; stat.pedidos_externos += 1; insere_pedido(fila_normal, receber_info(query.name, &dest, dns->id, sockfd)); break; } } } #ifdef DEBUG imprimir_fila(fila_normal); imprimir_fila(fila_prioridade); #endif if(valido == 1) { #ifdef DEBUG printf("A ENVIAR SINAL PARA A THREAD...\n"); #endif pthread_cond_signal(&cond); } else { if(ptr_config->flag % 2 != 0) { printf("Durante o modo de manuntencao apenas pedidos locais sao aceites!\n"); printf("A enviar 0.0.0.0...\n"); } else { printf("Pedido negado!\nDe momento os dominios aceites sao:\n"); for(i=0;i<ptr_config->n_domains;i++) { printf("-> Dominio %d = %s\n",i,ptr_config->domains[i]); } } sendReply(dns->id, query.name, inet_addr("0.0.0.0"), sockfd, dest); printf("\n\n-- Waiting for DNS message --\n\n"); } if(valido == 0) { stat.pedidos_negados += 1; } stat.total_pedidos += 1; // Enviar dados das ESTATISTICAS para o named pipe aqui #ifdef DEBUG printf("A escrever estatisticas para o named pipe\n."); #endif write(np, &stat, sizeof(struct stats)); } if (stats_process == 0 && cfg_process != 0) { // STATS Process signal(SIGALRM, handler_alarm); Config* ptr; if ((ptr = shmat(shmID, NULL, 0)) == (Config *) -1) { perror("error in shmat"); exit(1); } struct stats st; char* n_pipe = ptr_config->Named_pipe; if((r = open(n_pipe, O_RDONLY)) < 0) { perror("Child open Named pipe"); exit(1); } read(r, &st, sizeof(struct stats)); linha(st.data_hora_arranque); time_t clk2 = time(NULL); strcpy(st.data_info, ctime(&clk2)); linha(st.data_info); t_p = st.total_pedidos; p_n = st.pedidos_negados; p_l = st.pedidos_local; p_e = st.pedidos_externos; strcpy(d_a, st.data_hora_arranque); strcpy(u_i, st.data_info); alarm(FREQ); } } return 0; }