This commit is contained in:
Sven Luethi committed 2021-03-26 22:35:27 +01:00
1 parent f25a834fbd
commit 062f9c8c69
6 files changed
+99 -34

No files matched your search

+34 -16
View File
@@ -1,10 +1,9 @@
#include "EventBus.hpp" #include "EventBus.hpp"
#include <string>
namespace Plains { namespace Plains {
namespace Core { namespace Core {
// EventBus Class // ============================== EventBus Class ==============================
EventBus::EventBus(Logger& logger) : _logger(logger) { EventBus::EventBus(Logger& logger) : _logger(logger) {
_logger.debug.ln ("Starting Event Bus..."); _logger.debug.ln ("Starting Event Bus...");
@@ -17,29 +16,48 @@ EventBus::~EventBus() {
_logger.debug.ln ("EventBus has been shut down."); _logger.debug.ln ("EventBus has been shut down.");
} }
Event* EventBus::registerEvent(const string eventID) { Event& EventBus::registerEvent(const string& eventID) {
_logger.debug << "Registering new Event " << eventID << Logger::commit; bool eventFound = false;
for ( const auto event : _events ) {
if (event->getID() == eventID) {
_logger.debug << "Event '" << eventID << "' found" << Logger::commit;
return *event;
}
}
//If event with that id does not exist, create it: //If event with that id does not exist, create it:
Event* event = new Event(eventID); if (!eventFound) {
_events.push_back(event); _logger.debug << "Registering new Event '" << eventID << "'" << Logger::commit;
return event; Event* event = new Event(eventID);
} _events.push_back(event);
return *event;
Event* EventBus::getEvent(const string& eventID) { }
_logger.debug.ln("Registering new Event...");
//Temporarily return nothing:
return NULL;
} }
void EventBus::run() { void EventBus::run() {
_logger.debug.ln("Hi from EventBus run method! From within Event Thread!"); //_logger.debug.ln("Hi from EventBus run method! From within Event Thread!");
} }
// Event Class // =============================== Event Class ================================
Event::Event(const string id) : _id(id) {} Event::Event(const string id) : _id(id) {}
void Event::subscribe(EventSubscriber* es) { _subscribers.push_back(es); } Event::~Event() {
}
const string& Event::getID() const {
return _id;
};
void Event::subscribe(EventSubscriber* es) {
_subscribers.push_back(es);
}
void Event::fire() {
for (auto subscriber : _subscribers) {
subscriber->callback();
}
}
+19 -7
View File
@@ -72,20 +72,32 @@ void Logger::Sink::ln ( const char* msg ) {
if(_lineIsPrefixed) *_parent._output << endl; if(_lineIsPrefixed) *_parent._output << endl;
prefixLine(true); prefixLine(true);
*_parent._output << msg << endl; *_parent._output << msg << endl;
cout << msg << CC_RESET "\n";
} }
} }
void Logger::Sink::prefixLine(bool resetToFalse) { void Logger::Sink::prefixLine(bool resetToFalse) {
if (!_lineIsPrefixed) { if (!_lineIsPrefixed) {
const char* lvlString; const string timestamp = Logger::genTimeStamp();
switch ( _level) switch ( _level)
{ {
case DEBUG: lvlString = "DEBUG"; break; case DEBUG:
case INFO: lvlString = "INFO "; break; *_parent._output << timestamp << " " << "DEBUG ";
case WARN: lvlString = "WARN "; break; cout << timestamp << " " << CC_BLUE "DEBUG ";
case FATAL: lvlString = "FATAL"; break; break;
case INFO:
*_parent._output << timestamp << " " << "INFO ";
cout << timestamp << " " << CC_GREEN "INFO ";
break;
case WARN:
*_parent._output << timestamp << " " << "WARN ";
cout << timestamp << " " << CC_YELLOW "WARN ";
break;
case FATAL:
*_parent._output << timestamp << " " << "FATAL ";
cout << timestamp << " " << CC_RED "FATAL ";
break;
} }
*_parent._output << Logger::genTimeStamp() << " " << lvlString << " ";
_lineIsPrefixed = resetToFalse ? false : true; _lineIsPrefixed = resetToFalse ? false : true;
} }
} }
@@ -106,7 +118,6 @@ Logger::Sink& Logger::Sink::operator<<(const char* msg) {
return *this; return *this;
} }
Logger::Sink& Logger::Sink::operator<<(const int msg) { Logger::Sink& Logger::Sink::operator<<(const int msg) {
if ( _level >= _parent._logLevel ) { if ( _level >= _parent._logLevel ) {
prefixLine(); prefixLine();
@@ -137,6 +148,7 @@ Logger::Sink& Logger::Sink::operator<<(const char character) {
// COMMIT - End message line // COMMIT - End message line
case 0: case 0:
*_parent._output << endl; *_parent._output << endl;
cout << CC_RESET "\n";
_lineIsPrefixed = false; _lineIsPrefixed = false;
break; break;
} }
+5 -3
View File
@@ -4,6 +4,7 @@
#include <thread> #include <thread>
#include <string> #include <string>
#include <vector> #include <vector>
#include <memory>
namespace Plains { namespace Plains {
namespace Core { namespace Core {
@@ -11,6 +12,7 @@ namespace Core {
using std::thread; using std::thread;
using std::string; using std::string;
using std::vector; using std::vector;
using std::unique_ptr;
class EventSubscriber { class EventSubscriber {
public: public:
@@ -20,7 +22,8 @@ class EventSubscriber {
class Event { class Event {
public: public:
Event(const string _id); Event(const string _id);
string& getID() const; ~Event();
const string& getID() const;
void fire(); void fire();
void subscribe(EventSubscriber* es); void subscribe(EventSubscriber* es);
@@ -33,8 +36,7 @@ class EventBus {
public: public:
EventBus(Logger& logger); EventBus(Logger& logger);
~EventBus(); ~EventBus();
Event* registerEvent(string eventID); Event& registerEvent(const string& eventID);
Event* getEvent(const string& eventID);
private: private:
Logger& _logger; Logger& _logger;
+14 -4
View File
@@ -3,6 +3,18 @@
#include <string> #include <string>
#include <fstream> #include <fstream>
#define CC(COLOR) CC_##COLOR
#define CC_RESET "\033[0m"
#define CC_BLACK "\e[0;30m"
#define CC_RED "\e[0;31m"
#define CC_GREEN "\e[0;32m"
#define CC_YELLOW "\e[0;33m"
#define CC_BLUE "\e[0;34m"
#define CC_PURPLE "\e[0;35m"
#define CC_CYAN "\e[0;36m"
#define CC_WHITE "\e[0;37m"
namespace Plains { namespace Plains {
namespace Core { namespace Core {
@@ -26,6 +38,7 @@ class Logger {
bool _lineIsPrefixed; bool _lineIsPrefixed;
Logger& _parent; Logger& _parent;
const Logger::Level _level; const Logger::Level _level;
const char* _colorCode;
void prefixLine(bool resetToFalse = false); void prefixLine(bool resetToFalse = false);
public: public:
Sink(const Level level, Logger& parent); Sink(const Level level, Logger& parent);
@@ -39,10 +52,7 @@ class Logger {
Sink& operator<<(const unsigned long msg); Sink& operator<<(const unsigned long msg);
}; };
Sink debug; Sink debug, info, warn, fatal;
Sink info;
Sink warn;
Sink fatal;
private: private:
enum Level _logLevel; enum Level _logLevel;
+24 -2
View File
@@ -25,11 +25,33 @@ int main(int args, char **argv) {
EventBus eventBus(logger); EventBus eventBus(logger);
Resolver::Server resolver(logger, eventBus); Resolver::Server resolver(logger, eventBus);
Event* e = eventBus.registerEvent("Test Event");
//e->subscribe()
// Some test code below:
class Testi : public EventSubscriber {
public:
Testi(Logger& logger, string message) : _logger(logger), _msg(message) {}
void callback() { _logger.debug.ln(_msg); }
private:
Logger& _logger;
string _msg;
};
Testi testi1(logger, "Testi1 Event Callback for event");
Event& e1 = eventBus.registerEvent("Test Event");
e1.subscribe(&testi1);
Testi testi2(logger, "Testi2 Event Callback for event");
Event& e2 = eventBus.registerEvent("Test Event");
e2.subscribe(&testi2);
e1.fire();
//Record testrec = ARecord(); //Record testrec = ARecord();
//logger.debug.ln ("Running DNS resolver..."); //logger.debug.ln ("Running DNS resolver...");
// End of Testcode
//resolver.run(); //resolver.run();
logger.info.ln ("PlaiNS successfully started."); logger.info.ln ("PlaiNS successfully started.");
// Run something on main thread... // Run something on main thread...
+3 -2
View File
@@ -2,6 +2,7 @@
<html> <html>
<head> <head>
<title>Plains Control Panel</title> <title>Plains Control Panel</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style> <style>
#wrapper * { #wrapper * {
box-sizing: border-box; box-sizing: border-box;
@@ -22,7 +23,7 @@
margin: 0 auto 0 auto; margin: 0 auto 0 auto;
vertical-align: top; vertical-align: top;
width: 800px; width: 800px;
min-height: 900px; min-height: 600px;
} }
#wrapper section { #wrapper section {
@@ -32,7 +33,7 @@
#page { #page {
margin-top: 40px; /*This has to match hight of #nav_top so nav-top dosesnt overlap as its not in the same flow*/ margin-top: 40px; /*This has to match hight of #nav_top so nav-top dosesnt overlap as its not in the same flow*/
width: inherit; width: inherit;
height: 800px; min-height: inherit;
background-color: #FFFFFF; background-color: #FFFFFF;
color: #000000; color: #000000;
} }