From 93c6283171350aa879d392b927ee50f31dce861b Mon Sep 17 00:00:00 2001 From: bfsc19 <bfsc19@inf.ufpr.br> Date: Sat, 25 Feb 2023 17:55:06 -0300 Subject: [PATCH] feat: rodado o clang-format --- .clang-format | 24 ++++----- headers/client.h | 125 +++++++++++++++++++-------------------------- headers/conexao.h | 10 ++-- headers/crc8.h | 2 +- headers/macros.h | 17 ++++--- headers/server.h | 127 ++++++++++++++++++++++------------------------ src/crc8.cpp | 29 +++++------ src/exemplo.cpp | 68 +++++++++++-------------- 8 files changed, 180 insertions(+), 222 deletions(-) diff --git a/.clang-format b/.clang-format index 48b2c67..f4ed5e0 100644 --- a/.clang-format +++ b/.clang-format @@ -14,11 +14,11 @@ AlignTrailingComments: true AllowAllArgumentsOnNextLine: true AllowAllParametersOfDeclarationOnNextLine: true AllowShortEnumsOnASingleLine: true -AllowShortBlocksOnASingleLine: Never +AllowShortBlocksOnASingleLine: true AllowShortCaseLabelsOnASingleLine: false AllowShortFunctionsOnASingleLine: All AllowShortLambdasOnASingleLine: All -AllowShortIfStatementsOnASingleLine: Never +AllowShortIfStatementsOnASingleLine: true AllowShortLoopsOnASingleLine: false AlwaysBreakAfterDefinitionReturnType: None AlwaysBreakAfterReturnType: None @@ -29,20 +29,20 @@ AttributeMacros: BinPackArguments: true BinPackParameters: true BraceWrapping: - AfterCaseLabel: false - AfterClass: false - AfterControlStatement: Never - AfterEnum: false - AfterFunction: false + AfterCaseLabel: true + AfterClass: true + AfterControlStatement: true + AfterEnum: true + AfterFunction: true AfterNamespace: false AfterObjCDeclaration: false - AfterStruct: false - AfterUnion: false + AfterStruct: true + AfterUnion: true AfterExternBlock: false - BeforeCatch: false - BeforeElse: false + BeforeCatch: true + BeforeElse: true BeforeLambdaBody: false - BeforeWhile: false + BeforeWhile: true IndentBraces: false SplitEmptyFunction: true SplitEmptyRecord: true diff --git a/headers/client.h b/headers/client.h index 67cb295..0590722 100644 --- a/headers/client.h +++ b/headers/client.h @@ -2,6 +2,7 @@ #ifndef _CLIENT_ #define _CLIENT_ +#include <arpa/inet.h> #include <bits/stdc++.h> #include <fstream> #include <iostream> @@ -15,9 +16,8 @@ #include <string.h> #include <sys/ioctl.h> #include <sys/socket.h> -#include <sys/types.h> #include <sys/stat.h> -#include <arpa/inet.h> +#include <sys/types.h> // include local #include "conexao.h" @@ -71,41 +71,33 @@ public: // ------------------------------ PRIVATE --------------------------------- // // Verifica se recebeu um ack valido -frame *client::receive_ack(frame *f) -{ +frame *client::receive_ack(frame *f) { frame *ack_res = NULL; // se recebemos algo, e NÃO ẽ o ACK que estamos // esperando, continuamos tentando receber do { ack_res = socket->receive_frame(); - if (ack_res && ack_res->get_tipo() == ERRO) - { + if (ack_res && ack_res->get_tipo() == ERRO) { cout << "Espaco insulficiente no destino\n"; return NULL; } - - } while ( - ack_res && - !(verify_ack(ack_res, f) && - ack_res->get_dado()[0] == f->get_seq()) - ); + } while (ack_res && + !(verify_ack(ack_res, f) && ack_res->get_dado()[0] == f->get_seq())); return ack_res; } // Solicita ao socket que envie um frame -int client::send_frame_socket(frame *f) -{ +int client::send_frame_socket(frame *f) { // Fica tentando enviar o frame até receber o ack - frame* ack_res = NULL; + frame *ack_res = NULL; int retries = 0; do { // envia um frame da fila socket->send_frame(f); ack_res = receive_ack(f); retries++; - } while (ack_res == NULL && retries < NUM_RETRIES); if (ack_res == NULL && retries == NUM_RETRIES) { @@ -119,13 +111,11 @@ int client::send_frame_socket(frame *f) } // Inicia a transmissao com o servidor -int client::start_transmission() -{ +int client::start_transmission() { cout << "\tIniciando transmissao\n"; - frame *ini = new frame(INIT, 0, vector<char>(1,0)); + frame *ini = new frame(INIT, 0, vector<char>(1, 0)); int enviado = send_frame_socket(ini); - if ( !enviado ) - { + if (!enviado) { cout << "\tFalha ao iniciar a transmissao\n"; return 0; } @@ -135,13 +125,11 @@ int client::start_transmission() } // Encerra a transmissao com o servidor -int client::end_transmission() -{ +int client::end_transmission() { cout << "\tEncerrando a transmissao\n"; - frame *end= new frame(FIMT, 0, vector<char>(1,0)); + frame *end = new frame(FIMT, 0, vector<char>(1, 0)); int enviado = send_frame_socket(end); - if ( !enviado ) - { + if (!enviado) { cout << "\tFalha ao encerrar a transmissao\n"; return 0; } @@ -157,7 +145,7 @@ int client::end_transmission() * @return int */ int client::send_frames(vector<frame *> frames) { - if ( !start_transmission() ) { return 0; } + if (!start_transmission()) { return 0; } // Envia um frame por vez for (size_t i = 0; i < frames.size(); i++) { @@ -166,8 +154,7 @@ int client::send_frames(vector<frame *> frames) { frames[i]->imprime(DEC); int enviado = send_frame_socket(frames[i]); - if ( !enviado ) - { + if (!enviado) { cout << "\tFalha ao enviar o frame\n"; return 0; } @@ -175,7 +162,7 @@ int client::send_frames(vector<frame *> frames) { cout << "\tFrame enviado com sucesso\n"; } - if ( !end_transmission() ) { return 0; } + if (!end_transmission()) { return 0; } cout << "\tTerminou de enviar todos os frames\n"; return 1; } @@ -190,7 +177,7 @@ int client::send_frames(vector<frame *> frames) { * @return false */ bool client::verify_ack(frame *received, frame *sent) { - return ( received->get_tipo() == ACK && received->chk_crc8()); + return (received->get_tipo() == ACK && received->chk_crc8()); } /** @@ -208,10 +195,9 @@ int client::send_message(vector<char> data, int type) { return send_frames(frames); } -string client::calc_file_size(string fileName) -{ +string client::calc_file_size(string fileName) { struct stat buffer; - if ( stat(fileName.c_str(), &buffer) == -1 ) { + if (stat(fileName.c_str(), &buffer) == -1) { cout << "Arquivo inexistente. Operacao abortada\n"; return {}; } @@ -220,16 +206,14 @@ string client::calc_file_size(string fileName) return to_string(fileSize); } -vector<char> client::read_file(string fileName) -{ +vector<char> client::read_file(string fileName) { fstream file; - file.open(fileName, ios::in); - + file.open(fileName, ios::in); + string teste; vector<char> fileData; char c; - while ( (file.get(c), file.eof() == false) ) - { + while ((file.get(c), file.eof() == false)) { fileData.push_back(c); teste.push_back(c); } @@ -248,37 +232,36 @@ void client::send_file() { do { cout << "Digite o nome do arquivo(maximo de " << TAM_DADOS << " char):\n"; getline(cin, fileName); - } while ( fileName.size() > TAM_DADOS ); + } while (fileName.size() > TAM_DADOS); // Envia o primeiro frame com o tamanho do arquivo string fileSize = calc_file_size(fileName); - if ( fileSize.empty() ) { return; } + if (fileSize.empty()) { return; } cout << "Tamanho do arquivo: " << fileSize << "\n"; cout << "Enviando tamanho do arquivo\n"; - if (!send_message(vector<char>(fileSize.begin(), fileSize.end()), MIDIA)) - { + 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"; -// if (!send_message(vector<char>(fileName.begin(), fileName.end()), MIDIA)) -// { -// cout << "Limite de timout, arquivo nao foi enviado\n"; -// return; -// } -// -// cout << "Enviando arquivo\n"; -// vector<char> file = read_file(fileName); -// if (file.empty() || !send_message(file, DADOS)) -// { -// cout << "Limite de timout, arquivo nao foi enviado\n"; -// return; -// } -// -// cout << "Arquivo enviado com sucesso\n"; + // cout << "Enviando nome do arquivo\n"; + // if (!send_message(vector<char>(fileName.begin(), fileName.end()), MIDIA)) + // { + // cout << "Limite de timout, arquivo nao foi enviado\n"; + // return; + // } + // + // cout << "Enviando arquivo\n"; + // vector<char> file = read_file(fileName); + // if (file.empty() || !send_message(file, DADOS)) + // { + // cout << "Limite de timout, arquivo nao foi enviado\n"; + // return; + // } + // + // cout << "Arquivo enviado com sucesso\n"; } /** @@ -290,7 +273,7 @@ void client::send_text(string message) { cout << "Enviando mensagem\n"; - vector<char> data (message.begin(), message.end()); + vector<char> data(message.begin(), message.end()); if (!send_message(data, TEXTO)) cout << "Limite de timout, mensagem nao foi enviada\n"; @@ -308,13 +291,14 @@ vector<frame *> client::create_frames(vector<char> data, int type) { vector<frame *> frames; int i = 0; - int frameCnt = (data.size()/TAM_DADOS) + bool(data.size()%TAM_DADOS); + int frameCnt = (data.size() / TAM_DADOS) + bool(data.size() % TAM_DADOS); while (i < frameCnt) { frame *f = new frame(); f->set_tipo(type); f->set_seq(i); - f->set_dado(vector<char>(data.data()+i*TAM_DADOS, data.data()+ - min(data.size(), (size_t)(i+1)*TAM_DADOS))); + f->set_dado(vector<char>( + data.data() + i * TAM_DADOS, + data.data() + min(data.size(), (size_t)(i + 1) * TAM_DADOS))); frames.push_back(f); i++; } @@ -325,23 +309,18 @@ vector<frame *> client::create_frames(vector<char> data, int type) { bool client::string_has(string str, vector<string> strList) { for (int i = 0; i < strList.size(); i++) { - if (str.find(strList[i]) != string::npos) { - return true; - } + if (str.find(strList[i]) != string::npos) { return true; } } return false; } char client::string_cmd(string str) { - if (string_has(str, CMD_HELP)) - return 'h'; + if (string_has(str, CMD_HELP)) return 'h'; - if (string_has(str, CMD_EXIT)) - return 'e'; + if (string_has(str, CMD_EXIT)) return 'e'; - if (string_has(str, CMD_SEND)) - return 's'; + if (string_has(str, CMD_SEND)) return 's'; return 'm'; } diff --git a/headers/conexao.h b/headers/conexao.h index f736ce3..072f624 100644 --- a/headers/conexao.h +++ b/headers/conexao.h @@ -122,9 +122,7 @@ 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 byteSend; } @@ -144,8 +142,7 @@ 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; + if (f[i] == 0x88 || f[i] == 0x81) out[j++] = 0xFF; } return j; @@ -156,8 +153,7 @@ int conexao::remove_escapes(char *f, char *out) { for (size_t i = 0; j < sizeof(frame); i++) { out[j++] = f[i]; - if (f[i] == 0x88 || f[i] == 0x81) - i++; + if (f[i] == 0x88 || f[i] == 0x81) i++; } return j; diff --git a/headers/crc8.h b/headers/crc8.h index a479e67..2a9e5a6 100644 --- a/headers/crc8.h +++ b/headers/crc8.h @@ -1,8 +1,8 @@ #ifndef _CRC8_ #define _CRC8_ -#include <string> #include "macros.h" +#include <string> using namespace std; diff --git a/headers/macros.h b/headers/macros.h index ea108d0..a936d2f 100644 --- a/headers/macros.h +++ b/headers/macros.h @@ -8,22 +8,23 @@ #define MASKR(des) (1 << des) // Macro que fica em 1 ou 0 de acordo com o valor do bit avaliado -#define BIT(value, des) ( (value & MASKR(des)) ? 1 : 0 ) +#define BIT(value, des) ((value & MASKR(des)) ? 1 : 0) // Macro que zera uma sequencia de bits -#define ZERA(value) ( value &= ~value ) +#define ZERA(value) (value &= ~value) // Macro que coloca todos os bits de uma sequencia em 1 -#define UM(seq) ( seq |= ~seq ) +#define UM(seq) (seq |= ~seq) // Macro que zera determinado bit de uma sequencia -#define ZERABIT(seq, bit) ( seq &= ~MASKR(bit) ) +#define ZERABIT(seq, bit) (seq &= ~MASKR(bit)) // Macro para a "p" potencia de 2 -#define BITPOW(p) ( MASKR(6) ) +#define BITPOW(p) (MASKR(6)) // Macro que imprime uma sequencia de bits -#define IMPRIME(seq, tam) for ( int i = tam-1; i >= 0; i-- ) \ - cout << BIT(seq, i); \ - cout << "\n"; +#define IMPRIME(seq, tam) \ + for (int i = tam - 1; i >= 0; i--) \ + cout << BIT(seq, i); \ + cout << "\n"; #endif diff --git a/headers/server.h b/headers/server.h index 9a8c362..1294953 100644 --- a/headers/server.h +++ b/headers/server.h @@ -7,6 +7,7 @@ #include <iostream> #include <linux/if.h> #include <linux/if_packet.h> +#include <mutex> #include <net/ethernet.h> #include <netinet/in.h> #include <stdio.h> @@ -14,9 +15,8 @@ #include <string.h> #include <sys/ioctl.h> #include <sys/socket.h> -#include <sys/types.h> -#include <mutex> #include <sys/statvfs.h> +#include <sys/types.h> #include <arpa/inet.h> @@ -106,33 +106,29 @@ int server::send_nack(frame *fReceive) { } // Recebe uma mensagem em forma de texto -void server::receive_text(frame *f) -{ +void server::receive_text(frame *f) { string textoReceive; textoReceive.append(f->get_dado()); int lastSeq = f->get_seq(); - + do { - if ( !receive_valid_frame(&f) ) { continue; } - if ( f->get_tipo() != TEXTO ) { continue; } - if ( f->get_seq() == lastSeq) { continue; } - + if (!receive_valid_frame(&f)) { continue; } + 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 ); + } while (f->get_tipo() != FIMT); cout << "Mensagem recebida: " << textoReceive << "\n"; } // Verifica o espaco disponivel em disco -unsigned long server::chk_available_size() -{ +unsigned long server::chk_available_size() { struct statvfs st; - if ( statvfs(FILE_DESTINATION, &st) == -1 ) - { + if (statvfs(FILE_DESTINATION, &st) == -1) { cout << "Erro no statvfs, abortado\n"; - //send_error(); + // send_error(); return -1; } @@ -140,17 +136,15 @@ unsigned long server::chk_available_size() } // Recebe o frame com o tamanho do arquivo -long long server::receive_file_size(frame *f) -{ +long long server::receive_file_size(frame *f) { unsigned long availSize = chk_available_size(); - if ( availSize == -1 ) { return -1; } + if (availSize == -1) { return -1; } - unsigned long fileSize = stoi(f->get_dado()); + unsigned long fileSize = stoi(f->get_dado()); - if ( fileSize > availSize*0.9 ) - { + if (fileSize > availSize * 0.9) { cout << "Tamanho do arquivo muito grande, abortado\n"; - //send_error(); + // send_error(); return -1; } @@ -158,27 +152,24 @@ long long server::receive_file_size(frame *f) return 1; } -void server::receive_midia(frame *f) -{ - //if ( !create_received_dir) { return; } - if ( !receive_file_size(f) ) { return; } - //if ( !receive_file_name() { return; } - //receive_file_data(); +void server::receive_midia(frame *f) { + // if ( !create_received_dir) { return; } + if (!receive_file_size(f)) { return; } + // if ( !receive_file_name() { return; } + // receive_file_data(); } // Recebe um frame do cliente -frame *server::receive_frame_socket() -{ +frame *server::receive_frame_socket() { frame *fReceive; int retries = 0; - do - { + do { retries++; - if ( ! (fReceive = socket->receive_frame()) ) { continue; } - } while ( fReceive == NULL && retries < NUM_RETRIES ); + if (!(fReceive = socket->receive_frame())) { continue; } + } 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"; return NULL; } @@ -186,53 +177,52 @@ frame *server::receive_frame_socket() return fReceive; } -int server::receive_valid_frame(frame **f) -{ +int server::receive_valid_frame(frame **f) { int crc8; do { // Se nao conseguir receber o frame, mata a comunicacao *f = receive_frame_socket(); - if ( *f == NULL ) { return 0; } + if (*f == NULL) { return 0; } // Avisa o cliente se nao conseguiu receber o frame crc8 = (*f)->chk_crc8(); - if ( crc8 ) { send_ack(*f); break; } + if (crc8) { + send_ack(*f); + break; + } send_nack(*f); - - } while ( !crc8 ); + } while (!crc8); return 1; } -void server::start_receveing_message() -{ +void server::start_receveing_message() { int endTransmission = 0; frame *f; - do { - if ( !receive_valid_frame(&f) ) { return; } - if ( !f ) { return; } + do { + if (!receive_valid_frame(&f)) { return; } + if (!f) { return; } int frameType = f->get_tipo(); - switch (frameType) - { - case TEXTO: - receive_text(f); - endTransmission = 1; - break; - - case MIDIA: - receive_midia(f); - endTransmission = 1; - break; - - default: - break; } - } while ( !endTransmission ); + switch (frameType) { + case TEXTO: + receive_text(f); + endTransmission = 1; + break; + + case MIDIA: + receive_midia(f); + endTransmission = 1; + break; + + default: + break; + } + } while (!endTransmission); } - // ------------------------------- PUBLIC --------------------------------- // void server::run() { @@ -241,16 +231,19 @@ void server::run() { /*-- listening local ip and waiting for messages --*/ /*-- ignore if the package is not valid --*/ frame *fReceive; - if ( ! (fReceive = socket->receive_frame()) ) { continue; } + if (!(fReceive = socket->receive_frame())) { continue; } cout << "Frame recebido:" << endl; fReceive->imprime(HEX); // Verifica se o frame eh de inicio de transmissao e se nao veio com erro int frameType = fReceive->get_tipo(); - if ( frameType != INIT ) { continue; } - - if ( !fReceive->chk_crc8() ) { send_nack(fReceive); continue; } + if (frameType != INIT) { continue; } + + if (!fReceive->chk_crc8()) { + send_nack(fReceive); + continue; + } send_ack(fReceive); start_receveing_message(); diff --git a/src/crc8.cpp b/src/crc8.cpp index dc69487..dbbc6c0 100644 --- a/src/crc8.cpp +++ b/src/crc8.cpp @@ -1,5 +1,5 @@ -#include <iostream> #include <iomanip> +#include <iostream> #include <string> #include "../headers/crc8.h" @@ -12,35 +12,30 @@ using namespace std; uint8_t crc8_table[256]; // Gera a tabela com os valores do crc -void gen_crc8_table() -{ +void gen_crc8_table() { // Calcula o crc para todos os valores de 1 a 255 - for ( int divident = 0; divident < 256; divident++ ) - { - unsigned int currByte = divident; - + for (int divident = 0; divident < 256; divident++) { + unsigned int currByte = divident; + // Calcula bit a bit - for ( int bit = 0; bit < 8; bit++ ) - { + for (int bit = 0; bit < 8; bit++) { currByte <<= 1; - + // Se o bit mais significativo for 1, faz a divisao - if ( currByte & 0x100 ) { currByte ^= POLINOMIO; } + if (currByte & 0x100) { currByte ^= POLINOMIO; } } - crc8_table[divident] = uint8_t(currByte); + crc8_table[divident] = uint8_t(currByte); } } -uint8_t calc_crc8(uint8_t *msg, int tamBytes) -{ +uint8_t calc_crc8(uint8_t *msg, int tamBytes) { uint8_t crc = 0; - for ( int i = 0; i < tamBytes; i++ ) - { + for (int i = 0; i < tamBytes; i++) { uint8_t data = msg[i] ^ crc; crc = crc8_table[data]; } - + return crc; } diff --git a/src/exemplo.cpp b/src/exemplo.cpp index cbad342..c2ae18a 100644 --- a/src/exemplo.cpp +++ b/src/exemplo.cpp @@ -2,12 +2,12 @@ #include <vector> // include local +#include "../headers/client.h" #include "../headers/conexao.h" #include "../headers/crc8.h" #include "../headers/frame.h" #include "../headers/macros.h" #include "../headers/server.h" -#include "../headers/client.h" using namespace std; @@ -15,57 +15,51 @@ using namespace std; typedef enum { CLIENT, SERVER } STATUS_E; -int get_status( char *argv ) -{ - if ( !strcmp( argv, "client") ) { return CLIENT; } - if ( !strcmp( argv, "server") ) { return SERVER; } - else { return -1; } +int get_status(char *argv) { + if (!strcmp(argv, "client")) { return CLIENT; } + if (!strcmp(argv, "server")) { + return SERVER; + } else { + return -1; + } } int main(int argc, char *argv[]) { gen_crc8_table(); - char* device = argv[2]; + char *device = argv[2]; cout << "Device: " << device << endl; conexao socket(device); int status = get_status(argv[1]); - switch ( status ) - { - case CLIENT: - { - client cliente(&socket); - cliente.run(); - } - break; - - case SERVER: - { - server servidor(&socket); - servidor.run(); - } - break; + switch (status) { + case CLIENT: { + client cliente(&socket); + cliente.run(); + } break; - default: - cout << "Comando invalido\n"; - break; - } + case SERVER: { + server servidor(&socket); + servidor.run(); + } break; + default: + cout << "Comando invalido\n"; + break; + } + // thread clientSend(&client::run, &cliente); -// thread clientSend(&client::run, &cliente); + // server servidor(&local, &target); + // thread serverReceive(&server::run, &servidor); -// server servidor(&local, &target); -// thread serverReceive(&server::run, &servidor); + // int receive = 0; + // while (true) { + // receive++; + // } -// int receive = 0; -// while (true) { -// receive++; -// } - -// serverReceive.join(); -// clientSend.join(); + // serverReceive.join(); + // clientSend.join(); return 0; } - -- GitLab