diff --git a/.gitignore b/.gitignore index 8bd242e..49dca78 100644 --- a/.gitignore +++ b/.gitignore @@ -3,8 +3,12 @@ Release *.o *~ *.mk +.history libServerCore.a docs/latex/ docs/html */*.ipch */mmap_address.bin +.history/* +html +latex \ No newline at end of file diff --git a/.history/SubscriptionHandler_20221013185305.cpp b/.history/SubscriptionHandler_20221013185305.cpp new file mode 100644 index 0000000..ce40556 --- /dev/null +++ b/.history/SubscriptionHandler_20221013185305.cpp @@ -0,0 +1,68 @@ +#include "Subscription.h" +#include "TCPSession.h" +#include "Log.h" +#include + +namespace core +{ + + Subscription::Subscription(std::string id, std::string mode) + : id(id), mode(mode), owner(NULL) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode) + : id(id), mode(mode), owner(&session) {} + + Subscription::~Subscription() + { + std::stringstream out; + out << "cancel:" << id << std::endl; + for (auto subscriber : subscribers) + { + subscriber->write(out.str()); + } + } + + int Subscription::subscribe(TCPSession &session) + { + onSubscribe(session); + subscribers.push_back(&session); + return 1; + } + + int Subscription::unsubscribe(TCPSession &session) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + { + if (*subscriber == &session) + { + subscribers.erase(subscriber++); + return 1; + } + } + return 0; + } + + int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) + { + out << "event:" << request[1] << ":" << request[2] << std::endl; + return 1; + } + + int Subscription::event(std::stringstream &out) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + (*subscriber)->write(out.str()); + return 1; + } + + bool Subscription::ifSubscriber(TCPSession &session) + { + return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end()); + } + + int Subscription::onSubscribe(TCPSession &session) + { + return 0; + } + +} diff --git a/.history/SubscriptionHandler_20221013191001.h b/.history/SubscriptionHandler_20221013191001.h new file mode 100644 index 0000000..43d7bd0 --- /dev/null +++ b/.history/SubscriptionHandler_20221013191001.h @@ -0,0 +1,24 @@ +#ifndef __SubscriptionHandler_h__ +#define __SubscriptionHandler_h__ + +#include "ZString.h" +#include +#include + +namespace core +{ + + class SubscriptionHandler + { + + public: + SubscriptionHandler(); + + virtual int process(coreutils::ZString &request, std::stringstream &out, TCPSession &session); + + virtual int onSubscribe(TCPSession &session); + + }; +} + +#endif diff --git a/.history/SubscriptionHandler_20221013204338.h b/.history/SubscriptionHandler_20221013204338.h new file mode 100644 index 0000000..6fc8994 --- /dev/null +++ b/.history/SubscriptionHandler_20221013204338.h @@ -0,0 +1,23 @@ +#ifndef __SubscriptionHandler_h__ +#define __SubscriptionHandler_h__ + +#include "ZString.h" +#include +#include + +namespace core +{ + + class SubscriptionHandler + { + + public: + SubscriptionHandler(); + + virtual int process(coreutils::ZString &request, std::stringstream &out, TCPSession &session); + + virtual int onSubscribe(TCPSession &session); + }; +} + +#endif diff --git a/.history/SubscriptionHandler_20221013204409.cpp b/.history/SubscriptionHandler_20221013204409.cpp new file mode 100644 index 0000000..23c2e94 --- /dev/null +++ b/.history/SubscriptionHandler_20221013204409.cpp @@ -0,0 +1,68 @@ +#include "SubscriptionHandler.h" +#include "Log.h" +#include "TCPSession.h" +#include + +namespace core +{ + + Subscription::Subscription(std::string id, std::string mode) + : id(id), mode(mode), owner(NULL) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode) + : id(id), mode(mode), owner(&session) {} + + Subscription::~Subscription() + { + std::stringstream out; + out << "cancel:" << id << std::endl; + for (auto subscriber : subscribers) + { + subscriber->write(out.str()); + } + } + + int Subscription::subscribe(TCPSession &session) + { + onSubscribe(session); + subscribers.push_back(&session); + return 1; + } + + int Subscription::unsubscribe(TCPSession &session) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + { + if (*subscriber == &session) + { + subscribers.erase(subscriber++); + return 1; + } + } + return 0; + } + + int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) + { + out << "event:" << request[1] << ":" << request[2] << std::endl; + return 1; + } + + int Subscription::event(std::stringstream &out) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + (*subscriber)->write(out.str()); + return 1; + } + + bool Subscription::ifSubscriber(TCPSession &session) + { + return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end()); + } + + int Subscription::onSubscribe(TCPSession &session) + { + return 0; + } + +} diff --git a/.history/SubscriptionHandler_20221013204504.h b/.history/SubscriptionHandler_20221013204504.h new file mode 100644 index 0000000..6fc8994 --- /dev/null +++ b/.history/SubscriptionHandler_20221013204504.h @@ -0,0 +1,23 @@ +#ifndef __SubscriptionHandler_h__ +#define __SubscriptionHandler_h__ + +#include "ZString.h" +#include +#include + +namespace core +{ + + class SubscriptionHandler + { + + public: + SubscriptionHandler(); + + virtual int process(coreutils::ZString &request, std::stringstream &out, TCPSession &session); + + virtual int onSubscribe(TCPSession &session); + }; +} + +#endif diff --git a/.history/SubscriptionHandler_20221013204612.cpp b/.history/SubscriptionHandler_20221013204612.cpp new file mode 100644 index 0000000..45cb3fb --- /dev/null +++ b/.history/SubscriptionHandler_20221013204612.cpp @@ -0,0 +1,68 @@ +#include "Log.h" +#include "Subscription.h" +#include "TCPSession.h" +#include + +namespace core +{ + + Subscription::Subscription(std::string id, std::string mode) + : id(id), mode(mode), owner(NULL) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode) + : id(id), mode(mode), owner(&session) {} + + Subscription::~Subscription() + { + std::stringstream out; + out << "cancel:" << id << std::endl; + for (auto subscriber : subscribers) + { + subscriber->write(out.str()); + } + } + + int Subscription::subscribe(TCPSession &session) + { + onSubscribe(session); + subscribers.push_back(&session); + return 1; + } + + int Subscription::unsubscribe(TCPSession &session) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + { + if (*subscriber == &session) + { + subscribers.erase(subscriber++); + return 1; + } + } + return 0; + } + + int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) + { + out << "event:" << request[1] << ":" << request[2] << std::endl; + return 1; + } + + int Subscription::event(std::stringstream &out) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + (*subscriber)->write(out.str()); + return 1; + } + + bool Subscription::ifSubscriber(TCPSession &session) + { + return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end()); + } + + int Subscription::onSubscribe(TCPSession &session) + { + return 0; + } + +} diff --git a/.history/SubscriptionHandler_20221014152050.h b/.history/SubscriptionHandler_20221014152050.h new file mode 100644 index 0000000..a625a99 --- /dev/null +++ b/.history/SubscriptionHandler_20221014152050.h @@ -0,0 +1,22 @@ +#ifndef __SubscriptionHandler_h__ +#define __SubscriptionHandler_h__ + +#include "TCPSession.h" +#include "ZString.h" +#include +#include +#include + +namespace core +{ + + class SubscriptionHandler + { + + public: + virtual int process(coreutils::ZString &request, std::stringstream &out, TCPSession &session); + virtual int onSubscribe(TCPSession &session){}; + }; +} + +#endif diff --git a/.history/SubscriptionHandler_20221014152131.h b/.history/SubscriptionHandler_20221014152131.h new file mode 100644 index 0000000..fef3b55 --- /dev/null +++ b/.history/SubscriptionHandler_20221014152131.h @@ -0,0 +1,22 @@ +#ifndef __SubscriptionHandler_h__ +#define __SubscriptionHandler_h__ + +#include "TCPSession.h" +#include "ZString.h" +#include +#include +#include + +namespace core +{ + + class SubscriptionHandler + { + + public: + virtual int process(coreutils::ZString &request, std::stringstream &out, TCPSession &session); + virtual int onSubscribe(TCPSession &session) {} + }; +} + +#endif diff --git a/.history/SubscriptionHandler_20221014152241.h b/.history/SubscriptionHandler_20221014152241.h new file mode 100644 index 0000000..1c8484b --- /dev/null +++ b/.history/SubscriptionHandler_20221014152241.h @@ -0,0 +1,22 @@ +#ifndef __SubscriptionHandler_h__ +#define __SubscriptionHandler_h__ + +#include "TCPSession.h" +#include "ZString.h" +#include +#include +#include + +namespace core +{ + + class SubscriptionHandler : public Base + { + + public: + virtual int process(coreutils::ZString &request, std::stringstream &out, TCPSession &session); + virtual int onSubscribe(TCPSession &session) {} + }; +} + +#endif diff --git a/.history/SubscriptionHandler_20221014152252.h b/.history/SubscriptionHandler_20221014152252.h new file mode 100644 index 0000000..fef3b55 --- /dev/null +++ b/.history/SubscriptionHandler_20221014152252.h @@ -0,0 +1,22 @@ +#ifndef __SubscriptionHandler_h__ +#define __SubscriptionHandler_h__ + +#include "TCPSession.h" +#include "ZString.h" +#include +#include +#include + +namespace core +{ + + class SubscriptionHandler + { + + public: + virtual int process(coreutils::ZString &request, std::stringstream &out, TCPSession &session); + virtual int onSubscribe(TCPSession &session) {} + }; +} + +#endif diff --git a/.history/SubscriptionHandler_20221014152513.h b/.history/SubscriptionHandler_20221014152513.h new file mode 100644 index 0000000..7ad43f5 --- /dev/null +++ b/.history/SubscriptionHandler_20221014152513.h @@ -0,0 +1,22 @@ +#ifndef __SubscriptionHandler_h__ +#define __SubscriptionHandler_h__ + +#include "TCPSession.h" +#include "ZString.h" +#include +#include +#include + +namespace core +{ + + class SubscriptionHandler + { + + public: + virtual int process(coreutils::ZString &request, std::stringstream &out, TCPSession &session); + virtual int onSubscribe(TCPSession &session) { return 1; } + }; +} + +#endif diff --git a/.history/SubscriptionHandler_20221014153238.h b/.history/SubscriptionHandler_20221014153238.h new file mode 100644 index 0000000..71929a1 --- /dev/null +++ b/.history/SubscriptionHandler_20221014153238.h @@ -0,0 +1,23 @@ +#ifndef __SubscriptionHandler_h__ +#define __SubscriptionHandler_h__ + +#include "ZString.h" +#include +#include +#include + +namespace core +{ + + class TCPSession; + + class SubscriptionHandler + { + + public: + virtual int process(coreutils::ZString &request, std::stringstream &out, TCPSession &session); + virtual int onSubscribe(TCPSession &session); + }; +} + +#endif diff --git a/.history/SubscriptionManager_20221013225816.h b/.history/SubscriptionManager_20221013225816.h new file mode 100644 index 0000000..d748e1c --- /dev/null +++ b/.history/SubscriptionManager_20221013225816.h @@ -0,0 +1,33 @@ +#ifndef __SubscriptionManager_h__ +#define __SubscriptionManager_h__ + +#include "TCPSession.h" +#include "Subscription.h" +#include "Command.h" +#include "ZString.h" +#include +#include + +namespace core { + + class SubscriptionManager : public Command { + + public: + SubscriptionManager(); + + int add(Subscription &subscription); + int addHandler(std::string name, SubscriptionHandler &handler) + + int removeSessionSubscriptions(TCPSession &session); + + int processCommand(coreutils::ZString &request, TCPSession &session) override; + + private: + std::map subscriptions; + std::map handlers; + std::mutex lock; + + }; +} + +#endif diff --git a/.history/SubscriptionManager_20221013231834.h b/.history/SubscriptionManager_20221013231834.h new file mode 100644 index 0000000..6113b52 --- /dev/null +++ b/.history/SubscriptionManager_20221013231834.h @@ -0,0 +1,34 @@ +#ifndef __SubscriptionManager_h__ +#define __SubscriptionManager_h__ + +#include "Command.h" +#include "Subscription.h" +#include "TCPSession.h" +#include "ZString.h" +#include +#include + +namespace core +{ + + class SubscriptionManager : public Command + { + + public: + SubscriptionManager(); + + int add(Subscription &subscription); + int addHandler(std::string name, SubscriptionHandler &handler); + + int removeSessionSubscriptions(TCPSession &session); + + int processCommand(coreutils::ZString &request, TCPSession &session) override; + + private: + std::map subscriptions; + std::map handlers; + std::mutex lock; + }; +} + +#endif diff --git a/.history/SubscriptionManager_20221013232252.h b/.history/SubscriptionManager_20221013232252.h new file mode 100644 index 0000000..69e27ad --- /dev/null +++ b/.history/SubscriptionManager_20221013232252.h @@ -0,0 +1,34 @@ +#ifndef __SubscriptionManager_h__ +#define __SubscriptionManager_h__ + +#include "Command.h" +#include "Subscription.h" +#include "TCPSession.h" +#include "ZString.h" +#include +#include + +namespace core +{ + + class SubscriptionManager : public Command + { + + public: + SubscriptionManager(); + + int add(Subscription &subscription); + int addHandler(std::string name, SubscriptionHandler &handler); + + int removeSessionSubscriptions(TCPSession &session); + + int processCommand(coreutils::ZString &request, TCPSession &session) override; + + private: + std::map subscriptions; + std::map handlers; + std::mutex lock; + }; +} + +#endif diff --git a/.history/SubscriptionManager_20221013232501.h b/.history/SubscriptionManager_20221013232501.h new file mode 100644 index 0000000..33bfae4 --- /dev/null +++ b/.history/SubscriptionManager_20221013232501.h @@ -0,0 +1,34 @@ +#ifndef __SubscriptionManager_h__ +#define __SubscriptionManager_h__ + +#include "Command.h" +#include "Subscription.h" +#include "TCPSession.h" +#include "ZString.h" +#include +#include + +namespace core +{ + + class SubscriptionManager : public Command + { + + public: + SubscriptionManager(); + + int add(Subscription &subscription, SubscriptionHandler &handlers); + int addHandler(std::string name, SubscriptionHandler &handler); + + int removeSessionSubscriptions(TCPSession &session); + + int processCommand(coreutils::ZString &request, TCPSession &session) override; + + private: + std::map subscriptions; + std::map handlers; + std::mutex lock; + }; +} + +#endif diff --git a/.history/SubscriptionManager_20221013232513.h b/.history/SubscriptionManager_20221013232513.h new file mode 100644 index 0000000..22b4d1d --- /dev/null +++ b/.history/SubscriptionManager_20221013232513.h @@ -0,0 +1,34 @@ +#ifndef __SubscriptionManager_h__ +#define __SubscriptionManager_h__ + +#include "Command.h" +#include "Subscription.h" +#include "TCPSession.h" +#include "ZString.h" +#include +#include + +namespace core +{ + + class SubscriptionManager : public Command + { + + public: + SubscriptionManager(); + + int add(Subscription &subscription, SubscriptionHandler *handlers); + int addHandler(std::string name, SubscriptionHandler &handler); + + int removeSessionSubscriptions(TCPSession &session); + + int processCommand(coreutils::ZString &request, TCPSession &session) override; + + private: + std::map subscriptions; + std::map handlers; + std::mutex lock; + }; +} + +#endif diff --git a/.history/SubscriptionManager_20221013232519.h b/.history/SubscriptionManager_20221013232519.h new file mode 100644 index 0000000..4e31671 --- /dev/null +++ b/.history/SubscriptionManager_20221013232519.h @@ -0,0 +1,34 @@ +#ifndef __SubscriptionManager_h__ +#define __SubscriptionManager_h__ + +#include "Command.h" +#include "Subscription.h" +#include "TCPSession.h" +#include "ZString.h" +#include +#include + +namespace core +{ + + class SubscriptionManager : public Command + { + + public: + SubscriptionManager(); + + int add(Subscription &subscription, SubscriptionHandler *handler); + int addHandler(std::string name, SubscriptionHandler &handler); + + int removeSessionSubscriptions(TCPSession &session); + + int processCommand(coreutils::ZString &request, TCPSession &session) override; + + private: + std::map subscriptions; + std::map handlers; + std::mutex lock; + }; +} + +#endif diff --git a/.history/SubscriptionManager_20221015140418.h b/.history/SubscriptionManager_20221015140418.h new file mode 100644 index 0000000..e370a76 --- /dev/null +++ b/.history/SubscriptionManager_20221015140418.h @@ -0,0 +1,37 @@ +#ifndef __SubscriptionManager_h__ +#define __SubscriptionManager_h__ + +#include "Command.h" +#include "Subscription.h" +#include "SubscriptionHandler.h" +#include "TCPSession.h" +#include "ZString.h" +#include +#include + +namespace core +{ + + class SubscriptionManager : public Command + { + + public: + SubscriptionManager(); + + int add(Subscription &subscription); + int add(Subscription &subscription, std::string handler); + int addHandler(std::string name, SubscriptionHandler &handler); + + int removeSessionSubscriptions(TCPSession &session); + + int processCommand(coreutils::ZString &request, TCPSession &session) override; + + private: + std::map subscriptions; + std::map handlers; + Subscription *newSubscription; + std::mutex lock; + }; +} + +#endif diff --git a/.history/SubscriptionManager_20221015201436.h b/.history/SubscriptionManager_20221015201436.h new file mode 100644 index 0000000..7e928e4 --- /dev/null +++ b/.history/SubscriptionManager_20221015201436.h @@ -0,0 +1,38 @@ +#ifndef __SubscriptionManager_h__ +#define __SubscriptionManager_h__ + +#include "Command.h" +#include "Subscription.h" +#include "SubscriptionHandler.h" +#include "TCPSession.h" +#include "ZString.h" +#include +#include + +namespace core +{ + + class SubscriptionManager : public Command + { + + public: + SubscriptionManager(); + + int add(Subscription &subscription); + int add(Subscription &subscription, std::string handler); + int addHandler(std::string name, SubscriptionHandler &handler); + + int removeSessionSubscriptions(TCPSession &session); + + int processCommand(coreutils::ZString &request, TCPSession &session) override; + + private: + Subscription subscription; + std::map subscriptions; + std::map handlers; + Subscription *newSubscription; + std::mutex lock; + }; +} + +#endif diff --git a/.history/SubscriptionManager_20221015201440.h b/.history/SubscriptionManager_20221015201440.h new file mode 100644 index 0000000..566cac8 --- /dev/null +++ b/.history/SubscriptionManager_20221015201440.h @@ -0,0 +1,38 @@ +#ifndef __SubscriptionManager_h__ +#define __SubscriptionManager_h__ + +#include "Command.h" +#include "Subscription.h" +#include "SubscriptionHandler.h" +#include "TCPSession.h" +#include "ZString.h" +#include +#include + +namespace core +{ + + class SubscriptionManager : public Command + { + + public: + SubscriptionManager(); + + int add(Subscription &subscription); + int add(Subscription &subscription, std::string handler); + int addHandler(std::string name, SubscriptionHandler &handler); + + int removeSessionSubscriptions(TCPSession &session); + + int processCommand(coreutils::ZString &request, TCPSession &session) override; + + private: + Subscription *subscription; + std::map subscriptions; + std::map handlers; + Subscription *newSubscription; + std::mutex lock; + }; +} + +#endif diff --git a/.history/SubscriptionManager_20221016180248.h b/.history/SubscriptionManager_20221016180248.h new file mode 100644 index 0000000..d7613fd --- /dev/null +++ b/.history/SubscriptionManager_20221016180248.h @@ -0,0 +1,38 @@ +#ifndef __SubscriptionManager_h__ +#define __SubscriptionManager_h__ + +#include "Command.h" +#include "Subscription.h" +#include "SubscriptionHandler.h" +#include "TCPSession.h" +#include "ZString.h" +#include +#include + +namespace core +{ + + class SubscriptionManager : public Command + { + + public: + SubscriptionManager(); + + int add(Subscription &subscription); + int add(Subscription &subscription, std::string handler); + int addHandler(std::string name, SubscriptionHandler *handler); + + int removeSessionSubscriptions(TCPSession &session); + + int processCommand(coreutils::ZString &request, TCPSession &session) override; + + private: + Subscription *subscription; + std::map subscriptions; + std::map handlers; + Subscription *newSubscription; + std::mutex lock; + }; +} + +#endif diff --git a/.history/Subscription_20221013190812.h b/.history/Subscription_20221013190812.h new file mode 100644 index 0000000..e995db7 --- /dev/null +++ b/.history/Subscription_20221013190812.h @@ -0,0 +1,46 @@ +#ifndef __Subscription_h__ +#define __Subscription_h__ + +#include "ZString.h" +#include +#include + +namespace core +{ + + class TCPSession; + + class Subscription + { + + public: + Subscription(std::string id, std::string mode = "*AUTHOR"); + Subscription(std::string id, TCPSession &session, std::string mode = "*AUTHOR"); + virtual ~Subscription(); + + int subscribe(TCPSession &session); + int unsubscribe(TCPSession &session); + + virtual int process(coreutils::ZString &request, std::stringstream &out, TCPSession &session); + + virtual int onSubscribe(TCPSession &session); + + int event(std::stringstream &out); + + bool ifSubscriber(TCPSession &session); + + void setHandler(SubscriptionHandler *handler); + + // int processCommand(coreutils::ZString &request, TCPSession &session) override; + + std::string id; + std::string mode; + TCPSession *owner; + + SubscriptionHandler *handler; + + std::vector subscribers; + }; +} + +#endif diff --git a/.history/Subscription_20221013190943.cpp b/.history/Subscription_20221013190943.cpp new file mode 100644 index 0000000..6fa4cd0 --- /dev/null +++ b/.history/Subscription_20221013190943.cpp @@ -0,0 +1,75 @@ +#include "Subscription.h" +#include "TCPSession.h" +#include "Log.h" +#include + +namespace core +{ + + Subscription::Subscription(std::string id, std::string mode) + : id(id), mode(mode), owner(NULL) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode) + : id(id), mode(mode), owner(&session) {} + + Subscription::~Subscription() + { + std::stringstream out; + out << "cancel:" << id << std::endl; + for (auto subscriber : subscribers) + { + subscriber->write(out.str()); + } + } + + int Subscription::subscribe(TCPSession &session) + { + onSubscribe(session); + subscribers.push_back(&session); + return 1; + } + + int Subscription::unsubscribe(TCPSession &session) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + { + if (*subscriber == &session) + { + subscribers.erase(subscriber++); + return 1; + } + } + return 0; + } + + int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) + { + if(handler) + handler->process(request, out, session); + else + out << "event:" << request[1] << ":" << request[2] << std::endl; + return 1; + } + + int Subscription::event(std::stringstream &out) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + (*subscriber)->write(out.str()); + return 1; + } + + bool Subscription::ifSubscriber(TCPSession &session) + { + return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end()); + } + + int Subscription::onSubscribe(TCPSession &session) + { + return 0; + } + + void setHandler(SubscriptionHandler *handler) { + this->handler = handler; + } + +} diff --git a/.history/Subscription_20221013204658.h b/.history/Subscription_20221013204658.h new file mode 100644 index 0000000..e2c4d84 --- /dev/null +++ b/.history/Subscription_20221013204658.h @@ -0,0 +1,47 @@ +#ifndef __Subscription_h__ +#define __Subscription_h__ + +#include "SubscriptionHandler.h" +#include "ZString.h" +#include +#include + +namespace core +{ + + class TCPSession; + + class Subscription + { + + public: + Subscription(std::string id, std::string mode = "*AUTHOR"); + Subscription(std::string id, TCPSession &session, std::string mode = "*AUTHOR"); + virtual ~Subscription(); + + int subscribe(TCPSession &session); + int unsubscribe(TCPSession &session); + + virtual int process(coreutils::ZString &request, std::stringstream &out, TCPSession &session); + + virtual int onSubscribe(TCPSession &session); + + int event(std::stringstream &out); + + bool ifSubscriber(TCPSession &session); + + void setHandler(SubscriptionHandler *handler); + + // int processCommand(coreutils::ZString &request, TCPSession &session) override; + + std::string id; + std::string mode; + TCPSession *owner; + + SubscriptionHandler *handler; + + std::vector subscribers; + }; +} + +#endif diff --git a/.history/Subscription_20221013204725.cpp b/.history/Subscription_20221013204725.cpp new file mode 100644 index 0000000..3f683db --- /dev/null +++ b/.history/Subscription_20221013204725.cpp @@ -0,0 +1,76 @@ +#include "Subscription.h" +#include "Log.h" +#include "TCPSession.h" +#include + +namespace core +{ + + Subscription::Subscription(std::string id, std::string mode) + : id(id), mode(mode), owner(NULL) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode) + : id(id), mode(mode), owner(&session) {} + + Subscription::~Subscription() + { + std::stringstream out; + out << "cancel:" << id << std::endl; + for (auto subscriber : subscribers) + { + subscriber->write(out.str()); + } + } + + int Subscription::subscribe(TCPSession &session) + { + onSubscribe(session); + subscribers.push_back(&session); + return 1; + } + + int Subscription::unsubscribe(TCPSession &session) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + { + if (*subscriber == &session) + { + subscribers.erase(subscriber++); + return 1; + } + } + return 0; + } + + int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) + { + if (handler) + handler->process(request, out, session); + else + out << "event:" << request[1] << ":" << request[2] << std::endl; + return 1; + } + + int Subscription::event(std::stringstream &out) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + (*subscriber)->write(out.str()); + return 1; + } + + bool Subscription::ifSubscriber(TCPSession &session) + { + return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end()); + } + + int Subscription::onSubscribe(TCPSession &session) + { + return 0; + } + + void setHandler(SubscriptionHandler *handler) + { + this->handler = handler; + } + +} diff --git a/.history/Subscription_20221013204753.cpp b/.history/Subscription_20221013204753.cpp new file mode 100644 index 0000000..3f683db --- /dev/null +++ b/.history/Subscription_20221013204753.cpp @@ -0,0 +1,76 @@ +#include "Subscription.h" +#include "Log.h" +#include "TCPSession.h" +#include + +namespace core +{ + + Subscription::Subscription(std::string id, std::string mode) + : id(id), mode(mode), owner(NULL) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode) + : id(id), mode(mode), owner(&session) {} + + Subscription::~Subscription() + { + std::stringstream out; + out << "cancel:" << id << std::endl; + for (auto subscriber : subscribers) + { + subscriber->write(out.str()); + } + } + + int Subscription::subscribe(TCPSession &session) + { + onSubscribe(session); + subscribers.push_back(&session); + return 1; + } + + int Subscription::unsubscribe(TCPSession &session) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + { + if (*subscriber == &session) + { + subscribers.erase(subscriber++); + return 1; + } + } + return 0; + } + + int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) + { + if (handler) + handler->process(request, out, session); + else + out << "event:" << request[1] << ":" << request[2] << std::endl; + return 1; + } + + int Subscription::event(std::stringstream &out) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + (*subscriber)->write(out.str()); + return 1; + } + + bool Subscription::ifSubscriber(TCPSession &session) + { + return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end()); + } + + int Subscription::onSubscribe(TCPSession &session) + { + return 0; + } + + void setHandler(SubscriptionHandler *handler) + { + this->handler = handler; + } + +} diff --git a/.history/Subscription_20221013205054.cpp b/.history/Subscription_20221013205054.cpp new file mode 100644 index 0000000..2fe2d13 --- /dev/null +++ b/.history/Subscription_20221013205054.cpp @@ -0,0 +1,76 @@ +#include "Subscription.h" +#include "Log.h" +#include "TCPSession.h" +#include + +namespace core +{ + + Subscription::Subscription(std::string id, std::string mode) + : id(id), mode(mode), owner(NULL) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode) + : id(id), mode(mode), owner(&session) {} + + Subscription::~Subscription() + { + std::stringstream out; + out << "cancel:" << id << std::endl; + for (auto subscriber : subscribers) + { + subscriber->write(out.str()); + } + } + + int Subscription::subscribe(TCPSession &session) + { + onSubscribe(session); + subscribers.push_back(&session); + return 1; + } + + int Subscription::unsubscribe(TCPSession &session) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + { + if (*subscriber == &session) + { + subscribers.erase(subscriber++); + return 1; + } + } + return 0; + } + + int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) + { + if (handler) + handler->process(request, out, session); + else + out << "event:" << request[1] << ":" << request[2] << std::endl; + return 1; + } + + int Subscription::event(std::stringstream &out) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + (*subscriber)->write(out.str()); + return 1; + } + + bool Subscription::ifSubscriber(TCPSession &session) + { + return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end()); + } + + int Subscription::onSubscribe(TCPSession &session) + { + return 0; + } + + void setHandler(SubscriptionHandler *handler) + { + handler = handler; + } + +} diff --git a/.history/Subscription_20221014003150.h b/.history/Subscription_20221014003150.h new file mode 100644 index 0000000..e2c4d84 --- /dev/null +++ b/.history/Subscription_20221014003150.h @@ -0,0 +1,47 @@ +#ifndef __Subscription_h__ +#define __Subscription_h__ + +#include "SubscriptionHandler.h" +#include "ZString.h" +#include +#include + +namespace core +{ + + class TCPSession; + + class Subscription + { + + public: + Subscription(std::string id, std::string mode = "*AUTHOR"); + Subscription(std::string id, TCPSession &session, std::string mode = "*AUTHOR"); + virtual ~Subscription(); + + int subscribe(TCPSession &session); + int unsubscribe(TCPSession &session); + + virtual int process(coreutils::ZString &request, std::stringstream &out, TCPSession &session); + + virtual int onSubscribe(TCPSession &session); + + int event(std::stringstream &out); + + bool ifSubscriber(TCPSession &session); + + void setHandler(SubscriptionHandler *handler); + + // int processCommand(coreutils::ZString &request, TCPSession &session) override; + + std::string id; + std::string mode; + TCPSession *owner; + + SubscriptionHandler *handler; + + std::vector subscribers; + }; +} + +#endif diff --git a/.history/Subscription_20221014212452.h b/.history/Subscription_20221014212452.h new file mode 100644 index 0000000..6f8ef82 --- /dev/null +++ b/.history/Subscription_20221014212452.h @@ -0,0 +1,49 @@ +#ifndef __Subscription_h__ +#define __Subscription_h__ + +#include "SubscriptionHandler.h" +#include "TCPSession.h" +#include "ZString.h" +#include +#include + +namespace core +{ + + class TCPSession; + + class Subscription + { + + public: + Subscription(std::string id, std::string mode = "*AUTHOR"); + Subscription(std::string id, TCPSession &session, std::string mode); + Subscription(std::string id, TCPSession &session, std::string mode, std::string handler); + virtual ~Subscription(); + + int subscribe(TCPSession &session); + int unsubscribe(TCPSession &session); + + virtual int process(coreutils::ZString &request, std::stringstream &out, TCPSession &session); + + virtual int onSubscribe(TCPSession &session); + + int event(std::stringstream &out); + + bool ifSubscriber(TCPSession &session); + + void setHandler(SubscriptionHandler *handler); + + // int processCommand(coreutils::ZString &request, TCPSession &session) override; + + std::string id; + std::string mode; + TCPSession *owner; + + SubscriptionHandler *handler; + + std::vector subscribers; + }; +} + +#endif diff --git a/.history/Subscription_20221014212457.h b/.history/Subscription_20221014212457.h new file mode 100644 index 0000000..d55b0e7 --- /dev/null +++ b/.history/Subscription_20221014212457.h @@ -0,0 +1,47 @@ +#ifndef __Subscription_h__ +#define __Subscription_h__ + +#include "SubscriptionHandler.h" +#include "TCPSession.h" +#include "ZString.h" +#include +#include + +namespace core +{ + + class Subscription + { + + public: + Subscription(std::string id, std::string mode = "*AUTHOR"); + Subscription(std::string id, TCPSession &session, std::string mode); + Subscription(std::string id, TCPSession &session, std::string mode, std::string handler); + virtual ~Subscription(); + + int subscribe(TCPSession &session); + int unsubscribe(TCPSession &session); + + virtual int process(coreutils::ZString &request, std::stringstream &out, TCPSession &session); + + virtual int onSubscribe(TCPSession &session); + + int event(std::stringstream &out); + + bool ifSubscriber(TCPSession &session); + + void setHandler(SubscriptionHandler *handler); + + // int processCommand(coreutils::ZString &request, TCPSession &session) override; + + std::string id; + std::string mode; + TCPSession *owner; + + SubscriptionHandler *handler; + + std::vector subscribers; + }; +} + +#endif diff --git a/.history/Subscription_20221014212653.h b/.history/Subscription_20221014212653.h new file mode 100644 index 0000000..c5ef04a --- /dev/null +++ b/.history/Subscription_20221014212653.h @@ -0,0 +1,48 @@ +#ifndef __Subscription_h__ +#define __Subscription_h__ + +#include "SubscriptionHandler.h" +#include "ZString.h" +#include +#include + +namespace core +{ + + class TCPSession; + + class Subscription + { + + public: + Subscription(std::string id, std::string mode = "*AUTHOR"); + Subscription(std::string id, TCPSession &session, std::string mode); + Subscription(std::string id, TCPSession &session, std::string mode, std::string handler); + virtual ~Subscription(); + + int subscribe(TCPSession &session); + int unsubscribe(TCPSession &session); + + virtual int process(coreutils::ZString &request, std::stringstream &out, TCPSession &session); + + virtual int onSubscribe(TCPSession &session); + + int event(std::stringstream &out); + + bool ifSubscriber(TCPSession &session); + + void setHandler(SubscriptionHandler *handler); + + // int processCommand(coreutils::ZString &request, TCPSession &session) override; + + std::string id; + std::string mode; + TCPSession *owner; + + SubscriptionHandler *handler; + + std::vector subscribers; + }; +} + +#endif diff --git a/.history/Subscription_20221014220951.cpp b/.history/Subscription_20221014220951.cpp new file mode 100644 index 0000000..6a1cb7b --- /dev/null +++ b/.history/Subscription_20221014220951.cpp @@ -0,0 +1,76 @@ +#include "Subscription.h" +#include "Log.h" +#include "TCPSession.h" +#include + +namespace core +{ + + Subscription::Subscription(std::string id, std::string mode) + : id(id), mode(mode), owner(NULL) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode, std::string handler) + : id(id), mode(mode), owner(&session) {} + + Subscription::~Subscription() + { + std::stringstream out; + out << "cancel:" << id << std::endl; + for (auto subscriber : subscribers) + { + subscriber->write(out.str()); + } + } + + int Subscription::subscribe(TCPSession &session) + { + onSubscribe(session); + subscribers.push_back(&session); + return 1; + } + + int Subscription::unsubscribe(TCPSession &session) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + { + if (*subscriber == &session) + { + subscribers.erase(subscriber++); + return 1; + } + } + return 0; + } + + int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) + { + if (handler) + handler->process(request, out, session); + else + out << "event:" << request[1] << ":" << request[2] << std::endl; + return 1; + } + + int Subscription::event(std::stringstream &out) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + (*subscriber)->write(out.str()); + return 1; + } + + bool Subscription::ifSubscriber(TCPSession &session) + { + return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end()); + } + + int Subscription::onSubscribe(TCPSession &session) + { + return 0; + } + + void Subscription::setHandler(SubscriptionHandler *handler) + { + handler = handler; + } + +} diff --git a/.history/Subscription_20221015131611.cpp b/.history/Subscription_20221015131611.cpp new file mode 100644 index 0000000..6a1cb7b --- /dev/null +++ b/.history/Subscription_20221015131611.cpp @@ -0,0 +1,76 @@ +#include "Subscription.h" +#include "Log.h" +#include "TCPSession.h" +#include + +namespace core +{ + + Subscription::Subscription(std::string id, std::string mode) + : id(id), mode(mode), owner(NULL) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode, std::string handler) + : id(id), mode(mode), owner(&session) {} + + Subscription::~Subscription() + { + std::stringstream out; + out << "cancel:" << id << std::endl; + for (auto subscriber : subscribers) + { + subscriber->write(out.str()); + } + } + + int Subscription::subscribe(TCPSession &session) + { + onSubscribe(session); + subscribers.push_back(&session); + return 1; + } + + int Subscription::unsubscribe(TCPSession &session) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + { + if (*subscriber == &session) + { + subscribers.erase(subscriber++); + return 1; + } + } + return 0; + } + + int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) + { + if (handler) + handler->process(request, out, session); + else + out << "event:" << request[1] << ":" << request[2] << std::endl; + return 1; + } + + int Subscription::event(std::stringstream &out) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + (*subscriber)->write(out.str()); + return 1; + } + + bool Subscription::ifSubscriber(TCPSession &session) + { + return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end()); + } + + int Subscription::onSubscribe(TCPSession &session) + { + return 0; + } + + void Subscription::setHandler(SubscriptionHandler *handler) + { + handler = handler; + } + +} diff --git a/.history/Subscription_20221015142023.cpp b/.history/Subscription_20221015142023.cpp new file mode 100644 index 0000000..640cd6b --- /dev/null +++ b/.history/Subscription_20221015142023.cpp @@ -0,0 +1,79 @@ +#include "Subscription.h" +#include "Log.h" +#include "TCPSession.h" +#include + +namespace core +{ + + Subscription::Subscription(std::string id, std::string mode) + : id(id), mode(mode), owner(NULL) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode, std::string handler) + : id(id), mode(mode), owner(&session) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode) + : id(id), mode(mode), owner(&session) {} + + Subscription::~Subscription() + { + std::stringstream out; + out << "cancel:" << id << std::endl; + for (auto subscriber : subscribers) + { + subscriber->write(out.str()); + } + } + + int Subscription::subscribe(TCPSession &session) + { + onSubscribe(session); + subscribers.push_back(&session); + return 1; + } + + int Subscription::unsubscribe(TCPSession &session) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + { + if (*subscriber == &session) + { + subscribers.erase(subscriber++); + return 1; + } + } + return 0; + } + + int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) + { + if (handler) + handler->process(request, out, session); + else + out << "event:" << request[1] << ":" << request[2] << std::endl; + return 1; + } + + int Subscription::event(std::stringstream &out) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + (*subscriber)->write(out.str()); + return 1; + } + + bool Subscription::ifSubscriber(TCPSession &session) + { + return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end()); + } + + int Subscription::onSubscribe(TCPSession &session) + { + return 0; + } + + void Subscription::setHandler(SubscriptionHandler *handler) + { + handler = handler; + } + +} diff --git a/.history/Subscription_20221015175629.cpp b/.history/Subscription_20221015175629.cpp new file mode 100644 index 0000000..640cd6b --- /dev/null +++ b/.history/Subscription_20221015175629.cpp @@ -0,0 +1,79 @@ +#include "Subscription.h" +#include "Log.h" +#include "TCPSession.h" +#include + +namespace core +{ + + Subscription::Subscription(std::string id, std::string mode) + : id(id), mode(mode), owner(NULL) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode, std::string handler) + : id(id), mode(mode), owner(&session) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode) + : id(id), mode(mode), owner(&session) {} + + Subscription::~Subscription() + { + std::stringstream out; + out << "cancel:" << id << std::endl; + for (auto subscriber : subscribers) + { + subscriber->write(out.str()); + } + } + + int Subscription::subscribe(TCPSession &session) + { + onSubscribe(session); + subscribers.push_back(&session); + return 1; + } + + int Subscription::unsubscribe(TCPSession &session) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + { + if (*subscriber == &session) + { + subscribers.erase(subscriber++); + return 1; + } + } + return 0; + } + + int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) + { + if (handler) + handler->process(request, out, session); + else + out << "event:" << request[1] << ":" << request[2] << std::endl; + return 1; + } + + int Subscription::event(std::stringstream &out) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + (*subscriber)->write(out.str()); + return 1; + } + + bool Subscription::ifSubscriber(TCPSession &session) + { + return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end()); + } + + int Subscription::onSubscribe(TCPSession &session) + { + return 0; + } + + void Subscription::setHandler(SubscriptionHandler *handler) + { + handler = handler; + } + +} diff --git a/.history/Subscription_20221015182553.h b/.history/Subscription_20221015182553.h new file mode 100644 index 0000000..7f0398f --- /dev/null +++ b/.history/Subscription_20221015182553.h @@ -0,0 +1,49 @@ +#ifndef __Subscription_h__ +#define __Subscription_h__ + +#include "SubscriptionHandler.h" +#include "ZString.h" +#include +#include + +namespace core +{ + + class TCPSession; + + class Subscription + { + + public: + Subscription(std::string id, std::string mode = "*AUTHOR"); + Subscription(std::string id, TCPSession &session, std::string mode); + Subscription(std::string id, TCPSession &session, std::string mode, std::string handler); + virtual ~Subscription(); + + int subscribe(TCPSession &session); + int unsubscribe(TCPSession &session); + + virtual int process(coreutils::ZString &request, std::stringstream &out, TCPSession &session); + + virtual int onSubscribe(TCPSession &session); + + int event(std::stringstream &out); + + bool ifSubscriber(TCPSession &session); + + void setHandler(SubscriptionHandler *handler); + + // int processCommand(coreutils::ZString &request, TCPSession &session) override; + + std::string id; + std::string mode; + TCPSession *owner; + + + SubscriptionHandler *handler; + + std::vector subscribers; + }; +} + +#endif diff --git a/.history/Subscription_20221015182613.h b/.history/Subscription_20221015182613.h new file mode 100644 index 0000000..0d98e5e --- /dev/null +++ b/.history/Subscription_20221015182613.h @@ -0,0 +1,49 @@ +#ifndef __Subscription_h__ +#define __Subscription_h__ + +#include "SubscriptionHandler.h" +#include "ZString.h" +#include +#include + +namespace core +{ + + class TCPSession; + + class Subscription + { + + public: + Subscription(std::string id, std::string mode = "*AUTHOR"); + Subscription(std::string id, TCPSession &session, std::string mode); + Subscription(std::string id, TCPSession &session, std::string mode, std::string handler); + virtual ~Subscription(); + + int subscribe(TCPSession &session); + int unsubscribe(TCPSession &session); + + virtual int process(coreutils::ZString &request, std::stringstream &out, TCPSession &session); + + virtual int onSubscribe(TCPSession &session); + + int event(std::stringstream &out); + + bool ifSubscriber(TCPSession &session); + + void setHandler(SubscriptionHandler *handler); + + // int processCommand(coreutils::ZString &request, TCPSession &session) override; + + std::string id; + std::string mode; + TCPSession *owner; + std::string handler; + + SubscriptionHandler *handler; + + std::vector subscribers; + }; +} + +#endif diff --git a/.history/Subscription_20221015182633.cpp b/.history/Subscription_20221015182633.cpp new file mode 100644 index 0000000..3c1199a --- /dev/null +++ b/.history/Subscription_20221015182633.cpp @@ -0,0 +1,79 @@ +#include "Subscription.h" +#include "Log.h" +#include "TCPSession.h" +#include + +namespace core +{ + + Subscription::Subscription(std::string id, std::string mode) + : id(id), mode(mode), owner(NULL) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode, std::string handler) + : id(id), mode(mode), owner(&session), handler(handler) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode) + : id(id), mode(mode), owner(&session) {} + + Subscription::~Subscription() + { + std::stringstream out; + out << "cancel:" << id << std::endl; + for (auto subscriber : subscribers) + { + subscriber->write(out.str()); + } + } + + int Subscription::subscribe(TCPSession &session) + { + onSubscribe(session); + subscribers.push_back(&session); + return 1; + } + + int Subscription::unsubscribe(TCPSession &session) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + { + if (*subscriber == &session) + { + subscribers.erase(subscriber++); + return 1; + } + } + return 0; + } + + int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) + { + if (handler) + handler->process(request, out, session); + else + out << "event:" << request[1] << ":" << request[2] << std::endl; + return 1; + } + + int Subscription::event(std::stringstream &out) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + (*subscriber)->write(out.str()); + return 1; + } + + bool Subscription::ifSubscriber(TCPSession &session) + { + return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end()); + } + + int Subscription::onSubscribe(TCPSession &session) + { + return 0; + } + + void Subscription::setHandler(SubscriptionHandler *handler) + { + handler = handler; + } + +} diff --git a/.history/Subscription_20221015182644.cpp b/.history/Subscription_20221015182644.cpp new file mode 100644 index 0000000..fc07da5 --- /dev/null +++ b/.history/Subscription_20221015182644.cpp @@ -0,0 +1,79 @@ +#include "Subscription.h" +#include "Log.h" +#include "TCPSession.h" +#include + +namespace core +{ + + Subscription::Subscription(std::string id, std::string mode) + : id(id), mode(mode), owner(NULL) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode, std::string handlers) + : id(id), mode(mode), owner(&session), handler(handlers) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode) + : id(id), mode(mode), owner(&session) {} + + Subscription::~Subscription() + { + std::stringstream out; + out << "cancel:" << id << std::endl; + for (auto subscriber : subscribers) + { + subscriber->write(out.str()); + } + } + + int Subscription::subscribe(TCPSession &session) + { + onSubscribe(session); + subscribers.push_back(&session); + return 1; + } + + int Subscription::unsubscribe(TCPSession &session) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + { + if (*subscriber == &session) + { + subscribers.erase(subscriber++); + return 1; + } + } + return 0; + } + + int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) + { + if (handler) + handler->process(request, out, session); + else + out << "event:" << request[1] << ":" << request[2] << std::endl; + return 1; + } + + int Subscription::event(std::stringstream &out) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + (*subscriber)->write(out.str()); + return 1; + } + + bool Subscription::ifSubscriber(TCPSession &session) + { + return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end()); + } + + int Subscription::onSubscribe(TCPSession &session) + { + return 0; + } + + void Subscription::setHandler(SubscriptionHandler *handler) + { + handler = handler; + } + +} diff --git a/.history/Subscription_20221015182655.h b/.history/Subscription_20221015182655.h new file mode 100644 index 0000000..53fd588 --- /dev/null +++ b/.history/Subscription_20221015182655.h @@ -0,0 +1,49 @@ +#ifndef __Subscription_h__ +#define __Subscription_h__ + +#include "SubscriptionHandler.h" +#include "ZString.h" +#include +#include + +namespace core +{ + + class TCPSession; + + class Subscription + { + + public: + Subscription(std::string id, std::string mode = "*AUTHOR"); + Subscription(std::string id, TCPSession &session, std::string mode); + Subscription(std::string id, TCPSession &session, std::string mode, std::string handler); + virtual ~Subscription(); + + int subscribe(TCPSession &session); + int unsubscribe(TCPSession &session); + + virtual int process(coreutils::ZString &request, std::stringstream &out, TCPSession &session); + + virtual int onSubscribe(TCPSession &session); + + int event(std::stringstream &out); + + bool ifSubscriber(TCPSession &session); + + void setHandler(SubscriptionHandler *handler); + + // int processCommand(coreutils::ZString &request, TCPSession &session) override; + + std::string id; + std::string mode; + TCPSession *owner; + std::string handlers; + + SubscriptionHandler *handler; + + std::vector subscribers; + }; +} + +#endif diff --git a/.history/Subscription_20221015182706.cpp b/.history/Subscription_20221015182706.cpp new file mode 100644 index 0000000..c583ea8 --- /dev/null +++ b/.history/Subscription_20221015182706.cpp @@ -0,0 +1,79 @@ +#include "Subscription.h" +#include "Log.h" +#include "TCPSession.h" +#include + +namespace core +{ + + Subscription::Subscription(std::string id, std::string mode) + : id(id), mode(mode), owner(NULL) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode, std::string handlers) + : id(id), mode(mode), owner(&session), handlers(handlers) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode) + : id(id), mode(mode), owner(&session) {} + + Subscription::~Subscription() + { + std::stringstream out; + out << "cancel:" << id << std::endl; + for (auto subscriber : subscribers) + { + subscriber->write(out.str()); + } + } + + int Subscription::subscribe(TCPSession &session) + { + onSubscribe(session); + subscribers.push_back(&session); + return 1; + } + + int Subscription::unsubscribe(TCPSession &session) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + { + if (*subscriber == &session) + { + subscribers.erase(subscriber++); + return 1; + } + } + return 0; + } + + int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) + { + if (handler) + handler->process(request, out, session); + else + out << "event:" << request[1] << ":" << request[2] << std::endl; + return 1; + } + + int Subscription::event(std::stringstream &out) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + (*subscriber)->write(out.str()); + return 1; + } + + bool Subscription::ifSubscriber(TCPSession &session) + { + return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end()); + } + + int Subscription::onSubscribe(TCPSession &session) + { + return 0; + } + + void Subscription::setHandler(SubscriptionHandler *handler) + { + handler = handler; + } + +} diff --git a/.history/Subscription_20221015182712.cpp b/.history/Subscription_20221015182712.cpp new file mode 100644 index 0000000..c583ea8 --- /dev/null +++ b/.history/Subscription_20221015182712.cpp @@ -0,0 +1,79 @@ +#include "Subscription.h" +#include "Log.h" +#include "TCPSession.h" +#include + +namespace core +{ + + Subscription::Subscription(std::string id, std::string mode) + : id(id), mode(mode), owner(NULL) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode, std::string handlers) + : id(id), mode(mode), owner(&session), handlers(handlers) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode) + : id(id), mode(mode), owner(&session) {} + + Subscription::~Subscription() + { + std::stringstream out; + out << "cancel:" << id << std::endl; + for (auto subscriber : subscribers) + { + subscriber->write(out.str()); + } + } + + int Subscription::subscribe(TCPSession &session) + { + onSubscribe(session); + subscribers.push_back(&session); + return 1; + } + + int Subscription::unsubscribe(TCPSession &session) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + { + if (*subscriber == &session) + { + subscribers.erase(subscriber++); + return 1; + } + } + return 0; + } + + int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) + { + if (handler) + handler->process(request, out, session); + else + out << "event:" << request[1] << ":" << request[2] << std::endl; + return 1; + } + + int Subscription::event(std::stringstream &out) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + (*subscriber)->write(out.str()); + return 1; + } + + bool Subscription::ifSubscriber(TCPSession &session) + { + return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end()); + } + + int Subscription::onSubscribe(TCPSession &session) + { + return 0; + } + + void Subscription::setHandler(SubscriptionHandler *handler) + { + handler = handler; + } + +} diff --git a/.history/Subscription_20221015203632.h b/.history/Subscription_20221015203632.h new file mode 100644 index 0000000..b9340b2 --- /dev/null +++ b/.history/Subscription_20221015203632.h @@ -0,0 +1,49 @@ +#ifndef __Subscription_h__ +#define __Subscription_h__ + +#include "SubscriptionHandler.h" +#include "ZString.h" +#include +#include + +namespace core +{ + + class TCPSession; + + class Subscription + { + + public: + Subscription(std::string id, std::string mode = "*AUTHOR"); + Subscription(std::string id, TCPSession &session, std::string mode); + Subscription(std::string id, TCPSession &session, std::string mode, std::string handler); + virtual ~Subscription(); + + int subscribe(TCPSession &session); + int unsubscribe(TCPSession &session); + + virtual int process(coreutils::ZString &request, std::stringstream &out, TCPSession &session); + + virtual int onSubscribe(TCPSession &session); + + int event(std::stringstream &out); + + bool ifSubscriber(TCPSession &session); + + void setHandler(SubscriptionHandler *handler); + + // int processCommand(coreutils::ZString &request, TCPSession &session) override; + + std::string id; + std::string mode; + TCPSession *owner; + std::string handlers; + + SubscriptionHandler &handler; + + std::vector subscribers; + }; +} + +#endif diff --git a/.history/Subscription_20221015203643.cpp b/.history/Subscription_20221015203643.cpp new file mode 100644 index 0000000..ac861a6 --- /dev/null +++ b/.history/Subscription_20221015203643.cpp @@ -0,0 +1,79 @@ +#include "Subscription.h" +#include "Log.h" +#include "TCPSession.h" +#include + +namespace core +{ + + Subscription::Subscription(std::string id, std::string mode) + : id(id), mode(mode), owner(NULL) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode, std::string handlers) + : id(id), mode(mode), owner(&session), handlers(handlers) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode) + : id(id), mode(mode), owner(&session) {} + + Subscription::~Subscription() + { + std::stringstream out; + out << "cancel:" << id << std::endl; + for (auto subscriber : subscribers) + { + subscriber->write(out.str()); + } + } + + int Subscription::subscribe(TCPSession &session) + { + onSubscribe(session); + subscribers.push_back(&session); + return 1; + } + + int Subscription::unsubscribe(TCPSession &session) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + { + if (*subscriber == &session) + { + subscribers.erase(subscriber++); + return 1; + } + } + return 0; + } + + int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) + { + if (handler) + handler.process(request, out, session); + else + out << "event:" << request[1] << ":" << request[2] << std::endl; + return 1; + } + + int Subscription::event(std::stringstream &out) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + (*subscriber)->write(out.str()); + return 1; + } + + bool Subscription::ifSubscriber(TCPSession &session) + { + return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end()); + } + + int Subscription::onSubscribe(TCPSession &session) + { + return 0; + } + + void Subscription::setHandler(SubscriptionHandler *handler) + { + handler = handler; + } + +} diff --git a/.history/Subscription_20221015203655.cpp b/.history/Subscription_20221015203655.cpp new file mode 100644 index 0000000..73d9ad2 --- /dev/null +++ b/.history/Subscription_20221015203655.cpp @@ -0,0 +1,79 @@ +#include "Subscription.h" +#include "Log.h" +#include "TCPSession.h" +#include + +namespace core +{ + + Subscription::Subscription(std::string id, std::string mode) + : id(id), mode(mode), owner(NULL) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode, std::string handlers) + : id(id), mode(mode), owner(&session), handlers(handlers) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode) + : id(id), mode(mode), owner(&session) {} + + Subscription::~Subscription() + { + std::stringstream out; + out << "cancel:" << id << std::endl; + for (auto subscriber : subscribers) + { + subscriber->write(out.str()); + } + } + + int Subscription::subscribe(TCPSession &session) + { + onSubscribe(session); + subscribers.push_back(&session); + return 1; + } + + int Subscription::unsubscribe(TCPSession &session) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + { + if (*subscriber == &session) + { + subscribers.erase(subscriber++); + return 1; + } + } + return 0; + } + + int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) + { + if (&handler) + handler.process(request, out, session); + else + out << "event:" << request[1] << ":" << request[2] << std::endl; + return 1; + } + + int Subscription::event(std::stringstream &out) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + (*subscriber)->write(out.str()); + return 1; + } + + bool Subscription::ifSubscriber(TCPSession &session) + { + return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end()); + } + + int Subscription::onSubscribe(TCPSession &session) + { + return 0; + } + + void Subscription::setHandler(SubscriptionHandler *handler) + { + handler = handler; + } + +} diff --git a/.history/Subscription_20221015203702.cpp b/.history/Subscription_20221015203702.cpp new file mode 100644 index 0000000..73d9ad2 --- /dev/null +++ b/.history/Subscription_20221015203702.cpp @@ -0,0 +1,79 @@ +#include "Subscription.h" +#include "Log.h" +#include "TCPSession.h" +#include + +namespace core +{ + + Subscription::Subscription(std::string id, std::string mode) + : id(id), mode(mode), owner(NULL) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode, std::string handlers) + : id(id), mode(mode), owner(&session), handlers(handlers) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode) + : id(id), mode(mode), owner(&session) {} + + Subscription::~Subscription() + { + std::stringstream out; + out << "cancel:" << id << std::endl; + for (auto subscriber : subscribers) + { + subscriber->write(out.str()); + } + } + + int Subscription::subscribe(TCPSession &session) + { + onSubscribe(session); + subscribers.push_back(&session); + return 1; + } + + int Subscription::unsubscribe(TCPSession &session) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + { + if (*subscriber == &session) + { + subscribers.erase(subscriber++); + return 1; + } + } + return 0; + } + + int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) + { + if (&handler) + handler.process(request, out, session); + else + out << "event:" << request[1] << ":" << request[2] << std::endl; + return 1; + } + + int Subscription::event(std::stringstream &out) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + (*subscriber)->write(out.str()); + return 1; + } + + bool Subscription::ifSubscriber(TCPSession &session) + { + return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end()); + } + + int Subscription::onSubscribe(TCPSession &session) + { + return 0; + } + + void Subscription::setHandler(SubscriptionHandler *handler) + { + handler = handler; + } + +} diff --git a/.history/Subscription_20221015203734.h b/.history/Subscription_20221015203734.h new file mode 100644 index 0000000..53fd588 --- /dev/null +++ b/.history/Subscription_20221015203734.h @@ -0,0 +1,49 @@ +#ifndef __Subscription_h__ +#define __Subscription_h__ + +#include "SubscriptionHandler.h" +#include "ZString.h" +#include +#include + +namespace core +{ + + class TCPSession; + + class Subscription + { + + public: + Subscription(std::string id, std::string mode = "*AUTHOR"); + Subscription(std::string id, TCPSession &session, std::string mode); + Subscription(std::string id, TCPSession &session, std::string mode, std::string handler); + virtual ~Subscription(); + + int subscribe(TCPSession &session); + int unsubscribe(TCPSession &session); + + virtual int process(coreutils::ZString &request, std::stringstream &out, TCPSession &session); + + virtual int onSubscribe(TCPSession &session); + + int event(std::stringstream &out); + + bool ifSubscriber(TCPSession &session); + + void setHandler(SubscriptionHandler *handler); + + // int processCommand(coreutils::ZString &request, TCPSession &session) override; + + std::string id; + std::string mode; + TCPSession *owner; + std::string handlers; + + SubscriptionHandler *handler; + + std::vector subscribers; + }; +} + +#endif diff --git a/.history/Subscription_20221015203735.cpp b/.history/Subscription_20221015203735.cpp new file mode 100644 index 0000000..c583ea8 --- /dev/null +++ b/.history/Subscription_20221015203735.cpp @@ -0,0 +1,79 @@ +#include "Subscription.h" +#include "Log.h" +#include "TCPSession.h" +#include + +namespace core +{ + + Subscription::Subscription(std::string id, std::string mode) + : id(id), mode(mode), owner(NULL) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode, std::string handlers) + : id(id), mode(mode), owner(&session), handlers(handlers) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode) + : id(id), mode(mode), owner(&session) {} + + Subscription::~Subscription() + { + std::stringstream out; + out << "cancel:" << id << std::endl; + for (auto subscriber : subscribers) + { + subscriber->write(out.str()); + } + } + + int Subscription::subscribe(TCPSession &session) + { + onSubscribe(session); + subscribers.push_back(&session); + return 1; + } + + int Subscription::unsubscribe(TCPSession &session) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + { + if (*subscriber == &session) + { + subscribers.erase(subscriber++); + return 1; + } + } + return 0; + } + + int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) + { + if (handler) + handler->process(request, out, session); + else + out << "event:" << request[1] << ":" << request[2] << std::endl; + return 1; + } + + int Subscription::event(std::stringstream &out) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + (*subscriber)->write(out.str()); + return 1; + } + + bool Subscription::ifSubscriber(TCPSession &session) + { + return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end()); + } + + int Subscription::onSubscribe(TCPSession &session) + { + return 0; + } + + void Subscription::setHandler(SubscriptionHandler *handler) + { + handler = handler; + } + +} diff --git a/.history/Subscription_20221015203805.cpp b/.history/Subscription_20221015203805.cpp new file mode 100644 index 0000000..c583ea8 --- /dev/null +++ b/.history/Subscription_20221015203805.cpp @@ -0,0 +1,79 @@ +#include "Subscription.h" +#include "Log.h" +#include "TCPSession.h" +#include + +namespace core +{ + + Subscription::Subscription(std::string id, std::string mode) + : id(id), mode(mode), owner(NULL) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode, std::string handlers) + : id(id), mode(mode), owner(&session), handlers(handlers) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode) + : id(id), mode(mode), owner(&session) {} + + Subscription::~Subscription() + { + std::stringstream out; + out << "cancel:" << id << std::endl; + for (auto subscriber : subscribers) + { + subscriber->write(out.str()); + } + } + + int Subscription::subscribe(TCPSession &session) + { + onSubscribe(session); + subscribers.push_back(&session); + return 1; + } + + int Subscription::unsubscribe(TCPSession &session) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + { + if (*subscriber == &session) + { + subscribers.erase(subscriber++); + return 1; + } + } + return 0; + } + + int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) + { + if (handler) + handler->process(request, out, session); + else + out << "event:" << request[1] << ":" << request[2] << std::endl; + return 1; + } + + int Subscription::event(std::stringstream &out) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + (*subscriber)->write(out.str()); + return 1; + } + + bool Subscription::ifSubscriber(TCPSession &session) + { + return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end()); + } + + int Subscription::onSubscribe(TCPSession &session) + { + return 0; + } + + void Subscription::setHandler(SubscriptionHandler *handler) + { + handler = handler; + } + +} diff --git a/.history/Subscription_20221015204939.cpp b/.history/Subscription_20221015204939.cpp new file mode 100644 index 0000000..dde1003 --- /dev/null +++ b/.history/Subscription_20221015204939.cpp @@ -0,0 +1,79 @@ +#include "Subscription.h" +#include "Log.h" +#include "TCPSession.h" +#include + +namespace core +{ + + Subscription::Subscription(std::string id, std::string mode) + : id(id), mode(mode), owner(NULL) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode, std::string handlers) + : id(id), mode(mode), owner(&session), handlers(handlers) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode) + : id(id), mode(mode), owner(&session) {} + + Subscription::~Subscription() + { + std::stringstream out; + out << "cancel:" << id << std::endl; + for (auto subscriber : subscribers) + { + subscriber->write(out.str()); + } + } + + int Subscription::subscribe(TCPSession &session) + { + onSubscribe(session); + subscribers.push_back(&session); + return 1; + } + + int Subscription::unsubscribe(TCPSession &session) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + { + if (*subscriber == &session) + { + subscribers.erase(subscriber++); + return 1; + } + } + return 0; + } + + int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) + { + if (!handler) + handler->process(request, out, session); + else + out << "event:" << request[1] << ":" << request[2] << std::endl; + return 1; + } + + int Subscription::event(std::stringstream &out) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + (*subscriber)->write(out.str()); + return 1; + } + + bool Subscription::ifSubscriber(TCPSession &session) + { + return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end()); + } + + int Subscription::onSubscribe(TCPSession &session) + { + return 0; + } + + void Subscription::setHandler(SubscriptionHandler *handler) + { + handler = handler; + } + +} diff --git a/.history/Subscription_20221016151339.cpp b/.history/Subscription_20221016151339.cpp new file mode 100644 index 0000000..c583ea8 --- /dev/null +++ b/.history/Subscription_20221016151339.cpp @@ -0,0 +1,79 @@ +#include "Subscription.h" +#include "Log.h" +#include "TCPSession.h" +#include + +namespace core +{ + + Subscription::Subscription(std::string id, std::string mode) + : id(id), mode(mode), owner(NULL) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode, std::string handlers) + : id(id), mode(mode), owner(&session), handlers(handlers) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode) + : id(id), mode(mode), owner(&session) {} + + Subscription::~Subscription() + { + std::stringstream out; + out << "cancel:" << id << std::endl; + for (auto subscriber : subscribers) + { + subscriber->write(out.str()); + } + } + + int Subscription::subscribe(TCPSession &session) + { + onSubscribe(session); + subscribers.push_back(&session); + return 1; + } + + int Subscription::unsubscribe(TCPSession &session) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + { + if (*subscriber == &session) + { + subscribers.erase(subscriber++); + return 1; + } + } + return 0; + } + + int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) + { + if (handler) + handler->process(request, out, session); + else + out << "event:" << request[1] << ":" << request[2] << std::endl; + return 1; + } + + int Subscription::event(std::stringstream &out) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + (*subscriber)->write(out.str()); + return 1; + } + + bool Subscription::ifSubscriber(TCPSession &session) + { + return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end()); + } + + int Subscription::onSubscribe(TCPSession &session) + { + return 0; + } + + void Subscription::setHandler(SubscriptionHandler *handler) + { + handler = handler; + } + +} diff --git a/.history/Subscription_20221016162014.cpp b/.history/Subscription_20221016162014.cpp new file mode 100644 index 0000000..c583ea8 --- /dev/null +++ b/.history/Subscription_20221016162014.cpp @@ -0,0 +1,79 @@ +#include "Subscription.h" +#include "Log.h" +#include "TCPSession.h" +#include + +namespace core +{ + + Subscription::Subscription(std::string id, std::string mode) + : id(id), mode(mode), owner(NULL) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode, std::string handlers) + : id(id), mode(mode), owner(&session), handlers(handlers) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode) + : id(id), mode(mode), owner(&session) {} + + Subscription::~Subscription() + { + std::stringstream out; + out << "cancel:" << id << std::endl; + for (auto subscriber : subscribers) + { + subscriber->write(out.str()); + } + } + + int Subscription::subscribe(TCPSession &session) + { + onSubscribe(session); + subscribers.push_back(&session); + return 1; + } + + int Subscription::unsubscribe(TCPSession &session) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + { + if (*subscriber == &session) + { + subscribers.erase(subscriber++); + return 1; + } + } + return 0; + } + + int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) + { + if (handler) + handler->process(request, out, session); + else + out << "event:" << request[1] << ":" << request[2] << std::endl; + return 1; + } + + int Subscription::event(std::stringstream &out) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + (*subscriber)->write(out.str()); + return 1; + } + + bool Subscription::ifSubscriber(TCPSession &session) + { + return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end()); + } + + int Subscription::onSubscribe(TCPSession &session) + { + return 0; + } + + void Subscription::setHandler(SubscriptionHandler *handler) + { + handler = handler; + } + +} diff --git a/.history/Subscription_20221016172637.cpp b/.history/Subscription_20221016172637.cpp new file mode 100644 index 0000000..0040988 --- /dev/null +++ b/.history/Subscription_20221016172637.cpp @@ -0,0 +1,79 @@ +#include "Subscription.h" +#include "Log.h" +#include "TCPSession.h" +#include + +namespace core +{ + + Subscription::Subscription(std::string id, std::string mode) + : id(id), mode(mode), owner(NULL) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode, std::string handlers) + : id(id), mode(mode), owner(&session), handlers(handlers) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode) + : id(id), mode(mode), owner(&session) {} + + Subscription::~Subscription() + { + std::stringstream out; + out << "cancel:" << id << std::endl; + for (auto subscriber : subscribers) + { + subscriber->write(out.str()); + } + } + + int Subscription::subscribe(TCPSession &session) + { + onSubscribe(session); + subscribers.push_back(&session); + return 1; + } + + int Subscription::unsubscribe(TCPSession &session) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + { + if (*subscriber == &session) + { + subscribers.erase(subscriber++); + return 1; + } + } + return 0; + } + + int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) + { + if (handler) + handler->process(request, out, session); + else + out << "event:" << request[1] << ":" << request[2] << std::endl; + return 1; + } + + int Subscription::event(std::stringstream &out) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + (*subscriber)->write(out.str()); + return 1; + } + + bool Subscription::ifSubscriber(TCPSession &session) + { + return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end()); + } + + int Subscription::onSubscribe(TCPSession &session) + { + return 0; + } + + void Subscription::setHandler(SubscriptionHandler *handler) + { + handler = handler; + coreutils::Log(coreutils::LOG_DEBUG_1) << "Setting handler" << handler; + } +} diff --git a/.history/Subscription_20221016172643.cpp b/.history/Subscription_20221016172643.cpp new file mode 100644 index 0000000..0040988 --- /dev/null +++ b/.history/Subscription_20221016172643.cpp @@ -0,0 +1,79 @@ +#include "Subscription.h" +#include "Log.h" +#include "TCPSession.h" +#include + +namespace core +{ + + Subscription::Subscription(std::string id, std::string mode) + : id(id), mode(mode), owner(NULL) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode, std::string handlers) + : id(id), mode(mode), owner(&session), handlers(handlers) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode) + : id(id), mode(mode), owner(&session) {} + + Subscription::~Subscription() + { + std::stringstream out; + out << "cancel:" << id << std::endl; + for (auto subscriber : subscribers) + { + subscriber->write(out.str()); + } + } + + int Subscription::subscribe(TCPSession &session) + { + onSubscribe(session); + subscribers.push_back(&session); + return 1; + } + + int Subscription::unsubscribe(TCPSession &session) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + { + if (*subscriber == &session) + { + subscribers.erase(subscriber++); + return 1; + } + } + return 0; + } + + int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) + { + if (handler) + handler->process(request, out, session); + else + out << "event:" << request[1] << ":" << request[2] << std::endl; + return 1; + } + + int Subscription::event(std::stringstream &out) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + (*subscriber)->write(out.str()); + return 1; + } + + bool Subscription::ifSubscriber(TCPSession &session) + { + return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end()); + } + + int Subscription::onSubscribe(TCPSession &session) + { + return 0; + } + + void Subscription::setHandler(SubscriptionHandler *handler) + { + handler = handler; + coreutils::Log(coreutils::LOG_DEBUG_1) << "Setting handler" << handler; + } +} diff --git a/.history/Subscription_20221016173312.cpp b/.history/Subscription_20221016173312.cpp new file mode 100644 index 0000000..ee6d5ea --- /dev/null +++ b/.history/Subscription_20221016173312.cpp @@ -0,0 +1,79 @@ +#include "Subscription.h" +#include "Log.h" +#include "TCPSession.h" +#include + +namespace core +{ + + Subscription::Subscription(std::string id, std::string mode) + : id(id), mode(mode), owner(NULL) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode, std::string handlers) + : id(id), mode(mode), owner(&session), handlers(handlers) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode) + : id(id), mode(mode), owner(&session) {} + + Subscription::~Subscription() + { + std::stringstream out; + out << "cancel:" << id << std::endl; + for (auto subscriber : subscribers) + { + subscriber->write(out.str()); + } + } + + int Subscription::subscribe(TCPSession &session) + { + onSubscribe(session); + subscribers.push_back(&session); + return 1; + } + + int Subscription::unsubscribe(TCPSession &session) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + { + if (*subscriber == &session) + { + subscribers.erase(subscriber++); + return 1; + } + } + return 0; + } + + int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) + { + if (handler) + handler->process(request, out, session); + else + out << "event:" << request[1] << ":" << request[2] << std::endl; + return 1; + } + + int Subscription::event(std::stringstream &out) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + (*subscriber)->write(out.str()); + return 1; + } + + bool Subscription::ifSubscriber(TCPSession &session) + { + return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end()); + } + + int Subscription::onSubscribe(TCPSession &session) + { + return 0; + } + + void Subscription::setHandler(SubscriptionHandler *handler) + { + handler = handler; + coreutils::Log(coreutils::LOG_DEBUG_1) << "Setting handler"; + } +} diff --git a/.history/Subscription_20221016173458.cpp b/.history/Subscription_20221016173458.cpp new file mode 100644 index 0000000..b843257 --- /dev/null +++ b/.history/Subscription_20221016173458.cpp @@ -0,0 +1,79 @@ +#include "Subscription.h" +#include "Log.h" +#include "TCPSession.h" +#include + +namespace core +{ + + Subscription::Subscription(std::string id, std::string mode) + : id(id), mode(mode), owner(NULL) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode, std::string handlers) + : id(id), mode(mode), owner(&session), handlers(handlers) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode) + : id(id), mode(mode), owner(&session) {} + + Subscription::~Subscription() + { + std::stringstream out; + out << "cancel:" << id << std::endl; + for (auto subscriber : subscribers) + { + subscriber->write(out.str()); + } + } + + int Subscription::subscribe(TCPSession &session) + { + onSubscribe(session); + subscribers.push_back(&session); + return 1; + } + + int Subscription::unsubscribe(TCPSession &session) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + { + if (*subscriber == &session) + { + subscribers.erase(subscriber++); + return 1; + } + } + return 0; + } + + int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) + { + if (handler) + handler->process(request, out, session); + else + out << "event:" << request[1] << ":" << request[2] << std::endl; + return 1; + } + + int Subscription::event(std::stringstream &out) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + (*subscriber)->write(out.str()); + return 1; + } + + bool Subscription::ifSubscriber(TCPSession &session) + { + return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end()); + } + + int Subscription::onSubscribe(TCPSession &session) + { + return 0; + } + + void Subscription::setHandler(SubscriptionHandler *handler) + { + // handler = handler; + coreutils::Log(coreutils::LOG_DEBUG_1) << "Setting handler"; + } +} diff --git a/.history/Subscription_20221016173903.cpp b/.history/Subscription_20221016173903.cpp new file mode 100644 index 0000000..ee6d5ea --- /dev/null +++ b/.history/Subscription_20221016173903.cpp @@ -0,0 +1,79 @@ +#include "Subscription.h" +#include "Log.h" +#include "TCPSession.h" +#include + +namespace core +{ + + Subscription::Subscription(std::string id, std::string mode) + : id(id), mode(mode), owner(NULL) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode, std::string handlers) + : id(id), mode(mode), owner(&session), handlers(handlers) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode) + : id(id), mode(mode), owner(&session) {} + + Subscription::~Subscription() + { + std::stringstream out; + out << "cancel:" << id << std::endl; + for (auto subscriber : subscribers) + { + subscriber->write(out.str()); + } + } + + int Subscription::subscribe(TCPSession &session) + { + onSubscribe(session); + subscribers.push_back(&session); + return 1; + } + + int Subscription::unsubscribe(TCPSession &session) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + { + if (*subscriber == &session) + { + subscribers.erase(subscriber++); + return 1; + } + } + return 0; + } + + int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) + { + if (handler) + handler->process(request, out, session); + else + out << "event:" << request[1] << ":" << request[2] << std::endl; + return 1; + } + + int Subscription::event(std::stringstream &out) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + (*subscriber)->write(out.str()); + return 1; + } + + bool Subscription::ifSubscriber(TCPSession &session) + { + return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end()); + } + + int Subscription::onSubscribe(TCPSession &session) + { + return 0; + } + + void Subscription::setHandler(SubscriptionHandler *handler) + { + handler = handler; + coreutils::Log(coreutils::LOG_DEBUG_1) << "Setting handler"; + } +} diff --git a/.history/Subscription_20221016174406.cpp b/.history/Subscription_20221016174406.cpp new file mode 100644 index 0000000..0040988 --- /dev/null +++ b/.history/Subscription_20221016174406.cpp @@ -0,0 +1,79 @@ +#include "Subscription.h" +#include "Log.h" +#include "TCPSession.h" +#include + +namespace core +{ + + Subscription::Subscription(std::string id, std::string mode) + : id(id), mode(mode), owner(NULL) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode, std::string handlers) + : id(id), mode(mode), owner(&session), handlers(handlers) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode) + : id(id), mode(mode), owner(&session) {} + + Subscription::~Subscription() + { + std::stringstream out; + out << "cancel:" << id << std::endl; + for (auto subscriber : subscribers) + { + subscriber->write(out.str()); + } + } + + int Subscription::subscribe(TCPSession &session) + { + onSubscribe(session); + subscribers.push_back(&session); + return 1; + } + + int Subscription::unsubscribe(TCPSession &session) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + { + if (*subscriber == &session) + { + subscribers.erase(subscriber++); + return 1; + } + } + return 0; + } + + int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) + { + if (handler) + handler->process(request, out, session); + else + out << "event:" << request[1] << ":" << request[2] << std::endl; + return 1; + } + + int Subscription::event(std::stringstream &out) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + (*subscriber)->write(out.str()); + return 1; + } + + bool Subscription::ifSubscriber(TCPSession &session) + { + return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end()); + } + + int Subscription::onSubscribe(TCPSession &session) + { + return 0; + } + + void Subscription::setHandler(SubscriptionHandler *handler) + { + handler = handler; + coreutils::Log(coreutils::LOG_DEBUG_1) << "Setting handler" << handler; + } +} diff --git a/.history/Subscription_20221016174556.cpp b/.history/Subscription_20221016174556.cpp new file mode 100644 index 0000000..d62d2b5 --- /dev/null +++ b/.history/Subscription_20221016174556.cpp @@ -0,0 +1,78 @@ +#include "Subscription.h" +#include "Log.h" +#include "TCPSession.h" +#include + +namespace core +{ + + Subscription::Subscription(std::string id, std::string mode) + : id(id), mode(mode), owner(NULL) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode, std::string handlers) + : id(id), mode(mode), owner(&session), handlers(handlers) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode) + : id(id), mode(mode), owner(&session) {} + + Subscription::~Subscription() + { + std::stringstream out; + out << "cancel:" << id << std::endl; + for (auto subscriber : subscribers) + { + subscriber->write(out.str()); + } + } + + int Subscription::subscribe(TCPSession &session) + { + onSubscribe(session); + subscribers.push_back(&session); + return 1; + } + + int Subscription::unsubscribe(TCPSession &session) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + { + if (*subscriber == &session) + { + subscribers.erase(subscriber++); + return 1; + } + } + return 0; + } + + int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) + { + if (handler) + handler->process(request, out, session); + else + out << "event:" << request[1] << ":" << request[2] << std::endl; + return 1; + } + + int Subscription::event(std::stringstream &out) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + (*subscriber)->write(out.str()); + return 1; + } + + bool Subscription::ifSubscriber(TCPSession &session) + { + return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end()); + } + + int Subscription::onSubscribe(TCPSession &session) + { + return 0; + } + + void Subscription::setHandler(SubscriptionHandler *handler) + { + handler = handler; + } +} diff --git a/.history/Subscription_20221016174634.cpp b/.history/Subscription_20221016174634.cpp new file mode 100644 index 0000000..6b2e0b2 --- /dev/null +++ b/.history/Subscription_20221016174634.cpp @@ -0,0 +1,78 @@ +#include "Subscription.h" +#include "Log.h" +#include "TCPSession.h" +#include + +namespace core +{ + + Subscription::Subscription(std::string id, std::string mode) + : id(id), mode(mode), owner(NULL) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode, std::string handlers) + : id(id), mode(mode), owner(&session), handlers(handlers) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode) + : id(id), mode(mode), owner(&session) {} + + Subscription::~Subscription() + { + std::stringstream out; + out << "cancel:" << id << std::endl; + for (auto subscriber : subscribers) + { + subscriber->write(out.str()); + } + } + + int Subscription::subscribe(TCPSession &session) + { + onSubscribe(session); + subscribers.push_back(&session); + return 1; + } + + int Subscription::unsubscribe(TCPSession &session) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + { + if (*subscriber == &session) + { + subscribers.erase(subscriber++); + return 1; + } + } + return 0; + } + + int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) + { + if (handler) + handler->process(request, out, session); + else + out << "event:" << request[1] << ":" << request[2] << std::endl; + return 1; + } + + int Subscription::event(std::stringstream &out) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + (*subscriber)->write(out.str()); + return 1; + } + + bool Subscription::ifSubscriber(TCPSession &session) + { + return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end()); + } + + int Subscription::onSubscribe(TCPSession &session) + { + return 0; + } + + void Subscription::setHandler(SubscriptionHandler *handler) + { + handler = handler; + } +} diff --git a/.history/Subscription_20221016174652.cpp b/.history/Subscription_20221016174652.cpp new file mode 100644 index 0000000..ce23872 --- /dev/null +++ b/.history/Subscription_20221016174652.cpp @@ -0,0 +1,78 @@ +#include "Subscription.h" +#include "Log.h" +#include "TCPSession.h" +#include + +namespace core +{ + + Subscription::Subscription(std::string id, std::string mode) + : id(id), mode(mode), owner(NULL) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode, std::string handlers) + : id(id), mode(mode), owner(&session), handlers(handlers) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode) + : id(id), mode(mode), owner(&session) {} + + Subscription::~Subscription() + { + std::stringstream out; + out << "cancel:" << id << std::endl; + for (auto subscriber : subscribers) + { + subscriber->write(out.str()); + } + } + + int Subscription::subscribe(TCPSession &session) + { + onSubscribe(session); + subscribers.push_back(&session); + return 1; + } + + int Subscription::unsubscribe(TCPSession &session) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + { + if (*subscriber == &session) + { + subscribers.erase(subscriber++); + return 1; + } + } + return 0; + } + + int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) + { + if (handler) + handler->process(request, out, session); + else + out << "event:" << request[1] << ":" << request[2] << std::endl; + return 1; + } + + int Subscription::event(std::stringstream &out) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + (*subscriber)->write(out.str()); + return 1; + } + + bool Subscription::ifSubscriber(TCPSession &session) + { + return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end()); + } + + int Subscription::onSubscribe(TCPSession &session) + { + return 0; + } + + void Subscription::setHandler(SubscriptionHandler *handler) + { + handler = &handler; + } +} diff --git a/.history/Subscription_20221016174712.cpp b/.history/Subscription_20221016174712.cpp new file mode 100644 index 0000000..6b2e0b2 --- /dev/null +++ b/.history/Subscription_20221016174712.cpp @@ -0,0 +1,78 @@ +#include "Subscription.h" +#include "Log.h" +#include "TCPSession.h" +#include + +namespace core +{ + + Subscription::Subscription(std::string id, std::string mode) + : id(id), mode(mode), owner(NULL) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode, std::string handlers) + : id(id), mode(mode), owner(&session), handlers(handlers) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode) + : id(id), mode(mode), owner(&session) {} + + Subscription::~Subscription() + { + std::stringstream out; + out << "cancel:" << id << std::endl; + for (auto subscriber : subscribers) + { + subscriber->write(out.str()); + } + } + + int Subscription::subscribe(TCPSession &session) + { + onSubscribe(session); + subscribers.push_back(&session); + return 1; + } + + int Subscription::unsubscribe(TCPSession &session) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + { + if (*subscriber == &session) + { + subscribers.erase(subscriber++); + return 1; + } + } + return 0; + } + + int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) + { + if (handler) + handler->process(request, out, session); + else + out << "event:" << request[1] << ":" << request[2] << std::endl; + return 1; + } + + int Subscription::event(std::stringstream &out) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + (*subscriber)->write(out.str()); + return 1; + } + + bool Subscription::ifSubscriber(TCPSession &session) + { + return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end()); + } + + int Subscription::onSubscribe(TCPSession &session) + { + return 0; + } + + void Subscription::setHandler(SubscriptionHandler *handler) + { + handler = handler; + } +} diff --git a/.history/Subscription_20221016174852.h b/.history/Subscription_20221016174852.h new file mode 100644 index 0000000..6010ad0 --- /dev/null +++ b/.history/Subscription_20221016174852.h @@ -0,0 +1,49 @@ +#ifndef __Subscription_h__ +#define __Subscription_h__ + +#include "SubscriptionHandler.h" +#include "ZString.h" +#include +#include + +namespace core +{ + + class TCPSession; + + class Subscription + { + + public: + Subscription(std::string id, std::string mode = "*AUTHOR"); + Subscription(std::string id, TCPSession &session, std::string mode); + Subscription(std::string id, TCPSession &session, std::string mode, std::string handler); + virtual ~Subscription(); + + int subscribe(TCPSession &session); + int unsubscribe(TCPSession &session); + + virtual int process(coreutils::ZString &request, std::stringstream &out, TCPSession &session); + + virtual int onSubscribe(TCPSession &session); + + int event(std::stringstream &out); + + bool ifSubscriber(TCPSession &session); + + void setHandler(SubscriptionHandler *handler); + + // int processCommand(coreutils::ZString &request, TCPSession &session) override; + + std::string id; + std::string mode; + TCPSession *owner; + std::string handlers; + + SubscriptionHandler *handlers; + + std::vector subscribers; + }; +} + +#endif diff --git a/.history/Subscription_20221016174910.h b/.history/Subscription_20221016174910.h new file mode 100644 index 0000000..53fd588 --- /dev/null +++ b/.history/Subscription_20221016174910.h @@ -0,0 +1,49 @@ +#ifndef __Subscription_h__ +#define __Subscription_h__ + +#include "SubscriptionHandler.h" +#include "ZString.h" +#include +#include + +namespace core +{ + + class TCPSession; + + class Subscription + { + + public: + Subscription(std::string id, std::string mode = "*AUTHOR"); + Subscription(std::string id, TCPSession &session, std::string mode); + Subscription(std::string id, TCPSession &session, std::string mode, std::string handler); + virtual ~Subscription(); + + int subscribe(TCPSession &session); + int unsubscribe(TCPSession &session); + + virtual int process(coreutils::ZString &request, std::stringstream &out, TCPSession &session); + + virtual int onSubscribe(TCPSession &session); + + int event(std::stringstream &out); + + bool ifSubscriber(TCPSession &session); + + void setHandler(SubscriptionHandler *handler); + + // int processCommand(coreutils::ZString &request, TCPSession &session) override; + + std::string id; + std::string mode; + TCPSession *owner; + std::string handlers; + + SubscriptionHandler *handler; + + std::vector subscribers; + }; +} + +#endif diff --git a/.history/Subscription_20221016174913.h b/.history/Subscription_20221016174913.h new file mode 100644 index 0000000..b9e448b --- /dev/null +++ b/.history/Subscription_20221016174913.h @@ -0,0 +1,49 @@ +#ifndef __Subscription_h__ +#define __Subscription_h__ + +#include "SubscriptionHandler.h" +#include "ZString.h" +#include +#include + +namespace core +{ + + class TCPSession; + + class Subscription + { + + public: + Subscription(std::string id, std::string mode = "*AUTHOR"); + Subscription(std::string id, TCPSession &session, std::string mode); + Subscription(std::string id, TCPSession &session, std::string mode, std::string handler); + virtual ~Subscription(); + + int subscribe(TCPSession &session); + int unsubscribe(TCPSession &session); + + virtual int process(coreutils::ZString &request, std::stringstream &out, TCPSession &session); + + virtual int onSubscribe(TCPSession &session); + + int event(std::stringstream &out); + + bool ifSubscriber(TCPSession &session); + + void setHandler(SubscriptionHandler *handlers); + + // int processCommand(coreutils::ZString &request, TCPSession &session) override; + + std::string id; + std::string mode; + TCPSession *owner; + std::string handlers; + + SubscriptionHandler *handler; + + std::vector subscribers; + }; +} + +#endif diff --git a/.history/Subscription_20221016174921.cpp b/.history/Subscription_20221016174921.cpp new file mode 100644 index 0000000..e18853c --- /dev/null +++ b/.history/Subscription_20221016174921.cpp @@ -0,0 +1,78 @@ +#include "Subscription.h" +#include "Log.h" +#include "TCPSession.h" +#include + +namespace core +{ + + Subscription::Subscription(std::string id, std::string mode) + : id(id), mode(mode), owner(NULL) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode, std::string handlers) + : id(id), mode(mode), owner(&session), handlers(handlers) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode) + : id(id), mode(mode), owner(&session) {} + + Subscription::~Subscription() + { + std::stringstream out; + out << "cancel:" << id << std::endl; + for (auto subscriber : subscribers) + { + subscriber->write(out.str()); + } + } + + int Subscription::subscribe(TCPSession &session) + { + onSubscribe(session); + subscribers.push_back(&session); + return 1; + } + + int Subscription::unsubscribe(TCPSession &session) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + { + if (*subscriber == &session) + { + subscribers.erase(subscriber++); + return 1; + } + } + return 0; + } + + int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) + { + if (handler) + handler->process(request, out, session); + else + out << "event:" << request[1] << ":" << request[2] << std::endl; + return 1; + } + + int Subscription::event(std::stringstream &out) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + (*subscriber)->write(out.str()); + return 1; + } + + bool Subscription::ifSubscriber(TCPSession &session) + { + return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end()); + } + + int Subscription::onSubscribe(TCPSession &session) + { + return 0; + } + + void Subscription::setHandler(SubscriptionHandler *handlers) + { + handler = handler; + } +} diff --git a/.history/Subscription_20221016174923.cpp b/.history/Subscription_20221016174923.cpp new file mode 100644 index 0000000..21c5b39 --- /dev/null +++ b/.history/Subscription_20221016174923.cpp @@ -0,0 +1,78 @@ +#include "Subscription.h" +#include "Log.h" +#include "TCPSession.h" +#include + +namespace core +{ + + Subscription::Subscription(std::string id, std::string mode) + : id(id), mode(mode), owner(NULL) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode, std::string handlers) + : id(id), mode(mode), owner(&session), handlers(handlers) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode) + : id(id), mode(mode), owner(&session) {} + + Subscription::~Subscription() + { + std::stringstream out; + out << "cancel:" << id << std::endl; + for (auto subscriber : subscribers) + { + subscriber->write(out.str()); + } + } + + int Subscription::subscribe(TCPSession &session) + { + onSubscribe(session); + subscribers.push_back(&session); + return 1; + } + + int Subscription::unsubscribe(TCPSession &session) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + { + if (*subscriber == &session) + { + subscribers.erase(subscriber++); + return 1; + } + } + return 0; + } + + int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) + { + if (handler) + handler->process(request, out, session); + else + out << "event:" << request[1] << ":" << request[2] << std::endl; + return 1; + } + + int Subscription::event(std::stringstream &out) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + (*subscriber)->write(out.str()); + return 1; + } + + bool Subscription::ifSubscriber(TCPSession &session) + { + return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end()); + } + + int Subscription::onSubscribe(TCPSession &session) + { + return 0; + } + + void Subscription::setHandler(SubscriptionHandler *handlers) + { + handler = handlers; + } +} diff --git a/.history/Subscription_20221016175152.cpp b/.history/Subscription_20221016175152.cpp new file mode 100644 index 0000000..9f30eaf --- /dev/null +++ b/.history/Subscription_20221016175152.cpp @@ -0,0 +1,79 @@ +#include "Subscription.h" +#include "Log.h" +#include "TCPSession.h" +#include + +namespace core +{ + + Subscription::Subscription(std::string id, std::string mode) + : id(id), mode(mode), owner(NULL) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode, std::string handlers) + : id(id), mode(mode), owner(&session), handlers(handlers) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode) + : id(id), mode(mode), owner(&session) {} + + Subscription::~Subscription() + { + std::stringstream out; + out << "cancel:" << id << std::endl; + for (auto subscriber : subscribers) + { + subscriber->write(out.str()); + } + } + + int Subscription::subscribe(TCPSession &session) + { + onSubscribe(session); + subscribers.push_back(&session); + return 1; + } + + int Subscription::unsubscribe(TCPSession &session) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + { + if (*subscriber == &session) + { + subscribers.erase(subscriber++); + return 1; + } + } + return 0; + } + + int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) + { + if (handler) + handler->process(request, out, session); + else + out << "event:" << request[1] << ":" << request[2] << std::endl; + return 1; + } + + int Subscription::event(std::stringstream &out) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + (*subscriber)->write(out.str()); + return 1; + } + + bool Subscription::ifSubscriber(TCPSession &session) + { + return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end()); + } + + int Subscription::onSubscribe(TCPSession &session) + { + return 0; + } + + void Subscription::setHandler(SubscriptionHandler *handlers) + { + handler = handlers; + coreutils::Log(coreutils::LOG_DEBUG_1) << "Test Test"; + } +} diff --git a/.history/Subscription_20221016175838.cpp b/.history/Subscription_20221016175838.cpp new file mode 100644 index 0000000..33256cc --- /dev/null +++ b/.history/Subscription_20221016175838.cpp @@ -0,0 +1,80 @@ +#include "Subscription.h" +#include "Log.h" +#include "TCPSession.h" +#include + +namespace core +{ + + Subscription::Subscription(std::string id, std::string mode) + : id(id), mode(mode), owner(NULL) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode, std::string handlers) + : id(id), mode(mode), owner(&session), handlers(handlers) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode) + : id(id), mode(mode), owner(&session) {} + + Subscription::~Subscription() + { + std::stringstream out; + out << "cancel:" << id << std::endl; + for (auto subscriber : subscribers) + { + subscriber->write(out.str()); + } + } + + int Subscription::subscribe(TCPSession &session) + { + onSubscribe(session); + subscribers.push_back(&session); + return 1; + } + + int Subscription::unsubscribe(TCPSession &session) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + { + if (*subscriber == &session) + { + subscribers.erase(subscriber++); + return 1; + } + } + return 0; + } + + int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) + { + if (handler) + handler->process(request, out, session); + else + out << "event:" << request[1] << ":" << request[2] << std::endl; + return 1; + } + + int Subscription::event(std::stringstream &out) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + (*subscriber)->write(out.str()); + return 1; + } + + bool Subscription::ifSubscriber(TCPSession &session) + { + return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end()); + } + + int Subscription::onSubscribe(TCPSession &session) + { + return 0; + } + + void Subscription::setHandler(SubscriptionHandler *handlers) + { + handler = handlers; + + coreutils::Log(coreutils::LOG_DEBUG_1) << "Test Test"; + } +} diff --git a/.history/Subscription_20221016175855.cpp b/.history/Subscription_20221016175855.cpp new file mode 100644 index 0000000..6271e2e --- /dev/null +++ b/.history/Subscription_20221016175855.cpp @@ -0,0 +1,80 @@ +#include "Subscription.h" +#include "Log.h" +#include "TCPSession.h" +#include + +namespace core +{ + + Subscription::Subscription(std::string id, std::string mode) + : id(id), mode(mode), owner(NULL) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode, std::string handlers) + : id(id), mode(mode), owner(&session), handlers(handlers) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode) + : id(id), mode(mode), owner(&session) {} + + Subscription::~Subscription() + { + std::stringstream out; + out << "cancel:" << id << std::endl; + for (auto subscriber : subscribers) + { + subscriber->write(out.str()); + } + } + + int Subscription::subscribe(TCPSession &session) + { + onSubscribe(session); + subscribers.push_back(&session); + return 1; + } + + int Subscription::unsubscribe(TCPSession &session) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + { + if (*subscriber == &session) + { + subscribers.erase(subscriber++); + return 1; + } + } + return 0; + } + + int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) + { + if (handler) + handler->process(request, out, session); + else + out << "event:" << request[1] << ":" << request[2] << std::endl; + return 1; + } + + int Subscription::event(std::stringstream &out) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + (*subscriber)->write(out.str()); + return 1; + } + + bool Subscription::ifSubscriber(TCPSession &session) + { + return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end()); + } + + int Subscription::onSubscribe(TCPSession &session) + { + return 0; + } + + void Subscription::setHandler(SubscriptionHandler *handlers) + { + handler = handlers; + + return handler; + } +} diff --git a/.history/Subscription_20221016175911.cpp b/.history/Subscription_20221016175911.cpp new file mode 100644 index 0000000..33256cc --- /dev/null +++ b/.history/Subscription_20221016175911.cpp @@ -0,0 +1,80 @@ +#include "Subscription.h" +#include "Log.h" +#include "TCPSession.h" +#include + +namespace core +{ + + Subscription::Subscription(std::string id, std::string mode) + : id(id), mode(mode), owner(NULL) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode, std::string handlers) + : id(id), mode(mode), owner(&session), handlers(handlers) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode) + : id(id), mode(mode), owner(&session) {} + + Subscription::~Subscription() + { + std::stringstream out; + out << "cancel:" << id << std::endl; + for (auto subscriber : subscribers) + { + subscriber->write(out.str()); + } + } + + int Subscription::subscribe(TCPSession &session) + { + onSubscribe(session); + subscribers.push_back(&session); + return 1; + } + + int Subscription::unsubscribe(TCPSession &session) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + { + if (*subscriber == &session) + { + subscribers.erase(subscriber++); + return 1; + } + } + return 0; + } + + int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) + { + if (handler) + handler->process(request, out, session); + else + out << "event:" << request[1] << ":" << request[2] << std::endl; + return 1; + } + + int Subscription::event(std::stringstream &out) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + (*subscriber)->write(out.str()); + return 1; + } + + bool Subscription::ifSubscriber(TCPSession &session) + { + return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end()); + } + + int Subscription::onSubscribe(TCPSession &session) + { + return 0; + } + + void Subscription::setHandler(SubscriptionHandler *handlers) + { + handler = handlers; + + coreutils::Log(coreutils::LOG_DEBUG_1) << "Test Test"; + } +} diff --git a/.history/Subscription_20221016175912.cpp b/.history/Subscription_20221016175912.cpp new file mode 100644 index 0000000..33256cc --- /dev/null +++ b/.history/Subscription_20221016175912.cpp @@ -0,0 +1,80 @@ +#include "Subscription.h" +#include "Log.h" +#include "TCPSession.h" +#include + +namespace core +{ + + Subscription::Subscription(std::string id, std::string mode) + : id(id), mode(mode), owner(NULL) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode, std::string handlers) + : id(id), mode(mode), owner(&session), handlers(handlers) {} + + Subscription::Subscription(std::string id, TCPSession &session, std::string mode) + : id(id), mode(mode), owner(&session) {} + + Subscription::~Subscription() + { + std::stringstream out; + out << "cancel:" << id << std::endl; + for (auto subscriber : subscribers) + { + subscriber->write(out.str()); + } + } + + int Subscription::subscribe(TCPSession &session) + { + onSubscribe(session); + subscribers.push_back(&session); + return 1; + } + + int Subscription::unsubscribe(TCPSession &session) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + { + if (*subscriber == &session) + { + subscribers.erase(subscriber++); + return 1; + } + } + return 0; + } + + int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) + { + if (handler) + handler->process(request, out, session); + else + out << "event:" << request[1] << ":" << request[2] << std::endl; + return 1; + } + + int Subscription::event(std::stringstream &out) + { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) + (*subscriber)->write(out.str()); + return 1; + } + + bool Subscription::ifSubscriber(TCPSession &session) + { + return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end()); + } + + int Subscription::onSubscribe(TCPSession &session) + { + return 0; + } + + void Subscription::setHandler(SubscriptionHandler *handlers) + { + handler = handlers; + + coreutils::Log(coreutils::LOG_DEBUG_1) << "Test Test"; + } +} diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 7adc94f..6720a7b 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -7,7 +7,7 @@ "${workspaceFolder}/../CoreUtils" ], "defines": [], - "compilerPath": "/usr/bin/g++-9", + "compilerPath": "/usr/bin/clang-12", "cStandard": "c17", "cppStandard": "gnu++20", "intelliSenseMode": "windows-gcc-x64", diff --git a/CommandList.cpp b/CommandList.cpp index df97968..919836e 100644 --- a/CommandList.cpp +++ b/CommandList.cpp @@ -11,20 +11,21 @@ namespace core { void CommandList::remove(Command &command) {} - bool CommandList::processRequest(coreutils::ZString &request, TCPSession &session) { - if(session.grab != NULL) + int CommandList::processRequest(coreutils::ZString &request, TCPSession &session) { + if(session.grab != NULL) { return session.grab->processCommand(request, session); + } else { if(request.equals("")) - return false; - request.split(delimiter, 10); + return 0; + request.split(delimiter, depth); request.reset(); try { auto command = commands.at(request[0].str()); - return command->processCommand(request, session); + return command->processCommand(request, session); } catch(...) { - return false; + return 0; } } return true; @@ -40,12 +41,9 @@ namespace core { } int CommandList::processCommand(coreutils::ZString &request, TCPSession &session) { -// for(Command *command : commands) -// session.out << command->getName() << std::endl; + // for(Command *command : commands) + // session.out << command->getName() << std::endl; return true; } } - - - diff --git a/CommandList.h b/CommandList.h index 5d62707..acbc99d 100644 --- a/CommandList.h +++ b/CommandList.h @@ -40,7 +40,7 @@ namespace core { /// then control is given to the process handler holding the grab on the input. /// - bool processRequest(coreutils::ZString &request, TCPSession &session); + int processRequest(coreutils::ZString &request, TCPSession &session); /// /// Use grabInput() within a Command object to force the requesting handler to receive diff --git a/ConsoleSession.cpp b/ConsoleSession.cpp index a21affa..06627ee 100644 --- a/ConsoleSession.cpp +++ b/ConsoleSession.cpp @@ -10,6 +10,8 @@ namespace core { void ConsoleSession::protocol(coreutils::ZString &data) { + coreutils::Log(coreutils::LOG_DEBUG_1) << data; + coreutils::ZString blank(""); switch (status) { @@ -33,7 +35,7 @@ namespace core { out << "Password: "; status = WAIT_PASSWORD; break; - + case WAIT_PASSWORD: status = PROMPT; protocol(blank); diff --git a/EPoll.cpp b/EPoll.cpp index 9f669e9..15c57b9 100644 --- a/EPoll.cpp +++ b/EPoll.cpp @@ -67,16 +67,6 @@ namespace core { return terminateThreads; } - bool EPoll::registerSocket(Socket *socket) { - enableSocket(socket); - return true; - } - - bool EPoll::unregisterSocket(Socket *socket) { - disableSocket(socket); - return true; - } - int EPoll::getDescriptor() { return epfd; } @@ -91,24 +81,4 @@ namespace core { return 1; } - void EPoll::enableSocket(Socket *socket) { - struct epoll_event event; - event.data.ptr = socket; - event.events = EPOLLIN | EPOLLONESHOT | EPOLLRDHUP | EPOLLET; - epoll_ctl(epfd, EPOLL_CTL_ADD, socket->getDescriptor(), &event); - } - - void EPoll::disableSocket(Socket *socket) { - epoll_ctl(epfd, EPOLL_CTL_DEL, socket->getDescriptor(), NULL); - } - - void EPoll::resetSocket(Socket *socket) { - struct epoll_event event; - event.data.ptr = socket; - event.events = EPOLLIN | EPOLLONESHOT | EPOLLRDHUP | EPOLLET; - if(socket->needsToWrite()) - event.events |= EPOLLWRNORM; - epoll_ctl(epfd, EPOLL_CTL_MOD, socket->getDescriptor(), &event); - } - } diff --git a/EPoll.h b/EPoll.h index dcd389b..d1440e3 100644 --- a/EPoll.h +++ b/EPoll.h @@ -32,6 +32,8 @@ namespace core { public: + volatile long long eventId = 0; + /// /// The constructor for the BMAEPoll object. /// @@ -41,9 +43,9 @@ namespace core { /// /// The destructor for the BMAEPoll object. /// - + ~EPoll(); - + /// /// Use the start() method to initiate the threads and begin epoll queue processing. /// @@ -67,24 +69,7 @@ namespace core { /// bool isStopping(); ///< Returns a true if the stop command has been requested. - - /// - /// Use registerSocket to add a new socket to the ePoll event watch list. This enables - /// a new BMASocket object to receive events when data is received as well as to write - /// data output to the socket. - /// - /// @param socket a pointer to a BMASocket object. - /// @return a booelean that indicates the socket was registered or not. - /// - - bool registerSocket(Socket *socket); ///< Register a BMASocket for monitoring by BMAEPoll. - - /// - /// Use this method to remove a socket from receiving events from the epoll system. - /// - - bool unregisterSocket(Socket *socket); ///< Unregister a BMASocket from monitoring by BMAEPoll. - + /// /// Use this method to obtain the current descriptor socket number for the epoll function call. /// @@ -109,22 +94,18 @@ namespace core { /// /// @param session the session to write the requested data to. /// - + int processCommand(coreutils::ZString &request, TCPSession &session) override; /// threads; - volatile bool terminateThreads; - void enableSocket(Socket *socket); - void disableSocket(Socket *socket); - + int numberOfThreads; + std::vector threads; + volatile bool terminateThreads; + }; - + } #endif diff --git a/INotify.cpp b/INotify.cpp index da82e79..010435c 100644 --- a/INotify.cpp +++ b/INotify.cpp @@ -1,17 +1,18 @@ #include "INotify.h" #include "Log.h" +#include "ZString.h" namespace core { - + INotify::INotify(EPoll &ePoll) : Socket(ePoll, "INotify") { - setDescriptor(inotify_init()); - } - + setDescriptor(inotify_init()); + } + INotify::~INotify() { shutdown(); } - - int INotify::addWatch(std::string watch) { + + int INotify::addWatch(coreutils::ZString &watch) { return inotify_add_watch(getDescriptor(), watch.c_str(), IN_ALL_EVENTS); } @@ -22,37 +23,37 @@ namespace core { void INotify::onDataReceived(coreutils::ZString &buffer) { const struct inotify_event *event; char *ptr; - for (ptr = buffer.getData(); ptr < buffer.getData() + buffer.getLength(); + for (ptr = buffer.getData(); + ptr < buffer.getData() + buffer.getLength(); ptr += sizeof(struct inotify_event) + event->len) { event = (const struct inotify_event *) ptr; - - if(event->mask & IN_ACCESS) - inAccess(std::string(event->name)); - if(event->mask & IN_ATTRIB) + coreutils::ZString name(event->name); + + if(event->mask & IN_ACCESS) + inAccess(name); + if(event->mask & IN_ATTRIB) inAttrib(std::string(event->name)); - if(event->mask & IN_CLOSE_WRITE) + if(event->mask & IN_CLOSE_WRITE) inCloseWrite(std::string(event->name)); - if(event->mask & IN_CLOSE_NOWRITE) + if(event->mask & IN_CLOSE_NOWRITE) inCloseNoWrite(std::string(event->name)); - if(event->mask & IN_CREATE) - inCreate(std::string(event->name)); - if(event->mask & IN_DELETE) + if(event->mask & IN_CREATE) + inCreate(name); + if(event->mask & IN_DELETE) inDelete(std::string(event->name)); - if(event->mask & IN_DELETE_SELF) + if(event->mask & IN_DELETE_SELF) inDeleteSelf(std::string(event->name)); - if(event->mask & IN_MODIFY) + if(event->mask & IN_MODIFY) inModify(std::string(event->name)); - if(event->mask & IN_MOVE_SELF) + if(event->mask & IN_MOVE_SELF) inMoveSelf(std::string(event->name)); - if(event->mask & IN_MOVED_FROM) + if(event->mask & IN_MOVED_FROM) inMovedFrom(std::string(event->name)); - if(event->mask & IN_MOVED_TO) + if(event->mask & IN_MOVED_TO) inMovedTo(std::string(event->name)); - if(event->mask & IN_OPEN) + if(event->mask & IN_OPEN) inOpen(std::string(event->name)); - - } + } } - - + } diff --git a/INotify.h b/INotify.h index b63abf9..4961710 100644 --- a/INotify.h +++ b/INotify.h @@ -5,33 +5,33 @@ #include "Socket.h" namespace core { - + class INotify : Socket { - + public: INotify(EPoll &ePoll); ~INotify(); - - int addWatch(std::string watch); + + int addWatch(coreutils::ZString &watch); void removeWatch(int wd); - void onDataReceived(coreutils::ZString &data) override; + void onDataReceived(coreutils::ZString &data) override; - virtual void inAccess(std::string name) {} - virtual void inAttrib(std::string name) {} + virtual void inAccess(coreutils::ZString name) {} + virtual void inAttrib(std::string name) {} virtual void inCloseWrite(std::string name) {} virtual void inCloseNoWrite(std::string name) {} - virtual void inCreate(std::string name) {} - virtual void inDelete(std::string name) {} - virtual void inDeleteSelf(std::string name) {} - virtual void inModify(std::string name) {} - virtual void inMoveSelf(std::string name) {} - virtual void inMovedFrom(std::string name) {} - virtual void inMovedTo(std::string name) {} - virtual void inOpen(std::string name) {} - + virtual void inCreate(coreutils::ZString &name) {} + virtual void inDelete(std::string name) {} + virtual void inDeleteSelf(std::string name) {} + virtual void inModify(std::string name) {} + virtual void inMoveSelf(std::string name) {} + virtual void inMovedFrom(std::string name) {} + virtual void inMovedTo(std::string name) {} + virtual void inOpen(std::string name) {} + }; - + } #endif diff --git a/IPAddress.cpp b/IPAddress.cpp index c4f5e69..af35ce6 100644 --- a/IPAddress.cpp +++ b/IPAddress.cpp @@ -1,37 +1,37 @@ #include "IPAddress.h" namespace core { - + IPAddress::IPAddress() { addressLength = sizeof(addr); } - IPAddress::IPAddress(std::string address) { - std::string url = address.substr(0, address.find(":")); - std::string s_port = address.substr(address.find(":") + 1); - std::stringstream convert(s_port); - short int port = 0; - convert >> port; - IPAddress(url, port); - } - - IPAddress::IPAddress(std::string address, int port) { + IPAddress::IPAddress(std::string address) { + std::string url = address.substr(0, address.find(":")); + std::string s_port = address.substr(address.find(":") + 1); + std::stringstream convert(s_port); + short int port = 0; + convert >> port; + IPAddress(url, port); + } + + IPAddress::IPAddress(std::string address, int port) { memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(port); struct hostent *hp = gethostbyname(address.c_str()); memcpy((void *)&addr.sin_addr, hp->h_addr_list[0], hp->h_length); addressLength = sizeof(addr); - } - + } + IPAddress::~IPAddress() { } - - struct sockaddr * IPAddress::getPointer() { - return (sockaddr *)&addr; - } - + + struct sockaddr * IPAddress::getPointer() { + return (sockaddr *)&addr; + } + std::string IPAddress::getClientAddress() { std::string result; return result; @@ -45,9 +45,9 @@ namespace core { } int IPAddress::getClientPort() { - int result = -1; + int result = -1; return result; } - + } diff --git a/IPAddress.h b/IPAddress.h index 4beca82..d1029f7 100644 --- a/IPAddress.h +++ b/IPAddress.h @@ -7,20 +7,20 @@ namespace core { class IPAddress : public Object { - + public: IPAddress(); IPAddress(std::string address); IPAddress(std::string address, int port); ~IPAddress(); - - struct sockaddr_in addr; + + struct sockaddr_in addr; socklen_t addressLength; struct sockaddr * getPointer(); std::string getClientAddress(); ///descriptor = descriptor; onRegister(); - ePoll.registerSocket(this); + enableSocket(); onRegistered(); } - int Socket::getDescriptor() { + int Socket::getDescriptor() + { return descriptor; } - void Socket::setBufferSize(int length) { + void Socket::setBufferSize(int length) + { this->length = length; buffer = (char *)realloc(buffer, length); - } - int Socket::getBufferSize() { + int Socket::getBufferSize() + { return length; } @@ -60,102 +67,141 @@ namespace core { void Socket::onUnregistered() {} - bool Socket::eventReceived(struct epoll_event event) { + bool Socket::eventReceived(struct epoll_event event, long long eventId) { +// coreutils::Log(coreutils::LOG_DEBUG_1) << "Event process beginning for socket " << getDescriptor(); + if(inHandler) + // coreutils::Log(coreutils::LOG_DEBUG_2) << "inHandler was already true."; inHandler = true; if(event.events & EPOLLRDHUP) { +// coreutils::Log(coreutils::LOG_DEBUG_2) << "EPOLLRDHUP"; readHangup = true; shutdown("hangup received"); } - else if(event.events & EPOLLIN) { + if(event.events & EPOLLIN) { +// coreutils::Log(coreutils::LOG_DEBUG_2) << "EPOLLIN"; coreutils::ZString zbuffer(buffer, length); - receiveData(zbuffer); + lock.lock(); + receiveData(zbuffer); + if(!shutDown) { + inHandler = false; + lock.unlock(); + resetSocket(); + } } - else if(event.events & EPOLLWRNORM) { - writeSocket(); + if(event.events & EPOLLWRNORM) { +// coreutils::Log(coreutils::LOG_DEBUG_2) << "EPOLLWRNORM"; + writeSocket(); + inHandler = false; + resetSocket(); } - else if(event.events & EPOLLHUP) { - shutdown(); - } - inHandler = false; + inHandler = false; +// coreutils::Log(coreutils::LOG_DEBUG_1) << "Event process ending for socket " << getDescriptor(); return !shutDown; } - - void Socket::onDataReceived(std::string data) { + + void Socket::onDataReceived(std::string data) + { throw coreutils::Exception("Need to override onDataReceived.", __FILE__, __LINE__, -1); } - void Socket::onDataReceived(coreutils::ZString &data) { + void Socket::onDataReceived(coreutils::ZString &data) + { onDataReceived(std::string(data.getData(), data.getLength())); } void Socket::receiveData(coreutils::ZString &buffer) { - coreutils::ZString blank(""); - if(buffer.getLength() <= 0) throw coreutils::Exception("Request to receive data with a zero buffer length.", __FILE__, __LINE__, -1); - int len; int error = -1; - if((len = ::read(getDescriptor(), buffer.getData(), buffer.getLength())) >= 0) { coreutils::ZString zbuffer(buffer.getData(), len); - coreutils::Log(coreutils::LOG_DEBUG_1) << zbuffer; - onDataReceived(zbuffer); +// coreutils::Log(coreutils::LOG_DEBUG_1) << zbuffer; + onDataReceived(zbuffer); } - else { + else + { - error = errno; + error = errno; - switch (error) { + switch (error) + { - // When a listening socket receives a connection - // request we get one of these. - // - case ENOTCONN: + // When a listening socket receives a connection + // request we get one of these. + // + case ENOTCONN: onDataReceived(blank); - break; - - case ECONNRESET: break; - default: + case ECONNRESET: + break; + + default: throw coreutils::Exception("Error in read of data from socket.", __FILE__, __LINE__, error); - } + } } } void Socket::writeSocket() { + outlock.lock(); +// coreutils::Log(coreutils::LOG_DEBUG_3) << "writing data to socket " << getDescriptor(); if(fifo.size() > 0) { - outlock.lock(); - if(!shutDown) - ::write(descriptor, fifo.front().c_str(), fifo.front().length()); + if(!shutDown) + int rc = ::write(descriptor, fifo.front().c_str(), fifo.front().length()); fifo.pop(); - outlock.unlock(); } + outlock.unlock(); } int Socket::write(std::string data) { outlock.lock(); fifo.emplace(data); outlock.unlock(); - if(!inHandler) - ePoll.resetSocket(this); + if(lock.try_lock()) { + resetSocket(); + lock.unlock(); + } return 1; } - void Socket::output(std::stringstream &out) { + void Socket::output(std::stringstream &out) + { out << "|" << descriptor << "|"; } - bool Socket::needsToWrite() { + bool Socket::needsToWrite() + { return fifo.size() > 0; } void Socket::shutdown(std::string text) { - coreutils::Log(coreutils::LOG_DEBUG_2) << "Shutdown requested on socket " << descriptor << " with reason " << text << "."; + coreutils::Log(coreutils::LOG_DEBUG_2) << "Shutdown requested from " << this->text << " (" << descriptor << ") with reason: " << text << "."; shutDown = true; reset = false; } + void Socket::enableSocket() { + struct epoll_event event; + event.data.ptr = this; + event.events = EPOLLIN | EPOLLRDHUP | EPOLLONESHOT | EPOLLET; + epoll_ctl(ePoll.getDescriptor(), EPOLL_CTL_ADD, getDescriptor(), &event); + } + + void Socket::disableSocket() { + epoll_ctl(ePoll.getDescriptor(), EPOLL_CTL_DEL, getDescriptor(), NULL); + } + + void Socket::resetSocket() { + struct epoll_event event; + event.data.ptr = this; + event.events = EPOLLIN | EPOLLRDHUP | EPOLLONESHOT | EPOLLET; + if(fifo.size() > 0) + event.events |= EPOLLWRNORM; + if(!shutDown) + epoll_ctl(ePoll.getDescriptor(), EPOLL_CTL_MOD, getDescriptor(), &event); + } + + } diff --git a/Socket.h b/Socket.h index d0049c0..fbd6395 100644 --- a/Socket.h +++ b/Socket.h @@ -60,39 +60,39 @@ namespace core { /// /// setDescriptor establishes the file descriptor for the socket and registers the socket - /// on the EPoll controller. setDescriptor will invoke the onRegister() event. + /// on the EPoll controller. setDescriptor will invoke the onRegister() event. /// - + void setDescriptor(int descriptor); /// fifo; - + + void resetSocket(); + + std::mutex lock; + }; } diff --git a/Subscription.cpp b/Subscription.cpp index ce40556..e3df1a8 100644 --- a/Subscription.cpp +++ b/Subscription.cpp @@ -1,40 +1,42 @@ #include "Subscription.h" -#include "TCPSession.h" #include "Log.h" +#include "TCPSession.h" #include -namespace core -{ +namespace core { Subscription::Subscription(std::string id, std::string mode) - : id(id), mode(mode), owner(NULL) {} + : id(id), mode(mode), owner(NULL), handler(NULL) {} Subscription::Subscription(std::string id, TCPSession &session, std::string mode) - : id(id), mode(mode), owner(&session) {} + : id(id), mode(mode), owner(&session), handler(NULL) {} - Subscription::~Subscription() - { + Subscription::Subscription(std::string id, TCPSession &session, std::string mode, SubscriptionHandler *handler) + : id(id), mode(mode), owner(&session), handler(handler) { +// coreutils::Log(coreutils::LOG_DEBUG_3) << "Subscription '" << id << "' with handler '" << handler->name << "'"; + } + + Subscription::~Subscription() { std::stringstream out; out << "cancel:" << id << std::endl; - for (auto subscriber : subscribers) - { + for (auto subscriber : subscribers) { subscriber->write(out.str()); } } - int Subscription::subscribe(TCPSession &session) - { - onSubscribe(session); + int Subscription::subscribe(TCPSession &session) { + if (handler) + handler->onSubscribe(session, this); + else + onSubscribe(session); + subscribers.push_back(&session); return 1; } - int Subscription::unsubscribe(TCPSession &session) - { - for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) - { - if (*subscriber == &session) - { + int Subscription::unsubscribe(TCPSession &session) { + for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) { + if (*subscriber == &session) { subscribers.erase(subscriber++); return 1; } @@ -42,27 +44,38 @@ namespace core return 0; } - int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) - { - out << "event:" << request[1] << ":" << request[2] << std::endl; + int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) { + if (handler) + handler->process(request, out, session, this); + else + out << "event:" << request[1] << ":" << request[2] << std::endl; return 1; } - int Subscription::event(std::stringstream &out) - { + int Subscription::event(std::stringstream &out) { for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) (*subscriber)->write(out.str()); return 1; } - bool Subscription::ifSubscriber(TCPSession &session) - { + bool Subscription::ifSubscriber(TCPSession &session) { return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end()); } - int Subscription::onSubscribe(TCPSession &session) - { + int Subscription::onSubscribe(TCPSession &session) { return 0; } + bool Subscription::subInvite(TCPSession &session) { + return 0; + } + + void Subscription::sendToAll(std::stringstream &data, TCPSession &sender) { + for (auto session : subscribers) + if (session != &sender) + session->write(data.str()); + data.str(""); + } + + } diff --git a/Subscription.h b/Subscription.h index 371b6c4..2ce5da7 100644 --- a/Subscription.h +++ b/Subscription.h @@ -1,21 +1,23 @@ #ifndef __Subscription_h__ #define __Subscription_h__ +#include "SubscriptionHandler.h" +#include "SessionFilter.h" #include "ZString.h" -#include #include +#include namespace core { class TCPSession; - class Subscription - { + class Subscription { - public: + public: Subscription(std::string id, std::string mode = "*AUTHOR"); - Subscription(std::string id, TCPSession &session, std::string mode = "*AUTHOR"); + Subscription(std::string id, TCPSession &session, std::string mode); + Subscription(std::string id, TCPSession &session, std::string mode, SubscriptionHandler *handler); virtual ~Subscription(); int subscribe(TCPSession &session); @@ -29,12 +31,17 @@ namespace core bool ifSubscriber(TCPSession &session); - // int processCommand(coreutils::ZString &request, TCPSession &session) override; + bool subInvite(TCPSession &session); + + void sendToAll(std::stringstream &data, TCPSession &sender); + void sendToAll(std::stringstream &data, TCPSession &sender, SessionFilter filter); std::string id; std::string mode; TCPSession *owner; + SubscriptionHandler *handler; + std::vector subscribers; }; } diff --git a/SubscriptionHandler.h b/SubscriptionHandler.h new file mode 100644 index 0000000..ef34d40 --- /dev/null +++ b/SubscriptionHandler.h @@ -0,0 +1,22 @@ +#ifndef __SubscriptionHandler_h__ +#define __SubscriptionHandler_h__ + +#include "ZString.h" +#include +#include +#include + +namespace core { + class Subscription; + class TCPSession; + + class SubscriptionHandler { + + public: + virtual int process(coreutils::ZString &request, std::stringstream &out, TCPSession &session, Subscription *subscription) { return 0; } + virtual int onSubscribe(TCPSession &session, Subscription *subscription) { return 0; } + + }; +} + +#endif diff --git a/SubscriptionHandlerFactory.h b/SubscriptionHandlerFactory.h new file mode 100644 index 0000000..54bad32 --- /dev/null +++ b/SubscriptionHandlerFactory.h @@ -0,0 +1,21 @@ +#ifndef __SubscriptionHandlerFactory_h__ +#define __SubscriptionHandlerFactory_h__ + +#include "SubscriptionHandler.h" +#include + +namespace core { + + class SubscriptionHandlerFactory { + + public: + + virtual SubscriptionHandler * getSubscriptionHandler(std::string name) { + return new SubscriptionHandler(); + } + + }; + +} + +#endif diff --git a/SubscriptionManager.cpp b/SubscriptionManager.cpp index 44c2468..02b348f 100644 --- a/SubscriptionManager.cpp +++ b/SubscriptionManager.cpp @@ -1,85 +1,104 @@ #include "SubscriptionManager.h" #include "Log.h" +#include "Subscription.h" +#include "TCPServer.h" #include +#include "SubscriptionHandlerFactory.h" namespace core { - SubscriptionManager::SubscriptionManager() {} - - int SubscriptionManager::add(Subscription &subscription) { - lock.lock(); - subscriptions.insert(std::make_pair(subscription.id, &subscription)); - lock.unlock(); - return 1; - } - + int SubscriptionManager::add(Subscription &subscription) { + lock.lock(); + subscriptions.insert(std::make_pair(subscription.id, &subscription)); + lock.unlock(); + return 1; + } + int SubscriptionManager::removeSessionSubscriptions(TCPSession &session) { int countSubscribed = 0; int countPublished = 0; - lock.lock(); + lock.lock(); std::string temp = ""; - for(auto [key, subscription] : subscriptions) { - if(temp != "") { + for (auto [key, subscription] : subscriptions) + { + if (temp != "") + { subscriptions.erase(temp); temp = ""; } countSubscribed += subscription->unsubscribe(session); - if(subscription->owner == &session) { + if (subscription->owner == &session) + { temp = key; delete subscription; ++countPublished; - } + } } - if(temp != "") { + if (temp != "") + { subscriptions.erase(temp); temp = ""; } coreutils::Log(coreutils::LOG_DEBUG_2) << "Removed session from " << countSubscribed << " subscription(s)."; coreutils::Log(coreutils::LOG_DEBUG_2) << "Cancelled " << countPublished << " channel(s) for session."; - lock.unlock(); + lock.unlock(); return countSubscribed; } int SubscriptionManager::processCommand(coreutils::ZString &request, TCPSession &session) { - if(request[0].equals("publish")) { - Subscription *newSubscription = new Subscription(request[1].str(), session, request[2].str()); - subscriptions.insert(std::make_pair(request[1].str(), newSubscription)); - return 1; - } else if(request[0].equals("catalog")) { - session.out << ":catalog:"; - for(auto const& [key, subscription] : subscriptions) { - session.out << subscription->id << ";"; + if (request[0].equals("publish")) { + SubscriptionHandler *handler = NULL; + if(request.getList().size() > 3) { + factory->getSubscriptionHandler(request[3].str()); } - session.out << std::endl; - return 1; + newSubscription = new Subscription(request[1].str(), session, request[2].str(), handler); + subscriptions.insert(std::make_pair(request[1].str(), newSubscription)); + newSubscription->owner = &session; + return 1; + } else if (request[0].equals("catalog")) { + session.out << ":catalog:"; + for (auto const &[key, subscription] : subscriptions) { + session.out << subscription->id << ";"; + } + session.out << std::endl; + return 1; + } else if (request[0].equals("invite")) { + std::stringstream out; + coreutils::Log(coreutils::LOG_DEBUG_1) << request[2]; + std::string invitee = request[2].str(); + TCPSession *tempSession = session.server.getSessionByAlias(&invitee); + std::stringstream temp; + temp << "invite:" << request[1] << ":" << *(std::string *)session.alias; + tempSession->write(temp.str()); + return 1; } auto subscription = subscriptions[request[1].str()]; - if(request[1].equals(subscription->id)) { - if(request[0].equals("unpublish")) { - subscriptions.erase(request[1].str()); - } else if(request[0].equals("subscribe")) { - subscription->subscribe(session); + if (request[1].equals(subscription->id)) { + if (request[0].equals("unpublish")) { + subscriptions.erase(request[1].str()); + } else if (request[0].equals("subscribe")) { + subscription->subscribe(session); return 1; - } else if(request[0].equals("unsubscribe")) { - subscription->unsubscribe(session); + } else if (request[0].equals("unsubscribe")) { + subscription->unsubscribe(session); return 1; - } else if(request[0].equals("event")) { + } else if (request[0].equals("event")) { std::stringstream out; subscription->process(request, out, session); - if(subscription->mode == "*ANYONE") { - subscription->event(out); + if (subscription->mode == "*ANYONE") { + subscription->event(out); return 1; - } else if(subscription->mode == "*SUBSCRIBERS") { - if(subscription->ifSubscriber(session)) { - subscription->event(out); + } else if (subscription->mode == "*SUBSCRIBERS") { + if (subscription->ifSubscriber(session)) { + subscription->event(out); return 1; } - } else if(subscription->mode == "*AUTHOR") { - if(subscription->owner == &session) { - subscription->event(out); + } else if (subscription->mode == "*AUTHOR") { + if (subscription->owner == &session) { + subscription->event(out); return 1; } } @@ -88,5 +107,4 @@ namespace core { } return 0; } - } diff --git a/SubscriptionManager.h b/SubscriptionManager.h index 6fa042a..b8a754f 100644 --- a/SubscriptionManager.h +++ b/SubscriptionManager.h @@ -1,31 +1,35 @@ #ifndef __SubscriptionManager_h__ #define __SubscriptionManager_h__ -#include "TCPSession.h" -#include "Subscription.h" #include "Command.h" +#include "Subscription.h" +#include "SubscriptionHandler.h" +#include "TCPSession.h" #include "ZString.h" -#include #include +#include namespace core { + class SubscriptionHandlerFactory; + class SubscriptionManager : public Command { public: - SubscriptionManager(); - - int add(Subscription &subscription); + int add(Subscription &subscription); int removeSessionSubscriptions(TCPSession &session); - int processCommand(coreutils::ZString &request, TCPSession &session) override; + SubscriptionHandlerFactory *factory = NULL; + private: + Subscription *subscription; std::map subscriptions; - std::mutex lock; - + Subscription *newSubscription; + std::mutex lock; }; + } #endif diff --git a/TCPServer.cpp b/TCPServer.cpp index 24adf2d..7fb5554 100644 --- a/TCPServer.cpp +++ b/TCPServer.cpp @@ -1,34 +1,36 @@ #include "TCPServer.h" #include "EPoll.h" -#include "TCPSession.h" #include "Exception.h" #include "Log.h" +#include "TCPSession.h" -namespace core { +namespace core +{ TCPServer::TCPServer(EPoll &ePoll, IPAddress address, std::string delimiter, int depth, std::string text) - : TCPSocket(ePoll, text), commands(delimiter, depth) { + : TCPSocket(ePoll, text), commands(delimiter, depth) { - commands.add(subscriptions, "publish"); - commands.add(subscriptions, "unpublish"); - commands.add(subscriptions, "subscribe"); - commands.add(subscriptions, "unsubscribe"); - commands.add(subscriptions, "catalog"); - commands.add(subscriptions, "event"); + commands.add(subscriptions, "publish"); + commands.add(subscriptions, "unpublish"); + commands.add(subscriptions, "subscribe"); + commands.add(subscriptions, "unsubscribe"); + commands.add(subscriptions, "catalog"); + commands.add(subscriptions, "event"); + commands.add(subscriptions, "invite"); - setDescriptor(socket(AF_INET, SOCK_STREAM, 0)); - int yes = 1; - setsockopt(getDescriptor(), SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)); + setDescriptor(socket(AF_INET, SOCK_STREAM, 0)); + int yes = 1; + setsockopt(getDescriptor(), SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)); - if(bind(getDescriptor(), address.getPointer(), address.addressLength) < 0) - throw coreutils::Exception("Error on bind to socket: " + std::to_string(errno)); - - if(listen(getDescriptor(), 20) < 0) - throw coreutils::Exception("Error on listen to socket"); + if (bind(getDescriptor(), address.getPointer(), address.addressLength) < 0) + throw coreutils::Exception("Error on bind to socket: " + std::to_string(errno)); + if (listen(getDescriptor(), 20) < 0) + throw coreutils::Exception("Error on listen to socket"); } - TCPServer::~TCPServer() { + TCPServer::~TCPServer() + { coreutils::Log(coreutils::LOG_DEBUG_2) << "Closing server socket " << getDescriptor() << "."; close(getDescriptor()); } @@ -36,8 +38,8 @@ namespace core { void TCPServer::onDataReceived(std::string data) { lock.lock(); TCPSession *session = accept(); - if(session) - sessions.push_back(session); + if (session) + sessions.push_back(session); lock.unlock(); } @@ -47,22 +49,24 @@ namespace core { TCPSession *session = getSocketAccept(ePoll); session->setDescriptor(::accept(getDescriptor(), (struct sockaddr *)&session->ipAddress.addr, &session->ipAddress.addressLength)); - // if(blackList && blackList->contains(session->ipAddress.getClientAddress())) { - // session->shutdown(); - // Log(LOG_WARN) << "Client at IP address " << session->ipAddress.getClientAddress() << " is blacklisted and was denied a connection."; - // return NULL; - // } - // if(whiteList && !whiteList->contains(session->ipAddress.getClientAddress())) { - // session->shutdown(); - // Log(LOG_WARN) << "Client at IP address " << session->ipAddress.getClientAddress() << " is not authorized and was denied a connection."; - // return NULL; - // } + // if(blackList && blackList->contains(session->ipAddress.getClientAddress())) { + // session->shutdown(); + // Log(LOG_WARN) << "Client at IP address " << session->ipAddress.getClientAddress() << " is blacklisted and was denied a connection."; + // return NULL; + // } + // if(whiteList && !whiteList->contains(session->ipAddress.getClientAddress())) { + // session->shutdown(); + // Log(LOG_WARN) << "Client at IP address " << session->ipAddress.getClientAddress() << " is not authorized and was denied a connection."; + // return NULL; + // } return session; } - catch(coreutils::Exception e) { + catch (coreutils::Exception e) + { coreutils::Log(coreutils::LOG_EXCEPT) << "Major error on session initialization. Error is '" << e.text << "'."; } - catch(...) { + catch (...) + { coreutils::Log(coreutils::LOG_EXCEPT) << "Unnspecified error on session initialization."; } return NULL; @@ -70,13 +74,13 @@ namespace core { void TCPServer::removeFromSessionList(TCPSession *session) { std::vector::iterator cursor; - lock.lock(); + lock.lock(); for(cursor = sessions.begin(); cursor < sessions.end(); ++cursor) - if(*cursor == session) { - sessions.erase(cursor); - break; - } - lock.unlock(); + if(*cursor == session) { + sessions.erase(cursor); + break; + } + lock.unlock(); } void TCPServer::sessionErrorHandler(std::string errorString, std::stringstream &out) { @@ -93,33 +97,46 @@ namespace core { int TCPServer::processCommand(coreutils::ZString &request, TCPSession &session) { int sequence = 0; - for(auto *sessionx : sessions) { + for (auto *sessionx : sessions) + { session.out << "|" << ++sequence; sessionx->output(session.out); session.out << "|" << std::endl; } return 1; } - - void TCPServer::sendToAll(std::stringstream &data) { - for(auto session : sessions) - session->write(data.str()); + + void TCPServer::sendToAll(std::stringstream &data) + { + for (auto session : sessions) + session->write(data.str()); data.str(""); } - - void TCPServer::sendToAll(std::stringstream &data, TCPSession &sender) { - for(auto session : sessions) - if(session != &sender) - session->write(data.str()); + + void TCPServer::sendToAll(std::stringstream &data, TCPSession &sender) + { + for (auto session : sessions) + if (session != &sender) + session->write(data.str()); data.str(""); } - - void TCPServer::sendToAll(std::stringstream &data, TCPSession &sender, SessionFilter filter) { - for(auto session : sessions) - if(filter.test(*session)) - if(session != &sender) - session->write(data.str()); + + void TCPServer::sendToAll(std::stringstream &data, TCPSession &sender, SessionFilter filter) + { + for (auto session : sessions) + if (filter.test(*session)) + if (session != &sender) + session->write(data.str()); data.str(""); } - + + TCPSession *TCPServer::getSessionByAlias(void *alias) + { + coreutils::Log(coreutils::LOG_DEBUG_1) << alias; + for (auto session : sessions) + if (session->compareAlias(alias)) + return session; + return NULL; + } + } diff --git a/TCPServer.h b/TCPServer.h index 98aa8f3..2a85e10 100644 --- a/TCPServer.h +++ b/TCPServer.h @@ -1,149 +1,155 @@ #ifndef __TCPServer_h__ #define __TCPServer_h__ -#include "Socket.h" -#include "TCPSocket.h" -#include "IPAddressList.h" #include "Command.h" #include "CommandList.h" +#include "IPAddressList.h" +#include "Socket.h" #include "SubscriptionManager.h" +#include "TCPSession.h" +#include "TCPSocket.h" -namespace core { +namespace core +{ - /// - /// TCPServer - /// - /// Manage a socket connection as a TCP server type. Connections to the socket are processed through - /// the accept functionality. - /// - /// A list of connections is maintained in a vector object. - /// - /// This object extends the BMACommand object as well so it can be added to a Console object and - /// process commands to display status information. - /// + /// + /// TCPServer + /// + /// Manage a socket connection as a TCP server type. Connections to the socket are processed through + /// the accept functionality. + /// + /// A list of connections is maintained in a vector object. + /// + /// This object extends the BMACommand object as well so it can be added to a Console object and + /// process commands to display status information. + /// - class TCPServer : public TCPSocket, public Command { + class TCPServer : public TCPSocket, public Command + { - public: + public: + /// + /// The constructor for the TCPServer object. + /// + /// @param ePoll the EPoll instance that manages the socket. + /// @param url the IP address for the socket to receive connection requests. + /// @param port the port number that the socket will listen on. + /// @param commandName the name of the command used to invoke the status display for this object. + /// - /// - /// The constructor for the TCPServer object. - /// - /// @param ePoll the EPoll instance that manages the socket. - /// @param url the IP address for the socket to receive connection requests. - /// @param port the port number that the socket will listen on. - /// @param commandName the name of the command used to invoke the status display for this object. - /// + TCPServer(EPoll &ePoll, IPAddress address, std::string delimiter = " ", int depth = 10, std::string text = ""); - TCPServer(EPoll &ePoll, IPAddress address, std::string delimiter = " ", int depth = 10, std::string text = ""); + /// + /// The destructor for this object. + /// - /// - /// The destructor for this object. - /// + virtual ~TCPServer(); - virtual ~TCPServer(); + virtual void sessionErrorHandler(std::string errorString, std::stringstream &out); - virtual void sessionErrorHandler(std::string errorString, std::stringstream &out); + /// + /// getSocketAccept is designed to allow a polymorphic extension of this object to + /// return a type of object that extends the definition of the server socket. + /// Returning the appropriate session object that extends from Session provides + /// the mechanism where the server can select the protocol dialog for the desired + /// service. + /// - /// - /// getSocketAccept is designed to allow a polymorphic extension of this object to - /// return a type of object that extends the definition of the server socket. - /// Returning the appropriate session object that extends from Session provides - /// the mechanism where the server can select the protocol dialog for the desired - /// service. - /// + virtual TCPSession *getSocketAccept(EPoll &epoll); - virtual TCPSession * getSocketAccept(EPoll &epoll); + /// + /// The list of sessions that are currently open and being maintained by this object. + /// - /// - /// The list of sessions that are currently open and being maintained by this object. - /// + std::vector sessions; - std::vector sessions; + /// + /// The commands object is a CommandList and is used to store Command objects to be + /// parsed and run as data comes into the session. + /// - /// - /// The commands object is a CommandList and is used to store Command objects to be - /// parsed and run as data comes into the session. - /// + CommandList commands; - CommandList commands; + /// + /// If not NULL the blacklist object can be assigned to this server socket and the server + /// IP addresses connecting to the server attempting to accept a socket are contained in + /// this list then the connection is rejected and no accept is granted. + /// - /// - /// If not NULL the blacklist object can be assigned to this server socket and the server - /// IP addresses connecting to the server attempting to accept a socket are contained in - /// this list then the connection is rejected and no accept is granted. - /// + IPAddressList *blackList; - IPAddressList *blackList; + /// + /// If not NULL the blacklist object can be assigned to this server socket and the server + /// IP addresses connecting to the server attempting to accept a socket are contained in + /// this list then the connection is rejected and no accept is granted. + /// - /// - /// If not NULL the blacklist object can be assigned to this server socket and the server - /// IP addresses connecting to the server attempting to accept a socket are contained in - /// this list then the connection is rejected and no accept is granted. - /// + IPAddressList *whiteList; - IPAddressList *whiteList; + void removeFromSessionList(TCPSession *session); - void removeFromSessionList(TCPSession *session); + void output(std::stringstream &out); ///< Output the consoles array to the console. - void output(std::stringstream &out); ///alias = alias; + } + + void TCPSession::outputAlias(std::stringstream &out) { + out << alias; + } + void TCPSession::onRegistered() { onConnected(); - coreutils::ZString blank(""); - protocol(blank); send(); - if(term) - shutdown("termination requested"); + if (term) + shutdown("termination requested"); } void TCPSession::onConnected() {} void TCPSession::onDataReceived(coreutils::ZString &data) { - if(data.getLength() > 0) { - lineBuffer = (char *)realloc(lineBuffer, lineBufferSize + data.getLength()); - memcpy(lineBuffer + lineBufferSize, data.getData(), data.getLength()); - lineBufferSize += data.getLength(); - while(lineBufferSize > 0) { - if(blockSize == 0) { - lineLength = strcspn(lineBuffer, "\r\n"); - if(lineLength == lineBufferSize) - break; + if (data.getLength() > 0) { + lineBuffer = (char *)realloc(lineBuffer, lineBufferSize + data.getLength()); + memcpy(lineBuffer + lineBufferSize, data.getData(), data.getLength()); + lineBufferSize += data.getLength(); + while (lineBufferSize > 0) { + if (blockSize == 0) { + lineLength = strcspn(lineBuffer, "\r\n"); + if (lineLength == lineBufferSize) + break; coreutils::ZString zLine(lineBuffer, lineLength); - onLineReceived(zLine); - if(lineBuffer[lineLength] == '\r') - ++lineLength; - if(lineBuffer[lineLength] == '\n') - ++lineLength; - lineBufferSize -= lineLength; - if(lineBufferSize > 0) - memmove(lineBuffer, lineBuffer + lineLength, lineBufferSize); - lineBuffer = (char *)realloc(lineBuffer, lineBufferSize); - } else if(lineBufferSize >= blockLength) { - coreutils::ZString zBlock(lineBuffer, blockLength); - onBlockReceived(zBlock); - lineBufferSize -= blockLength; - if(lineBufferSize > 0) - memmove(lineBuffer, lineBuffer + blockLength, lineBufferSize); - lineBuffer = (char *)realloc(lineBuffer, lineBufferSize); - } - } + onLineReceived(zLine); + if (lineBuffer[lineLength] == '\r') + ++lineLength; + if (lineBuffer[lineLength] == '\n') + ++lineLength; + lineBufferSize -= lineLength; + if (lineBufferSize > 0) + memmove(lineBuffer, lineBuffer + lineLength, lineBufferSize); + lineBuffer = (char *)realloc(lineBuffer, lineBufferSize); + } + else if (lineBufferSize >= blockLength) + { + coreutils::ZString zBlock(lineBuffer, blockLength); + onBlockReceived(zBlock); + lineBufferSize -= blockLength; + if (lineBufferSize > 0) + memmove(lineBuffer, lineBuffer + blockLength, lineBufferSize); + lineBuffer = (char *)realloc(lineBuffer, lineBufferSize); + } + } } } - void TCPSession::setBlockSize(int blockSize) { + void TCPSession::setBlockSize(int blockSize) + { this->blockSize = blockSize; } - void TCPSession::onLineReceived(coreutils::ZString &line) { + void TCPSession::onLineReceived(coreutils::ZString &line) + { protocol(line); send(); - if(term) - shutdown("termination requested"); + if (term) + shutdown("termination requested"); } - void TCPSession::onBlockReceived(coreutils::ZString &block) { + void TCPSession::onBlockReceived(coreutils::ZString &block) + { coreutils::Log(coreutils::LOG_DEBUG_3) << "[" << block.getLength() << "]"; - if(term) - shutdown("termination requested"); + if (term) + shutdown("termination requested"); } void TCPSession::send() { if(out.tellp() > 0) - write(out.str()); + write(out.str()); out.str(""); } - void TCPSession::terminate() { + void TCPSession::terminate() + { term = true; } diff --git a/TCPSession.h b/TCPSession.h index 61a26b9..88074d3 100644 --- a/TCPSession.h +++ b/TCPSession.h @@ -1,142 +1,165 @@ #ifndef __Session_h__ -# define __Session_h__ +#define __Session_h__ -#include "TCPSocket.h" #include "SessionFilter.h" +#include "TCPSocket.h" -namespace core { +namespace core +{ - class Command; - class TCPServer; + class Command; + class TCPServer; - /// - /// TCPSession - /// - /// TCPSession defines the nature of the interaction with the client - /// and stores persistent data for a defined session. TCPSession objects - /// are not sockets but instead provide a communications control - /// mechanism. Protocol conversations are provided through extensions - /// from this object. - /// - /// - /// + /// + /// TCPSession + /// + /// TCPSession defines the nature of the interaction with the client + /// and stores persistent data for a defined session. TCPSession objects + /// are not sockets but instead provide a communications control + /// mechanism. Protocol conversations are provided through extensions + /// from this object. + /// + /// + /// - class TCPSession : public TCPSocket { + class TCPSession : public TCPSocket + { - public: + public: + /// + /// + /// - /// - /// - /// + TCPSession(EPoll &ePoll, TCPServer &server, std::string text = ""); - TCPSession(EPoll &ePoll, TCPServer &server, std::string text = ""); + /// + /// + /// - /// - /// - /// + virtual ~TCPSession(); - virtual ~TCPSession(); + Command *grab = NULL; - Command *grab = NULL; + virtual void output(std::stringstream &data); - virtual void output(std::stringstream &data); + /// + /// The send method is used to output the contents of the out stream + /// to the session containing the stream. + /// - /// - /// The send method is used to output the contents of the out stream - /// to the session containing the stream. - /// + void send(); - void send(); + /// + /// Use this method to terminate this TCPSession. + /// - /// - /// Use this method to terminate this TCPSession. - /// + void terminate(); - void terminate(); + /// + /// + /// - /// - /// - /// + TCPServer &server; - TCPServer &server; + /// + /// Use out to send data to the session socket or other session sockets. + /// - /// - /// Use out to send data to the session socket or other session sockets. - /// + std::stringstream out; - std::stringstream out; + /// + /// uuid is generated automatically when the session object is instantiated. This + /// value can be used to uniquely identify a session and is the default value + /// pointed to by the alias pointer. + /// - protected: + char uuid[37]; - /// - /// - /// + /// + /// alias is a void pointer that can be set to point to any object that identifies + /// this session uniquely. Using this approach, inheriting objects can determine + /// how it knows the contacts that this server manages. + /// - virtual void onRegistered() override; + void *alias; - /// - /// Override this method to receive data directly from the socket as data is - /// received. If you need data split by line termination characters then - /// override the onLineReceived method instead. - /// - virtual void onDataReceived(coreutils::ZString &data) override; + /// + /// + /// - /// - /// Override the onLineReceived method to receive a string of characters that - /// represents a single line of data terminated by a LF or CRLF. If onDataReceived - /// was overriden this method will not be called unless the onDataReceived calls - /// this method explicitly using the class and member name. - /// + virtual bool compareAlias(void *alias); - virtual void onLineReceived(coreutils::ZString &line); + virtual void outputAlias(std::stringstream &out); - /// - /// Override the onBlockReceived method to receive a string of characters that - /// represents a single block of data of length determined by the block length value. If - /// onDataReceived was overriden this method will not be called unless the onDataReceived - /// calls this method explicitly using the class and member name. - /// + protected: + /// + /// + /// - virtual void onBlockReceived(coreutils::ZString &block); + virtual void onRegistered() override; - /// - /// This method is called from within the protocol method when protocol is called - /// on the initial connection where the data is an empty string. Use this method - /// to deliver a message to the connection upon connection. - /// + /// + /// Override this method to receive data directly from the socket as data is + /// received. If you need data split by line termination characters then + /// override the onLineReceived method instead. + /// + virtual void onDataReceived(coreutils::ZString &data) override; - virtual void onConnected(); + /// + /// Override the onLineReceived method to receive a string of characters that + /// represents a single line of data terminated by a LF or CRLF. If onDataReceived + /// was overriden this method will not be called unless the onDataReceived calls + /// this method explicitly using the class and member name. + /// - /// - /// Override the protocol method to manage and control the session communications - /// in your inherited session. If you do not override this method then the Session - /// default will process the 'commands' added to the server object using the - /// processRequest method on the session input. - /// - /// When data is received within the session two modes are available to pass the - /// data through the protocol method: LINE or BLOCK. - /// + virtual void onLineReceived(coreutils::ZString &line); - virtual void protocol(coreutils::ZString &data); + /// + /// Override the onBlockReceived method to receive a string of characters that + /// represents a single block of data of length determined by the block length value. If + /// onDataReceived was overriden this method will not be called unless the onDataReceived + /// calls this method explicitly using the class and member name. + /// - /// - /// Use setBlockSize to set the amount of data that should be read at once from the - /// session data buffer. - /// If this value is set to 0 then the data will be retrieved - /// + virtual void onBlockReceived(coreutils::ZString &block); - void setBlockSize(int size = 0); + /// + /// This method is called from within the protocol method when protocol is called + /// on the initial connection where the data is an empty string. Use this method + /// to deliver a message to the connection upon connection. + /// - private: - char *lineBuffer = NULL; - int lineBufferSize = 0; - int lineLength = 0; - int blockLength = 0; - std::mutex mtx; - bool term = false; - int blockSize = 0; + virtual void onConnected(); - }; + /// + /// Override the protocol method to manage and control the session communications + /// in your inherited session. If you do not override this method then the Session + /// default will process the 'commands' added to the server object using the + /// processRequest method on the session input. + /// + /// When data is received within the session two modes are available to pass the + /// data through the protocol method: LINE or BLOCK. + /// + + virtual void protocol(coreutils::ZString &data); + + /// + /// Use setBlockSize to set the amount of data that should be read at once from the + /// session data buffer. + /// If this value is set to 0 then the data will be retrieved + /// + + void setBlockSize(int size = 0); + + private: + char *lineBuffer = NULL; + int lineBufferSize = 0; + int lineLength = 0; + int blockLength = 0; + std::mutex mtx; + bool term = false; + int blockSize = 0; + }; } diff --git a/TCPSession2.cpp b/TCPSession2.cpp new file mode 100644 index 0000000..af272aa --- /dev/null +++ b/TCPSession2.cpp @@ -0,0 +1,85 @@ +#include "TCPSession2.h" +#include "Exception.h" +#include "Log.h" + +namespace core { + + TCPSession2::TCPSession2(EPoll &ePoll, std::string text) : TCPSocket(ePoll, text) {} + + TCPSession2::~TCPSession2() {} + + void TCPSession2::output(std::stringstream &data) { + data << "|" << ipAddress.getClientAddressAndPort(); + } + + void TCPSession2::protocol(coreutils::ZString &data) {} + + void TCPSession2::onRegistered() { + onConnected(); + send(); + if(term) + TCPSocket::shutdown("termination requested"); + } + + void TCPSession2::onConnected() {} + + void TCPSession2::onDataReceived(coreutils::ZString &data) { + if(data.getLength() > 0) { + lineBuffer = (char *)realloc(lineBuffer, lineBufferSize + data.getLength()); + memcpy(lineBuffer + lineBufferSize, data.getData(), data.getLength()); + lineBufferSize += data.getLength(); + while(lineBufferSize > 0) { + if(blockSize == 0) { + lineLength = strcspn(lineBuffer, "\r\n"); + if(lineLength == lineBufferSize) + break; + coreutils::ZString zLine(lineBuffer, lineLength); + onLineReceived(zLine); + if(lineBuffer[lineLength] == '\r') + ++lineLength; + if(lineBuffer[lineLength] == '\n') + ++lineLength; + lineBufferSize -= lineLength; + if(lineBufferSize > 0) + memmove(lineBuffer, lineBuffer + lineLength, lineBufferSize); + lineBuffer = (char *)realloc(lineBuffer, lineBufferSize); + } else if(lineBufferSize >= blockLength) { + coreutils::ZString zBlock(lineBuffer, blockLength); + onBlockReceived(zBlock); + lineBufferSize -= blockLength; + if(lineBufferSize > 0) + memmove(lineBuffer, lineBuffer + blockLength, lineBufferSize); + lineBuffer = (char *)realloc(lineBuffer, lineBufferSize); + } + } + } + } + + void TCPSession2::setBlockSize(int blockSize) { + this->blockSize = blockSize; + } + + void TCPSession2::onLineReceived(coreutils::ZString &line) { + protocol(line); + send(); + if(term) + TCPSocket::shutdown("termination requested"); + } + + void TCPSession2::onBlockReceived(coreutils::ZString &block) { + coreutils::Log(coreutils::LOG_DEBUG_3) << "[" << block.getLength() << "]"; + if(term) + TCPSocket::shutdown("termination requested"); + } + + void TCPSession2::send() { + if(out.tellp() > 0) + TCPSocket::write(out.str()); + out.str(""); + } + + void TCPSession2::terminate() { + term = true; + } + +} diff --git a/TCPSession2.h b/TCPSession2.h new file mode 100644 index 0000000..8ad4a20 --- /dev/null +++ b/TCPSession2.h @@ -0,0 +1,139 @@ +#ifndef __TCPSession2_h__ +# define __TCPSession2_h__ + +#include "TCPSocket.h" +#include "Timer.h" +#include "SessionFilter.h" + +namespace core { + + class Command; + class TCPServer; + + /// + /// TCPSession2 + /// + /// TCPSession defines the nature of the interaction with the client + /// and stores persistent data for a defined session. TCPSession objects + /// are not sockets but instead provide a communications control + /// mechanism. Protocol conversations are provided through extensions + /// from this object. + /// + /// TCPSession2 is designed to be 'connected' instead of being served + /// by a server. + /// + + class TCPSession2 : public TCPSocket { + + public: + + /// + /// + /// + + TCPSession2(EPoll &ePoll, std::string text = ""); + + /// + /// + /// + + virtual ~TCPSession2(); + + Command *grab = NULL; + + virtual void output(std::stringstream &data); + + /// + /// The send method is used to output the contents of the out stream + /// to the session containing the stream. + /// + + void send(); + + /// + /// Use this method to terminate this TCPSession. + /// + + void terminate(); + + /// + /// Use out to send data to the session socket or other session sockets. + /// + + std::stringstream out; + + protected: + + /// + /// + /// + + virtual void onRegistered() override; + + /// + /// Override this method to receive data directly from the socket as data is + /// received. If you need data split by line termination characters then + /// override the onLineReceived method instead. + /// + virtual void onDataReceived(coreutils::ZString &data) override; + + /// + /// Override the onLineReceived method to receive a string of characters that + /// represents a single line of data terminated by a LF or CRLF. If onDataReceived + /// was overriden this method will not be called unless the onDataReceived calls + /// this method explicitly using the class and member name. + /// + + virtual void onLineReceived(coreutils::ZString &line); + + /// + /// Override the onBlockReceived method to receive a string of characters that + /// represents a single block of data of length determined by the block length value. If + /// onDataReceived was overriden this method will not be called unless the onDataReceived + /// calls this method explicitly using the class and member name. + /// + + virtual void onBlockReceived(coreutils::ZString &block); + + /// + /// This method is called from within the protocol method when protocol is called + /// on the initial connection where the data is an empty string. Use this method + /// to deliver a message to the connection upon connection. + /// + + virtual void onConnected(); + + /// + /// Override the protocol method to manage and control the session communications + /// in your inherited session. If you do not override this method then the Session + /// default will process the 'commands' added to the server object using the + /// processRequest method on the session input. + /// + /// When data is received within the session two modes are available to pass the + /// data through the protocol method: LINE or BLOCK. + /// + + virtual void protocol(coreutils::ZString &data); + + /// + /// Use setBlockSize to set the amount of data that should be read at once from the + /// session data buffer. + /// If this value is set to 0 then the data will be retrieved + /// + + void setBlockSize(int size = 0); + + private: + char *lineBuffer = NULL; + int lineBufferSize = 0; + int lineLength = 0; + int blockLength = 0; + std::mutex mtx; + bool term = false; + int blockSize = 0; + + }; + +} + +#endif diff --git a/TCPSocket.cpp b/TCPSocket.cpp index 3345032..20afa4a 100644 --- a/TCPSocket.cpp +++ b/TCPSocket.cpp @@ -2,6 +2,7 @@ #include "EPoll.h" #include "Log.h" #include "Exception.h" +#include "errno.h" namespace core { @@ -14,7 +15,8 @@ namespace core { void TCPSocket::connect(IPAddress &address) { setDescriptor(socket(AF_INET, SOCK_STREAM, 0)); if(::connect(getDescriptor(), (struct sockaddr *)&address.addr, address.addressLength) == -1) - throw coreutils::Exception("Error on connect to TCP socket."); + throw coreutils::Exception("Error on connect to TCP socket." + errno); + coreutils::Log(coreutils::LOG_DEBUG_3) << "Connected to IP..." << address.getClientAddressAndPort(); } void TCPSocket::output(std::stringstream &out) { diff --git a/Thread.cpp b/Thread.cpp index d942b51..2a7628b 100644 --- a/Thread.cpp +++ b/Thread.cpp @@ -60,9 +60,12 @@ namespace core { } else if(rc > 0) { for(int ix = 0; ix < rc; ++ix) { ++count; - if(((Socket *)events[ix].data.ptr)->eventReceived(events[ix])) { - ePoll.resetSocket((Socket *)events[ix].data.ptr); - } else { + if(((Socket *)events[ix].data.ptr)->eventReceived(events[ix], ++ePoll.eventId)) { +// coreutils::Log(coreutils::LOG_DEBUG_4) << "return true"; +// ePoll.resetSocket((Socket *)events[ix].data.ptr); + } else { + ((Socket *)events[ix].data.ptr)->shutDown = true; +// coreutils::Log(coreutils::LOG_DEBUG_4) << "return false"; delete (Socket *)events[ix].data.ptr; } } diff --git a/Thread.h b/Thread.h index 023123a..41baa8e 100644 --- a/Thread.h +++ b/Thread.h @@ -10,7 +10,7 @@ namespace core { class EPoll; - + /// /// Thread /// @@ -18,37 +18,37 @@ namespace core { /// object to allow maintaining a status value for monitoring the thread activity. EPoll will instantiate /// a Thread object for each thread specified in the EPoll's start method. /// - + class Thread : public Object { - - public: + + public: Thread(EPoll &ePoll); Thread(EPoll &ePoll, ThreadScope *thread); ~Thread(); - + /// /// Start the thread object. This will cause the epoll scheduler to commence reading the epoll queue. /// - + void start(); void join(); - std::string getStatus(); + std::string getStatus(); pid_t getThreadId(); int getCount(); - void output(std::stringstream &data); - + void output(std::stringstream &data); + private: EPoll &ePoll; // The EPoll control object. std::string status; - int count; + int count; std::thread *_thread; void print_thread_start_log(); pid_t threadId; - void run(); + void run(); ThreadScope *thread; - + }; - + } #endif diff --git a/Timer.cpp b/Timer.cpp index 8f74c8d..9b3d536 100644 --- a/Timer.cpp +++ b/Timer.cpp @@ -1,61 +1,60 @@ #include "Timer.h" namespace core { - + Timer::Timer(EPoll &ePoll, double delay = 0.0f) : Socket(ePoll, "Timer") { setDescriptor(timerfd_create(CLOCK_REALTIME, 0)); setTimer(delay); } - + Timer::~Timer() { } - + void Timer::setTimer(double delay) { - + double integer; double fraction; struct itimerspec timer; - + delayValue = delay; - + timer.it_interval.tv_sec = 0; timer.it_interval.tv_nsec = 0; - + fraction = modf(delay, &integer); - + timer.it_value.tv_sec = (int)integer; timer.it_value.tv_nsec = (int)(fraction * 1000000000); - + timerfd_settime(getDescriptor(), 0, &timer, NULL); - + } - + void Timer::clearTimer() { - + struct itimerspec timer; - + timer.it_interval.tv_sec = 0; timer.it_interval.tv_nsec = 0; timer.it_value.tv_sec = 0; timer.it_value.tv_nsec = 0; - + timerfd_settime(getDescriptor(), 0, &timer, NULL); - } - + double Timer::getElapsed() { struct itimerspec timer; timerfd_gettime(getDescriptor(), &timer); double toTimeout = (double)((timer.it_value.tv_sec * 1000000000L) + timer.it_value.tv_nsec) / 1000000000L; return delayValue - toTimeout; } - - void Timer::onDataReceived(std::string data) { + + void Timer::onDataReceived(std::string data) { onTimeout(); - } - + } + double Timer::getEpoch() { return (double)std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count() /1000000000L; } - + } diff --git a/UDPServerSocket.cpp b/UDPServerSocket.cpp index efc9b47..9458698 100644 --- a/UDPServerSocket.cpp +++ b/UDPServerSocket.cpp @@ -24,7 +24,7 @@ namespace core { listen(getDescriptor(), 10); - ePoll.registerSocket(this); +// ePoll.registerSocket(this); } diff --git a/compile b/compile index ca6498e..65f0717 100755 --- a/compile +++ b/compile @@ -31,4 +31,5 @@ echo -n "Building library documentation manual..." doxygen docs/latex/doxygen.sty >/dev/null 2>/dev/null echo "OK" -rm *.o \ No newline at end of file +rm *.o +rm *~ diff --git a/output/main b/output/main index 71318cc..9e69513 100755 Binary files a/output/main and b/output/main differ