void connectClient(void) { char *server = getServidor(); char *port = getPuerto(); connectToServer(server, port); }
/** * @brief Sends a request to connect a client to a server using gtk fields. * @details */ void connectClient(void){ char *name, *real_name; char message[BUFFER]; /*Checks if it is already connected*/ semaphore_br(&readers_num, readers, writer, mutex_access, mutex_rvariables); if(connected){ interfaceText(NULL, "Ya está conectado a un servidor.", ERROR_TEXT, MAIN_THREAD); semaphore_ar(&readers_num, writer, mutex_rvariables); return; } semaphore_ar(&readers_num, writer, mutex_rvariables); /* Getting text of fields */ semaphore_bw(writer, readers); strcpy(nick, getApodo()); semaphore_aw(writer, readers); name = getNombre(); real_name = getNombreReal(); semaphore_bw(writer, readers); if(server_called){ server_called = 0; } else{ strcpy(client_server, getServidor()); port = getPuerto(); } semaphore_aw(writer, readers); /* Empty nick, name, real_name or client_server field */ semaphore_br(&readers_num, readers, writer, mutex_access, mutex_rvariables); if(!(strlen(nick) && strlen(name) && strlen(real_name) && strlen(client_server))) { interfaceErrorWindow("Los campos\n\t-Apodo\n\t-Nombre\n\t-Nombre real\n\t-Servidor\nno pueden estar vacíos", MAIN_THREAD); semaphore_ar(&readers_num, writer, mutex_rvariables); return; } semaphore_ar(&readers_num, writer, mutex_rvariables); /* Empty port field */ if(port <= 0){ port = DFLT_PORT; } /*Beginning of connection*/ sprintf(message, "Conectando con %s:%d ...", client_server, port); interfaceText(NULL, message, MSG_TEXT, MAIN_THREAD); /*Connecting to the server*/ if (client_connect_to_server(client_server, port, client_thread_listener) == ERROR){ sprintf(message, "Fallo en la conexión con %s\nEn el sistema de logs ha quedado registrado el error" "correspondiente a este intento de conexión.", client_server); interfaceErrorWindow(message, MAIN_THREAD); syslog(LOG_ERR, "Failed while connecting to %s: %s", client_server, strerror(errno)); interfaceText(NULL, "Error de conexión", ERROR_TEXT, MAIN_THREAD); return; } /*New session*/ semaphore_br(&readers_num, readers, writer, mutex_access, mutex_rvariables); if (client_new_session(nick, name, real_name, client_server) == ERROR){ sprintf(message, "Fallo en la conexión con %s\nEn el sistema de logs ha quedado registrado el error" "correspondiente a este intento de conexión.", client_server); interfaceErrorWindow(message, MAIN_THREAD); syslog(LOG_ERR, "Failed while connecting to %s: %s", client_server, strerror(errno)); interfaceText(NULL, "Error de conexión", ERROR_TEXT, MAIN_THREAD); cerrar_canal_SSL(ssl); semaphore_ar(&readers_num, writer, mutex_rvariables); return; } semaphore_ar(&readers_num, writer, mutex_rvariables); /*Connection successful*/ semaphore_bw(writer, readers); connected = 1; semaphore_aw(writer, readers); }