// Main service loop of datanode int mainLoop() { int server_socket = -1; //Create a server socket and listen on it, you can implement dfs_common.c and call it here server_socket = create_server_tcp_socket(datanode_listen_port); assert (server_socket != INVALID_SOCKET); // Listen to requests from the clients for (;;) { struct sockaddr_in client_address; int client_address_length = sizeof(client_address); int client_socket = -1; //Accept the client request client_socket = accept(server_socket, (struct sockaddr*) &client_address, &client_address_length); assert(client_socket != INVALID_SOCKET); dfs_cli_dn_req_t request; //Receive data from client_socket, and fill it to request receive_data(client_socket, &request, sizeof(request)); requests_dispatcher(client_socket, request); close(client_socket); } close(server_socket); return 0; }
int main(int argc, char **argv){ system("clear"); fprintf(stdout, "Server start\n"); //check parameters (Must be: port_number) if(argc != 2){ usage(argv[0]); } //Init signal process //TODO To update sigset_t mask, oldmask; sethandler(SIG_IGN, SIGPIPE); //sethandler(siginthandler, SIGINT); sigemptyset(&mask); sigaddset(&mask, SIGINT); //sigprocmask(SIG_BLOCK, &mask, &oldmask); //Create the server socket, bind it, start listening int sock = create_server_tcp_socket(atoi(argv[1]), BACKLOG); if(sock < 0){ fprintf(stderr, "Unable to start the server (Unable to create the socket)...\n"); return EXIT_FAILURE; } //Initialize server data ServerData server; server_data_init(&server); User *admin = user_create("admin"); //Admin user just for the default room server_data_add_room(&server, admin, ROOM_WELCOME_NAME); //Start listening for new clients server_start_listening_clients(&server, sock); //Close the socket fprintf(stdout, "Server is closing. Close socket...\n"); if(TEMP_FAILURE_RETRY(close(sock)) < 0){ fprintf(stderr, "Error while closing the socket...\n"); return EXIT_FAILURE; } fprintf(stdout, "Server is stopped\n"); return EXIT_SUCCESS; }
// main service loop of datanode int mainLoop() { //we don't consider concurrent operations in this assignment int server_socket = -1; //TODO: create a server socket and listen on it, you can implement dfs_common.c and call it here server_socket = create_server_tcp_socket(datanode_listen_port); assert (server_socket != INVALID_SOCKET); printf("dfs_datanode.c: mainloop(): Entering for loop to listen for clients. \n"); // Listen to requests from the clients for (;;) { sockaddr_in client_address; int client_socket = -1; //TODO: accept the client request int sin_len = sizeof(struct sockaddr_in); // int accept (int socket, struct sockaddr *addr, socklen_t *length_ptr) printf("dfs_datanode.c: mainloop(): Waiting to accept. \n"); client_socket = accept(server_socket, (struct sockaddr *)&client_address, &sin_len); // &sizeof?? sizeof(client_address) assert(client_socket != INVALID_SOCKET); dfs_cli_dn_req_t request; //TODO: receive data from client_socket, and fill it to request // ssize_t recv (int socket, void *buffer, size_t size, int flags) recv(client_socket, &request, sizeof(request), MSG_WAITALL); //printf("dfs_datanode.c: mainloop(): request.block.content: %s \n",request.block.content); printf("dfs_datanode.c: mainloop(): Received request from client, file: %s, block_id: %i, operation: %i(0 read, 1 write). \n", request.block.owner_name,request.block.block_id,request.op_type); requests_dispatcher(client_socket, request); close(client_socket); } close(server_socket); return 0; }