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
Branches
No related tags found
1 merge request!1Lento
......@@ -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(){
......
......@@ -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();
};
......
......@@ -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,
......
......@@ -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
#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
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
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment