diff --git a/Protocol.cpp b/Protocol.cpp index 02f2ef78de83e39f9a8ea586d30c748e08893a16..b243c2c7589d343138e8dcee7c0105b8ff4d68db 100644 --- a/Protocol.cpp +++ b/Protocol.cpp @@ -16,7 +16,6 @@ bool Protocol::sendMessages(int socket) { int status = sendMessage(socket, i); if(status < 0) { success = false; - cout << "fail" << endl; } } return success; @@ -49,7 +48,7 @@ string Protocol::getDataAsString(){ return str; } -void Protocol::setData(vector<BYTE> data, int type){ +int Protocol::setData(vector<BYTE> data, int type){ vector<BYTE>::const_iterator first, last; int i; for (i=0; i <= ((int)data.size())-MAXSIZE; i+=MAXSIZE){ @@ -82,23 +81,24 @@ void Protocol::setData(vector<BYTE> data, int type){ msg.calcParity(); messages.push_back(msg); } + return messages.size(); } int Protocol::recvMessage(int sockt){ BYTE dataRec[MAXSIZE+4]; int r = recv(sockt, dataRec, MAXSIZE+4, 0); cout << bitset<8>(dataRec[0]) << "|" << bitset<8>(dataRec[1]) << "|" << bitset<8>(dataRec[2]) << "|" << bitset<8>(dataRec[3]) << "|\t"; - // cout << "recv response: " << r << endl; + cout << "recv response: " << r << endl; if(dataRec[0] != BEGIN){ return NOISE; } Message msg = Message(); int size = (int)(dataRec[1]>>2); - // cout << "Tamanho:" << size << "\t"; + cout << "Tamanho:" << size << "\t"; msg.setBitFields(dataRec[0], dataRec[1], dataRec[2], dataRec[size+3]); - // cout << "Sequence:" << msg.sequence.to_ulong() << "\t"; - if(!receivedMessages.empty() && - (msg.sequence.to_ulong() != ((receivedMessages.back().sequence.to_ulong()+1)%(MAXSIZE+1))) && (msg.sequence.to_ulong() != receivedMessages.back().sequence.to_ulong()) ){ + cout << "Sequence:" << msg.sequence.to_ulong() << "\t"; + if(!messages.empty() && + (msg.sequence.to_ulong() != ((messages.back().sequence.to_ulong()+1)%(MAXSIZE+1)))){ return SEQ_MISS; } if(!msg.checkParity()){ @@ -111,8 +111,8 @@ int Protocol::recvMessage(int sockt){ BYTE msgData[size]; memcpy(msgData,dataRec+3,size); msg.data.insert(msg.data.end(), msgData, msgData+size); - receivedMessages.push_back(msg); - // cout << "Tipo:" << (int)msg.type.to_ulong() << endl; + messages.push_back(msg); + cout << "Tipo:" << (int)msg.type.to_ulong() << endl; return (int)msg.type.to_ulong(); } diff --git a/Protocol.h b/Protocol.h index 460d2dbb8e8e6de143db783306932fcce9cef451..7642201db47b2ad215b228e42f94868ad37197b0 100644 --- a/Protocol.h +++ b/Protocol.h @@ -7,7 +7,6 @@ class Protocol{ private: vector<Message> messages; - vector<Message> receivedMessages; int timeout; public: bool sendMessages(int socket); @@ -15,7 +14,7 @@ public: vector<Message> getMessages(); void setMessages(vector<Message> messages); vector<BYTE> getData(); - void setData(vector<BYTE> data, int type); + int setData(vector<BYTE> data, int type); string getDataAsString(); int recvMessage(int sockt); void addMessage(Message msg); diff --git a/server.cpp b/server.cpp index 16db764a2c5aee8ac6fb359297eb625adbb99bf0..a6eb670226732d10f08e3015aa27fa48dbbe59dd 100644 --- a/server.cpp +++ b/server.cpp @@ -5,25 +5,26 @@ int main(){ int sockt = ConexaoRawSocket(DEVICE); - Protocol protocol; + Protocol receiveProtocol, sendProtocol; cout << "Você está rodando o servidor Caco\n"; while(true){ - int status = protocol.recvMessage(sockt); + int status = receiveProtocol.recvMessage(sockt); cout << "status: " << status << endl; // cout << protocol.getDataAsString() << endl; if(status > 0){ if(status == ENDTX){ - protocol = Protocol(); + // protocol = Protocol(); //TODO: send ACK }else if(status == CD){ cout << "Recebeu CD\n"; - cd(protocol.getDataAsString()); + cd(receiveProtocol.getDataAsString()); }else if(status == LS){ - cout << protocol.getDataAsString() << endl; - string output = ls(protocol.getDataAsString()); - cout << "LS:" << output << endl; - protocol.setData(vector<BYTE>(output.begin(), output.end()), OUTPUT); - protocol.sendMessages(sockt); + string output = ls(receiveProtocol.getDataAsString()); + cout << "LS: " << output << endl; + // receiveProtocol = Protocol(); + receiveProtocol.setData(vector<BYTE>(output.begin(), output.end()), OUTPUT); + receiveProtocol.sendMessages(sockt); + // sendProtocol = Protocol(); }else if(status == PUT){ //TODO }else if(status == GET){