diff --git a/Message.cpp b/Message.cpp index 2d88346ca0bd37bee4fb65ea19a2ba3758f41d0f..4a303edb96d9f40995dc6010a581c85729ae49bb 100644 --- a/Message.cpp +++ b/Message.cpp @@ -79,12 +79,10 @@ string Message::getDataAsString() { ostream& operator<<(ostream& os, const Message& msg){ os << '|' << msg.begin << '|' << msg.size << '|' << msg.sequence << '|' << msg.type << '|'; - // os << msg.begin << msg.size << msg.sequence << msg.type; for(int i=0; i<msg.data.size(); ++i){ os << bitset<8>(msg.data[i]); } os << '|' << msg.parity << '|'; - // os << msg.parity; return os; } diff --git a/server.cpp b/server.cpp index e4cc73c02e60ac9b75d24cc3d8488553064741ce..beeb26678d438a7d9c16c438d4ad75bce16de88d 100644 --- a/server.cpp +++ b/server.cpp @@ -37,7 +37,7 @@ int main(){ receiveProtocol.reset(); receiveProtocol.receive(sockt,SIZE,WAIT_STOP,false); cout << "fileSize: " << receiveProtocol.getDataAsString() <<endl; - int fileSize = stoi(receiveProtocol.getDataAsString()); + unsigned long long fileSize = stoi(receiveProtocol.getDataAsString()); sendProtocol.reset(); if(hasEnoughSpace(fileSize)){ sendProtocol.setData(vector<BYTE>(1,(BYTE)0), OK); @@ -45,6 +45,7 @@ int main(){ }else{ sendProtocol.setData(vector<BYTE>(1,(BYTE)SPACE_ERR), ERROR); sendProtocol.sendMessage(sockt,0); + continue; } receiveProtocol.reset(); receiveProtocol.receive(sockt,DATA,SLIDING,true); @@ -52,12 +53,34 @@ int main(){ writeFile(getWorkingPath()+"/"+fileName,receiveProtocol.getData()); }else if(status == GET){ - //TODO + string filePath = getWorkingPath()+"/"+receiveProtocol.getDataAsString(); + if(fexists(filePath)) { + string size = to_string(filesize(filePath)); + cout << "ARQUIVO: " << filePath << "|" << size << endl; + sendProtocol.setData(vector<BYTE>(size.begin(), size.end()), SIZE); + sendProtocol.sendMessage(sockt, 0); + int error = receiveProtocol.receive(sockt, OK, WAIT_STOP, false); + if(error < 0) continue; + sendProtocol.reset(); + ifstream putFile (filePath); + stringstream buffer; + buffer << putFile.rdbuf(); + string data = buffer.str(); + sendProtocol.setData(vector<BYTE>(data.begin(), data.end()), DATA); + sendProtocol.transmit(sockt, SLIDING); + } else { + sendProtocol.setData(vector<BYTE>(1,(BYTE)DIR_ERR), ERROR); + sendProtocol.sendMessage(sockt,0); + continue; + } }else if(status == ENDTX){ sendProtocol.reset(); vector<BYTE> val(1,(BYTE)receiveProtocol.getMessages().back().sequence.to_ulong()); sendProtocol.setData(val, ACK); sendProtocol.sendMessages(sockt); + }else if(status < 0 && status != NOISE){ + sendProtocol.setData(vector<BYTE>(1,(BYTE)NACK), ERROR); + sendProtocol.sendMessage(sockt,0); } }catch(char const* strException){ cout << "Erro:" <<strException <<endl;