diff --git a/.gitignore b/.gitignore index 1c6283f8e4841cd471b0ed9fc11985f052ab6e73..809a03352956e45112fe7a17d704cf2d27f05445 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.o cacoclient -cacoserver \ No newline at end of file +cacoserver +*.txt diff --git a/client.cpp b/client.cpp index 2044d83b9b3fc34b81cf5f263813d8855c13dcb5..0ff6cf39b06635d0fec28758da7a673e8c2cc8c5 100644 --- a/client.cpp +++ b/client.cpp @@ -42,11 +42,35 @@ int main(){ receiveProtocol.receive(sockt, OUTPUT, WAIT_STOP, true); cout << receiveProtocol.getDataAsString() << endl; }else if(command == "put"){ - sendProtocol.setData(vector<BYTE>(line.begin(), line.end()), PUT); - sendProtocol.transmit(sockt, WAIT_STOP); + args = line.substr(pos+1, line.size()); + if(fexists(args)) { + int size = filesize(args); + cout << "ARQUIVO: " << args << "|" << size << endl; + sendProtocol.setData(vector<BYTE>(args.begin(), args.end()), PUT); + sendProtocol.sendMessage(sockt, 0); + int error = receiveProtocol.receive(sockt, OK, WAIT_STOP, false); + if(error < 0) continue; + sendProtocol.reset(); + sendProtocol.setData(vector<BYTE>(size), SIZE); + sendProtocol.sendMessage(sockt, 0); + error = receiveProtocol.receive(sockt, OK, WAIT_STOP, false); + if(error < 0) continue; + sendProtocol.reset(); + ifstream putFile (args); + stringstream buffer; + buffer << putFile.rdbuf(); + sendProtocol.setData(vector<BYTE>(buffer.str().begin(), buffer.str().begin()), PUT); + sendProtocol.transmit(sockt, SLIDING); + } else { + cout << "ERROR: arquivo não existe\n"; + } }else if(command == "get"){ - sendProtocol.setData(vector<BYTE>(line.begin(), line.end()), GET); - sendProtocol.transmit(sockt, WAIT_STOP); + args = line.substr(pos+1, line.size()); + sendProtocol.setData(vector<BYTE>(args.begin(), args.end()), GET); + sendProtocol.sendMessage(sockt, 0); + int error = receiveProtocol.receive()sockt, SIZE, WAIT_STOP, false; + if(error < 0) continue; + int fileSize = (int) receiveProtocol.getDataAsString(); }else if(command == "help"){ printCommandsList(); }else{ @@ -58,6 +82,8 @@ int main(){ }catch(out_of_range e){ cerr<<"Error: Esse comando requer argumentos."<<endl; } + sendProtocol.reset(); + receiveProtocol.reset(); } return 0; } diff --git a/definitions.h b/definitions.h index a5305db8e80d31492227baabe1c443ead7c1a19e..cad4cec6598d72b040bf6f0a02390e0fadba1445 100644 --- a/definitions.h +++ b/definitions.h @@ -6,6 +6,8 @@ #include <vector> #include <bitset> #include <cstdlib> +#include <fstream> +#include <sstream> using namespace std; @@ -16,7 +18,7 @@ using namespace std; //Delimiter indicating beginning of a message #define BEGIN 0x7E //Socket device -#define DEVICE "lo" +#define DEVICE "eth0" //Message types #define NACK 0 #define ACK 1 diff --git a/dirFunctions.cpp b/dirFunctions.cpp index d0e7aa4509560a894ee75b0e4ce921724bd2bce2..2845ba91e6cbe51ed3d268fdf179aa7aec5a0a0b 100644 --- a/dirFunctions.cpp +++ b/dirFunctions.cpp @@ -4,6 +4,8 @@ #include <string.h> #include <vector> #include <stdio.h> //popen +#include <sys/stat.h> +#include <fstream> using namespace std; @@ -28,3 +30,13 @@ string ls(string args){ pclose(lsOut); return output; } + +bool fexists(string path) { + struct stat buffer; + return (stat(path.c_str(), &buffer) == 0); +} + +int filesize(string path) { + ifstream in(path, ifstream::ate | ifstream::binary); + return in.tellg(); +} diff --git a/dirFunctions.h b/dirFunctions.h index fe120405bd8f732f027633b8b0ad3107c3d8b0a0..08832baa03082002b5421f3ce987daa11f881344 100644 --- a/dirFunctions.h +++ b/dirFunctions.h @@ -9,4 +9,8 @@ void cd(string path); string ls(string path); -#endif \ No newline at end of file +bool fexists(string path); + +int filesize(string path); + +#endif