int main() { Client client[] = { {"Ana", 50, 2353.81}, {"Marilyn Manson", 46, 2354.78}, {"Ton Roosendaal", 55, 4418.00}, {"Guts", 32, 230.30}, {"Vash", 131, 1200000.00}, {"Finn", 16, 3404.40}, {"Jake", 34, 6595.63}, }; int numberOfClients = sizeof (client) / sizeof (client[0]); printf("Unsorted: \n"); print_client(client, numberOfClients); printf("Sort by name\n"); sort_clients(client, numberOfClients, &name_compare); print_client(client, numberOfClients); printf("Sort by age\n"); sort_clients(client, numberOfClients, &age_compare); print_client(client, numberOfClients); printf("Sort by balance\n"); sort_clients(client, numberOfClients, &account_compare); print_client(client, numberOfClients); return (EXIT_SUCCESS); }
void client::run_client_print() { HANDLE Semaphores[3]; Semaphores[0] = Semaphore_client_start_print; Semaphores[1] = Semaphore_client_end_print; Semaphores[2] = Semaphore_client_print_exit; while (1) { flag = TRUE; DWORD number_of_bytes_read; message_to_print.clear(); int index = WaitForMultipleObjects(3, Semaphores, FALSE, INFINITE) - WAIT_OBJECT_0; if (index == 2) break; int number_of_blocks; int size; if (!ReadFile(hPipe, &number_of_blocks, sizeof(number_of_blocks), &number_of_bytes_read, NULL)) break; if (!ReadFile(hPipe, &size, sizeof(size), &number_of_bytes_read, NULL)) break; for (int i = 0; i < number_of_blocks; i++) { flag = ReadFile(hPipe, buff, size_of_buff, &number_of_bytes_read, NULL); if (!flag) break; message_to_print.append(buff, size_of_buff); } if (!flag) break; message_to_print.resize(size); print_client(); for (int i = 0; i < size; i++) { cout << message_to_print[i]; Sleep(SLEEP_TIME); } cout << endl; ReleaseSemaphore(Semaphore_client_end_print, 1, NULL); } }
/** Verifica que se cumplan las condiciones de los nodos */ bool client_check(Building* client) { /* El nodo debe estar conectado a un core */ if(!client -> cored) { printf("Cliente no abastecido: "); print_client(client); return false; } /* El nodo debe tener un color */ if(!client -> color) { printf("Cliente sin color: "); print_client(client); return false; } /* El nodo debe tener la cantidad deseada de vecinos */ if(client -> link_count != client -> capacity) { printf("Al cliente le faltan vecinos: "); print_client(client); return false; } for(int i = 0; i < client -> link_count; i++) { /* Estos vecinos deben ser no nulos */ if(!client -> linked[i]) { printf("Cliente conectado a un vecino NULL: "); print_client(client); return false; } /* Y no pueden ser el nodo mismo */ if(client -> linked[i] == client) { printf("Cliente conectado a si mismo: "); print_client(client); return false; } /* Y deben ser del mismo color que el nodo */ if(client -> linked[i] -> color != client -> color) { printf("Cliente conectado a un cliente de otro color: "); print_client(client); return false; } for(int j = i + 1; j < client -> link_count; j++) { /* Y deben ser distintos entre ellos */ if(client -> linked[i] == client -> linked[j]) { printf("Cliente conectado dos veces a un mismo cliente: "); print_client(client); return false; } } } return true; }
void on_accept (evutil_socket_t fd, short what, void *arg) { if (NULL == arg) { fprintf (LOG_STREAM, "[ALARM] 'on_accept' has not get the" " valid argument\n") ; exit (1) ; } if (!(what & EV_READ)) { // The event is not ready for reading goto _reloop ; } struct sockaddr_in cli ; int clifd ; struct event *evcli ; struct event *self = (struct event *)arg ; while (1) { errno = 0 ; // Clear the errno clifd = accept (fd, &cli, sizeof(cli)) ; #ifdef EWOULDBLOCK if (errno == EWOULDBLOCK || errno == EAGAIN) #else if (errno == EAGAIN) #endif break ; if (-1 == clifd) { fprintf (LOG_STREAM, "[ERROR] accpet in 'on_accept' failed\n") ; serv_exit () ; } // Bind the fd to a event and add it to the g_base evcli = event_new (g_base, clifd, EV_WRITE | EV_ET, on_cli, &cli) ; if (NULL == evcli) { fprintf (LOG_STREAM, "[ERROR] event_new in 'on_accept' failed\n") ; serv_exit () ; } fprintf (LOG_STREAM, "[NOTICE] Accept a new client\n") ; print_client (&cli) ; } _reloop : if (-1 == event_del (self)) { fprintf (LOG_STREAM, "[ERROR] event_del in 'on_accept' failed\n") ; serv_exit () ; } if (-1 == event_add (self, NULL)) { fprintf (LOG_STREAM, "[ERROR] event_add in 'on_accept' failed\n") ; serv_exit () ; } }
void print_pollfd_info(struct pollfd_info *p) { int i; fprintf(stderr, "Pollfd info: nfds = %d, size = %d:\n", p->nfds, p->size); for(i = 0; i < p->size; i++) { fprintf(stderr, "\t%d", i); if(p->pollfd[i].fd != -1) fprintf(stderr, ". (FD = %.2d, E = %hX): ", p->pollfd[i].fd, p->pollfd[i].events); else fprintf(stderr, ". "); print_client(p->clients[i]); fprintf(stderr, "\n"); } }
void server::server_send() { HANDLE Semaphores[2]; Semaphores[0] = Semaphore_server[3]; //Semafore_Server_Send Semaphores[1] = Semaphore_server[4]; //Semafore_Server_Send_exit while (1) { WaitForMultipleObjects(2, Semaphores, FALSE, INFINITE) - WAIT_OBJECT_0; //received permission ReleaseSemaphore(Semaphore_client[3], 1, NULL); //SEMAPHORE_CLIENT_SEND if (getch_noblock() != -1) { DWORD numberber_of_bytes_written; print_client(); cin.clear(); getline(cin, send_message); if (send_message == "end\0" || send_message == "exit\0") { ReleaseSemaphore(Semaphore_server[2], 1, NULL); //SEMAPHORE_SERVER_PRINT_EXIT ReleaseSemaphore(Semaphore_client[2], 1, NULL); //SEMAPHORE_CLIENT_PRINT_EXIT ReleaseSemaphore(Semaphore_client[4], 1, NULL); //SEMAPHORE_CLIENT_SEND_EXIT WaitForSingleObject(ChildProcessInfo.hProcess, INFINITE); break; } ReleaseSemaphore(Semaphore_client[0], 1, NULL); //SEMAPHORE_CLIENT_START_PRINT int numberber_of_blocks = send_message.size() / size_of_buffer + 1; int size = send_message.size(); WriteFile(hNamedPipe, &numberber_of_blocks, sizeof(numberber_of_blocks), &numberber_of_bytes_written, (LPOVERLAPPED)NULL); WriteFile(hNamedPipe, &size, sizeof(size), &numberber_of_bytes_written, (LPOVERLAPPED)NULL); for (int i = 0; i < numberber_of_blocks; i++) { send_message.copy(buffer, size_of_buffer, i * size_of_buffer); if (!WriteFile(hNamedPipe, buffer, size_of_buffer, &numberber_of_bytes_written, (LPOVERLAPPED)NULL)) { cout << "Write Error" << endl; } } WaitForSingleObject(Semaphore_client[1], INFINITE); //SEMAPHORE_CLIENT_END_PRINT } Sleep(1); } }