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

Merge branch 'putServer' into put

Conflicts:
	definitions.h
	dirFunctions.cpp
	dirFunctions.h
parents 93355d56 16a56139
No related branches found
No related tags found
1 merge request!1Lento
......@@ -27,7 +27,7 @@ bool Protocol::sendMessages(int socket) {
}
bool Protocol::sendMessage(int socket, int index) {
cout << "message: " << messages[index] << endl;
cout << "message sent: " << messages[index] << endl;
vector<BYTE> message = messages[index].getMessage();
unsigned char* msg = reinterpret_cast<unsigned char*> (message.data());
int status = send(socket, msg, messages[index].getMessageSize(), 0);
......@@ -88,7 +88,7 @@ int Protocol::setData(vector<BYTE> data, int type){
int Protocol::recvMessage(int sockt){
BYTE dataRec[MAXSIZE+4];
int r = recv(sockt, dataRec, MAXSIZE+4, 0);
cout << bitset<8>(dataRec[0]) << "|" << bitset<8>(dataRec[1]) << "|" << bitset<8>(dataRec[2]) << "|" << bitset<8>(dataRec[3]) << "|\t";
cout << "begin: "<< bitset<8>(dataRec[0]) <<endl;
cout << "recv response: " << r << endl;
if(dataRec[0] != BEGIN){
return NOISE;
......@@ -99,7 +99,6 @@ int Protocol::recvMessage(int sockt){
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];
memcpy(msgData,dataRec+3,size);
msg.data.insert(msg.data.end(), msgData, msgData+size);
......@@ -107,6 +106,7 @@ int Protocol::recvMessage(int sockt){
messages.push_back(msg);
cout << "Tipo:" << (int)msg.type.to_ulong() << endl;
cout <<"message received: "<< msg<<endl;
if(!msg.checkParity()){
return INCONSISTENT;
}
......@@ -190,6 +190,7 @@ int Protocol::receive(int sockt, int type, int window, bool dataEndable){
}
status = recvMessage(sockt);
cout << "receive status:" << status << endl;
cout << "sequence: "<<messages.back().sequence.to_ulong()<<" next: "<<nextSequence<<endl;
if(status == NOISE){
continue;
} else if(status == type) {
......@@ -209,11 +210,16 @@ int Protocol::receive(int sockt, int type, int window, bool dataEndable){
nextSequence = (messages.back().sequence.to_ulong()+1)%(MAXSIZE+1);
}
} else if(status == ERROR) {
string str(messages.back().data.begin(), messages.back().data.end());
cout << "ERROR: " << getDataAsString() << endl;
cout << "ERROR: " << messages.back().getDataAsString() << endl;
return -1;
}
}while(status != end);
if(dataEndable){
response.reset();
vector<BYTE> val(1,(BYTE)messages.back().sequence.to_ulong());
response.setData(val, ACK);
response.sendMessages(sockt);
}
return 0;
}
......
#include <iostream>
#include <fstream>
#include <errno.h>
#include <unistd.h>
#include <string.h>
......@@ -6,6 +7,7 @@
#include <stdio.h> //popen
#include <sys/stat.h>
#include <fstream>
#include <sys/statvfs.h>
using namespace std;
......@@ -31,6 +33,11 @@ string ls(string args){
return output;
}
string getWorkingPath(){
char temp[1024];
return (getcwd(temp, 1024) ? string(temp) : string(""));
}
bool fexists(string path) {
struct stat buffer;
return (stat(path.c_str(), &buffer) == 0);
......@@ -40,3 +47,23 @@ int filesize(string path) {
ifstream in(path, ifstream::ate | ifstream::binary);
return in.tellg();
}
bool hasEnoughSpace(int size){
struct statvfs fsData;
string path = getWorkingPath();
statvfs(path.c_str(), &fsData);
int freeSpace = fsData.f_bsize * fsData.f_bfree;
return (freeSpace > size);
}
void writeFile(string path, vector<BYTE>data){
cout << "path: "<< path<<endl;
string strData(data.begin(), data.end());
ofstream file(path);
if (file.is_open()){
file << strData;
file.close();
}
else cout << "Unable to open file";
}
......@@ -13,4 +13,10 @@ bool fexists(string path);
int filesize(string path);
bool hasEnoughSpace(int size);
string getWorkingPath();
void writeFile(string path, vector<BYTE>data);
#endif
......@@ -19,8 +19,7 @@ int main(){
cout << "Recebeu CD\n";
cout << "CD: " << receiveProtocol.getDataAsString() << endl;
cd(receiveProtocol.getDataAsString());
vector<BYTE> val(1,(BYTE)0);
sendProtocol.setData(val, OK);
sendProtocol.setData(vector<BYTE>(1,(BYTE)0), OK);
sendProtocol.sendMessage(sockt,0);
}else if(status == LS){
cout << "protocol data: " << receiveProtocol.getDataAsString() << endl;
......@@ -31,9 +30,34 @@ int main(){
sendProtocol.transmit(sockt, WAIT_STOP);
cout << "finished transmit" << endl;
}else if(status == PUT){
//TODO
string fileName = receiveProtocol.getDataAsString();
cout << "fileName: " << fileName <<endl;
sendProtocol.setData(vector<BYTE>(1,(BYTE)0), OK);
sendProtocol.sendMessage(sockt,0);
receiveProtocol.reset();
receiveProtocol.receive(sockt,SIZE,WAIT_STOP,false);
cout << "fileSize: " << receiveProtocol.getDataAsString() <<endl;
int fileSize = stoi(receiveProtocol.getDataAsString());
sendProtocol.reset();
if(hasEnoughSpace(fileSize)){
sendProtocol.setData(vector<BYTE>(1,(BYTE)0), OK);
sendProtocol.sendMessage(sockt,0);
}else{
sendProtocol.setData(vector<BYTE>(1,(BYTE)SPACE_ERR), ERROR);
sendProtocol.sendMessage(sockt,0);
}
receiveProtocol.reset();
receiveProtocol.receive(sockt,DATA,SLIDING,true);
cout <<"conteudo: "<< receiveProtocol.getDataAsString()<<endl;
writeFile(getWorkingPath()+"/"+fileName,receiveProtocol.getData());
}else if(status == GET){
//TODO
}else if(status == ENDTX){
sendProtocol.reset();
vector<BYTE> val(1,(BYTE)receiveProtocol.getMessages().back().sequence.to_ulong());
sendProtocol.setData(val, ACK);
sendProtocol.sendMessages(sockt);
}
}catch(char const* strException){
cout << "Erro:" <<strException <<endl;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment