diff --git a/caco.png b/caco.png new file mode 100644 index 0000000000000000000000000000000000000000..ed7f7696f6c27460bde083a10214df0d9473b37f Binary files /dev/null and b/caco.png differ diff --git a/cores b/cores new file mode 100755 index 0000000000000000000000000000000000000000..ecd6d797398104dabe518c3a07986a9536ce2572 Binary files /dev/null and b/cores differ diff --git a/headers/client.h b/headers/client.h index 0200722b9fc928f538f35999443ef47077fd8599..d4cc3c9856fcca52e2f3f7b72eb3eeac7a8758e6 100644 --- a/headers/client.h +++ b/headers/client.h @@ -75,13 +75,13 @@ frame *client::receive_ack_nack() { do { retries++; - if ( ! (response = socket->receive_frame()) ) { continue; } - } while ( response == NULL && retries < NUM_RETRIES ); + if (!(response = socket->receive_frame())) { continue; } + } while (response == NULL && retries < NUM_RETRIES); - if (response && response->get_tipo() == ERRO) { - cout << BOLDYELLOW << "Espaco insulficiente no destino\n" << RESET; - return NULL; - } + // if (response && response->get_tipo() == ERRO) { + // cout << BOLDYELLOW << "Espaco insulficiente no destino\n" << RESET; + // return NULL; + // } } while (response && !(verify_ack_nack(response))); return response; @@ -97,8 +97,9 @@ frame *client::send_frame_socket(frame *f) { if (!response) return NULL; } while (response->get_dado()[0] != f->get_seq()); - cout << "\tACK recebido:\n"; - response->imprime(DEC); + // cout << "\tACK recebido:\n"; // ->log + // response->imprime(DEC); // ->log + return response; } @@ -119,15 +120,15 @@ int client::start_transmission() { // Encerra a transmissao com o servidor int client::end_transmission() { - cout << "\tEncerrando a transmissao\n";// ->log + cout << "\tEncerrando a transmissao\n"; // ->log frame *end = new frame(FIMT, 0, vector<char>(1, 0)); frame *enviado = send_frame_socket(end); if (!enviado) { - cout << "\tFalha ao encerrar a transmissao\n";// ->log + cout << "\tFalha ao encerrar a transmissao\n"; // ->log return 0; } - cout << "\tTransmissao encerrada com sucesso\n";// ->log + cout << "\tTransmissao encerrada com sucesso\n"; // ->log return 1; } @@ -147,7 +148,7 @@ int client::send_frames(vector<frame *> frames) { // Adiciona o frame de fim de transmissao int next_seq = frames.back()->get_seq() + 1; - frame *end = new frame(FIMT, next_seq, vector<char>(1, next_seq )); + frame *end = new frame(FIMT, next_seq, vector<char>(1, next_seq)); frames.push_back(end); // Cria a fila de frames @@ -161,19 +162,20 @@ int client::send_frames(vector<frame *> frames) { if (iniJanela + frameCounter == frames.size()) { break; } janela.push((iniJanela + frameCounter) % 16); - cout << "\tEnviando frame\n"; - frames[iniJanela + frameCounter]->imprime(DEC); + // cout << "\tEnviando frame\n"; ->log + // frames[iniJanela + frameCounter]->imprime(DEC); ->log if (socket->send_frame(frames[iniJanela + frameCounter]) == -1) { - cout << "Falha ao enviar o frame\n"; + // cout << "Falha ao enviar o frame\n"; ->log return 0; } - cout << "\tFrame enviado com sucesso\n"; + // cout << "\tFrame enviado com sucesso\n"; ->log } // Recebe a resposta do servidor - while ( !janela.empty() ) { + while (!janela.empty()) { + cout << "Janela size: " << janela.size() << "\n"; frame *res = NULL; int retries = 0; @@ -181,7 +183,7 @@ int client::send_frames(vector<frame *> frames) { retries++; res = receive_ack_nack(); } while (res == NULL && retries < NUM_RETRIES); - + if (res == NULL && retries == NUM_RETRIES) { break; } cout << "Resposta recebida\n"; @@ -189,11 +191,19 @@ int client::send_frames(vector<frame *> frames) { << "Janela front: " << janela.front() << "\n"; if (res->get_tipo() == NACK && res->get_dado()[0] == janela.front()) { - cout << "NACK " << (int)res->get_dado()[0] << " recebido\n"; + // cout << "NACK " << (int)res->get_dado()[0] << " recebido\n"; ->log iniJanela += res->get_dado()[0]; janela.pop(); break; } + if (res->get_tipo() == ERRO) { + cout << BOLDBLUE << "\tErro recebido: " << RED << res->get_dado() << "\n" + << RESET; + + janela = queue<int>(); + frames.clear(); + return 0; + } if (res->get_tipo() == ACK && res->get_dado()[0] == janela.front()) { cout << "ACK " << (int)res->get_dado()[0] << " recebido\n"; @@ -205,7 +215,6 @@ int client::send_frames(vector<frame *> frames) { // apaga a janela while (!janela.empty()) janela.pop(); - } cout << "\tTerminou de enviar todos os frames\n"; //->log @@ -222,7 +231,11 @@ int client::send_frames(vector<frame *> frames) { * @return false */ bool client::verify_ack_nack(frame *received) { - return ((received->get_tipo() == ACK || received->get_tipo() == NACK) && + // received error frame + // cout << "Verificando ACK/NACK/Error\n"; ->log + + return ((received->get_tipo() == ACK || received->get_tipo() == NACK || + received->get_tipo() == ERRO) && received->chk_crc8()); } @@ -282,7 +295,7 @@ void client::send_file() { string fileName; do { - cout << BOLDWHITE << "Digite o nome do arquivo(maximo de " << TAM_DADOS + cout << BOLDYELLOW << "Digite o nome do arquivo(maximo de " << TAM_DADOS << " char):\n" << RESET; getline(cin, fileName); @@ -291,7 +304,7 @@ void client::send_file() { fileNameVector.insert(fileNameVector.begin(), fileName.begin(), fileName.end()); - cout << BOLDYELLOW << "\t--Enviando arquivo--\n" << RESET; + cout << BOLDBLUE << "\t--Enviando arquivo--\n" << RESET; if (!send_message(fileNameVector, MIDIA)) { cout << BOLDRED << "\t--Falha ao enviar o arquivo--\n" << RESET; return; @@ -307,11 +320,11 @@ void client::send_file() { */ void client::send_text(string message) { - cout << BOLDYELLOW << "\t--Enviando mensagem--\n" << RESET; + cout << BOLDBLUE << "\t--Enviando mensagem--\n" << RESET; vector<char> data(message.begin(), message.end()); if (!send_message(data, TEXTO)) - cout << BOLDRED << "\t--Limite de timout, mensagem nao foi enviada--\n" + cout << RED << "\t--Limite de timout, mensagem nao foi enviada--\n" << RESET; else cout << GREEN << "\t--Mensagem enviada com sucesso--\n" << RESET; @@ -323,7 +336,7 @@ vector<frame *> client::create_frames_midia(vector<char> vectorName) { // Cria um vetor com o tamanho do arquivo vector<char> vectorTam; string fileName = string(vectorName.begin(), vectorName.end()); - cout << "Nome do arquivo- create frames midia: " << fileName << endl; + // cout << "Nome do arquivo- create frames midia: " << fileName << endl; string fileSize = calc_file_size(fileName); if (fileSize.empty()) { return vector<frame *>(); } vectorTam.insert(vectorTam.begin(), fileSize.begin(), fileSize.end()); @@ -339,7 +352,7 @@ vector<frame *> client::create_frames_midia(vector<char> vectorName) { // Cria um vetor com os dados do arquivo vector<char> vectorData = read_file(fileName); if (vectorData.empty()) { - cout << BOLDRED << "\t--Falha ao abrir o arquivo para leitura--\n" + cout << REG_RDI << "\t--Falha ao abrir o arquivo para leitura--\n" << RESET; //->log return vector<frame *>(); } @@ -412,7 +425,7 @@ void client::run() { int i; print_help(); while (true) { - cout << BOLDWHITE << "\nDigite um comando ou mensagem:\n" << RESET; + cout << BOLDYELLOW << "\nDigite um comando ou mensagem:\n" << RESET; getline(cin, userInput); char userInputCMD = string_cmd(userInput); @@ -423,7 +436,7 @@ void client::run() { break; case 'e': - cout << BOLDYELLOW << "\tSaindo...\n" << RESET; + cout << BOLDBLUE << "\tSaindo...\n" << RESET; exit(0); break; diff --git a/headers/conexao.h b/headers/conexao.h index 2b1a362b8943e8b6a85f1967c1826a51d8416ed1..378cd1261e8ae935149cf529d761ca5243e63be9 100644 --- a/headers/conexao.h +++ b/headers/conexao.h @@ -139,8 +139,9 @@ int conexao::add_escapes(char *f, char *out) { for (size_t i = 0; i < sizeof(frame); i++) { out[j++] = f[i]; - + if (f[i] == 0x88 || f[i] == 0x81) out[j++] = 0xFF; + } return j; diff --git a/headers/server.h b/headers/server.h index c416286b0e214cc1c562f10506989b441bbfa0a2..e4cb4f7b0e1b33218c8aa934c07ff3a7379cc089 100644 --- a/headers/server.h +++ b/headers/server.h @@ -46,6 +46,7 @@ private: frame *create_ack_nack(int tipo, int seq); int send_nack(frame *fReceive); int send_ack(frame *fReceive); + int send_error(frame *fReceive, string msg); bool verify_seq(int seq, int lastSeq); int next_tipo_midia(frame *f); bool create_received_dir(); @@ -87,6 +88,28 @@ frame *server::create_ack_nack(int tipo, int seq) { return f; } + +/** + * @brief function that send an error frame to the target + * + */ +int server::send_error(frame *fReceive, string msg) { + frame *error = new frame(); + error->set_tipo(ERRO); + vector<char> msg_error = vector<char>(msg.begin(), msg.end()); + error->set_dado(msg_error); + + int errorSent = socket->send_frame(error); + if (errorSent == -1) { + // cout << "Falha ao enviar o erro\n"; -> log + return -1; + } + + // cout << "Erro enviado\n"; -> log + + return errorSent; +} + /** * @brief function that sends a ack frame to the target * @@ -150,7 +173,7 @@ int server::next_tipo_midia(frame *f) { if (f->get_tipo() != MIDIA) { return -1; } if (f->get_seq() == 0) { return MIDIA; } if (f->get_seq() == 1) { - cout << YELLOW << "\t--Recebendo dados arquivo--\n" << RESET; + cout << BOLDBLUE << "\t--Recebendo dados arquivo--\n" << RESET; return DADOS; } @@ -235,35 +258,28 @@ string server::create_file_destination(string fileName) { fileDestination.push_back('/'); fileDestination.append(fileName); + cout << BOLDYELLOW << "Arquivo será salvo em: " << BOLDWHITE + << fileDestination << BOLDYELLOW + << ". Digite novo nome ou enter para continuar: " << RESET; + string newDestination = ""; + getline(cin, newDestination); + if (!newDestination.empty() && newDestination != "\n") { + fileDestination = newDestination; + } + return fileDestination; } ofstream server::create_file(string fileName) { - cout << "Criando arquivo\n"; - cout << "Nome do arquivo: " << fileName << "\n"; // long long start = socket->timestamp(); // struct timeval timeout = {.tv_sec = 0, .tv_usec = socket->timeoutMillis * // 1000}; - string fileDestination = create_file_destination(fileName); - - // cout << BOLDWHITE << "Criando arquivo " << BOLDYELLOW << fileDestination - // << BOLDWHITE << ". Digite novo nome ou enter para continuar: " << - // RESET; - - // string newDestination = ""; - // do { - // getline(cin, newDestination); - // } while (socket->timestamp() - start <= socket->timeoutMillis); - - // if (!newDestination.empty() && newDestination != "\n") { - // fileDestination = newDestination; - // } - cout << YELLOW << "\t--Criando arquivo: " << fileDestination << "--\n" - << RESET; + string fileDestination = fileName.c_str(); // Abre o arquivo para escrita ofstream file; file.open(fileDestination, ios::binary); + return file; } @@ -284,12 +300,24 @@ int server::receive_midia(frame *f, ofstream *file) { // Segundo frame de midia string fileName = string(f->get_dado()); - (*file) = create_file(fileName); + //-- Aceita ou não o arquivo --// + cout << BOLDBLUE << "\t--Recebendo arquivo: " << fileName << "--\n" << RESET; + cout << BOLDYELLOW << "Aceitar arquivo? (s/n): " << RESET; + string accept; + cin >> accept; + if (accept != "s") { + cout << BOLDRED << "\t--Arquivo rejeitado--\n" << RESET; + send_error(f, "Arquivo rejeitado"); + return 0; + } + + string fileCurrentDir = create_file_destination(fileName); + (*file) = create_file(fileCurrentDir); if (!(*file).is_open()) { cout << RED << "\tFalha ao criar o arquivo. Abortado\n" << RESET; (*file).close(); - remove(create_file_destination(fileName).c_str()); + remove(fileCurrentDir.c_str()); return 0; } @@ -309,7 +337,7 @@ frame *server::receive_frame_socket() { } while (fReceive == NULL && retries < NUM_RETRIES); if (fReceive == NULL && retries == NUM_RETRIES) { - cout << "Desisti de receber o frame\n"; //->log + cout << "Desisti de receber o frame\n"; //->log return NULL; } @@ -375,7 +403,6 @@ queue<frame *> server::receive_frames_window(int lastSeq) { } while (frames_queue.size() < TAM_JANELA); - return frames_queue; } @@ -383,17 +410,18 @@ void server::start_receveing_message() { int continueTransmission = 1; int lastSeq = -1; int tipo_data = -1; + int qtdeMidia = 0; vector<char> data; ofstream file; queue<frame *> client_answer; // Fica ouvindo o cliente ate receber um FIMT do { - cout << "Recebendo frames\n"; + // cout << "Recebendo frames\n"; queue<frame *> frames = receive_frames_window(lastSeq); - if ( frames.empty() ) { break; } + if (frames.empty()) { break; } - cout << "Quantidade de frames na janela: " << frames.size() << "\n"; + // cout << "Quantidade de frames na janela: " << frames.size() << "\n"; // Ve o que faz com cada frame de acordo com o tipo while (!frames.empty()) { @@ -410,7 +438,7 @@ void server::start_receveing_message() { client_answer.push(create_ack_nack(ACK, f->get_seq())); } - cout << "Frame recebido: \n"; + // cout << "Frame recebido: \n"; ->log f->imprime(DEC); cout << "\n"; @@ -446,24 +474,24 @@ void server::start_receveing_message() { lastSeq = f->get_seq(); } - cout << "Recebeu todos os frames de uma janela\n"; + // cout << "Recebeu todos os frames de uma janela\n"; // Envia a reposta ao cliente - cout << "Enviando acks e nacks para o cliente\n"; + // cout << "Enviando acks e nacks para o cliente\n"; while (!client_answer.empty()) { frame *f_answer = client_answer.front(); client_answer.pop(); if (socket->send_frame(f_answer) == -1) { - cout << "Falha ao enviar a resposta\n"; + // cout << "Falha ao enviar a resposta\n"; ->log return; } - if (f_answer->get_tipo() == NACK) - cout << "NACK " << (int)f_answer->get_dado()[0] << " enviado\n"; + // if (f_answer->get_tipo() == NACK) + // cout << "NACK " << (int)f_answer->get_dado()[0] << " enviado\n"; - else - cout << "ACK " << (int)f_answer->get_dado()[0] << " enviado\n"; + // else + // cout << "ACK " << (int)f_answer->get_dado()[0] << " enviado\n"; ->log } cout << "Todos os ACKs e NACKs foram enviados\n"; diff --git a/received/caco.png b/received/caco.png new file mode 100644 index 0000000000000000000000000000000000000000..781ac8ded3238994b875d736a8be8364bbc73b8d Binary files /dev/null and b/received/caco.png differ diff --git a/testecor.cpp b/testecor.cpp new file mode 100644 index 0000000000000000000000000000000000000000..79228838598e28fc3e24042b013d6a023efd31dd --- /dev/null +++ b/testecor.cpp @@ -0,0 +1,24 @@ +#include "./headers/cores.h" +#include <iostream> + +int main() { + std::cout << BLACK << "Black\n"; + std::cout << RED << "Red\n"; + std::cout << GREEN << "Green\n"; + std::cout << YELLOW << "Yellow\n"; + std::cout << BLUE << "Blue\n"; + std::cout << MAGENTA << "Magenta\n"; + std::cout << CYAN << "Cyan\n"; + std::cout << WHITE << "White\n"; + std::cout << BOLDBLACK << "Bold Black\n"; + std::cout << BOLDRED << "Bold Red\n"; + std::cout << BOLDGREEN << "Bold Green\n"; + std::cout << BOLDYELLOW << "Bold Yellow\n"; + std::cout << BOLDBLUE << "Bold Blue\n"; + std::cout << BOLDMAGENTA << "Bold Magenta\n"; + std::cout << BOLDCYAN << "Bold Cyan\n"; + std::cout << BOLDWHITE << "Bold White\n"; + std::cout << RESET << "Default\n"; + + return 0; +} \ No newline at end of file