Skip to content
Snippets Groups Projects
Commit 002e600f authored by Israel Barreto Sant'Anna's avatar Israel Barreto Sant'Anna
Browse files

Started implementation of commands in server side

parent 53c7bbd5
No related branches found
No related tags found
1 merge request!1Lento
...@@ -10,28 +10,40 @@ void Protocol::setMessage(Message message){ ...@@ -10,28 +10,40 @@ void Protocol::setMessage(Message message){
this->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){ int Protocol::readMessage(int sockt){
unsigned char dataRec[MAXSIZE+4]; BYTE dataRec[MAXSIZE+4];
int ret = recv(sockt, dataRec, MAXSIZE, 0); recv(sockt, dataRec, MAXSIZE, 0);
Message msg; Message msg;
msg.c_ctrl.begin = dataRec[0]; msg.c_ctrl.begin = dataRec[0];
if(msg.i_ctrl.begin != BEGIN){ if(msg.i_ctrl.begin != BEGIN){
return 0; return -1;
} }
msg.c_ctrl.size = dataRec[1]; msg.c_ctrl.size = dataRec[1];
msg.c_ctrl.seqType = dataRec[2]; msg.c_ctrl.seqType = dataRec[2];
msg.c_ctrl.parity = dataRec[3+msg.i_ctrl.size]; msg.c_ctrl.parity = dataRec[3+msg.i_ctrl.size];
// TODO: Check parity // TODO: Check parity
if(msg.i_ctrl.type == FIM){ if(msg.i_ctrl.type == ENDTX){
this->message = msg; return ENDTX;
return 1;
} }
unsigned char msgData[msg.i_ctrl.size]; BYTE msgData[msg.i_ctrl.size];
memcpy(msgData,dataRec+3,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->data.insert(this->data.end(), msgData, msgData+msg.i_ctrl.size);
this->message = msg; this->message = msg;
return -1; return msg.i_ctrl.type;
} }
Protocol::Protocol(){ Protocol::Protocol(){
......
...@@ -6,13 +6,16 @@ class Protocol{ ...@@ -6,13 +6,16 @@ class Protocol{
private: private:
Message message; Message message;
vector<unsigned char> data; vector<BYTE> data;
int timeout; int timeout;
public: public:
Message getMessage(); Message getMessage();
void setMessage(Message message); void setMessage(Message message);
int readMessage(int socket); vector<BYTE> getData();
void setData(vector<BYTE> data);
string getDataAsString();
int readMessage(int sockt);
Protocol(); Protocol();
}; };
......
...@@ -7,10 +7,10 @@ ...@@ -7,10 +7,10 @@
using namespace std; using namespace std;
#define MAXSIZE 64 #define MAXSIZE 64 //number maximum of bytes in data field
#define MINSIZE 60 #define MINSIZE 60 //number minimum of bytes in data field
#define BEGIN 0x7E #define BEGIN 0x7E //begin delimiter value
#define DEVICE "lo" #define DEVICE "lo"
#define NACK 0 #define NACK 0
...@@ -20,10 +20,13 @@ using namespace std; ...@@ -20,10 +20,13 @@ using namespace std;
#define PUT 5 #define PUT 5
#define GET 6 #define GET 6
#define OK 8 #define OK 8
#define TAM 9 #define SIZE 9
#define TELA 10 #define OUTPUT 10
#define ERRO 14 #define DATA 13
#define FIM 15 #define ERROR 14
#define ENDTX 15
#define BYTE unsigned char
typedef struct{ typedef struct{
int begin : 8, int begin : 8,
......
...@@ -3,9 +3,7 @@ ...@@ -3,9 +3,7 @@
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <vector> #include <vector>
#include <numeric> //accumulate
#include <stdio.h> //popen #include <stdio.h> //popen
#include <iterator>
using namespace std; using namespace std;
...@@ -28,15 +26,4 @@ string ls(string args){ ...@@ -28,15 +26,4 @@ string ls(string args){
} }
pclose(lsOut); pclose(lsOut);
return output; 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
#include "definitions.h" #include "definitions.h"
#include "dirFunctions.h"
#include "ConexaoRawSocket.c" #include "ConexaoRawSocket.c"
int main(){ int main(){
int sockt = ConexaoRawSocket(DEVICE); int sockt = ConexaoRawSocket(DEVICE);
Protocol protocol; Protocol protocol;
while(true){ while(true){
int ret = protocol.readMessage(sockt); int status = protocol.readMessage(sockt);
if(ret){ if(status > 0){
if(ret == 1){ if(status == ENDTX){
//End of transmission 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 //TODO: Check window sequence and blablabla
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment