Merge branch 'refactor-pubsub' into 'develop'

Refactor pubsub

See merge request wirelos/sprocket-lib!16
This commit is contained in:
Patrick Balsiger
2018-12-28 19:21:16 +00:00
5 changed files with 19 additions and 13 deletions

View File

@@ -1,6 +1,12 @@
# Sprocket-Lib
# Sprocket-Core
A lightweight Arduino framework for event driven programming.
## Concepts
... topic based event channel / pubsub-pattern
... plugin system
## Lifecycle
## Sprocket Lifecycle
TODO
# Useful commands

View File

@@ -1,5 +1,5 @@
#ifndef __MEDIATOR__
#define __MEDIATOR__
#ifndef __SPROCKET_EVENT_CHANNEL__
#define __SPROCKET_EVENT_CHANNEL__
#include <Arduino.h>
#include <ArduinoJson.h>
@@ -12,7 +12,7 @@ using namespace std;
typedef std::function<void(String msg)> subscriptionHandler_t;
class Mediator {
class EventChannel {
public:
std::map<std::string, vector<subscriptionHandler_t>> subscriptions;
void subscribe(String topic, subscriptionHandler_t handler) {

View File

@@ -8,24 +8,24 @@
#include <TaskSchedulerDeclarations.h>
#include <Network.h>
#include <SprocketMessage.h>
#include <Mediator.h>
#include <EventChannel.h>
class Plugin {
public:
Mediator* mediator;
EventChannel* eventChannel;
virtual void activate(Scheduler*);
virtual void enable(){}
virtual void disable(){}
virtual void onMessage(SprocketMessage msg){}
Plugin* mediate(Mediator* m) {
mediator = m;
Plugin* mount(EventChannel* ec) {
eventChannel = ec;
return this;
}
void subscribe(String topic, subscriptionHandler_t handler){
mediator->subscribe(topic, handler);
eventChannel->subscribe(topic, handler);
}
void publish(String topic, String str){
mediator->publish(topic, str);
eventChannel->publish(topic, str);
}
};

View File

@@ -40,7 +40,7 @@ void Sprocket::dispatch( uint32_t from, String &msg ) {
}
void Sprocket::addPlugin(Plugin* p){
p->mediate(this);
p->mount(this);
plugins.reserve(1);
plugins.push_back(p);
}

View File

@@ -19,7 +19,7 @@
using namespace std;
using namespace std::placeholders;
class Sprocket : public Mediator {
class Sprocket : public EventChannel {
protected:
// TODO move scheduler out of Sprocket
// => see difference between standalone and mesh sprochet usage of scheduler