<commit_before>//*****************************************************************************
/*!
 *  \file   bayeuxmodule.cpp
 *
 *  \brief  A module for handling bayeux connections.  Hello Comet!!!
 *
 *  \version
 *      - <NAME>      23/03/2009
 *        Created
 *
 *****************************************************************************/

#include "bayeuxmodule.h"
#include "channel.h"
#include "../handlerstage.h"
#include "../request.h"
#include "../response.h"
#include "../../connection.h"
#include "json/json.h"
#include "json/tokenizer.h"
#include <uuid/uuid.h>

const char *FIELD_CHANNEL               = "channel";
const char *FIELD_VERSION               = "version";
const char *FIELD_MINVERSION            = "minimumVersion";
const char *FIELD_CONNTYPE              = "connectionType";
const char *FIELD_SUPPORTED_CONNTYPES   = "supportedConnectionTypes";
const char *FIELD_CLIENTID              = "clientId";
const char *FIELD_ADVICE                = "advice";
const char *FIELD_ID                    = "id";
const char *FIELD_EXT                   = "ext";
const char *FIELD_SUCCESSFUL            = "successful";
const char *FIELD_AUTHSUCCESSFUL        = "authSuccessful";
const char *FIELD_SUBSCRIPTION          = "subscription";

//! Registers a channel
bool SBayeuxModule::RegisterChannel(SBayeuxChannel *pChannel, bool replace)
{
    ChannelMap::iterator iter = channels.find(pChannel->Name());
    if (iter != channels.end())
    {
        if (!replace) return false;

        // TODO: should unregister be called instead?
        channels.erase(iter);
    }
    channels.insert(std::pair<SString, SBayeuxChannel *>(pChannel->Name(), pChannel));
    return true;
}

//! Removes a channel by name
bool SBayeuxModule::UnregisterChannel(const SString &name)
{
    ChannelMap::iterator iter = channels.find(name);
    if (iter == channels.end())
        return false;

    channels.erase(iter);
    return true;
}

//! Removes a channel
bool SBayeuxModule::UnregisterChannel(const SBayeuxChannel *pChannel)
{
    return UnregisterChannel(pChannel->Name());
}

//! Adds a new connection to the subscription list
// TODO: thread safety
bool SBayeuxModule::AddSubscription(const SString &channel, SConnection *pConnection)
{
    ChannelSubscription::iterator iter  = subscriptions.find(channel);
    SConnectionList *pConnList          = NULL;
    if (iter == subscriptions.end())
    {
        pConnList = new SConnectionList();
        subscriptions.insert(std::pair<SString, SConnectionList *>(channel, pConnList));
    }
    else
    {
        pConnList = iter->second;
    }

    if (find(pConnList->begin(), pConnList->end(), pConnection) != pConnList->end())
        return false;

    pConnList->push_back(pConnection);

    return true;
}

bool SBayeuxModule::RemoveSubscription(const SString &channel, SConnection *pConnection)
{
    ChannelSubscription::iterator iter  = subscriptions.find(channel);
    if (iter == subscriptions.end())
        return false;

    SConnectionList *pConnList = iter->second;

    SConnectionList::iterator iter2 = find(pConnList->begin(), pConnList->end(), pConnection);

    if (iter2 == pConnList->end())
        return false;

    pConnList->erase(iter2);

    return true;
}

