From 7b93c43ded3d5d7f289ecf1bfd754fc011c2ae7b Mon Sep 17 00:00:00 2001
From: Vytor Calixto <vytorcalixto@gmail.com>
Date: Mon, 7 Dec 2015 14:37:24 -0200
Subject: [PATCH] Put function added in client

---
 .gitignore       |  3 ++-
 client.cpp       | 34 ++++++++++++++++++++++++++++++----
 definitions.h    |  4 +++-
 dirFunctions.cpp | 12 ++++++++++++
 dirFunctions.h   |  6 +++++-
 5 files changed, 52 insertions(+), 7 deletions(-)

diff --git a/.gitignore b/.gitignore
index 1c6283f..809a033 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
 *.o
 cacoclient
-cacoserver
\ No newline at end of file
+cacoserver
+*.txt
diff --git a/client.cpp b/client.cpp
index 2044d83..0ff6cf3 100644
--- a/client.cpp
+++ b/client.cpp
@@ -42,11 +42,35 @@ int main(){
                 receiveProtocol.receive(sockt, OUTPUT, WAIT_STOP, true);
                 cout << receiveProtocol.getDataAsString() << endl;
             }else if(command == "put"){
-                sendProtocol.setData(vector<BYTE>(line.begin(), line.end()), PUT);
-                sendProtocol.transmit(sockt, WAIT_STOP);
+                args = line.substr(pos+1, line.size());
+                if(fexists(args)) {
+                    int size = filesize(args);
+                    cout << "ARQUIVO: " << args << "|" << size << endl;
+                    sendProtocol.setData(vector<BYTE>(args.begin(), args.end()), PUT);
+                    sendProtocol.sendMessage(sockt, 0);
+                    int error = receiveProtocol.receive(sockt, OK, WAIT_STOP, false);
+                    if(error < 0) continue;
+                    sendProtocol.reset();
+                    sendProtocol.setData(vector<BYTE>(size), SIZE);
+                    sendProtocol.sendMessage(sockt, 0);
+                    error = receiveProtocol.receive(sockt, OK, WAIT_STOP, false);
+                    if(error < 0) continue;
+                    sendProtocol.reset();
+                    ifstream putFile (args);
+                    stringstream buffer;
+                    buffer << putFile.rdbuf();
+                    sendProtocol.setData(vector<BYTE>(buffer.str().begin(), buffer.str().begin()), PUT);
+                    sendProtocol.transmit(sockt, SLIDING);
+                } else {
+                    cout << "ERROR: arquivo não existe\n";
+                }
             }else if(command == "get"){
-                sendProtocol.setData(vector<BYTE>(line.begin(), line.end()), GET);
-                sendProtocol.transmit(sockt, WAIT_STOP);
+                args = line.substr(pos+1, line.size());
+                sendProtocol.setData(vector<BYTE>(args.begin(), args.end()), GET);
+                sendProtocol.sendMessage(sockt, 0);
+                int error = receiveProtocol.receive()sockt, SIZE, WAIT_STOP, false;
+                if(error < 0) continue;
+                int fileSize = (int) receiveProtocol.getDataAsString();
             }else if(command == "help"){
                 printCommandsList();
             }else{
@@ -58,6 +82,8 @@ int main(){
         }catch(out_of_range e){
             cerr<<"Error: Esse comando requer argumentos."<<endl;
         }
+        sendProtocol.reset();
+        receiveProtocol.reset();
     }
     return 0;
 }
diff --git a/definitions.h b/definitions.h
index a5305db..cad4cec 100644
--- a/definitions.h
+++ b/definitions.h
@@ -6,6 +6,8 @@
 #include <vector>
 #include <bitset>
 #include <cstdlib>
+#include <fstream>
+#include <sstream>
 
 using namespace std;
 
@@ -16,7 +18,7 @@ using namespace std;
 //Delimiter indicating beginning of a message
 #define BEGIN 0x7E
 //Socket device
-#define DEVICE "lo"
+#define DEVICE "eth0"
 //Message types
 #define NACK 0
 #define ACK 1
diff --git a/dirFunctions.cpp b/dirFunctions.cpp
index d0e7aa4..2845ba9 100644
--- a/dirFunctions.cpp
+++ b/dirFunctions.cpp
@@ -4,6 +4,8 @@
 #include <string.h>
 #include <vector>
 #include <stdio.h> //popen
+#include <sys/stat.h>
+#include <fstream>
 
 using namespace std;
 
@@ -28,3 +30,13 @@ string ls(string args){
     pclose(lsOut);
     return output;
 }
+
+bool fexists(string path) {
+    struct stat buffer;
+    return (stat(path.c_str(), &buffer) == 0);
+}
+
+int filesize(string path) {
+    ifstream in(path, ifstream::ate | ifstream::binary);
+    return in.tellg();
+}
diff --git a/dirFunctions.h b/dirFunctions.h
index fe12040..08832ba 100644
--- a/dirFunctions.h
+++ b/dirFunctions.h
@@ -9,4 +9,8 @@ void cd(string path);
 
 string ls(string path);
 
-#endif
\ No newline at end of file
+bool fexists(string path);
+
+int filesize(string path);
+
+#endif
-- 
GitLab