void client_remove(struct WM_t *W, struct wmclient *C) { int idx = get_client_index(W, C->win), i; if (W->clients[idx] != C) { msg("client_remove: somehow get_client_index failed! :o\n"); return; } W->clients[idx] = NULL; /* Update all the focus numbers, i.e. decrease (bring forward) all the windows with bigger focus numbers. */ for (i = idx; i < W->nclients; i++) W->clients[i] = W->clients[i + 1]; W->nclients--; msg("Removing client \'%s\'\n", C->name); free(C->name); free(C); msg("About to print\n"); print_clients(W); msg("Printed, about to focus\n"); client_focus(W, W->clients[0]); msg("Focused\n"); }
/* * Problem 09. Clients * * Declare a struct called Client with the following members: name, age and * account balance. More on structures: * http://www.tutorialspoint.com/cprogramming/c_structures.htm * * Write a function that sorts an array of clients using a specific comparator. * The comparator should be another function that determines how the clients are * sorted. Write comparators for sorting the clients by name, age and account * balance. * * Sample Code * --------------------------------------------------------- * struct Client clients[] = { ... }; * int clientsCount = ...; * sort_clients(clients, clientsCount, &name_comparator); * sort_clients(clients, clientsCount, &balance_comparator); * sort_clients(clients, clientsCount, &age_comparator); */ int main() { struct Client pesho = { "Pesho", 25, 3300.1 }; struct Client gosho = { "Gosho", 38, 1255.6 }; struct Client atanas = { "Atanas", 47, 58020.78 }; struct Client veso = { "Veso", 39, 168.2 }; struct Client clients[] = { pesho, gosho, atanas, veso }; int clientsCount = sizeof(clients) / sizeof(struct Client); printf("\nClients sorted by Name\n--------------------------\n"); sort_clients(clients, clientsCount, &name_comparator); print_clients(clients, clientsCount); printf("\nClients sorted by Balance\n--------------------------\n"); sort_clients(clients, clientsCount, &balance_comparator); print_clients(clients, clientsCount); printf("\nClients sorted by Age\n--------------------------\n"); sort_clients(clients, clientsCount, &age_comparator); print_clients(clients, clientsCount); return (EXIT_SUCCESS); }
/* Register a client, steal its border, grap events, etc */ void client_register(struct WM_t *W, Window xwindow_id) { struct wmclient *C = malloc(sizeof(*C)); XFetchName(W->XDisplay, xwindow_id, &(C->name)); if (!(C->name)) C->name = strdup("<notitle>"); msg("Registering \'%s\':\n", C->name); C->win = xwindow_id; decide_new_window_size_pos(W, C); /* Unless it's a window found at startup, move it * to the current head/screen */ if (!registering_existing_windows) { C->x += curr_head_x(W); C->y += curr_head_y(W); } C->fullscreen = 0; XMapWindow(W->XDisplay, C->win); set_size_pos_border(W, C); client_select_events(W, C); get_pid(W, C); W->clients[W->nclients++] = C; client_focus(W, C); XFlush(W->XDisplay); print_clients(W); }
/** * parses the given log file and fills out the data structure * */ int parse_log_file(struct client *info, FILE *fp) { char buffer[BUFFER_SIZE]; char ip[16]; int i; char *f_ptr; char time[26]; char file[255]; num_clients = 0; for(i=0;i<MAX_CONCURRENT_CLIENTS;i++){ clients[i].address[0] = '\0'; clients[i].requests = NULL; } while( fgets(buffer, BUFFER_SIZE, fp) != NULL ){ struct request *req, *tmp; int index, count = 0; // Stripping out the newline char buffer[BUFFER_SIZE - 1] = '\0'; //printf("%s\n", buffer); //printf("%s\n", get_client_address(buffer, &ip)); get_client_address(&buffer[0], &ip[0]); get_request_time(&buffer[0], &time[0]); get_requested_file(&buffer[0], &file[0]); tmp = malloc(sizeof(struct request)); //tmp->file = file; //printf("file len = %d\n", (int)strlen(file)); f_ptr = malloc( (sizeof(char) * (int)strlen(file)) + 1 ); strcpy(f_ptr, file); tmp->file = f_ptr; //tmp->file = malloc(sizeof(char) * strlen(file)); //strcpy(tmp->file, file); strcpy(tmp->time, time); tmp->next = NULL; if( (index = check_client_exists(&ip[0])) != -1){ // we caught a persistant client //printf("file = %s\n", &file[0]); req = clients[index].requests; //printf("index = %d\n", index); //printf("req = %p\n", req); //printf("address = %s\n", clients[index].address); if(req != NULL){ while(req->next != NULL) req = req->next; //req = req->next; //printf("kkkkkkkkk\n"); //printf("new req = %p\n", req); req->next = tmp; }else{ req = tmp; } //printf("new req1 = %p\n", req); //printf("new requests = %p\n", clients[index].requests); /* count = 0; req = clients[index].requests; printf("new requestsxx = %p\n", clients[index].requests); while(req != NULL){ printf("=====================\n"); printf("new req3 = %p\n", req); printf("file = %s\n", req->file); printf("count = %d\n", ++count); printf("=====================\n"); req = req->next; } */ }else{ strcpy(clients[num_clients].address, ip); //clients[num_clients].requests = NULL; clients[num_clients].requests = tmp; //printf("tmp = %p\n", tmp); num_clients++; } //printf("%s\n", ip); } print_clients(); return 0; }
int server(int num_clients, int port) { WicServer server; wic_init_server(&server, port, num_clients); ClientData* clients = malloc(num_clients * sizeof(ClientData)); for(int i = 0; i < num_clients; i++) clients[i].joined = false; Command command; command.created = false; pthread_t command_thread; pthread_create(&command_thread, 0, accept_command, &command); printf("Type commands below...\n"); WicPacket recv; WicPacket send; while(true) { if(command.created == true) { if(strncmp(command.first_arg, "exit", 4) == 0) break; else if(strcmp(command.first_arg, "help") == 0) print_command_list(); else if(strcmp(command.first_arg, "kick") == 0) { ClientData* client = lookup_client(clients, command.second_arg, num_clients); if(client == 0) printf("ERROR: Client %s does not exist\n", command.second_arg); else wic_kick_client(&server, client->client_id); } else if(strcmp(command.first_arg, "ban-client") == 0) { ClientData* client = lookup_client(clients, command.second_arg, num_clients); if(client == 0) printf("ERROR: Client %s does not exist\n", command.second_arg); else wic_ban_client(&server, client->client_id); } else if(strcmp(command.first_arg, "ban-address") == 0) { wic_ban_address(&server, command.second_arg); } else if(strcmp(command.first_arg, "unban-address") == 0) { wic_unban_address(&server, command.second_arg); } else if(strcmp(command.first_arg, "ls-clients") == 0) { print_clients(clients, num_clients); } else printf("%s '%s'. %s", "ERROR: Invalid command", command.first_arg, "Type 'help' for command list\n"); command.created = false; pthread_create(&command_thread, 0, accept_command, &command); } enum WicError error = wic_server_recv_packet(&server, &recv); if(error == WICER_NONE) { unsigned char client_id = recv.client_id; if(recv.type.id == WIC_PACKET_JOIN.id) { clients[client_id].joined = true; clients[client_id].client_id = client_id; init_entity_manager(&clients[client_id].manager); printf("client %u joined\n", client_id); } if(recv.type.id == PACKET_USERNAME_INFO.id) { memcpy(clients[client_id].username, recv.data, PACKET_USERNAME_INFO.size); wic_send_packet_to_other_clients(&server, &recv, client_id); for(unsigned char id = 0; id < num_clients; id++) { if(clients[id].joined && id != client_id) { send.type = PACKET_USERNAME_INFO; send.client_id = id; memcpy(send.data, clients[id].username, sizeof(clients[id].username)); wic_send_packet_to_client(&server, &send, client_id); } } } else if(recv.type.id == WIC_PACKET_LEAVE.id) { clients[client_id].joined = false; free_entity_manager(&clients[client_id].manager); printf("client %u left\n", recv.client_id); } else if(recv.type.id == PACKET_UPDT_ENTITY.id) { Entity entity; memcpy(&entity, recv.data, PACKET_UPDT_ENTITY.size); updt_entity(&clients[client_id].manager, entity); wic_send_packet_to_other_clients(&server, &recv, client_id); } else if(recv.type.id == PACKET_REMV_ENTITY.id) { remove_entity(&clients[client_id].manager, recv.data[0]); wic_send_packet_to_other_clients(&server, &recv, client_id); } } } printf("Exiting server..\n"); wic_free_server(&server); return 0; }