From f22af4304708f4eea7abbbc21153214b585a00cb Mon Sep 17 00:00:00 2001
From: Vytor Calixto <vytorcalixto@gmail.com>
Date: Mon, 7 Dec 2015 02:50:51 -0200
Subject: [PATCH] Tryed to fix ack and nack in receive

---
 Protocol.cpp | 3 ++-
 server.cpp   | 2 ++
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/Protocol.cpp b/Protocol.cpp
index 081fde2..352e2c8 100644
--- a/Protocol.cpp
+++ b/Protocol.cpp
@@ -187,7 +187,7 @@ int Protocol::receive(int sockt, int type, int window, bool dataEndable){
         if(status == NOISE){
             continue;
         } else if(status == type) {
-            if(messages.back().sequence.to_ulong() != nextSequence){
+            if(!messages.empty() && (messages.back().sequence.to_ulong() != nextSequence)){
                 response.reset();
                 vector<BYTE> val(1,(BYTE)nextSequence);
                 response.setData(val, NACK);
@@ -197,6 +197,7 @@ int Protocol::receive(int sockt, int type, int window, bool dataEndable){
                 vector<BYTE> val(1,(BYTE)messages.back().sequence.to_ulong());
                 response.setData(val, ACK);
                 nextSequence = (messages.back().sequence.to_ulong()+1)%(MAXSIZE+1);
+                shouldSend = true;
             } else {
                 //TODO: treat something?
             }
diff --git a/server.cpp b/server.cpp
index 40b87c6..e2ae51e 100644
--- a/server.cpp
+++ b/server.cpp
@@ -17,6 +17,7 @@ int main(){
             }
             if(status == CD){
                 cout << "Recebeu CD\n";
+                cout << "CD: " << receiveProtocol.getDataAsString() << endl;
                 cd(receiveProtocol.getDataAsString());
             }else if(status == LS){
                 cout << "protocol data: " << receiveProtocol.getDataAsString() << endl;
@@ -25,6 +26,7 @@ int main(){
                 cout << "LS: " << output << endl;
                 sendProtocol.setData(vector<BYTE>(output.begin(), output.end()), OUTPUT);
                 sendProtocol.transmit(sockt, WAIT_STOP);
+                cout << "finished transmit" << endl;
             }else if(status == PUT){
                 //TODO
             }else if(status == GET){
-- 
GitLab