wip
This commit is contained in:
1 parent
f25a834fbd
commit
062f9c8c69
6 files changed
+96
-31
No files matched your search
+31
-13
@@ -1,10 +1,9 @@
|
||||
#include "EventBus.hpp"
|
||||
#include <string>
|
||||
|
||||
namespace Plains {
|
||||
namespace Core {
|
||||
|
||||
// EventBus Class
|
||||
// ============================== EventBus Class ==============================
|
||||
|
||||
EventBus::EventBus(Logger& logger) : _logger(logger) {
|
||||
_logger.debug.ln ("Starting Event Bus...");
|
||||
@@ -17,29 +16,48 @@ EventBus::~EventBus() {
|
||||
_logger.debug.ln ("EventBus has been shut down.");
|
||||
}
|
||||
|
||||
Event* EventBus::registerEvent(const string eventID) {
|
||||
_logger.debug << "Registering new Event " << eventID << Logger::commit;
|
||||
Event& EventBus::registerEvent(const string& eventID) {
|
||||
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 (!eventFound) {
|
||||
_logger.debug << "Registering new Event '" << eventID << "'" << Logger::commit;
|
||||
Event* event = new Event(eventID);
|
||||
_events.push_back(event);
|
||||
return event;
|
||||
return *event;
|
||||
}
|
||||
|
||||
Event* EventBus::getEvent(const string& eventID) {
|
||||
_logger.debug.ln("Registering new Event...");
|
||||
//Temporarily return nothing:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
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) {}
|
||||
|
||||
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
@@ -72,20 +72,32 @@ void Logger::Sink::ln ( const char* msg ) {
|
||||
if(_lineIsPrefixed) *_parent._output << endl;
|
||||
prefixLine(true);
|
||||
*_parent._output << msg << endl;
|
||||
cout << msg << CC_RESET "\n";
|
||||
}
|
||||
}
|
||||
|
||||
void Logger::Sink::prefixLine(bool resetToFalse) {
|
||||
if (!_lineIsPrefixed) {
|
||||
const char* lvlString;
|
||||
const string timestamp = Logger::genTimeStamp();
|
||||
switch ( _level)
|
||||
{
|
||||
case DEBUG: lvlString = "DEBUG"; break;
|
||||
case INFO: lvlString = "INFO "; break;
|
||||
case WARN: lvlString = "WARN "; break;
|
||||
case FATAL: lvlString = "FATAL"; break;
|
||||
case DEBUG:
|
||||
*_parent._output << timestamp << " " << "DEBUG ";
|
||||
cout << timestamp << " " << CC_BLUE "DEBUG ";
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -106,7 +118,6 @@ Logger::Sink& Logger::Sink::operator<<(const char* msg) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
Logger::Sink& Logger::Sink::operator<<(const int msg) {
|
||||
if ( _level >= _parent._logLevel ) {
|
||||
prefixLine();
|
||||
@@ -137,6 +148,7 @@ Logger::Sink& Logger::Sink::operator<<(const char character) {
|
||||
// COMMIT - End message line
|
||||
case 0:
|
||||
*_parent._output << endl;
|
||||
cout << CC_RESET "\n";
|
||||
_lineIsPrefixed = false;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <thread>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
namespace Plains {
|
||||
namespace Core {
|
||||
@@ -11,6 +12,7 @@ namespace Core {
|
||||
using std::thread;
|
||||
using std::string;
|
||||
using std::vector;
|
||||
using std::unique_ptr;
|
||||
|
||||
class EventSubscriber {
|
||||
public:
|
||||
@@ -20,7 +22,8 @@ class EventSubscriber {
|
||||
class Event {
|
||||
public:
|
||||
Event(const string _id);
|
||||
string& getID() const;
|
||||
~Event();
|
||||
const string& getID() const;
|
||||
void fire();
|
||||
void subscribe(EventSubscriber* es);
|
||||
|
||||
@@ -33,8 +36,7 @@ class EventBus {
|
||||
public:
|
||||
EventBus(Logger& logger);
|
||||
~EventBus();
|
||||
Event* registerEvent(string eventID);
|
||||
Event* getEvent(const string& eventID);
|
||||
Event& registerEvent(const string& eventID);
|
||||
|
||||
private:
|
||||
Logger& _logger;
|
||||
|
||||
@@ -3,6 +3,18 @@
|
||||
#include <string>
|
||||
#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 Core {
|
||||
|
||||
@@ -26,6 +38,7 @@ class Logger {
|
||||
bool _lineIsPrefixed;
|
||||
Logger& _parent;
|
||||
const Logger::Level _level;
|
||||
const char* _colorCode;
|
||||
void prefixLine(bool resetToFalse = false);
|
||||
public:
|
||||
Sink(const Level level, Logger& parent);
|
||||
@@ -39,10 +52,7 @@ class Logger {
|
||||
Sink& operator<<(const unsigned long msg);
|
||||
};
|
||||
|
||||
Sink debug;
|
||||
Sink info;
|
||||
Sink warn;
|
||||
Sink fatal;
|
||||
Sink debug, info, warn, fatal;
|
||||
|
||||
private:
|
||||
enum Level _logLevel;
|
||||
|
||||
+24
-2
@@ -25,11 +25,33 @@ int main(int args, char **argv) {
|
||||
EventBus eventBus(logger);
|
||||
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();
|
||||
//logger.debug.ln ("Running DNS resolver...");
|
||||
|
||||
// End of Testcode
|
||||
|
||||
//resolver.run();
|
||||
logger.info.ln ("PlaiNS successfully started.");
|
||||
// Run something on main thread...
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>Plains Control Panel</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<style>
|
||||
#wrapper * {
|
||||
box-sizing: border-box;
|
||||
@@ -22,7 +23,7 @@
|
||||
margin: 0 auto 0 auto;
|
||||
vertical-align: top;
|
||||
width: 800px;
|
||||
min-height: 900px;
|
||||
min-height: 600px;
|
||||
}
|
||||
|
||||
#wrapper section {
|
||||
@@ -32,7 +33,7 @@
|
||||
#page {
|
||||
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;
|
||||
height: 800px;
|
||||
min-height: inherit;
|
||||
background-color: #FFFFFF;
|
||||
color: #000000;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user