int main(int argc,char* argv[]){ char host_name[AM_MAX_MESSAGE]; BZERO(host_name,AM_MAX_MESSAGE); char num_avatars_str[AM_MAX_MESSAGE]; BZERO(num_avatars_str,AM_MAX_MESSAGE); char maze_difficulty_str[AM_MAX_MESSAGE]; BZERO(maze_difficulty_str,AM_MAX_MESSAGE); int num_avatars; int maze_difficulty; //calling function that will retrieve the arguments from the command line retrieve_arguments(argc,argv,host_name,num_avatars_str, maze_difficulty_str); //checking validity of those arguments int check = check_arguments(argc,host_name,num_avatars_str, maze_difficulty_str); if (check!=0){ printf("-n Usage: [nAvatars (0-10)]\n"); printf("-d [maze difficulty (0-9)]\n"); printf("-h [hostname (stratton.cs.dartmouth.edu)]\n"); return 1; } num_avatars=atoi(num_avatars_str); maze_difficulty=atoi(maze_difficulty_str); int sockfd; struct sockaddr_in servaddr; //establishing connection with the server and //constructing AM_INITIALIZE message AM_MESSAGE* sendline=malloc(sizeof(AM_MESSAGE)); MALLOC_CHECK(sendline); sendline->message_type=htonl(AM_INITIALIZE); sendline->msg.initialize.nAvatars=htonl(num_avatars); sendline->msg.initialize.Difficulty=htonl(maze_difficulty); AM_MESSAGE* recvline=malloc(sizeof(AM_MESSAGE)); MALLOC_CHECK(recvline); //creating a socket if ((sockfd=socket(AF_INET,SOCK_STREAM,0))<0){ printf("Failed to create the socket\n"); return 1; } //retrieve server IP from the hostname char serverIP[AM_MAX_MESSAGE]; BZERO(serverIP,AM_MAX_MESSAGE); check=retrieveIP(host_name,serverIP); if (check!=0){ return 1; } //creation of the socket memset(&servaddr,0,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=inet_addr(serverIP); servaddr.sin_port=htons(AM_SERVER_PORT); //establishing connection with the server if (connect(sockfd,(struct sockaddr *) &servaddr,sizeof(servaddr))<0){ printf("Could not connect to the server\n"); return 1; } //sending initialization message to the server send(sockfd,sendline,sizeof(AM_MESSAGE),0); //need to add more params if (recv(sockfd,recvline,sizeof(AM_MESSAGE),0)==0){ printf("Server terminated\n"); free(sendline); return 1; } if (ntohs(recvline->message_type) & AM_ERROR_MASK){ printf("Corrupted message from the server\n"); return 1; } free(sendline); //retrieving MazePort unsigned int maze_port=ntohl(recvline->msg.initialize_ok.MazePort); char MazePort[AM_MAX_MESSAGE]; BZERO(MazePort,AM_MAX_MESSAGE); snprintf(MazePort,AM_MAX_MESSAGE,"%d",maze_port); //getting maze dimensions unsigned int width=ntohl(recvline->msg.initialize_ok.MazeWidth); unsigned int height=ntohl(recvline->msg.initialize_ok.MazeHeight); //Creating shared map object //so that all of the avatars could share knowledge of the maze create_shared_map(width,height); free(recvline); //preparing to create a log file char logFileCommand[AM_MAX_MESSAGE]; BZERO(logFileCommand,AM_MAX_MESSAGE); char fileName[AM_MAX_MESSAGE]; BZERO(fileName,AM_MAX_MESSAGE); char *userName; userName=getenv("USER"); //getting user name to output in the log file if (userName==NULL){ printf("failed to retrieve user name\n"); return 1; }else{ strncpy(fileName,"Amazing_",AM_MAX_MESSAGE); strncat(fileName,userName,AM_MAX_MESSAGE); strncat(fileName,"_",AM_MAX_MESSAGE); strncat(fileName,num_avatars_str,AM_MAX_MESSAGE); strncat(fileName,"_",AM_MAX_MESSAGE); strncat(fileName,maze_difficulty_str,AM_MAX_MESSAGE); strncat(fileName,".log",AM_MAX_MESSAGE); } //creating a log file strncpy(logFileCommand,"echo -n "" > ",AM_MAX_MESSAGE); strncat(logFileCommand,fileName,AM_MAX_MESSAGE); system(logFileCommand); char date[AM_MAX_MESSAGE]; BZERO(date,AM_MAX_MESSAGE); time_t rawtime; struct tm *timeinfo; //getting current data and time time(&rawtime); timeinfo=localtime(&rawtime); strftime(date,AM_MAX_MESSAGE,"%Y-%m-%d-%H-%M-%S",timeinfo); //adding first line to the log file BZERO(logFileCommand,AM_MAX_MESSAGE); strncpy(logFileCommand,"echo ' ",AM_MAX_MESSAGE); strncat(logFileCommand,userName,AM_MAX_MESSAGE); strncat(logFileCommand,", ",AM_MAX_MESSAGE); strncat(logFileCommand,MazePort,AM_MAX_MESSAGE); strncat(logFileCommand,", ",AM_MAX_MESSAGE); strncat(logFileCommand,date,AM_MAX_MESSAGE); strncat(logFileCommand,";'",AM_MAX_MESSAGE); strncat(logFileCommand," >> ",AM_MAX_MESSAGE); strncat(logFileCommand,fileName,AM_MAX_MESSAGE); system(logFileCommand); //launching amazing client as many times as the number of avatars that //the iser inputted in the command line int avatar_id=0; for (int i=0;i<num_avatars;i++){ char amazingCommand[AM_MAX_MESSAGE]; BZERO(amazingCommand,AM_MAX_MESSAGE); char avatar_id_str[AM_MAX_MESSAGE]; BZERO(avatar_id_str,AM_MAX_MESSAGE); snprintf(avatar_id_str,AM_MAX_MESSAGE,"%d",avatar_id); strncpy(amazingCommand,"./amazing_client ",AM_MAX_MESSAGE); strncat(amazingCommand,avatar_id_str,AM_MAX_MESSAGE); strncat(amazingCommand," ",AM_MAX_MESSAGE); strncat(amazingCommand,num_avatars_str,AM_MAX_MESSAGE); strncat(amazingCommand," ",AM_MAX_MESSAGE); strncat(amazingCommand,maze_difficulty_str,AM_MAX_MESSAGE); strncat(amazingCommand," ",AM_MAX_MESSAGE); strncat(amazingCommand,serverIP,AM_MAX_MESSAGE); strncat(amazingCommand," ",AM_MAX_MESSAGE); strncat(amazingCommand,MazePort,AM_MAX_MESSAGE); strncat(amazingCommand," ",AM_MAX_MESSAGE); strncat(amazingCommand,fileName,AM_MAX_MESSAGE); strncat(amazingCommand," &",AM_MAX_MESSAGE); system(amazingCommand); avatar_id++; } //waits a little bit for children to terminate system("sleep 1"); return 0; }
int main (int argc, char *argv[]) { char my_ip[STRING_IP_SIZE]; int my_port = MY_PORT; char host_ip[STRING_IP_SIZE]; int host_port; char *resource = NULL, *response; socket_t *socket_tcp; unsigned short command_op, op, len; struct prompt_t prompt; int client_error = 0; /* Obtener los argumentos */ if ( retrieve_arguments(argc, argv, &host_port, host_ip, &resource) ) { printf("(cliente) el uso debe ser --> cliente -l SLR -p PSLR -r REC\n"); return -1; } /* Obtiene la ip del host local */ if( get_my_ip(my_ip) ) { printf("(cliente) no se puede obtener ip local.\n"); return -1; } /* Localizacion del recurso en el SLR */ if ( (locate_resource_from_slr( my_port, my_ip, &host_port, host_ip, resource )) ) { printf("(cliente) no se ha localizado el recurso en el slr.\n"); return -1; } // strncpy(host_ip, "192.168.1.34", 15); // host_port = 7000; printf("(cliente) recurso localizado en servidor SPR [con IP: %s y puerto remoto: %d].\n", host_ip, host_port); socket_tcp = socket_create(my_port, my_ip, host_port, host_ip, SOCK_STREAM); if ( socket_connect(socket_tcp ) ) { printf("(cliente) connect error.\n"); return -1; } /* Establece el timeout de respuestas */ socket_setrcvtimeout(socket_tcp, TIMEOUT); printf ("(cliente) conexion establecida con servidor SPR [con IP: %s y puerto remoto: %d].\n", host_ip, host_port); printf ("(cliente) enviando solicitud de conexion.\n"); if ( conection_request( socket_tcp ) ) { printf("(cliente) solicitud de conexion rechazada.\n"); return -1; } prompt_init(); do { prompt_read_command(&prompt); command_op = cmdtous(prompt.command); if ( command_op == OP_UNKNOW ) { printf ("(cliente) orden desconocida.\n"); continue; } if( !(client_error = operation_request(socket_tcp, command_op, prompt.parameters)) ) { op = OP_RESPONSE( socket_readushort(socket_tcp) ); if ( socket_tcp->expire_timeout ) break; len = socket_readushort(socket_tcp); if ( socket_tcp->expire_timeout ) break; response = socket_readnchar(socket_tcp, len); if ( socket_tcp->expire_timeout ) break; if (op != command_op && op != ERROR) { printf ("(cliente) recibida respuesta desconocida.\n"); client_error = 1; } else prompt_print(NULL, response); free(response); } } while( command_op != OP_SALIR && !client_error ); prompt_destroy(); /* Cierra el socket */ socket_destroy(socket_tcp); printf ("(cliente) conexion cerrada con servidor SPR.\n"); return(0); }