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

Put function added in client

parent e7e63434
No related branches found
No related tags found
1 merge request!1Lento
*.o
cacoclient
cacoserver
*.txt
......@@ -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;
}
......
......@@ -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
......
......@@ -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();
}
......@@ -9,4 +9,8 @@ void cd(string path);
string ls(string path);
bool fexists(string path);
int filesize(string path);
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment