Skip to content
Snippets Groups Projects
Commit cc28a42f authored by Vytor Calixto's avatar Vytor Calixto :space_invader:
Browse files

Sending lsr to server

parent 13cf73e5
Branches
No related tags found
1 merge request!1Lento
......@@ -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;
}
......
......@@ -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){
......
......@@ -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();
......
......@@ -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;
}
......
......@@ -4,6 +4,7 @@
#include <iostream>
#include <string.h>
#include <vector>
#include <cstdlib>
using namespace std;
......
......@@ -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){
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment