*NEXT/*DIE
This commit is contained in:
1 parent
48be555724
commit
9a39b6224c
14 files changed
+352
-209
No files matched your search
+98
-35
@@ -1,22 +1,35 @@
|
||||
#include "SubscriptionManager.h"
|
||||
#include "Log.h"
|
||||
#include "Subscription.h"
|
||||
#include "SubscriptionHandlerFactory.h"
|
||||
#include "TCPServer.h"
|
||||
#include <algorithm>
|
||||
#include "SubscriptionHandlerFactory.h"
|
||||
|
||||
namespace core {
|
||||
namespace core
|
||||
{
|
||||
|
||||
int SubscriptionManager::add(Subscription &subscription) {
|
||||
int SubscriptionManager::add(Subscription &subscription)
|
||||
{
|
||||
lock.lock();
|
||||
subscriptions.insert(std::make_pair(subscription.id, &subscription));
|
||||
lock.unlock();
|
||||
return 1;
|
||||
}
|
||||
bool SubscriptionManager::onClearSubscription(std::string temp, std::string key)
|
||||
{
|
||||
temp = key;
|
||||
temp = "";
|
||||
key = "";
|
||||
// delete subscription;
|
||||
|
||||
int SubscriptionManager::removeSessionSubscriptions(TCPSession &session) {
|
||||
return true;
|
||||
}
|
||||
|
||||
int SubscriptionManager::removeSessionSubscriptions(TCPSession &session)
|
||||
{
|
||||
int countSubscribed = 0;
|
||||
int countPublished = 0;
|
||||
int count = 0;
|
||||
|
||||
lock.lock();
|
||||
std::string temp = "";
|
||||
@@ -28,10 +41,37 @@ namespace core {
|
||||
temp = "";
|
||||
}
|
||||
countSubscribed += subscription->unsubscribe(session);
|
||||
if (subscription->owner == &session)
|
||||
if (subscription->owner == session.alias)
|
||||
{
|
||||
temp = key;
|
||||
delete subscription;
|
||||
if (subscription->will == "*DIE") //--> This happens if the owner of the subscription sets it, or if not enough people in Vector -->//
|
||||
{
|
||||
onClearSubscription(temp, key);
|
||||
}
|
||||
|
||||
else if (subscription->will == "*NEXT") //--> This is the other option
|
||||
{
|
||||
if (subscription->subscribers.size() < 1)
|
||||
{
|
||||
coreutils::Log(coreutils::LOG_DEBUG_1) << "There is not enough people to move to the next";
|
||||
onClearSubscription(temp, key);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
for (auto subscribed = subscription->subscribers.begin(); subscribed < subscription->subscribers.end(); subscribed++)
|
||||
{
|
||||
if (*subscribed == &session)
|
||||
{
|
||||
subscription->subscribers.erase(subscribed++);
|
||||
}
|
||||
subscription->subscriptionOwner((*subscribed)->alias, "*NEXT");
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
coreutils::Log(coreutils::LOG_DEBUG_1) << "Subscription doesn't exist";
|
||||
}
|
||||
++countPublished;
|
||||
}
|
||||
}
|
||||
@@ -46,58 +86,81 @@ namespace core {
|
||||
return countSubscribed;
|
||||
}
|
||||
|
||||
int SubscriptionManager::processCommand(coreutils::ZString &request, TCPSession &session) {
|
||||
if (request[0].equals("publish")) {
|
||||
int SubscriptionManager::processCommand(coreutils::ZString &request, TCPSession &session)
|
||||
{
|
||||
if (request[0].equals("publish"))
|
||||
{
|
||||
SubscriptionHandler *handler = NULL;
|
||||
if(request.getList().size() > 3) {
|
||||
factory->getSubscriptionHandler(request[3].str());
|
||||
if (request.getList().size() > 3)
|
||||
{
|
||||
handler = factory->getSubscriptionHandler(request[4].str());
|
||||
}
|
||||
coreutils::Log(coreutils::LOG_DEBUG_1) << request[1].str() << ":" << request[2].str();
|
||||
if (request[2].str() == "*OWNER")
|
||||
{
|
||||
newSubscription = new Subscription(request[1].str(), session.alias, request[2].str(), request[3].str(), handler, request[5].str(), request[6].str());
|
||||
subscriptions.insert(std::make_pair(request[1].str(), newSubscription));
|
||||
subscription->subscriptionOwner(session.alias, request[5].str());
|
||||
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")) {
|
||||
}
|
||||
else if (request[0].equals("catalog"))
|
||||
{
|
||||
session.out << ":catalog:";
|
||||
for (auto const &[key, subscription] : subscriptions) {
|
||||
for (auto const &[key, subscription] : subscriptions)
|
||||
{
|
||||
session.out << subscription->id << ";";
|
||||
}
|
||||
session.out << std::endl;
|
||||
return 1;
|
||||
} else if (request[0].equals("invite")) {
|
||||
}
|
||||
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());
|
||||
|
||||
// invitee = (coreutils::MString *)&request[2];
|
||||
|
||||
// TCPSession *tempSession = session.server.getSessionByAlias(invitee);
|
||||
// std::stringstream temp;
|
||||
// temp << "invite:" << request[1] << ":" << invitee;
|
||||
// tempSession->write(temp.str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
auto subscription = subscriptions[request[1].str()];
|
||||
|
||||
if (request[1].equals(subscription->id)) {
|
||||
if (request[0].equals("unpublish")) {
|
||||
if (request[1].equals(subscription->id))
|
||||
{
|
||||
if (request[0].equals("unpublish"))
|
||||
{
|
||||
subscriptions.erase(request[1].str());
|
||||
} else if (request[0].equals("subscribe")) {
|
||||
}
|
||||
else if (request[0].equals("subscribe"))
|
||||
{
|
||||
|
||||
subscription->subscribe(session);
|
||||
return 1;
|
||||
} else if (request[0].equals("unsubscribe")) {
|
||||
}
|
||||
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") {
|
||||
if (subscription->mode == "*ANYONE")
|
||||
{
|
||||
subscription->event(out);
|
||||
return 1;
|
||||
} else if (subscription->mode == "*SUBSCRIBERS") {
|
||||
if (subscription->ifSubscriber(session)) {
|
||||
subscription->event(out);
|
||||
return 1;
|
||||
}
|
||||
} else if (subscription->mode == "*AUTHOR") {
|
||||
if (subscription->owner == &session) {
|
||||
}
|
||||
else if (subscription->mode == "*SUBSCRIBERS")
|
||||
{
|
||||
if (subscription->ifSubscriber(session))
|
||||
{
|
||||
subscription->event(out);
|
||||
return 1;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user