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

pra mensagem deu boa acho

parent ff6de159
Branches
No related tags found
No related merge requests found
......@@ -96,7 +96,9 @@ frame* client::send_frame_socket(frame *f) {
int retries = 0;
do {
// envia um frame da fila
socket->send_frame(f);
int bytesSent = socket->send_frame(f);
if ( bytesSent == -1 ) { return NULL; }
response = receive_ack_nack();
if(!response)
return NULL;
......@@ -155,50 +157,62 @@ int client::send_frames(vector<frame *> frames) {
cout << "\tstart transmission\n";
if (!start_transmission()) { return 0; }
cout << "\t ->>> started transmission <<< -\n";
// Cria a fila de frames
queue<int> janela;
int frameCounter;
int iniJanela = 0;
while (iniJanela < frames.size()) {
//manda todos os frames de uma vez só
for(frameCounter = 0; frameCounter < TAM_JANELA; frameCounter++){
if(iniJanela+frameCounter == frames.size())
break;
if(iniJanela+frameCounter == frames.size()) { break; }
janela.push(frameCounter);
cout << "\tEnviando frame\n";
frames[iniJanela]->imprime(HEX);
if(socket->send_frame(frames[iniJanela+frameCounter]) >= 0)
if (socket->send_frame(frames[iniJanela+frameCounter]) == -1)
{
cout << "Falha ao enviar o frame\n";
return 0;
}
cout << "\tFrame enviado com sucesso\n";
}
// Recebe a resposta do servidor
for (int i = 0; i < min((int)TAM_JANELA, (int)frames.size()); i++) {
frame* res = NULL;
int retries = 0;
do {
res = receive_ack_nack();
retries++;
} while (res == NULL && retries < NUM_RETRIES);
if(res == NULL && retries == NUM_RETRIES){
break;
return 0;
}
if(res->get_tipo() == NACK && res->get_dado()[0] == janela.front()){
cout << "NACK " << (int)res->get_dado()[0] << " recebido\n" ;
iniJanela += res->get_dado()[0];
janela.pop();
break;
}
if(res->get_tipo() == ACK && res->get_dado()[0] == janela.front()){
cout << "ACK " << (int)res->get_dado()[0] << " recebido\n" ;
iniJanela++;
janela.pop();
}
else{
i--;
}
}
//apaga a janela
while(! janela.empty()) janela.pop();
}
......
......@@ -23,8 +23,9 @@
using namespace std;
#define BYTE "%02x"
#define NUM_RETRIES 100
#define NUM_RETRIES 10
#define TAM_JANELA 2
class conexao {
private:
......@@ -89,10 +90,6 @@ frame *conexao::receive_frame() {
do {
byteRecv = recv(device, bufferReceived, sizeof(frame) * 2, 0);
// for (int i = 0; i < byteRecv; i++) {
// cout << hex << (int(bufferReceived[i])&0xff) << " ";
// }
cout << "\n";
if ((byteRecv > 0) && (bufferReceived[0] == INI)) {
frame *f = new frame;
remove_escapes(bufferReceived, (char *)f);
......@@ -122,7 +119,10 @@ int conexao::send_frame(frame *f) {
cout << hex << (int(bufferSend[i]) & 0xff) << " ";
}
cout << "\n";
if (byteSend < 0) { cout << "Erro no sendto" << byteSend << "\n"; }
if (byteSend < 0) {
cout << "Erro no sendto" << byteSend << "\n";
return -1;
}
return byteSend;
}
......
......@@ -4,9 +4,6 @@
#define UC unsigned char
#define UI unsigned int
//tamanho da janela
#define TAM_JANELA 2
// Macro que cria uma sequencia com o 1 deslocado "des" posicoes a direita
#define MASKR(des) (1 << des)
......
......@@ -32,30 +32,32 @@ class server {
private:
// --------- Dados ---------- //
int soquete;
//int soquete;
int tipoReceivingFrames;
conexao *socket;
vector<frame *> framesMidia;
//vector<frame *> framesMidia;
// ---------- Funcoes -------- //
frame *create_ack_nack(int tipo, int seq);
int send_nack(frame *fReceive);
int send_ack(frame *fReceive);
void send_confirm(frame *fReceive);
void receive_text(frame *f);
void receive_midia(frame *f);
frame *receive_frame_socket();
int receive_valid_frame(frame **f);
unsigned long chk_available_size();
int receive_file_size(frame *f);
void start_receveing_message();
bool create_received_dir();
string receive_file_name();
int receive_file_data(string fileName);
bool verify_crc8(frame *f);
bool verify_seq(int seq, int lastSeq);
//void send_confirm(frame *fReceive);
//void receive_text(frame *f);
//void receive_midia(frame *f);
//int receive_valid_frame(frame **f);
//unsigned long chk_available_size();
//int receive_file_size(frame *f);
//bool create_received_dir();
//string receive_file_name();
//int receive_file_data(string fileName);
//bool verify_crc8(frame *f);
frame *receive_frame_socket();
queue<frame *> receive_frames_window(int lastSeq);
void start_receveing_message();
public:
// ------- Construtores ------ //
......@@ -67,188 +69,77 @@ public:
// ------------------------------ PRIVATE --------------------------------- //
/**
* @brief function that sends a ack frame to the target
* @brief function that creates a nack or a nack
*
* @param fReceive frame received
* @param tipo ack or nack
* @param seq ack or nack sequence number
* @return int
*/
int server::send_ack(frame *fReceive) {
frame *ack = new frame();
ack->set_tipo(ACK);
vector<char> seq;
seq.push_back(char(fReceive->get_seq()));
ack->set_dado(seq);
int ackSent = 0;
ackSent = socket->send_frame(ack);
frame *server::create_ack_nack(int tipo, int seq)
{
vector<char> seq_char;
frame *f = new frame();
cout << "ACK enviado\n";
f->set_tipo(tipo);
vector<char> seq_v;
seq_v.push_back(char(seq));
f->set_dado(seq_v);
return ackSent;
return f;
}
/**
* @brief function that sends a nack frame to the target
* @brief function that sends a ack frame to the target
*
* @param fReceive frame received
* @return int
*/
int server::send_nack(frame *fReceive) {
frame *nack = new frame();
nack->set_tipo(NACK);
vector<char> seq;
seq.push_back(char(fReceive->get_seq()));
nack->set_dado(seq);
int nackSent = 0;
nackSent = socket->send_frame(nack);
cout << "NACK enviado\n";
return nackSent;
}
// Recebe uma mensagem em forma de texto
void server::receive_text(frame *f) {
string textoReceive;
textoReceive.append(f->get_dado());
int lastSeq = f->get_seq();
do {
if (!receive_valid_frame(&f)) { return; }
if (f->get_tipo() != TEXTO) { continue; }
if (f->get_seq() == lastSeq) { continue; }
int server::send_ack(frame *fReceive) {
lastSeq = f->get_seq();
textoReceive.append(f->get_dado());
} while (f->get_tipo() != FIMT);
frame *ack = create_ack_nack(ACK, fReceive->get_seq());
cout << "Mensagem recebida: " << textoReceive << "\n";
}
// Verifica o espaco disponivel em disco
unsigned long server::chk_available_size() {
struct statvfs st;
if (statvfs(FILE_DESTINATION, &st) == -1) {
cout << "Erro no statvfs, abortado\n";
// send_error();
int ackSent = socket->send_frame(ack);
if ( ackSent == -1 )
{
cout << "Falha ao enviar o ack\n";
return -1;
}
return st.f_bsize * st.f_bavail;
}
// Recebe o frame com o tamanho do arquivo
int server::receive_file_size(frame *f) {
/*
> verifica a sequencia do frame e o tipo
> se o frame nao for a sequencia esperada, envia um ack(0) e espera receber
a sequencia esperada > se o frame for o esperado, verifica o crc8 > se o
crc estiver certo, envia um ack e continua > se estiver errado, envia um
nack e espera receber o proximo frame
*/
cout << "Recebendo tamanho do frame\n";
do {
if ( f->get_tipo() != MIDIA ) { continue; }
if ( f->get_seq() == 0 && verify_crc8(f) ) { break; }
if ( f->get_seq() != 0 )
cout << "Sequencia errada\n";
else
cout << "Crc errado\n";
cout << "Aguardando frame\n";
f = receive_frame_socket();
if (!f) { return 0; }
cout << "Frame recebido\n";
} while (true);
unsigned long availSize = chk_available_size();
if (availSize == -1) { return -1; }
cout << "ACK " << (int) fReceive->get_seq() << " enviado\n";
cout << "Frame file size:" << f->get_dado() << "\n";
int fileSize = stoi(f->get_dado());
if (fileSize > availSize * 0.9) {
cout << "Tamanho do arquivo muito grande, abortado\n";
// send_error();
return 0;
}
cout << "Espaco suficiente em disco\n";
return 1;
}
bool server::create_received_dir() {
// check if the directory exists
struct stat info;
if (stat(FILE_DESTINATION, &info) == 0 && (info.st_mode & S_IFDIR)) {
cout << "Diretorio ja existe\n";
return true;
}
// create the directory
if (mkdir(FILE_DESTINATION, 0700) == -1) {
cout << "Erro ao criar o diretorio\n";
return false;
}
cout << "Diretorio criado com sucesso\n";
return true;
return ackSent;
}
string server::receive_file_name() {
frame *fReceive;
// Aguarda receber um frame do tipo midia com o nome do arquivo
// E com a sequencia de numero 2
/*
> recebe o frame e verifica se o tipo e a sequencia estao corretos
> se estiverem, verifica o crc8
> se estiver correto, envia um ack e continua
> se estiver errado, envia um nack e espera receber o proximo frame
/**
* @brief function that sends a nack frame to the target
*
* @param fReceive frame received
* @return int
*/
cout << "Recebendo nome do arquivo\n";
do {
cout << "Aguardando frame\n";
fReceive = receive_frame_socket();
if (!fReceive) { return string{}; }
int server::send_nack(frame *fReceive) {
cout << "Frame recebido\n";
frame *nack = create_ack_nack(NACK, fReceive->get_seq());
if (fReceive->get_tipo() != MIDIA) { continue; }
if ( fReceive->get_seq() != 1 )
{
cout << "Sequencia errada\n";
fReceive->set_seq(0);
send_ack(fReceive);
continue;
int nackSent = socket->send_frame(nack);
if ( nackSent == -1 ) {
cout << "Falha ao enviar o nack";
return -1;
}
if (verify_crc8(fReceive)) { break; }
cout << "Crc invalido, aguardando retransmissao\n";
cout << "NACK " << (int) fReceive->get_seq() << " enviado\n";
} while (true);
cout << "Nome do arquivo recebido com sucesso\n";
cout << "Nome do arquivo: " << fReceive->get_dado() << "\n";
return string(fReceive->get_dado());
return nackSent;
}
/**
* @brief function that verify if two numbers are sequential
*
* @param seq
* @param lastSeq
* @return bool
*/
bool server::verify_seq(int seq, int lastSeq) {
cout << "seq: " << seq << " lastSeq: " << dec << lastSeq << "\n";
if ( seq == 0 )
{
if ( lastSeq != 15 ) { return false; }
......@@ -258,87 +149,223 @@ bool server::verify_seq(int seq, int lastSeq) {
if (seq != lastSeq + 1) { return false; }
return true;
}
//// Recebe uma mensagem em forma de texto
//void server::receive_text(frame *f) {
// string textoReceive;
// textoReceive.append(f->get_dado());
// int lastSeq = f->get_seq();
//
// do {
// if (!receive_valid_frame(&f)) { return; }
// if (f->get_tipo() != TEXTO) { continue; }
// if (f->get_seq() == lastSeq) { continue; }
//
// lastSeq = f->get_seq();
// textoReceive.append(f->get_dado());
// } while (f->get_tipo() != FIMT);
//
// cout << "Mensagem recebida: " << textoReceive << "\n";
//}
//
//// Verifica o espaco disponivel em disco
//unsigned long server::chk_available_size() {
// struct statvfs st;
// if (statvfs(FILE_DESTINATION, &st) == -1) {
// cout << "Erro no statvfs, abortado\n";
// // send_error();
// return -1;
// }
//
// return st.f_bsize * st.f_bavail;
//}
//
//// Recebe o frame com o tamanho do arquivo
//int server::receive_file_size(frame *f) {
//
// /*
// > verifica a sequencia do frame e o tipo
// > se o frame nao for a sequencia esperada, envia um ack(0) e espera receber
// a sequencia esperada > se o frame for o esperado, verifica o crc8 > se o
// crc estiver certo, envia um ack e continua > se estiver errado, envia um
// nack e espera receber o proximo frame
// */
// cout << "Recebendo tamanho do frame\n";
// do {
// if ( f->get_tipo() != MIDIA ) { continue; }
// if ( f->get_seq() == 0 && verify_crc8(f) ) { break; }
//
// if ( f->get_seq() != 0 )
// cout << "Sequencia errada\n";
// else
// cout << "Crc errado\n";
//
// cout << "Aguardando frame\n";
//
// f = receive_frame_socket();
// if (!f) { return 0; }
//
// cout << "Frame recebido\n";
//
// } while (true);
//
// unsigned long availSize = chk_available_size();
// if (availSize == -1) { return -1; }
//
// cout << "Frame file size:" << f->get_dado() << "\n";
// int fileSize = stoi(f->get_dado());
//
// if (fileSize > availSize * 0.9) {
// cout << "Tamanho do arquivo muito grande, abortado\n";
// // send_error();
// return 0;
// }
//
// cout << "Espaco suficiente em disco\n";
// return 1;
//}
//
//bool server::create_received_dir() {
//
// // check if the directory exists
// struct stat info;
// if (stat(FILE_DESTINATION, &info) == 0 && (info.st_mode & S_IFDIR)) {
// cout << "Diretorio ja existe\n";
// return true;
// }
//
// // create the directory
// if (mkdir(FILE_DESTINATION, 0700) == -1) {
// cout << "Erro ao criar o diretorio\n";
// return false;
// }
//
// cout << "Diretorio criado com sucesso\n";
// return true;
//}
//
//string server::receive_file_name() {
// frame *fReceive;
//
// // Aguarda receber um frame do tipo midia com o nome do arquivo
// // E com a sequencia de numero 2
// /*
// > recebe o frame e verifica se o tipo e a sequencia estao corretos
// > se estiverem, verifica o crc8
// > se estiver correto, envia um ack e continua
// > se estiver errado, envia um nack e espera receber o proximo frame
// */
// cout << "Recebendo nome do arquivo\n";
//
// do {
// cout << "Aguardando frame\n";
// fReceive = receive_frame_socket();
// if (!fReceive) { return string{}; }
//
// cout << "Frame recebido\n";
//
// if (fReceive->get_tipo() != MIDIA) { continue; }
// if ( fReceive->get_seq() != 1 )
// {
// cout << "Sequencia errada\n";
// fReceive->set_seq(0);
// send_ack(fReceive);
// continue;
// }
//
// if (verify_crc8(fReceive)) { break; }
//
// cout << "Crc invalido, aguardando retransmissao\n";
//
// } while (true);
//
// cout << "Nome do arquivo recebido com sucesso\n";
// cout << "Nome do arquivo: " << fReceive->get_dado() << "\n";
//
// return string(fReceive->get_dado());
//}
int server::receive_file_data(string fileName) {
string fileDestination;
fileDestination.append(FILE_DESTINATION);
fileDestination.push_back('/');
fileDestination.append(fileName);
// Abre o arquivo para escrita
ofstream file;
file.open(fileDestination, ios::binary);
if (!file.is_open()) {
cout << "Falha ao criar o arquivo. Abortado\n";
return 0;
}
cout << "Arquivo criado com sucesso\n";
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;
}
void server::receive_midia(frame *f) {
cout << "Recebendo frame midia\n";
if (!create_received_dir()) { return; }
if (!receive_file_size(f)) { return; }
string fileName = receive_file_name();
if (fileName.size() == 0) { return; }
if ( !receive_file_data(fileName) ) {
cout << "Falha ao receber o arquivo\n";
return;
}
cout << "Arquivo recebido com sucesso\n";
}
// Recebe um frame do cliente
//
//int server::receive_file_data(string fileName) {
// string fileDestination;
// fileDestination.append(FILE_DESTINATION);
// fileDestination.push_back('/');
// fileDestination.append(fileName);
//
// // Abre o arquivo para escrita
// ofstream file;
// file.open(fileDestination, ios::binary);
// if (!file.is_open()) {
// cout << "Falha ao criar o arquivo. Abortado\n";
// return 0;
// }
//
// cout << "Arquivo criado com sucesso\n";
//
// 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;
//}
//
//void server::receive_midia(frame *f) {
// cout << "Recebendo frame midia\n";
// if (!create_received_dir()) { return; }
// if (!receive_file_size(f)) { return; }
//
// string fileName = receive_file_name();
//
// if (fileName.size() == 0) { return; }
//
// if ( !receive_file_data(fileName) ) {
// cout << "Falha ao receber o arquivo\n";
// return;
// }
//
// cout << "Arquivo recebido com sucesso\n";
//}
//
//// Recebe um frame do cliente
frame *server::receive_frame_socket() {
frame *fReceive;
int retries = 0;
......@@ -355,28 +382,28 @@ frame *server::receive_frame_socket() {
return fReceive;
}
bool server::verify_crc8(frame *f) {
int crc8 = f->chk_crc8();
if (!crc8) {
send_nack(f);
}
else
send_ack(f);
return crc8;
}
int server::receive_valid_frame(frame **f) {
do {
// Se nao conseguir receber o frame, mata a comunicacao
*f = receive_frame_socket();
if (*f == NULL) { return 0; }
// Avisa o cliente se nao conseguiu receber o frame
} while (!verify_crc8(*f));
return 1;
}
//
//bool server::verify_crc8(frame *f) {
// int crc8 = f->chk_crc8();
// if (!crc8) {
// send_nack(f);
// }
// else
// send_ack(f);
// return crc8;
//}
//
//int server::receive_valid_frame(frame **f) {
//
// do {
// // Se nao conseguir receber o frame, mata a comunicacao
// *f = receive_frame_socket();
// if (*f == NULL) { return 0; }
//
// // Avisa o cliente se nao conseguiu receber o frame
// } while (!verify_crc8(*f));
// return 1;
//}
queue<frame *> server::receive_frames_window(int lastSeq)
{
......@@ -385,8 +412,7 @@ queue<frame *> server::receive_frames_window(int lastSeq)
int retries = 0;
do {
f = receive_frame_socket();
if ( f == NULL ) { continue; }
if ( ! (f = receive_frame_socket()) ) { continue; }
retries++;
......@@ -448,61 +474,90 @@ queue<frame *> server::receive_frames_window(int lastSeq)
} while ( (f == NULL && retries < NUM_RETRIES) || frames_queue.size() < TAM_JANELA );
if ( f == NULL && retries == NUM_RETRIES ) { return queue<frame *>(); }
return frames_queue;
}
void server::start_receveing_message() {
int continueTransmission = 1;
int lastSeq = -1;
int tipo_data = -1;
vector<char> data;
queue<frame *> client_answer;
cout << "Recebendo frames\n";
int lastSeq = -1;
// Fic aouvindo o cliente ate receber um FIMT
do {
queue<frame *> frames = receive_frames_window(lastSeq);
cout << "Quantidade de frames: " << frames.size() << "\n";
if ( frames.empty() ) { return; }
cout << "Quantidade de frames na janela: " << frames.size() << "\n";
// Ve o que faz com cada frame de acordo com o tipo
while ( !frames.empty() )
{
cout << "Frame recebido: \n";
frame *f = frames.front();
frames.pop();
// Recebeu um frame com erro, retorna um nack e sai da funcao
if ( !f->chk_crc8() ) {
client_answer.push(create_ack_nack(NACK, f->get_seq()));
continue;
}
else {
client_answer.push(create_ack_nack(ACK, f->get_seq()));
}
cout << "Frame recebido: \n";
f->imprime(HEX);
cout << "\n";
int tipo = f->get_tipo();
int tam = f->get_tam();
char *data_f = f->get_dado();
switch (tipo)
{
case FIMT:
cout << "Encerrou a transmissao\n";
continueTransmission = 0;
case TEXTO:
data.insert(data.end(), data_f, data_f + tam);
lastSeq = f->get_seq();
tipo_data = TEXTO;
}
}
cout << "Recebeu todos os frames de uma janela\n";
// if (!receive_valid_frame(&f)) { return; }
// if (!f) { return; }
// int frameType = f->get_tipo();
//
// switch (frameType) {
// case TEXTO:
// receive_text(f);
// continueTransmission = 0;
// break;
//
// case MIDIA:
// receive_midia(f);
// continueTransmission = 0;
// break;
//
// default:
// break;
// }
//
// Envia a reposta ao cliente
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";
return;
}
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";
}
cout << "Todos os ACKs e NACKs foram enviados\n";
} while (continueTransmission);
cout << "Encerrou a transmissao\n";
if ( tipo_data == TEXTO )
cout << ">>>>>>>>>>>>>> Mensagem recebida: " << string(data.begin(), data.end()) << "\n";
}
// ------------------------------- PUBLIC --------------------------------- //
......@@ -528,7 +583,7 @@ void server::run() {
continue;
}
send_ack(fReceive);
if ( send_ack(fReceive) == -1 ) { continue; }
start_receveing_message();
}
}
......
-> Ver se os frames que estao vindo sao arquivos e juntar eles no arquivo
-> Arrumar receive valid frame, retries
-> Arrumar nome arquivo > 63
-> Arrumar a interface
-> Merge correcao de erro
-> Enviar arquivo correcao de erros
-> Readme
-> Demonstracao
-> Demonstracao correcao de erros
-> Janela deslizante
-> Demonstracao
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment