long long NetworkManager::addClient(string IP, unsigned short server_port) { boost::asio::ip::udp::resolver resolver(io_service); boost::asio::ip::udp::resolver::query query(udp::v4(), IP, boost::lexical_cast< std::string >(server_port)); boost::asio::ip::udp::resolver::iterator iterator = resolver.resolve(query); remote_endpoint = *iterator; insert_client(remote_endpoint); return nextClientID; }
void authorize_client(t_client *c) { if (c->state == STATE_NEW) { c->authorized = 1; insert_client(c); cnt->newclient = del_in_list(cnt->newclient, c); } }
Code_t triplet_register(Client *client, Destination *dest, ZRealm *realm) { Triplet *triplet; unsigned long hashval; hashval = DEST_HASHVAL(*dest); for (triplet = triplet_bucket[hashval]; triplet; triplet = triplet->next) { if (ZDest_eq(&triplet->dest, dest)) return insert_client(triplet, client, realm); } /* Triplet not present in hash table, insert it. */ triplet = triplet_alloc(dest->classname, dest->inst, dest->recip); Triplet_insert(&triplet_bucket[hashval], triplet); return insert_client(triplet, client, realm); }
NetworkManager::NetworkManager(string IP, unsigned short server_port, string local_ip, unsigned short local_port) : socket(io_service, udp::endpoint(udp::v4(), local_port)), service_thread(boost::bind(&NetworkManager::run_service, this)) { boost::asio::ip::udp::resolver resolver(io_service); boost::asio::ip::udp::resolver::query query(udp::v4(), IP, boost::lexical_cast< std::string >(server_port)); boost::asio::ip::udp::resolver::iterator iterator = resolver.resolve(query); myIP = local_ip; myPort = socket.local_endpoint().port(); nextClientID = 0; receivedMessages = 0; receivedBytes = 0; sentMessages = 0; sentBytes = 0; remote_endpoint = *iterator; insert_client(remote_endpoint); }
void* sushi_bar(void* arg) { int client_id = *(int *) arg; while(1){ pthread_mutex_lock(&mutex); if(must_wait) { waiting+=1; /* CLIENTE ESPERANDO */ state[client_id] = W; display_table(client_id); /* MUTEX LIBERADO */ pthread_mutex_unlock(&mutex); sem_wait(&block); waiting -= 1; } eating+=1; must_wait = (eating == 5); /* muda o estado do cliente para SITTING */ state[client_id] = S; insert_client(client_id); /* muda estado do cliente para COMENDO */ state[client_id] = E; display_table(client_id); /* MUTEX LIBERADO */ if(waiting && !must_wait) sem_post(&block); else pthread_mutex_unlock(&mutex); /* Sleep randomico para cada thread comer */ sleep(rand() % 10 + 1); pthread_mutex_lock(&mutex); eating-=1; state[client_id] = L; /* CLIENTE SAINDO */ display_table(client_id); leaving++; remove_client(client_id); if(eating == 0) must_wait = 0; /* Muda o estado do cliente para OUT (saiu do Sushi Bar) */ state[client_id] = O; if(waiting && !must_wait) sem_post(&block); else pthread_mutex_unlock(&mutex); /* cliente espera 3s fora do bar */ sleep(3); } }
int cliser_server_clients(lua_State *L) { server_t *server = (server_t *)lua_touserdata(L, 1); uint32_t wait; const char *tag; int fi; int invert_order; if (lua_type(L, 2) == LUA_TFUNCTION) { wait = 0; fi = 2; if (lua_type(L, 3) == LUA_TSTRING) { tag = luaL_optstring(L, 3, NULL); invert_order = luaL_optinteger(L, 4, 0); } else { tag = NULL; invert_order = luaL_optinteger(L, 3, 0); } } else { wait = luaL_checkint(L, 2); if (lua_type(L, 3) != LUA_TFUNCTION) return LUA_HANDLE_ERROR_STR(L, "expected a callback function as argument #3"); fi = 3; tag = NULL; invert_order = 0; } while (wait > server->num_clients) { struct sockaddr addr; socklen_t addrlen = sizeof(addr); int ret = accept(server->sock, &addr, &addrlen); if (ret <= 0) return LUA_HANDLE_ERROR(L, errno); int sock = ret; int value = 1; ret = setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &value, sizeof(value)); if (ret) return LUA_HANDLE_ERROR(L, errno); int use_fastpath = can_use_fastpath(L, sock, server->ip_address, ((struct sockaddr_in *)&addr)->sin_addr.s_addr); client_t *client = (client_t *)calloc(1, sizeof(client_t)); client->sock = sock; client->send_rb = ringbuffer_create(SEND_RECV_SIZE); client->recv_rb = ringbuffer_create(SEND_RECV_SIZE); client->copy_context.use_fastpath = use_fastpath; insert_client(server, client); } client_t **clients = alloca(server->num_clients * sizeof(client_t*)); client_t *client = server->clients; uint32_t i = 0; while (client) { if (!tag || (client->tag && strcmp(tag, client->tag) == 0)) { clients[i] = client; i++; } client = client->next; } if (invert_order) { qsort(clients, i, sizeof(client_t*), compare_clients_inverted); } else { qsort(clients, i, sizeof(client_t*), compare_clients); } for (uint32_t j = 0; j < i; j++) { lua_pushvalue(L, fi); server_client_t *server_client = (server_client_t *)lua_newuserdata(L, sizeof(server_client_t)); server_client->server = server; server_client->client = clients[j]; luaL_getmetatable(L, "ipc.server.client"); lua_setmetatable(L, -2); lua_call(L, 1, 0); server_client->server = NULL; server_client->client = NULL; } lua_pushinteger(L, i); return 1; }
void booking(clients list, books type, books type2,int op) { char ans; int num_c, verif; Booking book; printf("\n\t\tAlready have a client account? [y for yes; n for no]: "); ans = getchar(); /*unacceptable answer*/ while(ans!= 'n' && ans != 'N' && ans != 'y' && ans != 'Y'){ getchar(); printf("\n\t\tOnly y or n are accepted.\n\n\t\tPlease, enter a new answer: ");getchar(); ans = getchar(); } /*acceptable answers*/ if(ans == 'n' || ans == 'N'){ /*no client account, creating one*/ Client person; printf("\n\t\t\033[0;92mCreate new account:\033[0m\n"); getchar(); person = create_account(); insert_client(list, &person); write_client(&person, "clients.txt"); book_it(&book, person.id_client, op); booking_validation(type,type2,&book,op); printf("\n\t\t\033[0;92m***Successfuly booked***\033[0m\n"); } else if(ans == 'y' || ans == 'Y'){ clients ant; clients actual; /*has client account, going to check who that person is*/ printf("\n\t\tEnter client id: "); scanf("%d", &num_c); search_clients_id(list, num_c, &ant, &actual); /*searches for the client id*/ if(actual == NULL){ /*If it doesn't exist, maybe it was a mistake, let's double check*/ printf("\n\t\tClient id does not exist. Please enter cellphone number: "); scanf("%d",&verif); /*using second parameter, cellphone number*/ search_clients_cell(list, verif, &ant, &actual); /*searches for the client cellphone*/ if(actual == NULL) /*If it doesn't exist, then the account doesn't exist either*/ { Client person; printf("\n\t\tThere's no account for that client. Let's create a new one.\n\n"); getchar(); person = create_account(); /*Than, let's create one*/ insert_client(list, &person); write_client(&person, "clients.txt"); book_it(&book,num_c, op); booking_validation(type,type2,&book,op); } else if(actual != NULL){ /*If the id exists, the account exists; Let's confirm by the name*/ /*Going to show cellphone and name, and then ask to confirm to proceed*/ printf("\n\t\tClient name: "); puts(actual->info.name); printf("\n\t\tCellphone number: %d\n\n", actual->info.cell); printf("\t\tConfirm? [y for yes; n for no]: ");getchar(); ans = getchar(); /*if confirmed, proceed to booking*/ if(ans == 'y' || ans == 'Y'){ book_it(&book, num_c, op); booking_validation(type,type2,&book,op); /*checks if book is valid or not*/ } } } else if(actual != NULL){ /*If the id exists, the account exists; Let's confirm by the name*/ /*Going to show cellphone and name, and then ask to confirm to proceed*/ printf("\n\t\tClient name: "); puts(actual->info.name); printf("\n\t\tCellphone number: %d\n\n", actual->info.cell); printf("\t\tConfirm? [y for yes; n for no]: ");getchar(); ans = getchar(); /*if confirmed, proceed to booking*/ if(ans == 'y' || ans == 'Y'){ book_it(&book, num_c, op); booking_validation(type,type2,&book,op); /*checks if book is valid or not*/ } } } if (op == 1){ write_to_file(type,"wash_books.txt"); } else if(op == 2){ write_to_file(type,"fix_books.txt"); } }