int server_do(int cs, char *cmd) { if (ft_strcmp(cmd, "quit") == 0) { printf("Client %d disconnected\n", cs); close(cs); exit(0); } else if (ft_strequ(cmd, "ls")) server_ls(cs); else if (ft_strcmp(cmd, "pwd") == 0) server_pwd(cs); else if (ft_strncmp(cmd, "mkdir ", 6) == 0) server_mkdir(cs, cmd + 6); else if (ft_strncmp(cmd, "put ", 4) == 0) server_put(cs, cmd + 4); else if (ft_strncmp(cmd, "get ", 4) == 0) server_get(cs, cmd + 4); else if (ft_strncmp(cmd, "cd ", 3) == 0) server_cd(cs, cmd + 3); else if (ft_strequ(cmd, "cd")) server_cd(cs, "/"); else server_sendbuf(cs, "ERROR: Incorrect command"); return (0); }
int main(int ac, char **av) { t_info *info; info = init(); parse_args(ac, av, info); if (info == 0) usage_server(); info->zone = create_world(info); add_server(info); server_get(info); free_info(info); return (0); }
void* thr_servidora() { while (TRUE) { sem_wait(&semaforo_servidora); buffer*aux; pthread_mutex_lock(&servidor); char *k,*v, *a; k=(char*)malloc(KV_SIZE*sizeof(char)); v=(char*)malloc(KV_SIZE*sizeof(char)); int ir; int d; aux=bf[podeServidor].buf; a=aux->nomeFuncao; ir=aux->shardId; k=aux->key; v=aux->value; d=aux->dim; podeServidor=(podeServidor+1)%tamanho_buf; pthread_mutex_unlock(&servidor); sem_post(&semaforo_cliente); pthread_mutex_lock(&acesso); if(strcmp(a,"g")==0) { server_get(aux, ir, k); } if(strcmp(a,"p")==0) { server_put(aux, ir, k,v); insereEmFicheiro(ir,k,v); } if(strcmp(a,"r")==0){ server_remove(aux, ir, k); removeFicheiro(ir,k); } if(strcmp(a,"k")==0) server_getAllKeys(aux, ir, d); pthread_mutex_unlock(&acesso); sem_post(&aux->semaforo_resposta); } return NULL; }
/** * @brief Processes commands from client shell and passes them to server_auth, server_get or server_set. * * @param sock The socket connected to the client. * @param cmd The command received from the client. * @return Returns 0 on success, 1 otherwise. */ int handle_command(int sock, char *cmd) { sprintf(log_buffer, "handle_command: Processing command '%s'\n", cmd); logger(server_log, log_buffer); // replace LOG commands with logger() calls char buf[MAX_CMD_LEN] = {0}; if(sscanf(cmd, "%s", buf) != 1) return 0; else if(strcmp(buf, "AUTH") == 0) server_auth(cmd); else if(strcmp(buf, "GET") == 0) { gettimeofday(&start_time, NULL); server_get(cmd); gettimeofday(&end_time, NULL); get_processing_time.tv_usec += (end_time.tv_sec - start_time.tv_sec)*1000000L + (end_time.tv_usec - start_time.tv_usec); fprintf(server_time_log, "get performed in %ld microseconds\n", (end_time.tv_sec - start_time.tv_sec)*1000000L + (end_time.tv_usec - start_time.tv_usec)); } else if(strcmp(buf, "SET") == 0) { gettimeofday(&start_time, NULL); server_set(cmd); gettimeofday(&end_time, NULL); set_processing_time.tv_usec += (end_time.tv_sec - start_time.tv_sec)*1000000L + (end_time.tv_usec - start_time.tv_usec); fprintf(server_time_log, "storage_set performed in %ld microseconds\n", (end_time.tv_sec - start_time.tv_sec)*1000000L + (end_time.tv_usec - start_time.tv_usec)); } else return 1; // Send back the response to the client. sendall(sock, cmd, strlen(cmd)); sendall(sock, "\n", 1); return 0; }