diff --git a/Message.cpp b/Message.cpp index ca83b764cb679e6f5b6b641abf1e8bfdf2a64760..e979626350b4e05cf9f260bee4fd835f6fc92ea7 100644 --- a/Message.cpp +++ b/Message.cpp @@ -19,7 +19,22 @@ bool Message::checkParity() { string Message::getSendData() { calcParity(); string s(data.begin(), data.end()); - string d = header.c_ctrl.begin + header.c_ctrl.sizeSeq + header.c_ctrl.seqType + s.c_str() + header.c_ctrl.parity; + int size = data.size(); + //TODO: quebrar em partes aqui? ou tem que quebrar no protocolo? + header.i_ctrl.size = size; + header.i_ctrl.sequence = 1; + char *fill; + if(size < 63) { + fill = (char*) malloc((63-size)*sizeof(char)); + memset(fill, 0, (63-size)*sizeof(char)); + } + string d; + d += header.c_ctrl.begin; + d += header.c_ctrl.sizeSeq; + d += header.c_ctrl.seqType; + d += s.c_str(); + d += fill; + d += header.c_ctrl.parity; return d; } diff --git a/Protocol.cpp b/Protocol.cpp index 4ad3707ab0a96cb4334ec810eceb66dcf96ea6f8..ab019cfc6772c152c80f8c0f40bd3b848e2e6b9d 100644 --- a/Protocol.cpp +++ b/Protocol.cpp @@ -10,9 +10,10 @@ void Protocol::setMessages(vector<Message> messages){ this->messages = messages; } -bool Protocol::send(int socket, int window) { +bool Protocol::sendMessages(int socket, int window) { for(int i=0; i < messages.size(); ++i) { string data = messages[i].getSendData(); + send(socket, data.c_str(), messages[i].header.i_ctrl.size, 0); } return true; } @@ -70,6 +71,7 @@ void Protocol::setData(vector<BYTE> data, int type){ int Protocol::recvMessage(int sockt){ BYTE dataRec[MAXSIZE+4]; recv(sockt, dataRec, MAXSIZE, 0); + cout << dataRec[0] << dataRec[1] << dataRec[2] << "|\t"; Message msg; msg.header.c_ctrl.begin = dataRec[0]; if(msg.header.i_ctrl.begin != BEGIN){ diff --git a/Protocol.h b/Protocol.h index 040f6d9fb1cb33f97a7ba44bd33d69bfb8b157b1..3c9e073e446d2f371ce59f7913343c32792d5bdb 100644 --- a/Protocol.h +++ b/Protocol.h @@ -9,7 +9,7 @@ private: vector<Message> messages; int timeout; public: - bool send(int socket, int window); + bool sendMessages(int socket, int window); vector<Message> getMessages(); void setMessages(vector<Message> messages); vector<BYTE> getData(); diff --git a/client.cpp b/client.cpp index 9d7a6e043569d721bd17cb1a9ee245dd79504b6c..3cdf3f0c8fa48f7f58ab7f74afe1bd61dbc18b7d 100644 --- a/client.cpp +++ b/client.cpp @@ -35,7 +35,7 @@ int main(){ msg.header.i_ctrl.type = LS; msg.data = vector<BYTE>(line.begin(), line.end()); protocol.addMessage(msg); - protocol.send(sockt, WAIT_STOP); + protocol.sendMessages(sockt, WAIT_STOP); // TODO: imprimir resposta }else if(command == "put"){ //TODO @@ -52,6 +52,24 @@ int main(){ }catch(out_of_range e){ cerr<<"Error: Esse comando requer argumentos."<<endl; } + + int status = protocol.recvMessage(sockt); + if(status > 0){ + if(status == ENDTX){ + protocol = Protocol(); + //TODO: send ACK + }else if(status == CD){ + cd(protocol.getDataAsString()); + }else if(status == LS){ + cout << "Recebeu LS\n"; + string output = ls(protocol.getDataAsString()); + //TODO: send output back + }else if(status == PUT){ + //TODO + }else if(status == GET){ + //TODO + } + } } return 0; } diff --git a/definitions.h b/definitions.h index 8269b16c18fc86321eaf8a3db16dbd77378880a5..2fb9e0a39bc5f205a556090951a9b74946b8ac3f 100644 --- a/definitions.h +++ b/definitions.h @@ -4,6 +4,7 @@ #include <iostream> #include <string.h> #include <vector> +#include <cstdlib> using namespace std; diff --git a/server.cpp b/server.cpp index 70971dbfcb328427b3f8f2c695d65c164190c852..e0fd2f1a5e85ee39c09e7d1cc12ece1364ea97dd 100644 --- a/server.cpp +++ b/server.cpp @@ -16,7 +16,7 @@ int main(){ }else if(status == CD){ cd(protocol.getDataAsString()); }else if(status == LS){ - cout << "Recebeu LS"; + cout << "Recebeu LS\n"; string output = ls(protocol.getDataAsString()); //TODO: send output back }else if(status == PUT){