Skip to content
Snippets Groups Projects
Commit d24ac469 authored by Nico Ramos's avatar Nico Ramos
Browse files

ksadfhjk

parent 67481d5f
Branches
No related tags found
No related merge requests found
Writing this to a file.
This diff is collapsed.
foto.jpg

133 KiB

...@@ -71,7 +71,13 @@ frame *client::receive_ack_nack() { ...@@ -71,7 +71,13 @@ frame *client::receive_ack_nack() {
// se recebemos algo, e NÃO ẽ o ACK que estamos // se recebemos algo, e NÃO ẽ o ACK que estamos
// esperando, continuamos tentando receber // esperando, continuamos tentando receber
do { do {
response = socket->receive_frame(); int retries = 0;
do {
retries++;
if ( ! (response = socket->receive_frame()) ) { continue; }
} while ( response == NULL && retries < NUM_RETRIES );
if (response && response->get_tipo() == ERRO) { if (response && response->get_tipo() == ERRO) {
cout << BOLDYELLOW << "Espaco insulficiente no destino\n" << RESET; cout << BOLDYELLOW << "Espaco insulficiente no destino\n" << RESET;
return NULL; return NULL;
...@@ -81,25 +87,15 @@ frame *client::receive_ack_nack() { ...@@ -81,25 +87,15 @@ frame *client::receive_ack_nack() {
return response; return response;
} }
// Solicita ao socket que envie um frame
frame *client::send_frame_socket(frame *f) { frame *client::send_frame_socket(frame *f) {
// Fica tentando enviar o frame até receber o ack
frame *response = new frame(); frame *response = new frame();
int retries = 0;
do { do {
// envia um frame da fila
int bytesSent = socket->send_frame(f); int bytesSent = socket->send_frame(f);
if (bytesSent == -1) { return NULL; } if (bytesSent == -1) { return NULL; }
response = receive_ack_nack(); response = receive_ack_nack();
if (!response) return NULL; if (!response) return NULL;
retries++; } while (response->get_dado()[0] != f->get_seq());
} while (response->get_dado()[0] != f->get_seq() && retries < NUM_RETRIES);
if (response == NULL && retries == NUM_RETRIES) {
cout << "Desisti de enviar\n";
return NULL;
}
cout << "\tACK recebido:\n"; cout << "\tACK recebido:\n";
response->imprime(DEC); response->imprime(DEC);
...@@ -123,15 +119,15 @@ int client::start_transmission() { ...@@ -123,15 +119,15 @@ int client::start_transmission() {
// Encerra a transmissao com o servidor // Encerra a transmissao com o servidor
int client::end_transmission() { 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 *end = new frame(FIMT, 0, vector<char>(1, 0));
frame *enviado = send_frame_socket(end); frame *enviado = send_frame_socket(end);
if (!enviado) { if (!enviado) {
// cout << "\tFalha ao encerrar a transmissao\n"; ->log cout << "\tFalha ao encerrar a transmissao\n";// ->log
return 0; return 0;
} }
// cout << "\tTransmissao encerrada com sucesso\n"; ->log cout << "\tTransmissao encerrada com sucesso\n";// ->log
return 1; return 1;
} }
...@@ -149,19 +145,24 @@ int client::send_frames(vector<frame *> frames) { ...@@ -149,19 +145,24 @@ int client::send_frames(vector<frame *> frames) {
if (!start_transmission()) { return 0; } if (!start_transmission()) { return 0; }
cout << "\t ->>> started transmission <<< -\n"; cout << "\t ->>> started transmission <<< -\n";
// 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 ));
frames.push_back(end);
// Cria a fila de frames // Cria a fila de frames
queue<int> janela; queue<int> janela;
int frameCounter; int frameCounter = 0;
int iniJanela = 0; long long iniJanela = 0;
while (iniJanela < frames.size()) { while (iniJanela < frames.size()) {
// manda todos os frames de uma vez só // manda todos os frames de uma vez só
for (frameCounter = 0; frameCounter < TAM_JANELA; frameCounter++) { for (frameCounter = 0; frameCounter < TAM_JANELA; frameCounter++) {
if (iniJanela + frameCounter == frames.size()) { break; } if (iniJanela + frameCounter == frames.size()) { break; }
janela.push(frameCounter); janela.push((iniJanela + frameCounter) % 16);
cout << "\tEnviando frame\n"; cout << "\tEnviando frame\n";
frames[iniJanela]->imprime(DEC); frames[iniJanela + frameCounter]->imprime(DEC);
if (socket->send_frame(frames[iniJanela + frameCounter]) == -1) { if (socket->send_frame(frames[iniJanela + frameCounter]) == -1) {
cout << "Falha ao enviar o frame\n"; cout << "Falha ao enviar o frame\n";
...@@ -172,16 +173,21 @@ int client::send_frames(vector<frame *> frames) { ...@@ -172,16 +173,21 @@ int client::send_frames(vector<frame *> frames) {
} }
// Recebe a resposta do servidor // Recebe a resposta do servidor
for (int i = 0; i < min((int)TAM_JANELA, (int)frames.size()); i++) { while ( !janela.empty() ) {
//for (int i = 0; i < min((int)TAM_JANELA, (int)frames.size()); i++) {
frame *res = NULL; frame *res = NULL;
int retries = 0; int retries = 0;
do { do {
res = receive_ack_nack();
retries++; retries++;
res = receive_ack_nack();
} while (res == NULL && retries < NUM_RETRIES); } while (res == NULL && retries < NUM_RETRIES);
if (res == NULL && retries == NUM_RETRIES) { return 0; } if (res == NULL && retries == NUM_RETRIES) { break; }
cout << "Resposta recebida\n";
cout << "Numero ack/nack: " << (int)res->get_dado()[0] << " ---- "
<< "Janela front: " << janela.front() << "\n";
if (res->get_tipo() == NACK && res->get_dado()[0] == janela.front()) { 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";
...@@ -196,18 +202,18 @@ int client::send_frames(vector<frame *> frames) { ...@@ -196,18 +202,18 @@ int client::send_frames(vector<frame *> frames) {
janela.pop(); janela.pop();
} }
else { //else {
i--; // i--;
} //}
} }
// apaga a janela // apaga a janela
while (!janela.empty()) while (!janela.empty())
janela.pop(); janela.pop();
} }
if (!end_transmission()) { return 0; } cout << "\tTerminou de enviar todos os frames\n"; //->log
// cout << "\tTerminou de enviar todos os frames\n"; ->log
return 1; return 1;
} }
......
...@@ -23,8 +23,8 @@ ...@@ -23,8 +23,8 @@
#include "frame.h" #include "frame.h"
using namespace std; using namespace std;
#define NUM_RETRIES 50 #define NUM_RETRIES 10
#define TAM_JANELA 2 #define TAM_JANELA 16
class conexao { class conexao {
private: private:
...@@ -50,7 +50,7 @@ private: ...@@ -50,7 +50,7 @@ private:
void close_connection(); // fecha a conexao void close_connection(); // fecha a conexao
public: public:
int timeoutMillis = 500; // Tempo que fica tentando ler int timeoutMillis = 200; // Tempo que fica tentando ler
// ------ Construtor ------ // // ------ Construtor ------ //
conexao(char *deviceIP); conexao(char *deviceIP);
......
...@@ -138,7 +138,7 @@ int server::send_nack(frame *fReceive) { ...@@ -138,7 +138,7 @@ int server::send_nack(frame *fReceive) {
*/ */
bool server::verify_seq(int seq, int lastSeq) { bool server::verify_seq(int seq, int lastSeq) {
if (seq == 0) { if (seq == 0) {
if (lastSeq != TAM_JANELA - 1) { return false; } if (lastSeq != 15) { return false; }
return true; return true;
} }
...@@ -229,55 +229,6 @@ bool server::create_received_dir() { ...@@ -229,55 +229,6 @@ bool server::create_received_dir() {
return true; return true;
} }
// int server::receive_file_data(string fileName) {
//
// int lastSeq = 1;
// frame *f;
//
// cout << "\tRecebendo dados arquivo\n";
// do {
// cout << "Aguardando frame\n";
//
// // Fica tentando receber um frame
// f = receive_frame_socket();
// if (f == NULL) { return 0; }
//
// cout << "Frame recebido\n";
// f->imprime(HEX);
//
// if (f->get_tipo() == FIMT) { break; }
//
// if (f->get_tipo() != DADOS) { continue; }
//
// if (f->get_seq() == lastSeq) { continue; }
//
// // Recebeu um frame com uma sequencia errada
// if (!verify_seq(f->get_seq(), lastSeq)) {
// cout << "Frame com a sequencia errada; Pedindo a certa\n";
// f->set_seq(lastSeq);
// send_ack(f);
// continue;
// }
//
// if (!verify_crc8(f)) {
// cout << "Crc invalido\n";
// continue;
// }
//
// cout << "Seq " << int(f->get_seq()) << "recebida com sucesso\n";
// lastSeq = f->get_seq();
// file.write(f->get_dado(), f->get_tam());
//
// } while (true);
//
// cout << "Dados do arquivo recebido com sucesso\n";
// send_ack(f);
//
// file.close();
// return 1;
// }
//
string server::create_file_destination(string fileName) { string server::create_file_destination(string fileName) {
string fileDestination; string fileDestination;
fileDestination.append(FILE_DESTINATION); fileDestination.append(FILE_DESTINATION);
...@@ -358,7 +309,7 @@ frame *server::receive_frame_socket() { ...@@ -358,7 +309,7 @@ frame *server::receive_frame_socket() {
} while (fReceive == NULL && retries < NUM_RETRIES); } while (fReceive == NULL && retries < NUM_RETRIES);
if (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; return NULL;
} }
...@@ -368,34 +319,34 @@ frame *server::receive_frame_socket() { ...@@ -368,34 +319,34 @@ frame *server::receive_frame_socket() {
queue<frame *> server::receive_frames_window(int lastSeq) { queue<frame *> server::receive_frames_window(int lastSeq) {
queue<frame *> frames_queue; queue<frame *> frames_queue;
frame *f; frame *f;
int retries = 0;
do { do {
if (!(f = receive_frame_socket())) { continue; } cout << "Recebendo frame\n";
if (!(f = receive_frame_socket())) { break; }
retries++; cout << "Frame recebido:\n";
int tipo = f->get_tipo(); int tipo = f->get_tipo();
f->imprime(DEC);
// Adiciona o frame de fim de transmissao // Adiciona o frame de fim de transmissao
if (tipo == FIMT) { if (tipo == FIMT) {
cout << "Frame FIMT recebido\n";
frames_queue.push(f); frames_queue.push(f);
return frames_queue; break;
} }
// Primeiro frame a ser recebido, seta o tipo // Primeiro frame a ser recebido, seta o tipo
if (lastSeq == -1) { else if (lastSeq == -1) {
// Ignora os frames perdidos na linha // Ignora os frames perdidos na linha
if ((tipo != MIDIA && tipo != TEXTO) || f->get_seq() != 0) { continue; } if ((tipo != MIDIA && tipo != TEXTO) || f->get_seq() != 0) { continue; }
tipoReceivingFrames = f->get_tipo(); tipoReceivingFrames = f->get_tipo();
frames_queue.push(f); frames_queue.push(f);
lastSeq = 0; lastSeq = 0;
retries = 0;
continue; continue;
} }
// Ignora se o frame nao for do tipo midia e esteja recebendo midia // Ignora se o frame nao for do tipo midia e esteja recebendo midia
if (tipo == MIDIA && tipoReceivingFrames == MIDIA) { else if (tipo == MIDIA && tipoReceivingFrames == MIDIA) {
// Ignora se for um frame do tipo midia que nao e o segundo da sequencia // Ignora se for um frame do tipo midia que nao e o segundo da sequencia
if (lastSeq != 0 || f->get_seq() != 1) { continue; } if (lastSeq != 0 || f->get_seq() != 1) { continue; }
...@@ -403,32 +354,27 @@ queue<frame *> server::receive_frames_window(int lastSeq) { ...@@ -403,32 +354,27 @@ queue<frame *> server::receive_frames_window(int lastSeq) {
tipoReceivingFrames = DADOS; tipoReceivingFrames = DADOS;
frames_queue.push(f); frames_queue.push(f);
lastSeq = f->get_seq(); lastSeq = f->get_seq();
retries = 0;
continue; continue;
} }
// Recebe os frames de dados de um arquivo // Recebe os frames de dados de um arquivo
if (tipoReceivingFrames == DADOS && tipo == DADOS) { else if (tipoReceivingFrames == DADOS && tipo == DADOS) {
if (!verify_seq(f->get_seq(), lastSeq)) { continue; } if (!verify_seq(f->get_seq(), lastSeq)) { continue; }
frames_queue.push(f); frames_queue.push(f);
retries = 0;
lastSeq = f->get_seq(); lastSeq = f->get_seq();
continue; continue;
} }
// Recebe os frames de uma mensagem // Recebe os frames de uma mensagem
if (tipoReceivingFrames == TEXTO && tipo == TEXTO) { else if (tipoReceivingFrames == TEXTO && tipo == TEXTO) {
if (!verify_seq(f->get_seq(), lastSeq)) { continue; } if (!verify_seq(f->get_seq(), lastSeq)) { continue; }
frames_queue.push(f); frames_queue.push(f);
retries = 0;
lastSeq = f->get_seq(); lastSeq = f->get_seq();
continue; continue;
} }
} while ((f == NULL && retries < NUM_RETRIES) || } while (frames_queue.size() < TAM_JANELA);
frames_queue.size() < TAM_JANELA);
if (f == NULL && retries == NUM_RETRIES) { return queue<frame *>(); }
return frames_queue; return frames_queue;
} }
...@@ -441,11 +387,11 @@ void server::start_receveing_message() { ...@@ -441,11 +387,11 @@ void server::start_receveing_message() {
ofstream file; ofstream file;
queue<frame *> client_answer; queue<frame *> client_answer;
cout << "Recebendo frames\n";
// Fica ouvindo o cliente ate receber um FIMT // Fica ouvindo o cliente ate receber um FIMT
do { do {
cout << "Recebendo frames\n";
queue<frame *> frames = receive_frames_window(lastSeq); queue<frame *> frames = receive_frames_window(lastSeq);
if (frames.empty()) { return; } if ( frames.empty() ) { break; }
cout << "Quantidade de frames na janela: " << frames.size() << "\n"; cout << "Quantidade de frames na janela: " << frames.size() << "\n";
......
...@@ -3,7 +3,6 @@ CPPFLAGS = -g -std=c++20 ...@@ -3,7 +3,6 @@ CPPFLAGS = -g -std=c++20
SRC = $(wildcard src/*.cpp) SRC = $(wildcard src/*.cpp)
OBJ = $(SRC:.cpp=.o) OBJ = $(SRC:.cpp=.o)
# O nome do target deve ser o nome do arquivo principal # O nome do target deve ser o nome do arquivo principal
TARGET = exemplo TARGET = exemplo
......
naruto.jpg

7.51 KiB

received/foto.jpg

133 KiB

received/naruto.jpg

7.51 KiB

//string fileSize = calc_file_size(fileName);
//if (fileSize.empty()) { return; }
//franesFile.insert(framesFile.end(), fileSize.begin(), fileSize.end());
//cout << "Tamanho do arquivo: " << fileSize << "\n";
//cout << "Enviando tamanho do arquivo\n";
//if (!send_message(vector<char>(fileSize.begin(), fileSize.end()), MIDIA)) {
// cout << "Limite de timout, arquivo nao foi enviado\n";
// return;
//}
// Envia o segundo frame com o nome do arquivo
//cout << "Enviando nome do arquivo\n";
//string name = "NAME";
//vector<char> fileNameVector(name.begin(), name.end());
//fileNameVector.insert(fileNameVector.end(), fileName.begin(), fileName.end());
//if (!send_message(fileNameVector, MIDIA)) {
// cout << "Limite de timout, arquivo nao foi enviado\n";
// return;
//}
//cout << "Enviando arquivo\n";
//vector<char> file = read_file(fileName);
//if ( file.empty())
//{
// cout << "Falha ao abrir o arquivo para leitura\n";
// return;
//}
//if (!send_message(file, DADOS)) {
// cout << "Limite de timout, arquivo nao foi enviado\n";
// return;
//}
File deleted
...@@ -48,18 +48,5 @@ int main(int argc, char *argv[]) { ...@@ -48,18 +48,5 @@ int main(int argc, char *argv[]) {
break; break;
} }
// thread clientSend(&client::run, &cliente);
// server servidor(&local, &target);
// thread serverReceive(&server::run, &servidor);
// int receive = 0;
// while (true) {
// receive++;
// }
// serverReceive.join();
// clientSend.join();
return 0; return 0;
} }
#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
//string fileSize = calc_file_size(fileName);
//if (fileSize.empty()) { return; }
//franesFile.insert(framesFile.end(), fileSize.begin(), fileSize.end());
//cout << "Tamanho do arquivo: " << fileSize << "\n";
//cout << "Enviando tamanho do arquivo\n";
//if (!send_message(vector<char>(fileSize.begin(), fileSize.end()), MIDIA)) {
// cout << "Limite de timout, arquivo nao foi enviado\n";
// return;
//}
// Envia o segundo frame com o nome do arquivo
//cout << "Enviando nome do arquivo\n";
//string name = "NAME";
//vector<char> fileNameVector(name.begin(), name.end());
//fileNameVector.insert(fileNameVector.end(), fileName.begin(), fileName.end());
//if (!send_message(fileNameVector, MIDIA)) {
// cout << "Limite de timout, arquivo nao foi enviado\n";
// return;
//}
//cout << "Enviando arquivo\n";
//vector<char> file = read_file(fileName);
//if ( file.empty())
//{
// cout << "Falha ao abrir o arquivo para leitura\n";
// return;
//}
//if (!send_message(file, DADOS)) {
// cout << "Limite de timout, arquivo nao foi enviado\n";
// return;
//}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment