<commit_before>/**************************************************************************************************************
* Copyright (C) *   2017, peterwinzell
*
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.*
*
*
***************************************************************************************************************/
#include "subscriptions.h"
#include "unsubnotifier.h"

Subscriptions* Subscriptions::m_instance = nullptr;
int Subscriptions::m_subscriptionIdCounter = 0;
QMutex Subscriptions::m_mutex;

Subscriptions* Subscriptions::getInstance()
{
    QMutexLocker mutexlock(&m_mutex);
    if (m_instance == nullptr)
    {
        m_subscriptionIdCounter = 0;
        m_instance = new Subscriptions();
     }
    return m_instance;
}

int Subscriptions::addSubcription(SubscribeHandler* handler)
{
    QMutexLocker lock(&m_mutex);

    m_subscriptionIdCounter++;
    QWebSocket* client = handler -> getSocketClient();
    UnsubNotifier* usubNotifier = new UnsubNotifier();
    // connect unsubscription
    connect(usubNotifier,&UnsubNotifier::unsubscribe,handler,&SubscribeHandler::unsubscribe);
    // handle unsubscribe
    m_notifiers.insert(m_subscriptionIdCounter,usubNotifier);
    //handle unsubscribe all
    m_clientsubscriptions.insert(client,m_subscriptionIdCounter);

    return m_subscriptionIdCounter;
}

bool Subscriptions::unsubscribe(int subscriptionId,QWebSocket* client)
{
    QMutexLocker lock(&m_mutex);


    UnsubNotifier* notifier = m_notifiers.find(subscriptionId).value();
    if (notifier)
    {
        notifier -> unsubScribe();
    }
    else
    {
        return false;
    }

    m_notifiers.remove(subscriptionId);
    m_clientsubscriptions.remove(client);

    return true;
}

bool Subscriptions::unsubscribeAll(QWebSocket* client)
{
    QMutexLocker lock(&m_mutex);

    QList<int> ids = m_clientsubscriptions.values(client);

    if (ids.size() == 0)
    {
        return false;
    }

    for (int i = 0; i < ids.size(); ++i)
    {
        UnsubNotifier* notifier = m_notifiers.find(ids.value(i)).value();
        notifier -> unsubScribe();
    }

    for(int i = 0; i < ids.size(); ++i)
    {
        m_notifiers.remove(ids.value(i));
    }

    m_clientsubscriptions.remove(client);

    return true;
}
<commit_msg> some small stuff<commit_after>/**************************************************************************************************************
* Copyright (C) *   2017, peterwinzell
*
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.*
*
*
***************************************************************************************************************/
#include "subscriptions.h"
#include "unsubnotifier.h"

//Initialize static members
Subscriptions* Subscriptions::m_instance = nullptr;
int Subscriptions::m_subscriptionIdCounter = 0;
QMutex Subscriptions::m_mutex;

Subscriptions* Subscriptions::getInstance()
{
    QMutexLocker mutexlock(&m_mutex);
    if (m_instance == nullptr)
    {
        m_subscriptionIdCounter = 0;
        m_instance = new Subscriptions();
     }
    return m_instance;
}

int Subscriptions::addSubcription(SubscribeHandler* handler)
{
    QMutexLocker lock(&m_mutex);

    m_subscriptionIdCounter++;
    QWebSocket* client = handler -> getSocketClient();
    UnsubNotifier* usubNotifier = new UnsubNotifier();
    // connect unsubscription
    connect(usubNotifier,&UnsubNotifier::unsubscribe,handler,&SubscribeHandler::unsubscribe);
    // handle unsubscribe
    m_notifiers.insert(m_subscriptionIdCounter,usubNotifier);
    //handle unsubscribe all
    m_clientsubscriptions.insert(client,m_subscriptionIdCounter);

    return m_subscriptionIdCounter;
}

bool Subscriptions::unsubscribe(int subscriptionId,QWebSocket* client)
{
    QMutexLocker lock(&m_mutex);


    UnsubNotifier* notifier = m_notifiers.find(subscriptionId).value();
    if (notifier)
    {
        notifier -> unsubScribe();
    }
    else
    {
        return false;
    }

    m_notifiers.remove(subscriptionId);
    m_clientsubscriptions.remove(client);

    return true;
}

bool Subscriptions::unsubscribeAll(QWebSocket* client)
{
    QMutexLocker lock(&m_mutex);

    QList<int> ids = m_clientsubscriptions.values(client);

    if (ids.size() == 0)
    {
        return false;
    }

    for (int i = 0; i < ids.size(); ++i)
    {
        UnsubNotifier* notifier = m_notifiers.find(ids.value(i)).value();
        notifier -> unsubScribe();
    }

    for(int i = 0; i < ids.size(); ++i)
    {
        m_notifiers.remove(ids.value(i));
    }

    m_clientsubscriptions.remove(client);

    return true;
}

{'fraction_non_alphanumeric': 0.17256944444444444, 'fraction_numerical': 0.003472222222222222, 'mean_word_length': 4.120888888888889, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2132648', 'n_tokens_mistral': 1656, 'n_tokens_neox': 1634, 'n_words': 428}