sc_result utils_collect_identifiers_initialize()
{
    utils_redis_host = sc_config_get_value_string(str_group_redis, str_key_redis_host);
    if (utils_redis_host == 0)
        utils_redis_host = "127.0.0.1";

    utils_redis_port = sc_config_get_value_int(str_group_redis, str_key_redis_port);
    if (utils_redis_port == 0)
        utils_redis_port = 6379;

    utils_redis_timeout = sc_config_get_value_int(str_group_redis, str_key_redis_timeout);
    if (utils_redis_timeout == 0)
        utils_redis_timeout = 1500;

    // connect to redis
    redisCtx = connectToRedis();


    // initialize agents
    event_add_idtf = sc_event_new(keynode_nrel_idtf, SC_EVENT_ADD_OUTPUT_ARC, 0, agent_append_idtf, (fDeleteCallback)0);
    if (event_add_idtf == 0)
        return SC_RESULT_ERROR;

    event_add_main_idtf = sc_event_new(keynode_nrel_main_idtf, SC_EVENT_ADD_OUTPUT_ARC, 0, agent_append_idtf, (fDeleteCallback)0);
    if (event_add_main_idtf == 0)
        return SC_RESULT_ERROR;

    event_add_sys_idtf = sc_event_new(keynode_nrel_system_identifier, SC_EVENT_ADD_OUTPUT_ARC, 0, agent_append_idtf, (fDeleteCallback)0);
    if (event_add_sys_idtf == 0)
        return SC_RESULT_ERROR;


    return SC_RESULT_OK;
}
Exemple #2
0
void disruptorKill( const char* address )
{
    redisContext* r;
    redisReply* reply;
    
    r = connectToRedis();
    if ( !r )
    {
        handleError( NULL, "failed to connect to redis." );
        return;
    }

    {
        reply = redisCommand( r, "KEYS disruptor:%s:*", address );
        if ( reply->type != REDIS_REPLY_ARRAY )
        {
            handleError( NULL, "failed to get keys." );
        }

        {
            size_t i;
            for ( i = 0; i < reply->elements; ++i )
            {
                redisReply* elem = reply->element[i];
                if ( elem && elem->type == REDIS_REPLY_STRING )
                {
                    const char* key = elem->str;
                    redisReply* reply2 = redisCommand( r, "DEL %s", key );
                    freeReplyObject( reply2 );
                }
            }
        }

        freeReplyObject( reply );
    }

    shmemUnlink( "disruptor:%s", address );
    shmemUnlink( "disruptor:%s:rb", address );

    {
        int i;
        for ( i = 0; i < MAX_CONNECTIONS; ++i )
        {
            shmemUnlink( "disruptor:%s:%d", address, i );
        }
    }
}
Exemple #3
0
static bool startup( disruptor* d )
{
    bool wasCreated = false;
    redisContext* r;
    redisReply* reply;

    /* validate inputs. */
    {
        if ( !isStringValid( d->address, 1, MAX_ADDRESS_LENGTH ) )
        {
            handleError( d, "invalid length for string '%s'", d->address );
            return false;
        }

        if ( !isStringValid( d->username, 1, MAX_USERNAME_LENGTH ) )
        {
            handleError( d, "invalid length for string '%s'", d->username );
            return false;
        }
    }

    /* connect to redis. */
    r = d->redis = connectToRedis();
    if ( !r )
    {
        handleError( d, "could not connect to redis" );
        return false;
    }

    /* determine a mapping for this connection. */
    {
        int id = -1;

        /* try to fetch an existing mapping. */
        {
            reply = redisCommand( r, "GET disruptor:%s:connections:%s:id", d->address, d->username );
            if ( reply->type == REDIS_REPLY_STRING )
            {
                id = atoi( reply->str );
            }
            freeReplyObject( reply );
        }

        /* if no mapping exists, then assign a new one. */
        if ( id < 0 )
        {
            {
                reply = redisCommand( r, "INCR disruptor:%s:connectionsCount", d->address );
                if ( reply->type == REDIS_REPLY_INTEGER )
                    id = ( reply->integer - 1 );
                freeReplyObject( reply );
            }

            assert( id >= 0 );
            if ( id < 0 )
            {
                handleError( d, "could not determine mapping for username '%s'", d->username );
                return false;
            }

            wasCreated = true;

            {
                reply = redisCommand( r, "SET disruptor:%s:connections:%s:id %d", d->address, d->username, id );
                freeReplyObject( reply );
            }

            {
                reply = redisCommand( r, "SET disruptor:%s:%d:username %s", d->address, id, d->username );
                freeReplyObject( reply );
            }
        }

        d->id = id;
    }

    /* determine the connection count. */
    {
        d->connectionsCount = 0;

        reply = redisCommand( r, "GET disruptor:%s:connectionsCount", d->address, d->username );
        if ( reply->type == REDIS_REPLY_STRING )
            d->connectionsCount = atoi( reply->str );
        freeReplyObject( reply );

        if ( d->connectionsCount <= 0 )
        {
            handleError( d, "could not determine the connection count." );
            return false;
        }
    }

    handleInfo( d, "id=%d total=%d", d->id, d->connectionsCount );

    /* open the shared header. */
    {
        d->shHeader = shmemOpen( sizeof(sharedHeader), SHMEM_DEFAULT, "disruptor:%s", d->address );
        d->header = shmemGetPtr( d->shHeader );
    }

    /* open the shared ringbuffer. */
    {
        d->shRingbuffer = shmemOpen( sizeof(sharedRingbuffer), SHMEM_DEFAULT, "disruptor:%s:rb", d->address );
        d->ringbuffer = shmemGetPtr( d->shRingbuffer );
    }

    {
        int i;

        /* create the shared memory sendBuffer. */
        if ( wasCreated )
        {
            shmem* s;
            handleInfo( d, "creating %d", d->id );
            s = shmemOpen( d->sendBufferSize, SHMEM_MUST_CREATE, "disruptor:%s:%d", d->address, d->id );
            shmemClose( s );
        }

        /* map each connection. */
        for ( i = 0; i < d->connectionsCount; ++i )
        {
            if ( !mapClient( d, i ) )
            {
                handleError( d, "could not map client %d", i );
                return false;
            }
        }
    }

    return true;
}