From 8c61df774d7be4a8223a64c3ee7f19e544311364 Mon Sep 17 00:00:00 2001 From: ninesens <58813984+ninesens@users.noreply.github.com> Date: Fri, 13 Dec 2019 14:00:50 +0900 Subject: [PATCH] Update main.cpp --- main.cpp | 316 +++++++++++++------------------------------------------ 1 file changed, 75 insertions(+), 241 deletions(-) diff --git a/main.cpp b/main.cpp index b2843c3..76c9c95 100644 --- a/main.cpp +++ b/main.cpp @@ -1,12 +1,9 @@ #include -//DESIGNATED WORDS - #define DEFAULT 0 #define PRIORITY 1 #define SUPER_PRIORITY 2 -//MEMORY SETTINGS (SIZE) #define BUFFER_MAX_SIZE 100 #define PRIORITY_BUFFER_MAX_SIZE 20 #define NETIN_BUFFER_MAX_SIZE 140 @@ -16,14 +13,8 @@ #define PROGRAM_MAX_NUMBER 10 #define UUID_MAX_NUMBER 20 -#define MAX_PACKETS_SEND 20 -//---------------------- - -//BEHAVIOR SETTINGS -bool deleteMessageIfMalformed = false; - +#define MAX_PACKETS_SEND 1 -//--------------------- using namespace std; struct sysdata{ @@ -35,11 +26,11 @@ struct sysdata{ short int writePos = BUFFER_MAX_SIZE-1; short int writePosPriority = PRIORITY_BUFFER_MAX_SIZE-1; short int writePosSuperPriority = PRIORITY_BUFFER_MAX_SIZE-1; - + //UUID short int lastUUID = 0; short int activeUUID = 0; - + }; struct packet{ @@ -66,35 +57,11 @@ struct packet ble_netout[PRIORITY_BUFFER_MAX_SIZE]; //super_priority struct sysdata data; struct packet tcp_netin[NETIN_BUFFER_MAX_SIZE]; -struct packet ble_netin[NETIN_BUFFER_MAX_SIZE]; - +int storeIndex=0; int TcpinStartMem[PROGRAM_MAX_NUMBER]; -int BleinStartMem[PROGRAM_MAX_NUMBER]; - int TcpinEndMem[PROGRAM_MAX_NUMBER]; -int BleinEndMem[PROGRAM_MAX_NUMBER]; - -short int recievingUUID[UUID_MAX_NUMBER]; - -//[x][y][0] means length of the array[x][y] - -int storeIndex=0; +int activeUUID[UUID_MAX_NUMBER]; -/// -void run_only_once(){ - for(short int i = 0; i < PROGRAM_MAX_NUMBER; i++){ - TcpinEndMem[i] = -1; //-1 means NOT DEFINED in READ buffers... - BleinEndMem[i] = -1; - TcpinStartMem[i] = -1; - BleinStartMem[i] = -1; - } - for(short int i = 0; i < UUID_MAX_NUMBER; i++){ - recievingUUID[i] = -1; - } -} -// - -/// void print_tcp_netin(){ cout << "[DEBUG] TCP_NETIN buffer state:\n --> UUID: "; for(int i = 0 ; i< NETIN_BUFFER_MAX_SIZE; i++){ @@ -113,7 +80,7 @@ void print_tcp_netin(){ void print_startmem(){ cout << "[DEBUG] STARTMEM buffer state:\n"; - for(int i = 0 ; i < PROGRAM_MAX_NUMBER; i++){ + for(int i = 0 ; i < NETIN_BUFFER_MAX_SIZE; i++){ cout << TcpinStartMem[i] << " "; } cout << "\n----------------------"; @@ -121,13 +88,11 @@ void print_startmem(){ void print_endmem(){ cout << "[DEBUG] ENDMEM buffer state:\n"; - for(int i = 0; i < PROGRAM_MAX_NUMBER; i++){ + for(int i = 0; i < NETIN_BUFFER_MAX_SIZE; i++){ cout << TcpinEndMem[i] << " "; } cout << "\n----------------------"; } -/// - int getNetinIndex(int index) { if (index<0) { @@ -144,157 +109,13 @@ void circularIndex(int d) { storeIndex=getNetinIndex(storeIndex+d); } -//For Reading from -string read(short int progID){ - - if(progID < 0 || progID > PROGRAM_MAX_NUMBER){ - cout << "" << endl; - return "[ER][READ](ERRCODE -1) Invalid PROGID passed! "; - } - - //BLE buffer READ - if(BleinStartMem[progID] != -1){ - cout << "[READ] Retrieving the most recent message in the BLE buffer..." <= NETIN_BUFFER_MAX_SIZE) { if (tcp_netin[storeIndex].STATE==-5) break; @@ -304,58 +125,77 @@ short int tcp_reciever(packet p) { return -1; //-1: Buffer Exceed } } - - //Reacts by State + + //React by State else if (p.STATE==-1) { printf("[TcpRecv] [!] Received ERROR Data (-1) (U: %d, P: %d)\n", tcp_netin[storeIndex].UUID, tcp_netin[storeIndex].programID); } - + else if (p.STATE==-2) { //System } - + else if (p.STATE==-3) { //Acknowledge } - /* - else if (p.STATE==-4) { - //End - } - */ - + /* + else if (p.STATE==-4) { + //End + } + */ + else if (p.STATE==-5) { //Empty circularIndex(-1); printf("[TcpRecv] [!] Received EMPTY Data (-5) (U: %d, P: %d)\n", tcp_netin[storeIndex].UUID, tcp_netin[storeIndex].programID); return 0; } - + tcp_netin[storeIndex]=p; printf("[TcpRecv] Received Data Successfully (U: %d, P: %d)\n", tcp_netin[storeIndex].UUID, tcp_netin[storeIndex].programID); - printf("> \"%s\"", tcp_netin[storeIndex].message.c_str()); + printf("> \"%s\"\n", tcp_netin[storeIndex].message.c_str()); for (int i=0; itcp_netin[storeIndex].UUID) + tcp_netin[TcpinEndMem[tcp_netin[storeIndex].UUID]].NEXTMEM=i; + } + /* + for (int i=storeIndex+1; itcp_netin[storeIndex].UUID) + } + */ //Should cope with this case (cause of cirular structure of buffer) + } + + //Replace End Memory of UUID TcpinEndMem[tcp_netin[storeIndex].programID]=storeIndex; - tcp_netin[getNetinIndex(storeIndex-1)].NEXTMEM=storeIndex; } - + //Makes Buffer Circular Structure storeIndex=getNetinIndex(storeIndex++); - - + + return 0; - + } //function to recieve functions void ble_reciever(packet p){ cout << "[BLE] Sending packet UUID: " << p.UUID << " ProgNo: " << p.programID << " STATE: " << p.STATE << "message: " << p.message << endl; @@ -370,15 +210,15 @@ short int getUUID(){ data.lastUUID = 0; else data.lastUUID++; - + data.activeUUID++; - + if(data.activeUUID > UUID_MAX_NUMBER){ data.activeUUID--; return -1; } - - + + return data.lastUUID; } @@ -387,10 +227,10 @@ short int getNextFreeBuffer(){ data.writePos = 0; else data.writePos++; - + if(tcp_netout[data.writePos].STATE != -5) return -1; //BUFFER FULL ERROR - + return data.writePos; } @@ -399,10 +239,10 @@ short int getNextFreeBufferPriority(){ data.writePosPriority = 0; else data.writePosPriority++; - + if(tcp_netout_priority[data.writePosPriority].STATE != -5) return -1; //BUFFER FULL ERROR - + return data.writePosPriority; } @@ -411,15 +251,15 @@ short int getNextFreeBufferSuperPriority(){ data.writePosSuperPriority = 0; else data.writePosSuperPriority++; - + if(ble_netout[data.writePosSuperPriority].STATE != -5) return -1; //BUFFER FULL ERROR - + return data.writePosSuperPriority; } void freeBuffer(short int pos){ - + short int lastpos; short int cnt=0; //unnecessary feature... if(pos == -1) @@ -438,7 +278,7 @@ void freeBuffer(short int pos){ } void freeBufferPriority(short int pos){ - + short int lastpos; short int cnt=0; //unnecessary feature... if(pos == -1) @@ -476,16 +316,16 @@ void freeBufferSuperPriority(short int pos){ short int send(string msg, short int progNo, short int priority){ struct packet temp; - + short int len = (short int)msg.length(); short int count = getPartCount(len); - + temp.UUID = getUUID(); //generates an unique UUID for the message; if(temp.UUID == -1){ cout << "[SEND ERROR] TOO MANY ACTIVE UUID..." << endl; return -2; //TO MANY UUID } - + temp.programID = progNo; if(temp.programID < 0 || temp.programID > PROGRAM_MAX_NUMBER){ cout << "[SEND ERROR] INVALID PROGRAM ID PASSED..." << endl; @@ -493,11 +333,11 @@ short int send(string msg, short int progNo, short int priority){ } short int lastMEMloc = -1; short int pos; - + for(short int i = 0; i < count; i++){ - - short int msgStartPos; - + + short int msgStartPos = 0; + if(i == count-1){ temp.STATE = -4; //END temp.message = msg.substr(PACKET_STR_SIZE*i, (len-1)%10+1); @@ -506,7 +346,7 @@ short int send(string msg, short int progNo, short int priority){ temp.STATE = i; temp.message = msg.substr(PACKET_STR_SIZE*i, PACKET_STR_SIZE); } - + switch(priority){ case 0: pos = getNextFreeBuffer(); @@ -554,8 +394,8 @@ short int send(string msg, short int progNo, short int priority){ cout << "[SEND ERROR] Invalid PROGNO Argument passed..." << endl; return -4; //INVALID ARGS } - } + return -20; } short int readBLEBuffer(){ @@ -592,7 +432,7 @@ short int readBuffer(){ data.readPos = 0; else data.readPos++; - + cout << "[READ TCP] Reading buffer " << data.readPos << endl; if((prev == data.writePos && tcp_netout[prev].STATE == -5) || tcp_netout[data.readPos].STATE == -5) { cout << "[READ TCP] No More to read in TCP buffer..." << endl; @@ -681,10 +521,10 @@ short int proc(){ tcp_netout[pos].UUID = 0; //debug tcp_netout[pos].STATE = -5; } + return -20; } int main(void){ - run_only_once(); bool terminate = false; while(!terminate){ cout << "Processing Packets..."<