From 334b290db111b5961da79ad4fae48149e4f7a675 Mon Sep 17 00:00:00 2001
From: "Israel B. Sant'Anna" <ibsa14@inf.ufpr.br>
Date: Mon, 7 Dec 2015 02:46:10 -0200
Subject: [PATCH] Size changed to actual data size,without counting the zero
 filling bytes; cdr sending OK back

Signed-off-by: Israel B. Sant'Anna <ibsa14@inf.ufpr.br>
---
 Message.cpp  | 2 +-
 Protocol.cpp | 6 ++++--
 server.cpp   | 3 +++
 3 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/Message.cpp b/Message.cpp
index 4461bcc..2d88346 100644
--- a/Message.cpp
+++ b/Message.cpp
@@ -64,7 +64,7 @@ char* Message::getMessageAsCharPointer(){
 }
 
 int Message::getMessageSize(){
-    return size.to_ulong()+4;
+    return size.to_ulong() < MINSIZE ? MINSIZE+4 : size.to_ulong()+4;
 }
 
 int Message::dataToInt() {
diff --git a/Protocol.cpp b/Protocol.cpp
index 081fde2..6a2613b 100644
--- a/Protocol.cpp
+++ b/Protocol.cpp
@@ -72,10 +72,11 @@ int Protocol::setData(vector<BYTE> data, int type){
         last = data.begin()+i+size+1;
         vector<BYTE> subvector(first, last);
         msg.data = subvector;
+        msg.size = bitset<SIZE_S>(size);
+        msg.calcParity();
         if(size < MINSIZE){
             BYTE zero = 0x00;
             msg.data.insert(msg.data.end(), MINSIZE-size, zero);
-            size = MINSIZE;
         }
         msg.size = bitset<SIZE_S>(size);
         msg.calcParity();
@@ -95,7 +96,8 @@ int Protocol::recvMessage(int sockt){
     Message msg = Message();
     int size = (int)(dataRec[1]>>2);
     cout << "Tamanho:" << size << "\t";
-    msg.setBitFields(dataRec[0], dataRec[1], dataRec[2], dataRec[size+3]);
+    int dataSize = size < MINSIZE ? MINSIZE : size;
+    msg.setBitFields(dataRec[0], dataRec[1], dataRec[2], dataRec[dataSize+3]);
     cout << "Sequence:" << msg.sequence.to_ulong() << "\t";
 
     BYTE msgData[size];
diff --git a/server.cpp b/server.cpp
index 40b87c6..637f0ce 100644
--- a/server.cpp
+++ b/server.cpp
@@ -18,6 +18,9 @@ int main(){
             if(status == CD){
                 cout << "Recebeu CD\n";
                 cd(receiveProtocol.getDataAsString());
+                vector<BYTE> val(1,(BYTE)0);
+                sendProtocol.setData(val, OK);
+                sendProtocol.sendMessage(sockt,0);
             }else if(status == LS){
                 cout << "protocol data: " << receiveProtocol.getDataAsString() << endl;
                 cout << "message data: " << receiveProtocol.getMessages().back().getDataAsString() << endl;
-- 
GitLab