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

Merge branch 'sliding-window' into develop

Conflicts:
	Protocol.cpp
parents bc4a7d8e b028852a
Branches
No related tags found
1 merge request!1Lento
...@@ -2,6 +2,11 @@ ...@@ -2,6 +2,11 @@
#include "Protocol.h" #include "Protocol.h"
#include "definitions.h" #include "definitions.h"
typedef struct{
int index;
bool sent;
}FrameItem;
vector<Message> Protocol::getMessages(){ vector<Message> Protocol::getMessages(){
return messages; return messages;
} }
...@@ -108,26 +113,31 @@ int Protocol::recvMessage(int sockt){ ...@@ -108,26 +113,31 @@ int Protocol::recvMessage(int sockt){
void Protocol::transmit(int sockt, int window){ void Protocol::transmit(int sockt, int window){
int status; int status;
vector<int> frame; vector<FrameItem> frame;
int lastFramed = 0; int lastFramed = 0;
int messagesLeft = messages.size(); int messagesLeft = messages.size();
bool shouldSend = true;
Protocol response; Protocol response;
while(messagesLeft > 0){ while(messagesLeft > 0){
for(int j=0; j < window; ++j) { for(int j=0; j < window; ++j) {
frame.push_back(lastFramed++); // FIXME: this will probably brake with many messages
if(shouldSend) sendMessage(sockt, frame[j]); FrameItem fi = {.index = lastFramed++, .sent=false};
frame.push_back(fi);
if(!frame[j].sent){
sendMessage(sockt, frame[j].index);
frame[j].sent = true;
}
} }
// TODO: timeout // TODO: timeout
status = response.recvMessage(sockt); status = response.recvMessage(sockt);
cout << "transmit status:" << status << endl; cout << "transmit status:" << status << endl;
shouldSend = true;
if(status == NACK){ if(status == NACK){
int nackIndex = stoi(response.getMessages().back().getDataAsString()); int nackIndex = stoi(getDataAsString());
for(int j=0; j < window; ++j) { for(int j=0; j < window; ++j) {
if(frame[j] < nackIndex) { if(frame[j].index < nackIndex) {
frame.erase(frame.begin() + j); frame.erase(frame.begin() + j);
--messagesLeft; --messagesLeft;
}else if(frame[j].index == nackIndex) {
frame[j].sent = false;
} }
} }
response.reset(); response.reset();
...@@ -135,7 +145,7 @@ void Protocol::transmit(int sockt, int window){ ...@@ -135,7 +145,7 @@ void Protocol::transmit(int sockt, int window){
}else if(status == ACK){ }else if(status == ACK){
int ackIndex = stoi(response.getMessages().back().getDataAsString()); int ackIndex = stoi(response.getMessages().back().getDataAsString());
for(int j=0; j < window; ++j) { for(int j=0; j < window; ++j) {
if(frame[j] <= ackIndex) { if(frame[j].index <= ackIndex) {
frame.erase(frame.begin() + j); frame.erase(frame.begin() + j);
--messagesLeft; --messagesLeft;
} }
...@@ -150,7 +160,6 @@ void Protocol::transmit(int sockt, int window){ ...@@ -150,7 +160,6 @@ void Protocol::transmit(int sockt, int window){
// cout << "Remoto:\n" << ((status == ERROR)?"ERROR: ":"") << getDataAsString() << endl; // cout << "Remoto:\n" << ((status == ERROR)?"ERROR: ":"") << getDataAsString() << endl;
} else { } else {
//TODO: treat error //TODO: treat error
shouldSend = false;
} }
} }
reset(); reset();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment