From 002e600f9494cc94eb797902494327dd24bff474 Mon Sep 17 00:00:00 2001
From: Israel Barreto Sant'Anna <ibsa14@inf.ufpr.br>
Date: Fri, 27 Nov 2015 17:53:45 -0200
Subject: [PATCH] Started implementation of commands in server side

Signed-off-by: Israel Barreto Sant'Anna <ibsa14@inf.ufpr.br>
---
 Protocol.cpp     | 28 ++++++++++++++++++++--------
 Protocol.h       |  7 +++++--
 definitions.h    | 17 ++++++++++-------
 dirFunctions.cpp | 13 -------------
 server.cpp       | 38 ++++++++++++++++++++++++++------------
 5 files changed, 61 insertions(+), 42 deletions(-)

diff --git a/Protocol.cpp b/Protocol.cpp
index bc6593a..1758dba 100644
--- a/Protocol.cpp
+++ b/Protocol.cpp
@@ -10,28 +10,40 @@ void Protocol::setMessage(Message message){
     this->message = message;
 }
 
+vector<BYTE> Protocol::getData(){
+    return data;
+}
+
+string Protocol::getDataAsString(){
+    string str(data.begin(), data.end());
+    return str;
+}
+
+void Protocol::setData(vector<BYTE> data){
+    this->data = data;
+}
+
 int Protocol::readMessage(int sockt){
-    unsigned char dataRec[MAXSIZE+4];
-    int ret = recv(sockt, dataRec, MAXSIZE, 0);
+    BYTE dataRec[MAXSIZE+4];
+    recv(sockt, dataRec, MAXSIZE, 0);
     Message msg;
     msg.c_ctrl.begin = dataRec[0];
     if(msg.i_ctrl.begin != BEGIN){
-        return 0;
+        return -1;
     }
     msg.c_ctrl.size = dataRec[1];
     msg.c_ctrl.seqType = dataRec[2];
     msg.c_ctrl.parity = dataRec[3+msg.i_ctrl.size];
     // TODO: Check parity
-    if(msg.i_ctrl.type == FIM){
-        this->message = msg;
-        return 1;
+    if(msg.i_ctrl.type == ENDTX){
+        return ENDTX;
     }
 
-    unsigned char msgData[msg.i_ctrl.size];
+    BYTE msgData[msg.i_ctrl.size];
     memcpy(msgData,dataRec+3,msg.i_ctrl.size);
     this->data.insert(this->data.end(), msgData, msgData+msg.i_ctrl.size);
     this->message = msg;
-    return -1;
+    return msg.i_ctrl.type;
 }
 
 Protocol::Protocol(){
diff --git a/Protocol.h b/Protocol.h
index ea1d57d..4794e52 100644
--- a/Protocol.h
+++ b/Protocol.h
@@ -6,13 +6,16 @@ class Protocol{
 
 private:
     Message message;
-    vector<unsigned char> data;
+    vector<BYTE> data;
     int timeout;
 public:
 
     Message getMessage();
     void setMessage(Message message);
-    int readMessage(int socket);
+    vector<BYTE> getData();
+    void setData(vector<BYTE> data);
+    string getDataAsString();
+    int readMessage(int sockt);
 
     Protocol();
 };
diff --git a/definitions.h b/definitions.h
index 02c948f..747b71c 100644
--- a/definitions.h
+++ b/definitions.h
@@ -7,10 +7,10 @@
 
 using namespace std;
 
-#define MAXSIZE 64
-#define MINSIZE 60
+#define MAXSIZE 64 //number maximum of bytes in data field
+#define MINSIZE 60 //number minimum of bytes in data field
 
-#define BEGIN 0x7E
+#define BEGIN 0x7E //begin delimiter value
 
 #define DEVICE "lo"
 #define NACK 0
@@ -20,10 +20,13 @@ using namespace std;
 #define PUT 5
 #define GET 6
 #define OK 8
-#define TAM 9
-#define TELA 10
-#define ERRO 14
-#define FIM 15
+#define SIZE 9
+#define OUTPUT 10
+#define DATA 13
+#define ERROR 14
+#define ENDTX 15
+
+#define BYTE unsigned char
 
 typedef struct{
 	int begin   : 8,
diff --git a/dirFunctions.cpp b/dirFunctions.cpp
index 527175c..bb8964a 100644
--- a/dirFunctions.cpp
+++ b/dirFunctions.cpp
@@ -3,9 +3,7 @@
 #include <unistd.h>
 #include <string.h>
 #include <vector>
-#include <numeric> //accumulate
 #include <stdio.h> //popen
-#include <iterator>
 
 using namespace std;
 
@@ -28,15 +26,4 @@ string ls(string args){
     }
     pclose(lsOut);
     return output;
-    //TODO: #1
-    // struct dirent *entry;
-    // DIR *dir = opendir(path.c_str());
-    // if(dir != NULL){
-    //     while((entry=readdir(dir)) != NULL){
-    //        cout << entry->d_name << endl;
-    //     }
-    //     closedir(dir);
-    // }else{
-    //     cout<<"Error: could not open directory."<<endl;
-    // }
 }
\ No newline at end of file
diff --git a/server.cpp b/server.cpp
index 9f1688c..a877aea 100644
--- a/server.cpp
+++ b/server.cpp
@@ -1,17 +1,31 @@
 #include "definitions.h"
+#include "dirFunctions.h"
 #include "ConexaoRawSocket.c"
 
 int main(){
-	int sockt = ConexaoRawSocket(DEVICE);
-	Protocol protocol;
-	while(true){
-		int ret = protocol.readMessage(sockt);
-		if(ret){
-			if(ret == 1){
-				//End of transmission
-			}
-			//TODO: Check window sequence and blablabla
-		}
-	}
-	return 0;
+    int sockt = ConexaoRawSocket(DEVICE);
+    Protocol protocol;
+    while(true){
+        int status = protocol.readMessage(sockt);
+        if(status > 0){
+            if(status == ENDTX){
+                protocol = Protocol();
+                if(protocol.getMessage().i_ctrl.type == DATA){
+                    //TODO: send ACK
+                }
+            }else if(status == CD){
+                cd(protocol.getDataAsString());
+            }else if(status == LS){
+                string output = ls(protocol.getDataAsString());
+                //TODO: send output back
+            }else if(status == PUT){
+                //TODO
+            }else if(status == GET){
+                //TODO
+                //intesristing link: http://stackoverflow.com/questions/15138353/reading-the-binary-file-into-the-vector-of-unsigned-chars
+            }
+            //TODO: Check window sequence and blablabla
+        }
+    }
+    return 0;
 }
\ No newline at end of file
-- 
GitLab