Skip to content

Commit

Permalink
Adding an optional loop function to the server call
Browse files Browse the repository at this point in the history
  • Loading branch information
madhephaestus committed Jul 27, 2019
1 parent b5c552e commit 3f2deb8
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
7 changes: 5 additions & 2 deletions src/PacketEvent.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#include "PacketEvent.h"

PacketEventAbstract::PacketEventAbstract(uint32_t id) {
PacketEvent::PacketEvent(uint32_t id) {
myId = id;
}
uint32_t PacketEventAbstract::getId() {
PacketEventAbstract::PacketEventAbstract(uint32_t id) :
PacketEvent(id) {
}
uint32_t PacketEvent::getId() {
return myId;
}
26 changes: 20 additions & 6 deletions src/PacketEvent.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#ifndef PacketEvent
#define PacketEvent
#ifndef PacketEvent_HEADER
#define PacketEvent_HEADER
#include <stdint.h>
class PacketEventAbstract {

class PacketEvent {
protected:
// Unique id of the function being called
uint32_t myId;
public:
bool noResponse = false;
// Packet ID needs to be set
PacketEventAbstract(uint32_t id);
virtual ~PacketEventAbstract() {}
PacketEvent(uint32_t id);
virtual ~PacketEvent(){}
//User function to be called when a packet comes in
// Buffer contains data from the packet coming in at the start of the function
// User data is written into the buffer to send it back
Expand All @@ -18,9 +19,22 @@ class PacketEventAbstract {
* An optional loop function
* If your server needs a pulse to keep it updated, implement this function
*/
virtual bool loop(){return false;}
virtual bool loop()=0;

uint32_t getId();
};

class PacketEventAbstract : public PacketEvent{
public:
// Packet ID needs to be set
PacketEventAbstract(uint32_t id);
virtual ~PacketEventAbstract() {}
/**
* An optional loop function
* If your server needs a pulse to keep it updated, implement this function
*/
virtual bool loop(){return false;};
};


#endif /* end of include guard: PacketEvent */
13 changes: 8 additions & 5 deletions src/SimplePacketComs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ uint32_t SimplePacketComsAbstract::getNumberOfFloatsInPacket() {
}

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

PacketEventAbstract * SimplePacketComsAbstract::detach(uint32_t id) {
PacketEventAbstract *event = fmap[id];
PacketEvent * SimplePacketComsAbstract::detach(uint32_t id) {
PacketEvent *event = fmap[id];
fmap.erase(id);
return event;
}
Expand All @@ -31,14 +31,17 @@ PacketEventAbstract * SimplePacketComsAbstract::detach(uint32_t id) {
* This runs the packet server and calls all events if a backet comes in
*/
void SimplePacketComsAbstract::server() {
for (std::map<uint32_t, PacketEvent*>::iterator it = fmap.begin();
it != fmap.end(); ++it) {
it->second->loop();
}
if (isPacketAvailible()) {
getPacket(buffer, numberOfBytes);
uint32_t currentId = getIdPointer()[0];
for (std::map<uint32_t, PacketEventAbstract*>::iterator it =
for (std::map<uint32_t, PacketEvent*>::iterator it =
fmap.begin(); it != fmap.end(); ++it) {
if (it->second->getId() == currentId) {
it->second->noResponse = false; // reset the response flag
it->second->loop();
it->second->event(getDataPointer());

if (it->second->noResponse == false) {
Expand Down
8 changes: 4 additions & 4 deletions src/SimplePacketComs.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class SimplePacketComsAbstract {

uint32_t numberOfBytes;
uint8_t * buffer;
std::map<uint32_t, PacketEventAbstract*> fmap;
std::map<uint32_t, PacketEvent*> fmap;
public:
SimplePacketComsAbstract();
/**
Expand Down Expand Up @@ -49,14 +49,14 @@ class SimplePacketComsAbstract {
/**
* Attach a function to a packet event
*/
void attach(PacketEventAbstract * eventImplementation);
void attach(PacketEvent * eventImplementation);
/**
* Detach a function from a packet event.
*
* @param id The packet id.
* @return The function that was attached.
*/
PacketEventAbstract * detach(uint32_t id);
PacketEvent * detach(uint32_t id);
/**
* This runs the packet server and calls all events if a backet comes in
*/
Expand All @@ -72,7 +72,7 @@ class SimplePacketComsAbstract {
float * getDataPointer() {
return (float *) (buffer + 4);
}
std::map<uint32_t, PacketEventAbstract*> * getfMap() {
std::map<uint32_t, PacketEvent*> * getfMap() {
return &fmap;
}

Expand Down

0 comments on commit 3f2deb8

Please sign in to comment.