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 TODO
# Useful commands # Useful commands

View File

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

View File

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

View File

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