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

quase

parent 0616a4de
No related branches found
No related tags found
No related merge requests found
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#define UC unsigned char #define UC unsigned char
#define UI unsigned int #define UI unsigned int
#define UL unsigned long
// Macro que cria uma sequencia com o 1 deslocado "des" posicoes a direita // Macro que cria uma sequencia com o 1 deslocado "des" posicoes a direita
#define MASKR(des) (1 << des) #define MASKR(des) (1 << des)
......
...@@ -21,8 +21,9 @@ ...@@ -21,8 +21,9 @@
#include <sys/types.h> #include <sys/types.h>
// include local // include local
#include "conexao.h"
#include "frame.h" #include "frame.h"
#include "macros.h"
#include "conexao.h"
#define FILE_DESTINATION "./received" #define FILE_DESTINATION "./received"
...@@ -45,14 +46,15 @@ private: ...@@ -45,14 +46,15 @@ private:
int send_nack (frame *fReceive); int send_nack (frame *fReceive);
int send_ack (frame *fReceive); int send_ack (frame *fReceive);
bool verify_seq (int seq, int lastSeq); bool verify_seq (int seq, int lastSeq);
int next_tipo_midia (frame *f);
bool create_received_dir ();
UL chk_available_size ();
int receive_file_size (frame *f);
ofstream create_file (string fileName);
int receive_midia (frame *f);
//void send_confirm(frame *fReceive); //void send_confirm(frame *fReceive);
//void receive_text(frame *f); //void receive_text(frame *f);
//void receive_midia(frame *f);
//int receive_valid_frame(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); //int receive_file_data(string fileName);
//bool verify_crc8(frame *f); //bool verify_crc8(frame *f);
frame *receive_frame_socket(); frame *receive_frame_socket();
...@@ -149,6 +151,15 @@ bool server::verify_seq(int seq, int lastSeq) { ...@@ -149,6 +151,15 @@ bool server::verify_seq(int seq, int lastSeq) {
if (seq != lastSeq + 1) { return false; } if (seq != lastSeq + 1) { return false; }
return true; return true;
} }
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 ) { return DADOS; }
return -1;
}
//// Recebe uma mensagem em forma de texto //// Recebe uma mensagem em forma de texto
//void server::receive_text(frame *f) { //void server::receive_text(frame *f) {
// string textoReceive; // string textoReceive;
...@@ -168,139 +179,59 @@ bool server::verify_seq(int seq, int lastSeq) { ...@@ -168,139 +179,59 @@ bool server::verify_seq(int seq, int lastSeq) {
//} //}
// //
//// Verifica o espaco disponivel em disco //// Verifica o espaco disponivel em disco
//unsigned long server::chk_available_size() {
// struct statvfs st; UL server::chk_available_size() {
// if (statvfs(FILE_DESTINATION, &st) == -1) { struct statvfs st;
// cout << "Erro no statvfs, abortado\n"; if (statvfs(FILE_DESTINATION, &st) == -1) {
// // send_error(); cout << "Erro no statvfs, abortado\n";
// return -1; // send_error();
// } return -1;
// }
// return st.f_bsize * st.f_bavail;
//} return st.f_bsize * st.f_bavail;
// }
//// Recebe o frame com o tamanho do arquivo //// Recebe o frame com o tamanho do arquivo
//int server::receive_file_size(frame *f) { 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());
//}
cout << "Recebendo tamanho do frame\n";
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() {
cout << "Criando diretorio";
// 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;
}
//
//int server::receive_file_data(string fileName) { //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; // int lastSeq = 1;
// frame *f; // frame *f;
...@@ -348,23 +279,53 @@ bool server::verify_seq(int seq, int lastSeq) { ...@@ -348,23 +279,53 @@ bool server::verify_seq(int seq, int lastSeq) {
// return 1; // return 1;
//} //}
// //
//void server::receive_midia(frame *f) {
// cout << "Recebendo frame midia\n"; ofstream server::create_file(string fileName) {
// if (!create_received_dir()) { return; } string fileDestination;
// if (!receive_file_size(f)) { return; } fileDestination.append(FILE_DESTINATION);
// fileDestination.push_back('/');
// string fileName = receive_file_name(); fileDestination.append(fileName);
//
// if (fileName.size() == 0) { return; } // Abre o arquivo para escrita
// ofstream file;
// if ( !receive_file_data(fileName) ) { file.open(fileDestination, ios::binary);
// cout << "Falha ao receber o arquivo\n"; return file;
// return; }
// }
// int server::receive_midia(frame *f) {
// cout << "Arquivo recebido com sucesso\n";
//} ofstream file;
//
// Escreve o dado no arquivo
if ( f->get_tipo() == DADOS )
{
file.write(f->get_dado(), f->get_tam());
return 1;
}
// Primeiro frame de midia
if ( f->get_seq() == 0 )
{
cout << "Recebendo tamanho do arquivo\n";
if (!create_received_dir()) { return 0; }
if (!receive_file_size(f)) { return 0; }
}
// Segundo frame de midia
string fileName = string(f->get_dado());
file = create_file(fileName);
if (!file.is_open())
{
cout << "Falha ao criar o arquivo. Abortado\n";
return 0;
}
cout << "Arquivo criado com sucesso\n";
if ( !file.is_open() ) { return 0; }
return 1;
}
//// Recebe um frame do cliente //// Recebe um frame do cliente
frame *server::receive_frame_socket() { frame *server::receive_frame_socket() {
frame *fReceive; frame *fReceive;
...@@ -517,6 +478,7 @@ void server::start_receveing_message() { ...@@ -517,6 +478,7 @@ void server::start_receveing_message() {
int tipo = f->get_tipo(); int tipo = f->get_tipo();
int tam = f->get_tam(); int tam = f->get_tam();
char *data_f = f->get_dado(); char *data_f = f->get_dado();
switch (tipo) switch (tipo)
{ {
case FIMT: case FIMT:
...@@ -531,9 +493,15 @@ void server::start_receveing_message() { ...@@ -531,9 +493,15 @@ void server::start_receveing_message() {
break; break;
case MIDIA: case MIDIA:
if ( !receive_midia(f) ) { return; }
lastSeq = f->get_seq();
tipo_data = next_tipo_midia(f);
break; break;
case DADOS: case DADOS:
if ( !receive_midia(f)) { return; }
lastSeq = f->get_seq();
tipo_data = DADOS;
break; break;
default: default:
...@@ -569,6 +537,9 @@ void server::start_receveing_message() { ...@@ -569,6 +537,9 @@ void server::start_receveing_message() {
if ( tipo_data == TEXTO ) if ( tipo_data == TEXTO )
cout << ">>>>>>>>>>>>>> Mensagem recebida: " << string(data.begin(), data.end()) << "\n"; cout << ">>>>>>>>>>>>>> Mensagem recebida: " << string(data.begin(), data.end()) << "\n";
if ( tipo_data == DADOS )
cout << ">>>>>>>>>>>>>> Arquivo recebido\n";
} }
// ------------------------------- PUBLIC --------------------------------- // // ------------------------------- PUBLIC --------------------------------- //
......
File moved
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment