Merge branch 'master' into develop
This commit is contained in:
commit
bbf904729f
4
.gitignore
vendored
4
.gitignore
vendored
@ -3,8 +3,12 @@ Release
|
||||
*.o
|
||||
*~
|
||||
*.mk
|
||||
.history
|
||||
libServerCore.a
|
||||
docs/latex/
|
||||
docs/html
|
||||
*/*.ipch
|
||||
*/mmap_address.bin
|
||||
.history/*
|
||||
html
|
||||
latex
|
68
.history/SubscriptionHandler_20221013185305.cpp
Normal file
68
.history/SubscriptionHandler_20221013185305.cpp
Normal file
@ -0,0 +1,68 @@
|
||||
#include "Subscription.h"
|
||||
#include "TCPSession.h"
|
||||
#include "Log.h"
|
||||
#include <algorithm>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
24
.history/SubscriptionHandler_20221013191001.h
Normal file
24
.history/SubscriptionHandler_20221013191001.h
Normal file
@ -0,0 +1,24 @@
|
||||
#ifndef __SubscriptionHandler_h__
|
||||
#define __SubscriptionHandler_h__
|
||||
|
||||
#include "ZString.h"
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
namespace core
|
||||
{
|
||||
|
||||
class SubscriptionHandler
|
||||
{
|
||||
|
||||
public:
|
||||
SubscriptionHandler();
|
||||
|
||||
virtual int process(coreutils::ZString &request, std::stringstream &out, TCPSession &session);
|
||||
|
||||
virtual int onSubscribe(TCPSession &session);
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
23
.history/SubscriptionHandler_20221013204338.h
Normal file
23
.history/SubscriptionHandler_20221013204338.h
Normal file
@ -0,0 +1,23 @@
|
||||
#ifndef __SubscriptionHandler_h__
|
||||
#define __SubscriptionHandler_h__
|
||||
|
||||
#include "ZString.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace core
|
||||
{
|
||||
|
||||
class SubscriptionHandler
|
||||
{
|
||||
|
||||
public:
|
||||
SubscriptionHandler();
|
||||
|
||||
virtual int process(coreutils::ZString &request, std::stringstream &out, TCPSession &session);
|
||||
|
||||
virtual int onSubscribe(TCPSession &session);
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
68
.history/SubscriptionHandler_20221013204409.cpp
Normal file
68
.history/SubscriptionHandler_20221013204409.cpp
Normal file
@ -0,0 +1,68 @@
|
||||
#include "SubscriptionHandler.h"
|
||||
#include "Log.h"
|
||||
#include "TCPSession.h"
|
||||
#include <algorithm>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
23
.history/SubscriptionHandler_20221013204504.h
Normal file
23
.history/SubscriptionHandler_20221013204504.h
Normal file
@ -0,0 +1,23 @@
|
||||
#ifndef __SubscriptionHandler_h__
|
||||
#define __SubscriptionHandler_h__
|
||||
|
||||
#include "ZString.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace core
|
||||
{
|
||||
|
||||
class SubscriptionHandler
|
||||
{
|
||||
|
||||
public:
|
||||
SubscriptionHandler();
|
||||
|
||||
virtual int process(coreutils::ZString &request, std::stringstream &out, TCPSession &session);
|
||||
|
||||
virtual int onSubscribe(TCPSession &session);
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
68
.history/SubscriptionHandler_20221013204612.cpp
Normal file
68
.history/SubscriptionHandler_20221013204612.cpp
Normal file
@ -0,0 +1,68 @@
|
||||
#include "Log.h"
|
||||
#include "Subscription.h"
|
||||
#include "TCPSession.h"
|
||||
#include <algorithm>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
22
.history/SubscriptionHandler_20221014152050.h
Normal file
22
.history/SubscriptionHandler_20221014152050.h
Normal file
@ -0,0 +1,22 @@
|
||||
#ifndef __SubscriptionHandler_h__
|
||||
#define __SubscriptionHandler_h__
|
||||
|
||||
#include "TCPSession.h"
|
||||
#include "ZString.h"
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace core
|
||||
{
|
||||
|
||||
class SubscriptionHandler
|
||||
{
|
||||
|
||||
public:
|
||||
virtual int process(coreutils::ZString &request, std::stringstream &out, TCPSession &session);
|
||||
virtual int onSubscribe(TCPSession &session){};
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
22
.history/SubscriptionHandler_20221014152131.h
Normal file
22
.history/SubscriptionHandler_20221014152131.h
Normal file
@ -0,0 +1,22 @@
|
||||
#ifndef __SubscriptionHandler_h__
|
||||
#define __SubscriptionHandler_h__
|
||||
|
||||
#include "TCPSession.h"
|
||||
#include "ZString.h"
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace core
|
||||
{
|
||||
|
||||
class SubscriptionHandler
|
||||
{
|
||||
|
||||
public:
|
||||
virtual int process(coreutils::ZString &request, std::stringstream &out, TCPSession &session);
|
||||
virtual int onSubscribe(TCPSession &session) {}
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
22
.history/SubscriptionHandler_20221014152241.h
Normal file
22
.history/SubscriptionHandler_20221014152241.h
Normal file
@ -0,0 +1,22 @@
|
||||
#ifndef __SubscriptionHandler_h__
|
||||
#define __SubscriptionHandler_h__
|
||||
|
||||
#include "TCPSession.h"
|
||||
#include "ZString.h"
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace core
|
||||
{
|
||||
|
||||
class SubscriptionHandler : public Base
|
||||
{
|
||||
|
||||
public:
|
||||
virtual int process(coreutils::ZString &request, std::stringstream &out, TCPSession &session);
|
||||
virtual int onSubscribe(TCPSession &session) {}
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
22
.history/SubscriptionHandler_20221014152252.h
Normal file
22
.history/SubscriptionHandler_20221014152252.h
Normal file
@ -0,0 +1,22 @@
|
||||
#ifndef __SubscriptionHandler_h__
|
||||
#define __SubscriptionHandler_h__
|
||||
|
||||
#include "TCPSession.h"
|
||||
#include "ZString.h"
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace core
|
||||
{
|
||||
|
||||
class SubscriptionHandler
|
||||
{
|
||||
|
||||
public:
|
||||
virtual int process(coreutils::ZString &request, std::stringstream &out, TCPSession &session);
|
||||
virtual int onSubscribe(TCPSession &session) {}
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
22
.history/SubscriptionHandler_20221014152513.h
Normal file
22
.history/SubscriptionHandler_20221014152513.h
Normal file
@ -0,0 +1,22 @@
|
||||
#ifndef __SubscriptionHandler_h__
|
||||
#define __SubscriptionHandler_h__
|
||||
|
||||
#include "TCPSession.h"
|
||||
#include "ZString.h"
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace core
|
||||
{
|
||||
|
||||
class SubscriptionHandler
|
||||
{
|
||||
|
||||
public:
|
||||
virtual int process(coreutils::ZString &request, std::stringstream &out, TCPSession &session);
|
||||
virtual int onSubscribe(TCPSession &session) { return 1; }
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
23
.history/SubscriptionHandler_20221014153238.h
Normal file
23
.history/SubscriptionHandler_20221014153238.h
Normal file
@ -0,0 +1,23 @@
|
||||
#ifndef __SubscriptionHandler_h__
|
||||
#define __SubscriptionHandler_h__
|
||||
|
||||
#include "ZString.h"
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace core
|
||||
{
|
||||
|
||||
class TCPSession;
|
||||
|
||||
class SubscriptionHandler
|
||||
{
|
||||
|
||||
public:
|
||||
virtual int process(coreutils::ZString &request, std::stringstream &out, TCPSession &session);
|
||||
virtual int onSubscribe(TCPSession &session);
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
33
.history/SubscriptionManager_20221013225816.h
Normal file
33
.history/SubscriptionManager_20221013225816.h
Normal file
@ -0,0 +1,33 @@
|
||||
#ifndef __SubscriptionManager_h__
|
||||
#define __SubscriptionManager_h__
|
||||
|
||||
#include "TCPSession.h"
|
||||
#include "Subscription.h"
|
||||
#include "Command.h"
|
||||
#include "ZString.h"
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
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<std::string, Subscription *> subscriptions;
|
||||
std::map<std::string, SubscriptionHandler> handlers;
|
||||
std::mutex lock;
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
34
.history/SubscriptionManager_20221013231834.h
Normal file
34
.history/SubscriptionManager_20221013231834.h
Normal file
@ -0,0 +1,34 @@
|
||||
#ifndef __SubscriptionManager_h__
|
||||
#define __SubscriptionManager_h__
|
||||
|
||||
#include "Command.h"
|
||||
#include "Subscription.h"
|
||||
#include "TCPSession.h"
|
||||
#include "ZString.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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<std::string, Subscription *> subscriptions;
|
||||
std::map<std::string, SubscriptionHandler> handlers;
|
||||
std::mutex lock;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
34
.history/SubscriptionManager_20221013232252.h
Normal file
34
.history/SubscriptionManager_20221013232252.h
Normal file
@ -0,0 +1,34 @@
|
||||
#ifndef __SubscriptionManager_h__
|
||||
#define __SubscriptionManager_h__
|
||||
|
||||
#include "Command.h"
|
||||
#include "Subscription.h"
|
||||
#include "TCPSession.h"
|
||||
#include "ZString.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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<std::string, Subscription *> subscriptions;
|
||||
std::map<std::string, SubscriptionHandler *> handlers;
|
||||
std::mutex lock;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
34
.history/SubscriptionManager_20221013232501.h
Normal file
34
.history/SubscriptionManager_20221013232501.h
Normal file
@ -0,0 +1,34 @@
|
||||
#ifndef __SubscriptionManager_h__
|
||||
#define __SubscriptionManager_h__
|
||||
|
||||
#include "Command.h"
|
||||
#include "Subscription.h"
|
||||
#include "TCPSession.h"
|
||||
#include "ZString.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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<std::string, Subscription *> subscriptions;
|
||||
std::map<std::string, SubscriptionHandler *> handlers;
|
||||
std::mutex lock;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
34
.history/SubscriptionManager_20221013232513.h
Normal file
34
.history/SubscriptionManager_20221013232513.h
Normal file
@ -0,0 +1,34 @@
|
||||
#ifndef __SubscriptionManager_h__
|
||||
#define __SubscriptionManager_h__
|
||||
|
||||
#include "Command.h"
|
||||
#include "Subscription.h"
|
||||
#include "TCPSession.h"
|
||||
#include "ZString.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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<std::string, Subscription *> subscriptions;
|
||||
std::map<std::string, SubscriptionHandler *> handlers;
|
||||
std::mutex lock;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
34
.history/SubscriptionManager_20221013232519.h
Normal file
34
.history/SubscriptionManager_20221013232519.h
Normal file
@ -0,0 +1,34 @@
|
||||
#ifndef __SubscriptionManager_h__
|
||||
#define __SubscriptionManager_h__
|
||||
|
||||
#include "Command.h"
|
||||
#include "Subscription.h"
|
||||
#include "TCPSession.h"
|
||||
#include "ZString.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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<std::string, Subscription *> subscriptions;
|
||||
std::map<std::string, SubscriptionHandler *> handlers;
|
||||
std::mutex lock;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
37
.history/SubscriptionManager_20221015140418.h
Normal file
37
.history/SubscriptionManager_20221015140418.h
Normal file
@ -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 <string>
|
||||
#include <vector>
|
||||
|
||||
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<std::string, Subscription *> subscriptions;
|
||||
std::map<std::string, SubscriptionHandler *> handlers;
|
||||
Subscription *newSubscription;
|
||||
std::mutex lock;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
38
.history/SubscriptionManager_20221015201436.h
Normal file
38
.history/SubscriptionManager_20221015201436.h
Normal file
@ -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 <string>
|
||||
#include <vector>
|
||||
|
||||
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<std::string, Subscription *> subscriptions;
|
||||
std::map<std::string, SubscriptionHandler *> handlers;
|
||||
Subscription *newSubscription;
|
||||
std::mutex lock;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
38
.history/SubscriptionManager_20221015201440.h
Normal file
38
.history/SubscriptionManager_20221015201440.h
Normal file
@ -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 <string>
|
||||
#include <vector>
|
||||
|
||||
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<std::string, Subscription *> subscriptions;
|
||||
std::map<std::string, SubscriptionHandler *> handlers;
|
||||
Subscription *newSubscription;
|
||||
std::mutex lock;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
38
.history/SubscriptionManager_20221016180248.h
Normal file
38
.history/SubscriptionManager_20221016180248.h
Normal file
@ -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 <string>
|
||||
#include <vector>
|
||||
|
||||
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<std::string, Subscription *> subscriptions;
|
||||
std::map<std::string, SubscriptionHandler *> handlers;
|
||||
Subscription *newSubscription;
|
||||
std::mutex lock;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
46
.history/Subscription_20221013190812.h
Normal file
46
.history/Subscription_20221013190812.h
Normal file
@ -0,0 +1,46 @@
|
||||
#ifndef __Subscription_h__
|
||||
#define __Subscription_h__
|
||||
|
||||
#include "ZString.h"
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
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<TCPSession *> subscribers;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
75
.history/Subscription_20221013190943.cpp
Normal file
75
.history/Subscription_20221013190943.cpp
Normal file
@ -0,0 +1,75 @@
|
||||
#include "Subscription.h"
|
||||
#include "TCPSession.h"
|
||||
#include "Log.h"
|
||||
#include <algorithm>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
47
.history/Subscription_20221013204658.h
Normal file
47
.history/Subscription_20221013204658.h
Normal file
@ -0,0 +1,47 @@
|
||||
#ifndef __Subscription_h__
|
||||
#define __Subscription_h__
|
||||
|
||||
#include "SubscriptionHandler.h"
|
||||
#include "ZString.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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<TCPSession *> subscribers;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
76
.history/Subscription_20221013204725.cpp
Normal file
76
.history/Subscription_20221013204725.cpp
Normal file
@ -0,0 +1,76 @@
|
||||
#include "Subscription.h"
|
||||
#include "Log.h"
|
||||
#include "TCPSession.h"
|
||||
#include <algorithm>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
76
.history/Subscription_20221013204753.cpp
Normal file
76
.history/Subscription_20221013204753.cpp
Normal file
@ -0,0 +1,76 @@
|
||||
#include "Subscription.h"
|
||||
#include "Log.h"
|
||||
#include "TCPSession.h"
|
||||
#include <algorithm>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
76
.history/Subscription_20221013205054.cpp
Normal file
76
.history/Subscription_20221013205054.cpp
Normal file
@ -0,0 +1,76 @@
|
||||
#include "Subscription.h"
|
||||
#include "Log.h"
|
||||
#include "TCPSession.h"
|
||||
#include <algorithm>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
47
.history/Subscription_20221014003150.h
Normal file
47
.history/Subscription_20221014003150.h
Normal file
@ -0,0 +1,47 @@
|
||||
#ifndef __Subscription_h__
|
||||
#define __Subscription_h__
|
||||
|
||||
#include "SubscriptionHandler.h"
|
||||
#include "ZString.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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<TCPSession *> subscribers;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
49
.history/Subscription_20221014212452.h
Normal file
49
.history/Subscription_20221014212452.h
Normal file
@ -0,0 +1,49 @@
|
||||
#ifndef __Subscription_h__
|
||||
#define __Subscription_h__
|
||||
|
||||
#include "SubscriptionHandler.h"
|
||||
#include "TCPSession.h"
|
||||
#include "ZString.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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<TCPSession *> subscribers;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
47
.history/Subscription_20221014212457.h
Normal file
47
.history/Subscription_20221014212457.h
Normal file
@ -0,0 +1,47 @@
|
||||
#ifndef __Subscription_h__
|
||||
#define __Subscription_h__
|
||||
|
||||
#include "SubscriptionHandler.h"
|
||||
#include "TCPSession.h"
|
||||
#include "ZString.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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<TCPSession *> subscribers;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
48
.history/Subscription_20221014212653.h
Normal file
48
.history/Subscription_20221014212653.h
Normal file
@ -0,0 +1,48 @@
|
||||
#ifndef __Subscription_h__
|
||||
#define __Subscription_h__
|
||||
|
||||
#include "SubscriptionHandler.h"
|
||||
#include "ZString.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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<TCPSession *> subscribers;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
76
.history/Subscription_20221014220951.cpp
Normal file
76
.history/Subscription_20221014220951.cpp
Normal file
@ -0,0 +1,76 @@
|
||||
#include "Subscription.h"
|
||||
#include "Log.h"
|
||||
#include "TCPSession.h"
|
||||
#include <algorithm>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
76
.history/Subscription_20221015131611.cpp
Normal file
76
.history/Subscription_20221015131611.cpp
Normal file
@ -0,0 +1,76 @@
|
||||
#include "Subscription.h"
|
||||
#include "Log.h"
|
||||
#include "TCPSession.h"
|
||||
#include <algorithm>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
79
.history/Subscription_20221015142023.cpp
Normal file
79
.history/Subscription_20221015142023.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
#include "Subscription.h"
|
||||
#include "Log.h"
|
||||
#include "TCPSession.h"
|
||||
#include <algorithm>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
79
.history/Subscription_20221015175629.cpp
Normal file
79
.history/Subscription_20221015175629.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
#include "Subscription.h"
|
||||
#include "Log.h"
|
||||
#include "TCPSession.h"
|
||||
#include <algorithm>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
49
.history/Subscription_20221015182553.h
Normal file
49
.history/Subscription_20221015182553.h
Normal file
@ -0,0 +1,49 @@
|
||||
#ifndef __Subscription_h__
|
||||
#define __Subscription_h__
|
||||
|
||||
#include "SubscriptionHandler.h"
|
||||
#include "ZString.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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<TCPSession *> subscribers;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
49
.history/Subscription_20221015182613.h
Normal file
49
.history/Subscription_20221015182613.h
Normal file
@ -0,0 +1,49 @@
|
||||
#ifndef __Subscription_h__
|
||||
#define __Subscription_h__
|
||||
|
||||
#include "SubscriptionHandler.h"
|
||||
#include "ZString.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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<TCPSession *> subscribers;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
79
.history/Subscription_20221015182633.cpp
Normal file
79
.history/Subscription_20221015182633.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
#include "Subscription.h"
|
||||
#include "Log.h"
|
||||
#include "TCPSession.h"
|
||||
#include <algorithm>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
79
.history/Subscription_20221015182644.cpp
Normal file
79
.history/Subscription_20221015182644.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
#include "Subscription.h"
|
||||
#include "Log.h"
|
||||
#include "TCPSession.h"
|
||||
#include <algorithm>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
49
.history/Subscription_20221015182655.h
Normal file
49
.history/Subscription_20221015182655.h
Normal file
@ -0,0 +1,49 @@
|
||||
#ifndef __Subscription_h__
|
||||
#define __Subscription_h__
|
||||
|
||||
#include "SubscriptionHandler.h"
|
||||
#include "ZString.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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<TCPSession *> subscribers;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
79
.history/Subscription_20221015182706.cpp
Normal file
79
.history/Subscription_20221015182706.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
#include "Subscription.h"
|
||||
#include "Log.h"
|
||||
#include "TCPSession.h"
|
||||
#include <algorithm>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
79
.history/Subscription_20221015182712.cpp
Normal file
79
.history/Subscription_20221015182712.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
#include "Subscription.h"
|
||||
#include "Log.h"
|
||||
#include "TCPSession.h"
|
||||
#include <algorithm>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
49
.history/Subscription_20221015203632.h
Normal file
49
.history/Subscription_20221015203632.h
Normal file
@ -0,0 +1,49 @@
|
||||
#ifndef __Subscription_h__
|
||||
#define __Subscription_h__
|
||||
|
||||
#include "SubscriptionHandler.h"
|
||||
#include "ZString.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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<TCPSession *> subscribers;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
79
.history/Subscription_20221015203643.cpp
Normal file
79
.history/Subscription_20221015203643.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
#include "Subscription.h"
|
||||
#include "Log.h"
|
||||
#include "TCPSession.h"
|
||||
#include <algorithm>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
79
.history/Subscription_20221015203655.cpp
Normal file
79
.history/Subscription_20221015203655.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
#include "Subscription.h"
|
||||
#include "Log.h"
|
||||
#include "TCPSession.h"
|
||||
#include <algorithm>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
79
.history/Subscription_20221015203702.cpp
Normal file
79
.history/Subscription_20221015203702.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
#include "Subscription.h"
|
||||
#include "Log.h"
|
||||
#include "TCPSession.h"
|
||||
#include <algorithm>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
49
.history/Subscription_20221015203734.h
Normal file
49
.history/Subscription_20221015203734.h
Normal file
@ -0,0 +1,49 @@
|
||||
#ifndef __Subscription_h__
|
||||
#define __Subscription_h__
|
||||
|
||||
#include "SubscriptionHandler.h"
|
||||
#include "ZString.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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<TCPSession *> subscribers;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
79
.history/Subscription_20221015203735.cpp
Normal file
79
.history/Subscription_20221015203735.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
#include "Subscription.h"
|
||||
#include "Log.h"
|
||||
#include "TCPSession.h"
|
||||
#include <algorithm>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
79
.history/Subscription_20221015203805.cpp
Normal file
79
.history/Subscription_20221015203805.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
#include "Subscription.h"
|
||||
#include "Log.h"
|
||||
#include "TCPSession.h"
|
||||
#include <algorithm>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
79
.history/Subscription_20221015204939.cpp
Normal file
79
.history/Subscription_20221015204939.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
#include "Subscription.h"
|
||||
#include "Log.h"
|
||||
#include "TCPSession.h"
|
||||
#include <algorithm>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
79
.history/Subscription_20221016151339.cpp
Normal file
79
.history/Subscription_20221016151339.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
#include "Subscription.h"
|
||||
#include "Log.h"
|
||||
#include "TCPSession.h"
|
||||
#include <algorithm>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
79
.history/Subscription_20221016162014.cpp
Normal file
79
.history/Subscription_20221016162014.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
#include "Subscription.h"
|
||||
#include "Log.h"
|
||||
#include "TCPSession.h"
|
||||
#include <algorithm>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
79
.history/Subscription_20221016172637.cpp
Normal file
79
.history/Subscription_20221016172637.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
#include "Subscription.h"
|
||||
#include "Log.h"
|
||||
#include "TCPSession.h"
|
||||
#include <algorithm>
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
79
.history/Subscription_20221016172643.cpp
Normal file
79
.history/Subscription_20221016172643.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
#include "Subscription.h"
|
||||
#include "Log.h"
|
||||
#include "TCPSession.h"
|
||||
#include <algorithm>
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
79
.history/Subscription_20221016173312.cpp
Normal file
79
.history/Subscription_20221016173312.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
#include "Subscription.h"
|
||||
#include "Log.h"
|
||||
#include "TCPSession.h"
|
||||
#include <algorithm>
|
||||
|
||||
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";
|
||||
}
|
||||
}
|
79
.history/Subscription_20221016173458.cpp
Normal file
79
.history/Subscription_20221016173458.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
#include "Subscription.h"
|
||||
#include "Log.h"
|
||||
#include "TCPSession.h"
|
||||
#include <algorithm>
|
||||
|
||||
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";
|
||||
}
|
||||
}
|
79
.history/Subscription_20221016173903.cpp
Normal file
79
.history/Subscription_20221016173903.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
#include "Subscription.h"
|
||||
#include "Log.h"
|
||||
#include "TCPSession.h"
|
||||
#include <algorithm>
|
||||
|
||||
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";
|
||||
}
|
||||
}
|
79
.history/Subscription_20221016174406.cpp
Normal file
79
.history/Subscription_20221016174406.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
#include "Subscription.h"
|
||||
#include "Log.h"
|
||||
#include "TCPSession.h"
|
||||
#include <algorithm>
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
78
.history/Subscription_20221016174556.cpp
Normal file
78
.history/Subscription_20221016174556.cpp
Normal file
@ -0,0 +1,78 @@
|
||||
#include "Subscription.h"
|
||||
#include "Log.h"
|
||||
#include "TCPSession.h"
|
||||
#include <algorithm>
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
78
.history/Subscription_20221016174634.cpp
Normal file
78
.history/Subscription_20221016174634.cpp
Normal file
@ -0,0 +1,78 @@
|
||||
#include "Subscription.h"
|
||||
#include "Log.h"
|
||||
#include "TCPSession.h"
|
||||
#include <algorithm>
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
78
.history/Subscription_20221016174652.cpp
Normal file
78
.history/Subscription_20221016174652.cpp
Normal file
@ -0,0 +1,78 @@
|
||||
#include "Subscription.h"
|
||||
#include "Log.h"
|
||||
#include "TCPSession.h"
|
||||
#include <algorithm>
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
78
.history/Subscription_20221016174712.cpp
Normal file
78
.history/Subscription_20221016174712.cpp
Normal file
@ -0,0 +1,78 @@
|
||||
#include "Subscription.h"
|
||||
#include "Log.h"
|
||||
#include "TCPSession.h"
|
||||
#include <algorithm>
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
49
.history/Subscription_20221016174852.h
Normal file
49
.history/Subscription_20221016174852.h
Normal file
@ -0,0 +1,49 @@
|
||||
#ifndef __Subscription_h__
|
||||
#define __Subscription_h__
|
||||
|
||||
#include "SubscriptionHandler.h"
|
||||
#include "ZString.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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<TCPSession *> subscribers;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
49
.history/Subscription_20221016174910.h
Normal file
49
.history/Subscription_20221016174910.h
Normal file
@ -0,0 +1,49 @@
|
||||
#ifndef __Subscription_h__
|
||||
#define __Subscription_h__
|
||||
|
||||
#include "SubscriptionHandler.h"
|
||||
#include "ZString.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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<TCPSession *> subscribers;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
49
.history/Subscription_20221016174913.h
Normal file
49
.history/Subscription_20221016174913.h
Normal file
@ -0,0 +1,49 @@
|
||||
#ifndef __Subscription_h__
|
||||
#define __Subscription_h__
|
||||
|
||||
#include "SubscriptionHandler.h"
|
||||
#include "ZString.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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<TCPSession *> subscribers;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
78
.history/Subscription_20221016174921.cpp
Normal file
78
.history/Subscription_20221016174921.cpp
Normal file
@ -0,0 +1,78 @@
|
||||
#include "Subscription.h"
|
||||
#include "Log.h"
|
||||
#include "TCPSession.h"
|
||||
#include <algorithm>
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
78
.history/Subscription_20221016174923.cpp
Normal file
78
.history/Subscription_20221016174923.cpp
Normal file
@ -0,0 +1,78 @@
|
||||
#include "Subscription.h"
|
||||
#include "Log.h"
|
||||
#include "TCPSession.h"
|
||||
#include <algorithm>
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
79
.history/Subscription_20221016175152.cpp
Normal file
79
.history/Subscription_20221016175152.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
#include "Subscription.h"
|
||||
#include "Log.h"
|
||||
#include "TCPSession.h"
|
||||
#include <algorithm>
|
||||
|
||||
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";
|
||||
}
|
||||
}
|
80
.history/Subscription_20221016175838.cpp
Normal file
80
.history/Subscription_20221016175838.cpp
Normal file
@ -0,0 +1,80 @@
|
||||
#include "Subscription.h"
|
||||
#include "Log.h"
|
||||
#include "TCPSession.h"
|
||||
#include <algorithm>
|
||||
|
||||
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";
|
||||
}
|
||||
}
|
80
.history/Subscription_20221016175855.cpp
Normal file
80
.history/Subscription_20221016175855.cpp
Normal file
@ -0,0 +1,80 @@
|
||||
#include "Subscription.h"
|
||||
#include "Log.h"
|
||||
#include "TCPSession.h"
|
||||
#include <algorithm>
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
80
.history/Subscription_20221016175911.cpp
Normal file
80
.history/Subscription_20221016175911.cpp
Normal file
@ -0,0 +1,80 @@
|
||||
#include "Subscription.h"
|
||||
#include "Log.h"
|
||||
#include "TCPSession.h"
|
||||
#include <algorithm>
|
||||
|
||||
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";
|
||||
}
|
||||
}
|
80
.history/Subscription_20221016175912.cpp
Normal file
80
.history/Subscription_20221016175912.cpp
Normal file
@ -0,0 +1,80 @@
|
||||
#include "Subscription.h"
|
||||
#include "Log.h"
|
||||
#include "TCPSession.h"
|
||||
#include <algorithm>
|
||||
|
||||
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";
|
||||
}
|
||||
}
|
2
.vscode/c_cpp_properties.json
vendored
2
.vscode/c_cpp_properties.json
vendored
@ -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",
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
|
30
EPoll.cpp
30
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
43
EPoll.h
43
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; ///<Output the threads array to the console.
|
||||
|
||||
void resetSocket(Socket *socket);
|
||||
|
||||
private:
|
||||
|
||||
|
||||
int epfd;
|
||||
int numberOfThreads;
|
||||
std::vector<Thread> threads;
|
||||
volatile bool terminateThreads;
|
||||
void enableSocket(Socket *socket);
|
||||
void disableSocket(Socket *socket);
|
||||
|
||||
int numberOfThreads;
|
||||
std::vector<Thread> threads;
|
||||
volatile bool terminateThreads;
|
||||
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
53
INotify.cpp
53
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));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
34
INotify.h
34
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
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -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(); ///<Get the client network address as xxx.xxx.xxx.xxx.
|
||||
std::string getClientAddressAndPort(); ///<Get the client network address and port as xxx.xxx.xxx.xxx:ppppp.
|
||||
int getClientPort(); ///<Get the client network port number.
|
||||
int getClientPort(); ///<Get the client network port number.
|
||||
|
||||
};
|
||||
|
||||
|
@ -2,18 +2,21 @@
|
||||
#define __SessionFilter_h__
|
||||
|
||||
//#include "Session.h"
|
||||
#include "Object.h"
|
||||
|
||||
namespace core {
|
||||
namespace core
|
||||
{
|
||||
|
||||
class TCPSession;
|
||||
class TCPSession;
|
||||
|
||||
class SessionFilter : public Object {
|
||||
class SessionFilter : public Object
|
||||
{
|
||||
|
||||
public:
|
||||
virtual bool test(TCPSession &session) {
|
||||
public:
|
||||
virtual bool test(TCPSession &session)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -1 +0,0 @@
|
||||
0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos:
|
156
Socket.cpp
156
Socket.cpp
@ -1,10 +1,11 @@
|
||||
#include "EPoll.h"
|
||||
#include "Socket.h"
|
||||
#include "EPoll.h"
|
||||
#include "Exception.h"
|
||||
#include "ZString.h"
|
||||
#include "Log.h"
|
||||
#include "ZString.h"
|
||||
|
||||
namespace core {
|
||||
namespace core
|
||||
{
|
||||
|
||||
void sigpipe_handler(int unused) {}
|
||||
|
||||
@ -15,40 +16,46 @@ namespace core {
|
||||
}
|
||||
|
||||
Socket::~Socket() {
|
||||
shutDown = true;
|
||||
onUnregister();
|
||||
disableSocket();
|
||||
coreutils::Log(coreutils::LOG_DEBUG_4) << "Free on socket " << descriptor;
|
||||
free(buffer);
|
||||
if(descriptor == -1)
|
||||
return;
|
||||
onUnregister();
|
||||
ePoll.unregisterSocket(this);
|
||||
coreutils::Log(coreutils::LOG_DEBUG_3) << "Socket destroyed for socket " << descriptor << ".";
|
||||
close(descriptor);
|
||||
coreutils::Log(coreutils::LOG_DEBUG_1) << text << " has ended (" << descriptor << ").";
|
||||
}
|
||||
|
||||
void Socket::setDescriptor(int descriptor) {
|
||||
if((descriptor == -1) && (errno == 24)) {
|
||||
shutdown("Too many files open");
|
||||
throw coreutils::Exception("Too many files open. Refusing connection.");
|
||||
void Socket::setDescriptor(int descriptor)
|
||||
{
|
||||
if ((descriptor == -1) && (errno == 24))
|
||||
{
|
||||
shutdown("Too many files open");
|
||||
throw coreutils::Exception("Too many files open. Refusing connection.");
|
||||
}
|
||||
coreutils::Log(coreutils::LOG_DEBUG_3) << "Descriptor set to " << descriptor << " for Socket.";
|
||||
coreutils::Log(coreutils::LOG_DEBUG_1) << text << " has started (" << descriptor << ").";
|
||||
if(descriptor < 3)
|
||||
throw coreutils::Exception("Descriptor out of range", __FILE__, __LINE__);
|
||||
this->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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
76
Socket.h
76
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); ///<Set the descriptor for the socket.
|
||||
|
||||
int getDescriptor(); ///< Get the descriptor for the socket.
|
||||
|
||||
|
||||
int getDescriptor(); ///< Get the descriptor for the socket.
|
||||
|
||||
///
|
||||
/// The event received from epoll is sent through the eventReceived
|
||||
/// method which will parse the event and call the read and write
|
||||
/// callbacks on the socket.
|
||||
/// callbacks on the socket.
|
||||
///
|
||||
/// This method is called by the BMAEPoll object and should not be called
|
||||
/// from any user extended classes unless an epoll event is being
|
||||
/// simulated.
|
||||
///
|
||||
/// The return value of false will delete the socket object causing the destructors to run.
|
||||
/// The return value of true will enable the socket on ePoll to receive more events.
|
||||
/// The return value of false will delete the socket object causing the destructors to run.
|
||||
/// The return value of true will enable the socket on ePoll to receive more events.
|
||||
///
|
||||
|
||||
bool eventReceived(struct epoll_event event); ///< Parse epoll event and call specified callbacks.
|
||||
|
||||
bool eventReceived(struct epoll_event event, long long eventId); ///< Parse epoll event and call specified callbacks.
|
||||
|
||||
///
|
||||
/// Write data to the socket.
|
||||
///
|
||||
|
||||
|
||||
int write(std::string data);
|
||||
void write(char *buffer, int length);
|
||||
|
||||
void output(std::stringstream &out);
|
||||
|
||||
///
|
||||
/// The onRegister method is called before the socket is registered with
|
||||
/// The onRegister method is called before the socket is registered with
|
||||
/// ePoll so objects extending the Socket definition can initialize the socket
|
||||
/// before receiving events. Evoked when the
|
||||
/// descriptor is set using setDescriptor for the socket.
|
||||
@ -100,11 +100,10 @@ namespace core {
|
||||
|
||||
virtual void onRegister(); ///< Called before the socket has registered with the epoll processing.
|
||||
virtual void onRegistered(); ///< Called after the socket has been registered with epoll processing.
|
||||
|
||||
|
||||
virtual void onUnregister();
|
||||
|
||||
|
||||
///
|
||||
/// The onUnregistered method is called whenever the socket is unregistered with
|
||||
/// ePoll and socket communcation events will be stopped. The default method will
|
||||
/// close the socket and clean up the connection. If this is overridden by an
|
||||
/// extended object then the object should call this method to clean the socket up.
|
||||
@ -113,25 +112,28 @@ namespace core {
|
||||
virtual void onUnregistered(); ///< Called when the socket has finished unregistering for the epoll processing.
|
||||
|
||||
bool needsToWrite();
|
||||
|
||||
|
||||
bool reset = false;
|
||||
|
||||
volatile bool shutDown = false;
|
||||
|
||||
void enableSocket();
|
||||
void disableSocket();
|
||||
|
||||
protected:
|
||||
|
||||
EPoll &ePoll; // The EPoll control object.
|
||||
|
||||
bool shutDown = false;
|
||||
|
||||
void setBufferSize(int length);
|
||||
|
||||
|
||||
int getBufferSize();
|
||||
|
||||
|
||||
///
|
||||
/// The onConnected method is called when the socket is ready to communicate.
|
||||
/// Writing to the socket can begin on this call to initiate a contact with the
|
||||
/// remote device.
|
||||
///
|
||||
|
||||
|
||||
// virtual void onConnected(); ///< Called when socket is open and ready to communicate.
|
||||
|
||||
///
|
||||
@ -139,7 +141,7 @@ namespace core {
|
||||
///
|
||||
|
||||
// virtual void onDisconnected(); ///< Called when socket is closing and no longer ready to communicate.
|
||||
|
||||
|
||||
///
|
||||
/// The onDataReceived method is called when the socket has received an event from
|
||||
/// epoll and there is data ready to be read from the socket. The default handler
|
||||
@ -147,39 +149,37 @@ namespace core {
|
||||
///
|
||||
/// @param data the data that has been received from the socket.
|
||||
///
|
||||
|
||||
|
||||
virtual void onDataReceived(std::string data); ///< Called when data is received from the socket.
|
||||
|
||||
///
|
||||
///
|
||||
///
|
||||
|
||||
virtual void onDataReceived(coreutils::ZString &data);
|
||||
|
||||
|
||||
virtual void onDataReceived(coreutils::ZString &data);
|
||||
|
||||
///
|
||||
/// receiveData will read the data from the socket and place it in the socket buffer.
|
||||
/// TLS layer overrides this to be able to read from SSL.
|
||||
///
|
||||
|
||||
virtual void receiveData(coreutils::ZString &buffer);
|
||||
|
||||
|
||||
private:
|
||||
|
||||
std::string text;
|
||||
int descriptor = -1;
|
||||
// std::mutex lock;
|
||||
std::mutex outlock;
|
||||
bool readHangup = false;
|
||||
bool inHandler = false;
|
||||
// struct epoll_event event; // Event selection construction structure.
|
||||
volatile bool inHandler = false;
|
||||
|
||||
//-------------------------------------------------------------------------------------
|
||||
// the writeSocket is called when epoll has received a write request for a socket.
|
||||
// the writeSocket is called when epoll has received a write request for a socket.
|
||||
// Writing data to this socket is queued in the streambuf and permission is requested
|
||||
// to write to the socket. This routine handles the writing of the streambuf data
|
||||
// buffer to the socket.
|
||||
//-------------------------------------------------------------------------------------
|
||||
|
||||
//-------------------------------------------------------------------------------------
|
||||
|
||||
void writeSocket();
|
||||
|
||||
// int_type underflow();
|
||||
@ -190,12 +190,12 @@ namespace core {
|
||||
char *buffer; // This is a pointer to the managed buffer space.
|
||||
int length; // This is the length of the buffer.
|
||||
|
||||
// const char * const begin_;
|
||||
// const char * const end_;
|
||||
// const char * const current_;
|
||||
|
||||
std::queue<std::string> fifo;
|
||||
|
||||
|
||||
void resetSocket();
|
||||
|
||||
std::mutex lock;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -1,40 +1,42 @@
|
||||
#include "Subscription.h"
|
||||
#include "TCPSession.h"
|
||||
#include "Log.h"
|
||||
#include "TCPSession.h"
|
||||
#include <algorithm>
|
||||
|
||||
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("");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,21 +1,23 @@
|
||||
#ifndef __Subscription_h__
|
||||
#define __Subscription_h__
|
||||
|
||||
#include "SubscriptionHandler.h"
|
||||
#include "SessionFilter.h"
|
||||
#include "ZString.h"
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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<TCPSession *> subscribers;
|
||||
};
|
||||
}
|
||||
|
22
SubscriptionHandler.h
Normal file
22
SubscriptionHandler.h
Normal file
@ -0,0 +1,22 @@
|
||||
#ifndef __SubscriptionHandler_h__
|
||||
#define __SubscriptionHandler_h__
|
||||
|
||||
#include "ZString.h"
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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
|
21
SubscriptionHandlerFactory.h
Normal file
21
SubscriptionHandlerFactory.h
Normal file
@ -0,0 +1,21 @@
|
||||
#ifndef __SubscriptionHandlerFactory_h__
|
||||
#define __SubscriptionHandlerFactory_h__
|
||||
|
||||
#include "SubscriptionHandler.h"
|
||||
#include <string>
|
||||
|
||||
namespace core {
|
||||
|
||||
class SubscriptionHandlerFactory {
|
||||
|
||||
public:
|
||||
|
||||
virtual SubscriptionHandler * getSubscriptionHandler(std::string name) {
|
||||
return new SubscriptionHandler();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
@ -1,85 +1,104 @@
|
||||
#include "SubscriptionManager.h"
|
||||
#include "Log.h"
|
||||
#include "Subscription.h"
|
||||
#include "TCPServer.h"
|
||||
#include <algorithm>
|
||||
#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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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 <vector>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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<std::string, Subscription *> subscriptions;
|
||||
std::mutex lock;
|
||||
|
||||
Subscription *newSubscription;
|
||||
std::mutex lock;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
127
TCPServer.cpp
127
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<TCPSession *>::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;
|
||||
}
|
||||
|
||||
}
|
||||
|
216
TCPServer.h
216
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<TCPSession *> sessions;
|
||||
|
||||
std::vector<TCPSession *> 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); ///<Output the consoles array to the console.
|
||||
///
|
||||
///
|
||||
///
|
||||
|
||||
///
|
||||
///
|
||||
///
|
||||
void sendToAll(std::stringstream &out);
|
||||
|
||||
void sendToAll(std::stringstream &out);
|
||||
///
|
||||
/// Use this sendToAll method to output the contents of the out stream
|
||||
/// to all the connections on the server excluding the sender session.
|
||||
///
|
||||
|
||||
///
|
||||
/// Use this sendToAll method to output the contents of the out stream
|
||||
/// to all the connections on the server excluding the sender session.
|
||||
///
|
||||
void sendToAll(std::stringstream &out, TCPSession &sender, SessionFilter filter);
|
||||
|
||||
void sendToAll(std::stringstream &out, TCPSession &sender, SessionFilter filter);
|
||||
///
|
||||
/// Use this sendToAll method to output the contents of the out stream
|
||||
/// to all the connections on the server excluding the sender session
|
||||
/// and the entries identified by the passed in filter object.
|
||||
///
|
||||
|
||||
///
|
||||
/// Use this sendToAll method to output the contents of the out stream
|
||||
/// to all the connections on the server excluding the sender session
|
||||
/// and the entries identified by the passed in filter object.
|
||||
///
|
||||
void sendToAll(std::stringstream &out, TCPSession &sender);
|
||||
|
||||
void sendToAll(std::stringstream &out, TCPSession &sender);
|
||||
///
|
||||
/// The Subscription Manager tracks all subscriptions on the server.
|
||||
///
|
||||
|
||||
///
|
||||
/// The Subscription Manager tracks all subscriptions on the server.
|
||||
///
|
||||
SubscriptionManager subscriptions;
|
||||
|
||||
SubscriptionManager subscriptions;
|
||||
///
|
||||
/// Use the getSessionByAlias to retrieve a session pointer by the value
|
||||
/// of the alias pointer.
|
||||
///
|
||||
|
||||
protected:
|
||||
TCPSession *getSessionByAlias(void *alias);
|
||||
|
||||
///
|
||||
/// Override the virtual dataReceived since for the server these
|
||||
/// are requests to accept the new connection socket.
|
||||
/// No data is to be read or written when this method is called. It is the response to
|
||||
/// the fact that a new connection is coming into the system
|
||||
///
|
||||
/// @param data the pointer to the buffer containing the received data.
|
||||
/// @param length the length of the associated data buffer.
|
||||
///
|
||||
protected:
|
||||
///
|
||||
/// Override the virtual dataReceived since for the server these
|
||||
/// are requests to accept the new connection socket.
|
||||
/// No data is to be read or written when this method is called. It is the response to
|
||||
/// the fact that a new connection is coming into the system
|
||||
///
|
||||
/// @param data the pointer to the buffer containing the received data.
|
||||
/// @param length the length of the associated data buffer.
|
||||
///
|
||||
|
||||
void onDataReceived(std::string data) override;
|
||||
void onDataReceived(std::string data) override;
|
||||
|
||||
///
|
||||
/// This method is called when the Command associated with this object is requested
|
||||
/// because a user has typed in the associated command name on a command entry line.
|
||||
///
|
||||
/// @param the session object to write the output to.
|
||||
///
|
||||
///
|
||||
/// This method is called when the Command associated with this object is requested
|
||||
/// because a user has typed in the associated command name on a command entry line.
|
||||
///
|
||||
/// @param the session object to write the output to.
|
||||
///
|
||||
|
||||
int processCommand(coreutils::ZString &request, TCPSession &session) override;
|
||||
int processCommand(coreutils::ZString &request, TCPSession &session) override;
|
||||
|
||||
private:
|
||||
|
||||
TCPSession * accept();
|
||||
std::mutex lock;
|
||||
|
||||
};
|
||||
private:
|
||||
TCPSession *accept();
|
||||
std::mutex lock;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
124
TCPSession.cpp
124
TCPSession.cpp
@ -1,96 +1,122 @@
|
||||
#include "TCPSession.h"
|
||||
#include "TCPServer.h"
|
||||
#include "Exception.h"
|
||||
#include "Log.h"
|
||||
#include "TCPServer.h"
|
||||
#include "uuid/uuid.h"
|
||||
|
||||
namespace core {
|
||||
namespace core
|
||||
{
|
||||
|
||||
TCPSession::TCPSession(EPoll &ePoll, TCPServer &server, std::string text) : TCPSocket(ePoll, text), server(server) {}
|
||||
TCPSession::TCPSession(EPoll &ePoll, TCPServer &server, std::string text) : TCPSocket(ePoll, text), server(server)
|
||||
{
|
||||
uuid_t uuidObject;
|
||||
uuid_generate(uuidObject);
|
||||
// std::string aaUuid = {uuidObject, uuidObject + 16};
|
||||
coreutils::Log(coreutils::LOG_DEBUG_1) << uuidObject;
|
||||
alias = (void *)uuidObject;
|
||||
}
|
||||
|
||||
TCPSession::~TCPSession() {
|
||||
TCPSession::~TCPSession()
|
||||
{
|
||||
server.removeFromSessionList(this);
|
||||
server.subscriptions.removeSessionSubscriptions(*this);
|
||||
}
|
||||
|
||||
void TCPSession::output(std::stringstream &data) {
|
||||
void TCPSession::output(std::stringstream &data)
|
||||
{
|
||||
data << "|" << ipAddress.getClientAddressAndPort();
|
||||
}
|
||||
|
||||
void TCPSession::protocol(coreutils::ZString &data) {
|
||||
if(data.getLength() != 0) {
|
||||
if(!server.commands.processRequest(data, *this)) {
|
||||
coreutils::Log(coreutils::LOG_DEBUG_1) << "Received data could not be parsed: " << data.str();
|
||||
void TCPSession::protocol(coreutils::ZString &data)
|
||||
{
|
||||
if (data.getLength() != 0)
|
||||
{
|
||||
if (!server.commands.processRequest(data, *this))
|
||||
{
|
||||
coreutils::Log(coreutils::LOG_DEBUG_1) << "Received data could not be parsed: " << data.str();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool TCPSession::compareAlias(void *alias) {
|
||||
return this->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;
|
||||
}
|
||||
|
||||
|
225
TCPSession.h
225
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;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
85
TCPSession2.cpp
Normal file
85
TCPSession2.cpp
Normal file
@ -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;
|
||||
}
|
||||
|
||||
}
|
139
TCPSession2.h
Normal file
139
TCPSession2.h
Normal file
@ -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
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user