Skip to content

Commit

Permalink
Merge pull request #3 from Octogonapus/feature/RGB/#2_add_detach
Browse files Browse the repository at this point in the history
Add detach method
  • Loading branch information
madhephaestus authored May 2, 2019
2 parents a9fc2a5 + 16b935b commit 3f97233
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode
26 changes: 18 additions & 8 deletions src/SimplePacketComs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ uint32_t SimplePacketComsAbstract::getNumberOfFloatsInPacket(){
}

void SimplePacketComsAbstract::attach(PacketEventAbstract * eventImplementation){
fmap.push_back(eventImplementation);
fmap[eventImplementation->getId()] = eventImplementation;
}

PacketEventAbstract * SimplePacketComsAbstract::detach(uint32_t id) {
PacketEventAbstract *event = fmap[id];
fmap.erase(id);
return event;
}

/**
Expand All @@ -27,13 +33,17 @@ void SimplePacketComsAbstract::server(){
if(isPacketAvailible()){
getPacket(buffer, numberOfBytes);
uint32_t currentId = getIdPointer()[0];
for (std::vector<PacketEventAbstract* >::iterator it = fmap.begin() ; it != fmap.end(); ++it){
if((*it)->getId() == currentId){
(*it)-> noResponse=false;// reset the response flag
(*it)->event(getDataPointer());
if((*it)->noResponse==false)// responde unless the no response flag is set
sendPacket(buffer, numberOfBytes);
return;// packet is responded to, fast return
for (std::map<uint32_t, PacketEventAbstract*>::iterator it = fmap.begin() ; it != fmap.end(); ++it){
if (it->second->getId() == currentId) {
it->second->noResponse = false; // reset the response flag
it->second->event(getDataPointer());

if (it->second->noResponse == false) {
// respond unless the no response flag is set
sendPacket(buffer, numberOfBytes);
}

return; // packet is responded to, fast return
}
}

Expand Down
13 changes: 10 additions & 3 deletions src/SimplePacketComs.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define SimplePacketComs
#include <stdint.h>
#include <stdio.h>
#include <vector>
#include <map>
#include <iostream>
#include "PacketEvent.h"
#include "client/AbstractSimpleComsDevice.h"
Expand All @@ -15,7 +15,7 @@ class SimplePacketComsAbstract {
private:
uint32_t numberOfBytes;
uint8_t * buffer;
std::vector<PacketEventAbstract*> fmap;
std::map<uint32_t, PacketEventAbstract*> fmap;
public:
SimplePacketComsAbstract();
/**
Expand Down Expand Up @@ -49,6 +49,13 @@ class SimplePacketComsAbstract {
* Attach a function to a packet event
*/
void attach(PacketEventAbstract * eventImplementation);
/**
* Detach a function from a packet event.
*
* @param id The packet id.
* @return The function that was attached.
*/
PacketEventAbstract * detach(uint32_t id);
/**
* This runs the packet server and calls all events if a backet comes in
*/
Expand All @@ -64,7 +71,7 @@ class SimplePacketComsAbstract {
float * getDataPointer(){
return (float *)(buffer+4);
}
std::vector<PacketEventAbstract*> * getfMap(){ return &fmap;}
std::map<uint32_t, PacketEventAbstract*> * getfMap(){ return &fmap;}

};

Expand Down

0 comments on commit 3f97233

Please sign in to comment.