//! Delivers an event to all subscribers of a channel
void SBayeuxModule::DeliverEvent(SBayeuxChannel *pChannel, JsonNodePtr &value)
{
    // do nothing if no handler stage available
    if (pHandlerStage == NULL) return ;

    ChannelSubscription::iterator iter  = subscriptions.find(pChannel->Name());

    if (iter == subscriptions.end())
        return ;

    SStringStream msgstream;
    DefaultJsonFormatter formatter;
    formatter.Format(msgstream, value);<commit_msg>Making sent out value on event "real".<commit_after>//*****************************************************************************
/*!
 *  \file   bayeuxmodule.cpp
 *
 *  \brief  A module for handling bayeux connections.  Hello Comet!!!
 *
 *  \version
 *      - <NAME>      23/03/2009
 *        Created
 *
 *****************************************************************************/

#include "bayeuxmodule.h"
#include "channel.h"
#include "../handlerstage.h"
#include "../request.h"
#include "../response.h"
#include "../../connection.h"
#include "json/json.h"
#include "json/tokenizer.h"
#include <uuid/uuid.h>

const char *FIELD_CHANNEL               = "channel";
const char *FIELD_DATA                  = "data";
const char *FIELD_VERSION               = "version";
const char *FIELD_MINVERSION            = "minimumVersion";
const char *FIELD_CONNTYPE              = "connectionType";
const char *FIELD_SUPPORTED_CONNTYPES   = "supportedConnectionTypes";
const char *FIELD_CLIENTID              = "clientId";
const char *FIELD_ADVICE                = "advice";
const char *FIELD_ID                    = "id";
const char *FIELD_EXT                   = "ext";
const char *FIELD_SUCCESSFUL            = "successful";
const char *FIELD_AUTHSUCCESSFUL        = "authSuccessful";
const char *FIELD_SUBSCRIPTION          = "subscription";

//! Registers a channel
bool SBayeuxModule::RegisterChannel(SBayeuxChannel *pChannel, bool replace)
{
    ChannelMap::iterator iter = channels.find(pChannel->Name());
    if (iter != channels.end())
    {
        if (!replace) return false;

        // TODO: should unregister be called instead?
        channels.erase(iter);
    }
    channels.insert(std::pair<SString, SBayeuxChannel *>(pChannel->Name(), pChannel));
    return true;
}

//! Removes a channel by name
bool SBayeuxModule::UnregisterChannel(const SString &name)
{
    ChannelMap::iterator iter = channels.find(name);
    if (iter == channels.end())
        return false;

    channels.erase(iter);
    return true;
}

//! Removes a channel
bool SBayeuxModule::UnregisterChannel(const SBayeuxChannel *pChannel)
{
    return UnregisterChannel(pChannel->Name());
}

//! Adds a new connection to the subscription list
// TODO: thread safety
bool SBayeuxModule::AddSubscription(const SString &channel, SConnection *pConnection)
{
    ChannelSubscription::iterator iter  = subscriptions.find(channel);
    SConnectionList *pConnList          = NULL;
    if (iter == subscriptions.end())
    {
        pConnList = new SConnectionList();
        subscriptions.insert(std::pair<SString, SConnectionList *>(channel, pConnList));
    }
    else
    {
        pConnList = iter->second;
    }

    if (find(pConnList->begin(), pConnList->end(), pConnection) != pConnList->end())
        return false;

    pConnList->push_back(pConnection);

    return true;
}

bool SBayeuxModule::RemoveSubscription(const SString &channel, SConnection *pConnection)
{
    ChannelSubscription::iterator iter  = subscriptions.find(channel);
    if (iter == subscriptions.end())
        return false;

    SConnectionList *pConnList = iter->second;

    SConnectionList::iterator iter2 = find(pConnList->begin(), pConnList->end(), pConnection);

    if (iter2 == pConnList->end())
        return false;

    pConnList->erase(iter2);

    return true;
}

//! Delivers an event to all subscribers of a channel
void SBayeuxModule::DeliverEvent(SBayeuxChannel *pChannel, JsonNodePtr &value)
{
    // do nothing if no handler stage available
    if (pHandlerStage == NULL) return ;

    ChannelSubscription::iterator iter  = subscriptions.find(pChannel->Name());

    if (iter == subscriptions.end())
        return ;

    JsonNodePtr realValue = JsonNodeFactory::ObjectNode();
    realValue->Set(FIELD_CHANNEL, JsonNodeFactory::StringNode(pChannel->Name()));
    realValue->Set(FIELD_DATA, value);

    SStringStream msgstream;
    DefaultJsonFormatter formatter;
    formatter.Format(msgstream, realValue);
{'fraction_non_alphanumeric': 0.15866008828875616, 'fraction_numerical': 0.002856400934822124, 'mean_word_length': 3.3866742596810933, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1671301', 'n_tokens_mistral': 2206, 'n_tokens_neox': 2150, 'n_words': 528}