Пример #1
0
sc_result resolve_nrel_system_identifier(sc_memory_context const * ctx)
{
    sc_addr *results = 0;
    sc_uint32 results_count = 0;
    sc_stream *stream = sc_stream_memory_new(keynodes_str[SC_KEYNODE_NREL_SYSTEM_IDENTIFIER],
                                             (sc_uint)(sizeof(sc_uchar) * strlen(keynodes_str[SC_KEYNODE_NREL_SYSTEM_IDENTIFIER])),
                                             SC_STREAM_FLAG_READ, SC_FALSE);
    sc_uint32 i = 0;
    sc_iterator5 *it = 0;
    sc_bool found = SC_FALSE;
    sc_addr addr1, addr2;

    // try to find nrel_system_identifier strings
    if (sc_memory_find_links_with_content(ctx, stream, &results, &results_count) == SC_RESULT_OK)
    {
        for (i = 0; i < results_count; i++)
        {
            it = sc_iterator5_a_a_f_a_a_new(ctx,
                                            sc_type_node | sc_type_const | sc_type_node_norole,
                                            sc_type_arc_common | sc_type_const,
                                            results[i],
                                            sc_type_arc_pos_const_perm,
                                            sc_type_const | sc_type_node | sc_type_node_norole);

            while (sc_iterator5_next(it))
            {

                addr1 = sc_iterator5_value(it, 0);
                addr2 = sc_iterator5_value(it, 4);
                // comare begin sc-element and attribute, they must be equivalent
                if (SC_ADDR_IS_EQUAL(addr1, addr2))
                {
                    if (found == SC_FALSE)
                    {
                        sc_keynodes[SC_KEYNODE_NREL_SYSTEM_IDENTIFIER] = addr1;
                        found = SC_TRUE;
                    }else
                    {
                        sc_iterator5_free(it);
                        sc_stream_free(stream);
                        g_free(results);
                        g_error("There are more then one sc-elements with system identifier nrel_system_identifier ");
                        return SC_RESULT_ERROR;
                    }
                }
            }

            sc_iterator5_free(it);
        }

        g_free(results);
    }else
        return SC_RESULT_ERROR;

    sc_stream_free(stream);

    return found == SC_TRUE ? SC_RESULT_OK : SC_RESULT_ERROR;
}
Пример #2
0
sc_result sc_helper_find_element_by_system_identifier(sc_memory_context const * ctx, const sc_char* data, sc_uint32 len, sc_addr *result_addr)
{
    sc_addr *results = 0;
    sc_uint32 results_count = 0;
    sc_stream *stream = 0;
    sc_uint32 i = 0;
    sc_iterator5 *it = 0;
    sc_bool found = SC_FALSE;

    g_assert(sc_helper_is_initialized == SC_TRUE);
    g_assert(sc_keynodes != 0);

    // try to find sc-links with that contains system identifier value
    stream = sc_stream_memory_new(data, sizeof(sc_char) * len, SC_STREAM_FLAG_READ, SC_FALSE);
    if (sc_memory_find_links_with_content(ctx, stream, &results, &results_count) == SC_RESULT_OK)
    {
        for (i = 0; i < results_count; i++)
        {
            it = sc_iterator5_a_a_f_a_f_new(ctx,
                                            0,
                                            sc_type_arc_common | sc_type_const,
                                            results[i],
                                            sc_type_arc_pos_const_perm,
                                            sc_keynodes[SC_KEYNODE_NREL_SYSTEM_IDENTIFIER]);
            if (sc_iterator5_next(it))
            {
                if (found == SC_FALSE)
                {
                    found = SC_TRUE;
                    *result_addr = sc_iterator5_value(it, 0);
                }else
                {
                    // don't foget to free allocated memory before return error
                    sc_iterator5_free(it);
                    sc_stream_free(stream);
                    g_free(results);
                    return SC_RESULT_ERROR_INVALID_STATE;
                }
            }

            sc_iterator5_free(it);
        }

        g_free(results);
    }

    sc_stream_free(stream);

    return found == SC_TRUE ? SC_RESULT_OK : SC_RESULT_ERROR;
}
Пример #3
0
scp_result resolve_numbers_1_2(const sc_char *operator_name, scp_operand *param1, scp_operand *param2, double *num1, double *num2)
{
    sc_stream *stream;
    sc_uint32 length = 0, read_length = 0;
    sc_char *data1, *data2;
    if (SCP_ASSIGN == param1->param_type || SCP_ASSIGN == param2->param_type)
    {
        return print_error(operator_name, "Both parameters must have FIXED modifier");
    }
    if (SC_FALSE == sc_memory_is_element(param1->addr))
    {
        return print_error(operator_name, "Parameter 1 has not value");
    }
    if (SC_FALSE == sc_memory_is_element(param2->addr))
    {
        return print_error(operator_name, "Parameter 2 has not value");
    }
    if (check_type(param1->addr, scp_type_link) == SCP_RESULT_FALSE || check_type(param2->addr, scp_type_link) == SCP_RESULT_FALSE)
    {
        return print_error(operator_name, "Both parameters must have link type");
    }
    if (SCP_RESULT_FALSE == check_numeric_type(param1->addr) || SCP_RESULT_FALSE == check_numeric_type(param2->addr))
    {
        return print_error(operator_name, "Both parameters must have numeric format");
    }

    if (sc_memory_get_link_content(param1->addr, &stream) != SC_RESULT_OK)
    {
        return print_error(operator_name, "Parameter 1 content reading error");
    }
    sc_stream_get_length(stream, &length);
    data1 = calloc(length, sizeof(sc_char));
    sc_stream_read_data(stream, data1, length, &read_length);
    sc_stream_free(stream);
    if (sc_memory_get_link_content(param2->addr, &stream) != SC_RESULT_OK)
    {
        return print_error(operator_name, "Parameter 2 content reading error");
    }
    sc_stream_get_length(stream, &length);
    data2 = calloc(length, sizeof(sc_char));
    sc_stream_read_data(stream, data2, length, &read_length);
    sc_stream_free(stream);

    (*num1) = atof(data1);
    (*num2) = atof(data2);
    free(data1);
    free(data2);
    return SCP_RESULT_TRUE;
}
scp_result contAssign(scp_operand *param1, scp_operand *param2)
{
    sc_stream *stream;
    if (SCP_ASSIGN == param2->param_type)
    {
        return print_error("contAssign", "Parameter 2 must have FIXED modifier");
    }
    if (SC_FALSE == sc_memory_is_element(param2->addr))
    {
        return print_error("contAssign", "Parameter 2 has not value");
    }
    if (check_type(param2->addr, scp_type_link) == SCP_RESULT_FALSE)
    {
        return print_error("contAssign", "Parameter 2 must have link type");
    }
    if (sc_memory_get_link_content(param2->addr, &stream) != SC_RESULT_OK)
    {
        return print_error("contAssign", "Parameter 2 content reading error");
    }
    if (SCP_RESULT_ERROR == check_link_parameter_1("contAssign", param1))
    {
        return SCP_RESULT_ERROR;
    }
    sc_memory_set_link_content(param1->addr, stream);
    sc_stream_free(stream);
    return SCP_RESULT_TRUE;
}
Пример #5
0
scp_result write_link_content_number(sc_memory_context *context, double data, sc_addr link)
{
    sc_stream *stream;
    char *content = calloc(NUMBER_PRECISE, sizeof(sc_char));
    g_snprintf(content, NUMBER_PRECISE, "%lf", data);
    stream = sc_stream_memory_new(content, (sc_uint)strlen(content), SC_STREAM_FLAG_READ, SC_FALSE);
    if (SC_RESULT_OK != sc_memory_set_link_content(context, link, stream))
    {
        free(content);
        sc_stream_free(stream);
        return SCP_RESULT_ERROR;
    }
    free(content);
    sc_stream_free(stream);
    return SCP_RESULT_TRUE;
}
Пример #6
0
sc_result sc_helper_init(sc_memory_context const * ctx)
{
    g_message("Initialize sc-helper");

    _init_keynodes_str();

    sc_keynodes = g_new0(sc_addr, SC_KEYNODE_COUNT);

    if (resolve_nrel_system_identifier(ctx) != SC_RESULT_OK)
    {
        g_message("Can't resovle nrel_system_identifier node. Create the last one");

        sc_addr addr = sc_memory_node_new(ctx, sc_type_const | sc_type_node_norole);
        sc_addr link = sc_memory_link_new(ctx);
        sc_stream *stream = sc_stream_memory_new(keynodes_str[SC_KEYNODE_NREL_SYSTEM_IDENTIFIER],
                                                 (sc_uint)(sizeof(sc_uchar) * strlen(keynodes_str[SC_KEYNODE_NREL_SYSTEM_IDENTIFIER])),
                                                 SC_STREAM_FLAG_READ, SC_FALSE);
        sc_memory_set_link_content(ctx, link, stream);
        sc_stream_free(stream);

        sc_addr arc = sc_memory_arc_new(ctx, sc_type_arc_common | sc_type_const, addr, link);
        sc_memory_arc_new(ctx, sc_type_arc_pos_const_perm, addr, arc);
        sc_keynodes[SC_KEYNODE_NREL_SYSTEM_IDENTIFIER] = addr;
    }

    sc_helper_is_initialized = SC_TRUE;

    return SC_RESULT_OK;
}
scp_result printL(scp_operand *param, sc_bool new_line)
{
    sc_stream *stream;
    sc_uint32 length = 0, read_length = 0;
    sc_char *data;
    if (SC_FALSE == sc_memory_is_element(param->addr))
    {
        return print_error("print", "Parameter has not value");
    }
    /*if (SCP_RESULT_FALSE == check_type(param->addr, sc_type_link))
    {
        return print_error("print", "Parameter is not an sc-link");
    }*/
    if (sc_memory_get_link_content(param->addr, &stream) != SC_RESULT_OK)
    {
        return print_error("print", "Content reading error");
    }
    sc_stream_get_length(stream, &length);
    data = calloc(length + 1, sizeof(sc_char));
    sc_stream_read_data(stream, data, length, &read_length);
    data[length] = '\0';
    printf("%s", data);
    if (SC_TRUE == new_line)
    {
        printf("\n");
    }
    sc_stream_free(stream);
    free(data);
    return SCP_RESULT_TRUE;
}
Пример #8
0
scp_result write_link_content_string(sc_memory_context *context, char* data, sc_addr link)
{
    sc_stream *stream;
    size_t data_len = strlen(data) + 1;
    char *content = calloc(data_len, sizeof(sc_char));
    g_snprintf(content, (gulong)data_len, "%s", data);
    stream = sc_stream_memory_new(content, (sc_uint)strlen(content), SC_STREAM_FLAG_READ, SC_FALSE);
    if (SC_RESULT_OK != sc_memory_set_link_content(context, link, stream))
    {
        free(content);
        sc_stream_free(stream);
        return SCP_RESULT_ERROR;
    }
    free(content);
    sc_stream_free(stream);
    return SCP_RESULT_TRUE;
}
Пример #9
0
sc_result sc_fs_storage_write_content(sc_addr addr, const sc_check_sum *check_sum, const sc_stream *stream)
{
    // write content into file
    sc_char buffer[BuffSize];
    sc_uint32 data_read, data_write;
    sc_stream *out_stream = 0;

    if (sc_fm_stream_new(fm_engine, check_sum, SC_STREAM_FLAG_WRITE, &out_stream) == SC_RESULT_OK)
    {
        g_assert(out_stream != null_ptr);
        // reset input stream positon to begin
        sc_stream_seek(stream, SC_STREAM_SEEK_SET, 0);

        while (sc_stream_eof(stream) == SC_FALSE)
        {
            if (sc_stream_read_data(stream, buffer, BuffSize, &data_read) == SC_RESULT_ERROR)
            {
                sc_stream_free(out_stream);
                return SC_RESULT_ERROR;
            }

            if (sc_stream_write_data(out_stream, buffer, data_read, &data_write) == SC_RESULT_ERROR)
            {
                sc_stream_free(out_stream);
                return SC_RESULT_ERROR;
            }

            if (data_read != data_write)
            {
                sc_stream_free(out_stream);
                return SC_RESULT_ERROR;
            }
        }
        sc_stream_free(out_stream);

        // reset input stream positon to begin
        sc_stream_seek(stream, SC_STREAM_SEEK_SET, 0);

        return sc_fs_storage_add_content_addr(addr, check_sum);
    }

    return SC_RESULT_ERROR_IO;
}
Пример #10
0
void test6()
{
    sc_uint32 i, j;
    sc_addr addr;
    sc_stream *stream = 0;
    sc_addr *results = 0;
    sc_uint32 results_count = 0;

    printf("Segments count: %d\n", sc_storage_get_segments_count());
    print_storage_statistics();

    timer = g_timer_new();

    //g_snprintf(test, 1024, "../CMakeLists.txt");
    g_timer_reset(timer);
    g_timer_start(timer);

    for (i = 0; i < link_append_count; i++)
    {
        //addr = sc_storage_link_new();
        printf("Find addrs for content %d\t", i);

        stream = sc_stream_memory_new((char*)&i, sizeof(i), SC_STREAM_READ, SC_FALSE);

        if (sc_storage_find_links_with_content(stream, &results, &results_count) == SC_RESULT_OK)
        {
            printf("founded: \n");
            for (j = 0; j < results_count; j++)
                printf("seg=%d, offset=%d\n", results[j].seg, results[j].offset);
            g_free(results);
            results = 0;
        }else
            printf("error\n");

        sc_stream_free(stream);
    }

    g_timer_stop(timer);

    printf("Created links: %d\n", link_append_count);
    printf("Links per second: %f\n", link_append_count / g_timer_elapsed(timer, 0));

    printf("Segments count: %d\n", sc_storage_get_segments_count());
    print_storage_statistics();

    g_timer_destroy(timer);
}
Пример #11
0
static scp_result resolve_string(sc_memory_context *context, const sc_char *operator_name, const sc_char *parameter_name, scp_operand *param, char **str)
{
    sc_stream *stream;
    sc_uint32 length = 0, read_length = 0;
    sc_char *data;

    if (SCP_ASSIGN == param->param_type)
    {
        return print_parameter_error(operator_name, parameter_name, "must have FIXED modifier");
    }

    if (SC_FALSE == sc_memory_is_element(context, param->addr))
    {
        return print_parameter_error(operator_name, parameter_name, "has not value");
    }

    if (SCP_RESULT_FALSE == check_type(context, param->addr, scp_type_link))
    {
        return print_parameter_error(operator_name, parameter_name, "must have link type");
    }

    if (SCP_RESULT_FALSE == check_string_type(context, param->addr))
    {
        return print_parameter_error(operator_name, parameter_name, "must have string format");
    }

    if (SC_RESULT_OK != sc_memory_get_link_content(context, param->addr, &stream))
    {
        return print_parameter_error(operator_name, parameter_name, "content reading error");
    }

    sc_stream_get_length(stream, &length);
    // extra byte needed for string terminator, which is neither counted nor read from an sc_stream
    data = calloc(length + 1, sizeof(sc_char));
    sc_stream_read_data(stream, data, length, &read_length);
    sc_stream_free(stream);

    size_t str_length = strlen(data) + 1;
    *str = calloc(str_length, sizeof(sc_char));
    memcpy(*str, data, str_length);
    free(data);

    return SCP_RESULT_TRUE;
}
Пример #12
0
scp_result resolve_number(sc_memory_context *context, const sc_char *operator_name, const sc_char *parameter_name, scp_operand *param, double *num)
{
    sc_stream *stream;
    sc_uint32 length = 0, read_length = 0;
    sc_char *data1;

    if (SCP_ASSIGN == param->param_type)
    {
        return print_parameter_error(operator_name, parameter_name, "must have FIXED modifier");
    }

    if (SC_FALSE == sc_memory_is_element(context, param->addr))
    {
        return print_parameter_error(operator_name, parameter_name, "has not value");
    }

    if (SCP_RESULT_FALSE == check_type(context, param->addr, scp_type_link))
    {
        return print_parameter_error(operator_name, parameter_name, "must have link type");
    }

    if (SCP_RESULT_FALSE == check_numeric_type(context, param->addr))
    {
        return print_parameter_error(operator_name, parameter_name, "must have numeric format");
    }

    if (SC_RESULT_OK != sc_memory_get_link_content(context, param->addr, &stream))
    {
        return print_parameter_error(operator_name, parameter_name, "content reading error");
    }

    sc_stream_get_length(stream, &length);
    data1 = calloc(length, sizeof(sc_char));
    sc_stream_read_data(stream, data1, length, &read_length);
    sc_stream_free(stream);

    *num = atof(data1);
    free(data1);

    return SCP_RESULT_TRUE;
}
void printIdtf(sc_memory_context *context, sc_addr element)
{
    sc_addr idtf;
    sc_type type;
    sc_memory_get_element_type(context, element, &type);

    if ((sc_type_node & type) == sc_type_node || (sc_type_link & type) == sc_type_link )
    {

        if (SC_RESULT_OK == sc_helper_get_system_identifier(context, element, &idtf))
        {
            sc_stream *stream;
            sc_uint32 length = 0, read_length = 0;
            sc_char *data;
            sc_memory_get_link_content(context, idtf, &stream);
            sc_stream_get_length(stream, &length);
            data = (sc_char *)calloc(length + 1, sizeof(sc_char));
            sc_stream_read_data(stream, data, length, &read_length);
            data[length] = '\0';
            printf("%s", data);
            sc_stream_free(stream);
            free(data);
        }
        else
        {
            printf("%u|%u", element.seg, element.offset);
        }
    }
    else
    {
        sc_addr elem1, elem2;
        sc_memory_get_arc_begin(context, element, &elem1);
        sc_memory_get_arc_end(context, element, &elem2);
        printf("(");
        printIdtf(context, elem1);
        printf(" -> ");
        printIdtf(context, elem2);
        printf(")");
    }
}
Пример #14
0
void test5()
{
    sc_uint32 i;
    sc_addr addr;
    sc_stream *stream = 0;

    printf("Segments count: %d\n", sc_storage_get_segments_count());
    print_storage_statistics();

    timer = g_timer_new();

    printf("Create %d links\n", link_append_count);

    //g_snprintf(test, 1024, "../CMakeLists.txt");
    g_timer_reset(timer);
    g_timer_start(timer);
    for (i = 0; i < link_append_count; i++)
    {
        addr = sc_storage_link_new();

        printf("Created sc-link: seg=%d, offset=%d, content=%d\n", addr.seg, addr.offset, i);

        stream = sc_stream_memory_new((char*)&i, sizeof(i), SC_STREAM_READ, SC_FALSE);
        sc_storage_set_link_content(addr, stream);
        sc_stream_free(stream);
    }

    g_timer_stop(timer);

    printf("Created links: %d\n", link_append_count);
    printf("Links per second: %f\n", link_append_count / g_timer_elapsed(timer, 0));

    printf("Segments count: %d\n", sc_storage_get_segments_count());
    print_storage_statistics();

    g_timer_destroy(timer);
}
Пример #15
0
sctpErrorCode sctpCommand::processFindLinks(quint32 cmdFlags, quint32 cmdId, QDataStream *params, QIODevice *outDevice)
{

    sc_addr addr;
    sc_uint32 data_len = 0;
    sc_char *data = 0;

    Q_UNUSED(cmdFlags);

    Q_ASSERT(params != 0);

    // read length of content data
    READ_PARAM(data_len);
    Q_ASSERT(data_len > 0);

    data = new sc_char[data_len];
    if (params->readRawData(data, data_len) != data_len)
        return SCTP_ERROR_CMD_READ_PARAMS;

    sc_stream *stream = sc_stream_memory_new(data, data_len, SC_STREAM_READ, SC_FALSE);
    sc_uint32 result_count = 0;
    sc_addr *result = 0;

    if (sc_memory_find_links_with_content(stream, &result, &result_count) != SC_RESULT_OK)
        writeResultHeader(SCTP_CMD_FIND_LINKS, cmdId, SCTP_RESULT_FAIL, 0, outDevice);
    else
    {
        writeResultHeader(SCTP_CMD_FIND_LINKS, cmdId, SCTP_RESULT_OK, result_count * sizeof(sc_addr) + sizeof(result_count), outDevice);
        outDevice->write((const char*)&result_count, sizeof(result_count));
        outDevice->write((const char*)result, sizeof(sc_addr) * result_count);
    }

    sc_stream_free(stream);

    return SCTP_ERROR_NO;
}
// --------------------------------------------------
sc_result agent_append_idtf(const sc_event *event, sc_addr arg)
{
    sc_addr arc, el, link, n;
    sc_stream *content = 0;
    sc_uint8 *data = 0;
    sc_uint32 data_len = 0, read_bytes = 0;
    redisReply * reply = null_ptr;

    if (sc_memory_get_arc_end(s_default_ctx, arg, &arc) != SC_RESULT_OK)
        return SC_RESULT_ERROR;

    if (sc_memory_get_arc_begin(s_default_ctx, arg, &n) != SC_RESULT_OK)
        return SC_RESULT_ERROR;

    // get element
    if (sc_memory_get_arc_begin(s_default_ctx, arc, &el) != SC_RESULT_OK)
        return SC_RESULT_ERROR;

    if (sc_helper_check_arc(s_default_ctx, keynode_system_element, el, sc_type_arc_pos_const_perm) == SC_TRUE)
        return SC_RESULT_OK;

    // get sc-link
    if (sc_memory_get_arc_end(s_default_ctx, arc, &link) != SC_RESULT_OK)
        return SC_RESULT_ERROR;

    // get content of sc-link
    if (sc_memory_get_link_content(s_default_ctx, link, &content) != SC_RESULT_OK)
        return SC_RESULT_ERROR;

    // get length of data
    if (sc_stream_get_length(content, &data_len) != SC_RESULT_OK)
    {
        sc_stream_free(content);
        return SC_RESULT_ERROR;
    }

    data = g_malloc0(data_len + 1);
    if (sc_stream_read_data(content, data, data_len, &read_bytes) != SC_RESULT_OK || read_bytes != data_len)
    {
        sc_stream_free(content);
        g_free(data);
        return SC_RESULT_ERROR;

    }
    sc_result res = SC_RESULT_OK;
    sc_addr el_addr = el;

    if (SC_ADDR_IS_EQUAL(n, keynode_nrel_idtf))
        el_addr = link;

    reply = do_sync_redis_command(redisCtx, "SET idtf:%s:%s %b",
                                              SC_ADDR_IS_EQUAL(n, keynode_nrel_main_idtf) ? str_main_idtf_postfix : (SC_ADDR_IS_EQUAL(n, keynode_nrel_idtf) ? str_idtf_postfix : str_sys_idtf_postfix),
                                              data, &el_addr, sizeof(el_addr));
    if (reply == 0 || reply->type == REDIS_REPLY_ERROR)
        res = SC_RESULT_ERROR;

    if (reply)
        freeReplyObject(reply);

    sc_stream_free(content);
    g_free(data);

    return res;
}
Пример #17
0
sctpErrorCode sctpCommand::processGetLinkContent(quint32 cmdFlags, quint32 cmdId, QDataStream *params, QIODevice *outDevice)
{
    sc_addr addr;
    sc_stream *stream = (sc_stream*)nullptr;
    sc_char data_buffer[1024];
    sc_uint32 data_len = 0;
    sc_uint32 data_written = 0;
    sc_uint32 data_read = 0;

    Q_UNUSED(cmdFlags);
    Q_ASSERT(params != 0);

    // read sc-addr of sc-element from parameters
    if (params->readRawData((char*)&addr, sizeof(addr)) != sizeof(addr))
        return SCTP_ERROR_CMD_READ_PARAMS;

    sctpResultCode resCode = (sc_memory_get_link_content(addr, &stream) == SC_RESULT_OK) ? SCTP_RESULT_OK : SCTP_RESULT_FAIL;


    if (resCode == SCTP_RESULT_OK)
    {
        if (sc_stream_get_length(stream, &data_len) != SC_RESULT_OK)
        {
            resCode = SCTP_RESULT_FAIL;
            sc_stream_free(stream);
            stream = (sc_stream*)nullptr;
        }
    }
    // send result
    writeResultHeader(SCTP_CMD_GET_LINK_CONTENT, cmdId, resCode, data_len, outDevice);

    if (resCode == SCTP_RESULT_FAIL)
    {
        if (stream != nullptr)
            sc_stream_free(stream);

        return SCTP_ERROR;
    }

    // write content data
    while (sc_stream_eof(stream) != SC_TRUE)
    {
        // if there are any error to read data, then
        // write null into output
        if (sc_stream_read_data(stream, data_buffer, 1024, &data_read) != SC_RESULT_OK)
        {
            if (data_written < data_len)
            {
                quint32 len = data_len - data_written;
                sc_char *data = new sc_char[len];
                memset(data, 0, len);
                outDevice->write(data, len);
                delete []data;

                sc_stream_free(stream);
                return SCTP_ERROR;
            }
        }

        outDevice->write(data_buffer, data_read);
        data_written += data_read;
    }

    Q_ASSERT(data_written == data_len);

    if (resCode == SCTP_RESULT_OK)
        sc_stream_free(stream);


    return SCTP_ERROR_NO;
}
Пример #18
0
int main(int argc, char* argv[])
{
  if( argc != 2 ) {
    printf("Usage: %s <intf>\n", argv[0]);
    exit(1);
  }
  const char* intf = argv[1];

  /* Attributes are used to specify options.  Examples include specifing
   * the sizes of buffers and tuning options.  For a full list of available
   * attributes run "solar_capture_doc attr".
   *
   * The defaults can be changed by setting the SC_ATTR environment
   * variable, and can be overridden programmatically as shown below.
   */
  struct sc_attr* attr;
  TRY(sc_attr_alloc(&attr));

  /* A SolarCapture session binds together a set of threads and components
   * that are doing a particular job.
   */
  struct sc_session* tg;
  TRY(sc_session_alloc(&tg, attr));

  struct sc_thread* t1;
  TRY(sc_thread_alloc(&t1, attr, tg));
  struct sc_thread* t2;
  TRY(sc_thread_alloc(&t2, attr, tg));

  /* Set the number of packet buffers to be allocated for the VI.  These
   * buffers are used to receive packets from the network adapter.
   */
  int n_bufs = 8192;
  sc_attr_set_int(attr, "n_bufs_rx", n_bufs);
  sc_attr_set_int(attr, "n_bufs_rx_min", n_bufs);

  /* A VI is used to receive packets from the network adapter. */
  struct sc_vi* vi;
  TRY(sc_vi_alloc(&vi, attr, t1, intf));

  /* Specify which packets should be delivered to this VI.  An sc_stream
   * object describes the set of packets wanted.
   *
   * The 'all' stream captures all packets that arrive at the interface,
   * excluding those explicitly steered elsewhere.  (Using the 'all' stream
   * requires administrative privileges because it steals packets away from
   * the kernel stack).
   */
  struct sc_stream* stream;
  TRY(sc_stream_alloc(&stream, attr, tg));
  TRY(sc_stream_all(stream));
  TRY(sc_vi_add_stream(vi, stream));
  TRY(sc_stream_free(stream));

  /* SolarCapture nodes perform packet processing functions such as
   * monitoring, packet modification, writing to disk, I/O etc.  When
   * allocating nodes you can specify node-specific arguments, which may be
   * required or optional.
   *
   * The 'sc_rate_monitor' node measures the packet rate and bandwidth for
   * packets passing through it, and exports that information via
   * solar_capture_monitor.  The 'period' argument gives the sampling
   * period in seconds.  The 'alpha' argument controls smoothing, and must
   * be in the range (0,1].  Values close to 1 give more weight to recent
   * samples, while values close to zero give more weight to previous
   * samples and so provide a greater level of smoothing.
   */
  struct sc_arg args[] = {
    SC_ARG_DBL("alpha",  0.5),
    SC_ARG_DBL("period", 0.1),
  };
  struct sc_node* rate_mon;
  TRY(sc_node_alloc_named(&rate_mon, attr, t2, "sc_rate_monitor", NULL,
                          args, sizeof(args) / sizeof(args[0])));

  /* Connect the VI to the rate monitor. */
  TRY(sc_vi_set_recv_node(vi, rate_mon, NULL));

  /* sc_session_go() starts the threads, and so starts packet handling. */
  sc_session_go(tg);
  while( 1 )
    sleep(100000);

  return 0;
}
Пример #19
0
sc_result sc_helper_set_system_identifier(sc_memory_context const * ctx, sc_addr addr, const sc_char* data, sc_uint32 len)
{
    sc_iterator5 *it5 = 0;
    sc_addr *results = 0;
    sc_uint32 results_count = 0;
    sc_stream *stream = 0;
    sc_uint32 i = 0;
    sc_addr idtf_addr, arc_addr;

    SC_ADDR_MAKE_EMPTY(idtf_addr)
    g_assert(sc_keynodes != 0);

    // check if specified system identifier already used
    stream = sc_stream_memory_new(data, sizeof(sc_char) * len, SC_STREAM_FLAG_READ, SC_FALSE);
    if (sc_memory_find_links_with_content(ctx, stream, &results, &results_count) == SC_RESULT_OK)
    {
        for (i = 0; i < results_count; i++)
        {
            it5 = sc_iterator5_a_a_f_a_f_new(ctx,
                                             0,
                                             sc_type_arc_common | sc_type_const,
                                             results[i],
                                             sc_type_arc_pos_const_perm,
                                             sc_keynodes[SC_KEYNODE_NREL_SYSTEM_IDENTIFIER]);
            if (sc_iterator5_next(it5))
            {
                // don't foget to free allocated memory before return error
                sc_iterator5_free(it5);
                sc_stream_free(stream);
                g_free(results);
                return SC_RESULT_ERROR_INVALID_PARAMS;
            }

            sc_iterator5_free(it5);
        }

        g_free(results);
    }

    // if there are no elements with specified system identitifier, then we can use it
    idtf_addr = sc_memory_link_new(ctx);
    if (sc_memory_set_link_content(ctx, idtf_addr, stream) != SC_RESULT_OK)
    {
        sc_stream_free(stream);
        return SC_RESULT_ERROR;
    }

    // we doesn't need link data anymore
    sc_stream_free(stream);

    // setup new system identifier
    arc_addr = sc_memory_arc_new(ctx, sc_type_arc_common | sc_type_const, addr, idtf_addr);
    if (SC_ADDR_IS_EMPTY(arc_addr))
        return SC_RESULT_ERROR;

    arc_addr = sc_memory_arc_new(ctx, sc_type_arc_pos_const_perm, sc_keynodes[SC_KEYNODE_NREL_SYSTEM_IDENTIFIER], arc_addr);
    if (SC_ADDR_IS_EMPTY(arc_addr))
        return SC_RESULT_ERROR;

    return SC_RESULT_OK;
}
Пример #20
0
sc_addr SCsTranslator::createScAddr(sElement *el)
{
    sc_addr addr;
    SC_ADDR_MAKE_EMPTY(addr);

    if (el->type & sc_type_node)
        addr = sc_memory_node_new(mContext, el->type);
    else if (el->type & sc_type_link)
    {
        addr = sc_memory_link_new(mContext);

        // setup link content
        if (el->link_is_file)
        {
            String file_path;
            if (_getAbsFilePath(el->file_path, file_path))
            {
                sc_stream *stream = sc_stream_file_new(file_path.c_str(), SC_STREAM_FLAG_READ);
                if (stream)
                {
                    sc_memory_set_link_content(mContext, addr, stream);
                    sc_stream_free(stream);
                } else
                {
                    THROW_EXCEPT(Exception::ERR_FILE_NOT_FOUND,
                                 "Can't open file " + el->file_path,
                                 mParams.fileName,
                                 -1);
                }
            } else
            {
                THROW_EXCEPT(Exception::ERR_INVALID_PARAMS,
                             "Unsupported link type  " + el->file_path,
                             mParams.fileName,
                             -1);
            }

        } else
        {           
            sc_stream *stream = sc_stream_memory_new(el->link_data.data.data(), (sc_uint)el->link_data.data.size(), SC_STREAM_FLAG_READ, SC_FALSE);
            sc_memory_set_link_content(mContext, addr, stream);
            sc_stream_free(stream);
        }


        // generate format information
        if (mParams.autoFormatInfo)
        {
            if (el->link_is_file)
            {
				size_t n = el->file_path.find_last_of(".");
                if (n != String::npos)
					generateFormatInfo(addr, el->file_path.substr(n + 1));
            }
        }
    }
    else
    {
        assert(el->arc_src && el->arc_trg);
        if (SC_ADDR_IS_EMPTY(el->arc_src->addr) || SC_ADDR_IS_EMPTY(el->arc_trg->addr))
            return addr;
        addr = sc_memory_arc_new(mContext, el->type, el->arc_src->addr, el->arc_trg->addr);
    }

    el->addr = addr;

    return addr;
}
Пример #21
0
void ScStream::Reset()
{
  if (m_stream)
    sc_stream_free(m_stream);
  m_stream = 0;
}
Пример #22
0
bool GwfTranslator::processString(const String &data)
{
    tinyxml2::XMLDocument doc;
    tinyxml2::XMLError error = doc.Parse(data.c_str());

    if (error != tinyxml2::XML_SUCCESS)
    {
        THROW_EXCEPT(Exception::ERR_PARSE,
                     doc.GetErrorStr2(),
                     mParams.fileName,
                     -1);
    }

    tinyxml2::XMLElement *root = doc.FirstChildElement("GWF");
    if (!root)
    {
        THROW_EXCEPT(Exception::ERR_PARSE,
                     "Can't find root element",
                     mParams.fileName,
                     -1);
    }
    root = root->FirstChildElement("staticSector");
    if (!root)
    {
        THROW_EXCEPT(Exception::ERR_PARSE,
                     "Cna't find static sector",
                     mParams.fileName,
                     -1);
    }

    // collect elements
    std::vector<tinyxml2::XMLElement*> nodes;
    std::vector<tinyxml2::XMLElement*> edges;
    std::vector<tinyxml2::XMLElement*> buses;
    std::vector<tinyxml2::XMLElement*> all;

    static std::string s_arc = "arc";
    static std::string s_pair = "pair";
    static std::string s_bus = "bus";

    tinyxml2::XMLElement *el = root->FirstChildElement();
    while (el)
    {
        all.push_back(el);
        if (el->Name() == s_arc || el->Name() == s_pair)
            edges.push_back(el);
        else if (el->Name() == s_bus)
            buses.push_back(el);
        else
            nodes.push_back(el);

        el = el->NextSiblingElement();
    }

    static std::string s_node = "node";
    static std::string s_contour = "contour";

    tStringAddrMap id_map;
    tStringAddrMap::iterator itId;

    // create all nodes
    std::vector<tinyxml2::XMLElement*>::iterator it, itEnd = nodes.end();
    for (it = nodes.begin(); it != itEnd; ++it)
    {
        el = *it;

        String idtf = el->Attribute("idtf");
        String id = el->Attribute("id");
        sc_addr addr;

        itId = id_map.find(id);
        if (itId != id_map.end())
            continue;

        if (idtf.size() > 0)
        {
            if (getScAddr(idtf, addr))
            {
                id_map[id] = addr;
                continue;    // skip elements that already exists
            }
        }

        if (el->Name() == s_contour)
        {
            addr = sc_memory_node_new(sc_type_const | sc_type_node_struct);
            appendScAddr(addr, idtf);
        } else
        {
            tinyxml2::XMLElement *content = el->FirstChildElement("content");
            if (!content)
            {
                THROW_EXCEPT(Exception::ERR_PARSE,
                             "There are no child content for node with id=" + id,
                             mParams.fileName,
                             -1);
            }

            if (content->IntAttribute("type") == 0)
            {
                addr = sc_memory_node_new(convertType(el->Attribute("type")));
                appendScAddr(addr, idtf);
            } else
            {
                // need to create link
                addr = sc_memory_link_new();
                // setup content
                String data = content->GetText();

                if (content->IntAttribute("type") == 4)
                    data = base64_decode(data);

                sc_stream *stream = sc_stream_memory_new(data.c_str(), data.size(), SC_STREAM_READ, SC_FALSE);
                sc_memory_set_link_content(addr, stream);
                sc_stream_free(stream);

                if (mParams.autoFormatInfo)
                {
                    String ext = StringUtil::getFileExtension(content->Attribute("file_name"));
                    if (!ext.empty())
                        generateFormatInfo(addr, ext);
                }
            }
        }

        if (!idtf.empty())
            sc_helper_set_system_identifier(addr, idtf.c_str(), idtf.size());

        id_map[id] = addr;
    }

    // process buses
    itEnd = buses.end();
    for (it = buses.begin(); it != itEnd; ++it)
    {
        el = *it;

        tStringAddrMap::iterator itOwner = id_map.find(el->Attribute("owner"));
        if (itOwner == id_map.end())
            continue;

        id_map[el->Attribute("id")] = itOwner->second;
    }

    // now create edges
    bool created = true;
    while (created)
    {
        created = false;

        itEnd = edges.end();
        for (it = edges.begin(); it != itEnd; ++it)
        {
            el = *it;

            sc_addr addr;
            String id = el->Attribute("id");
            String idtf = el->Attribute("idtf");

            if (id_map.find(id) != id_map.end())
                continue;

            if (getScAddr(idtf, addr))
                continue;

            // get begin and end elements
            tStringAddrMap::iterator itB = id_map.find(el->Attribute("id_b"));
            if (itB == id_map.end())
                continue;

            tStringAddrMap::iterator itE = id_map.find(el->Attribute("id_e"));
            if (itE == id_map.end())
                continue;

            // create arc
            created = true;
            addr = sc_memory_arc_new(convertType(el->Attribute("type")), itB->second, itE->second);
            appendScAddr(addr, idtf);
            id_map[id] = addr;

            if (!idtf.empty())
                sc_helper_set_system_identifier(addr, idtf.c_str(), idtf.size());
        }
    }

    // now append elemnts into contours
    itEnd = all.end();
    for (it = all.begin(); it != itEnd; ++it)
    {
        el = *it;

        tStringAddrMap::iterator itSelf = id_map.find(el->Attribute("id"));
        if (itSelf == id_map.end())
            continue;

        tStringAddrMap::iterator itP = id_map.find(el->Attribute("parent"));
        if (itP == id_map.end())
            continue;

        sc_memory_arc_new(sc_type_arc_pos_const_perm, itP->second, itSelf->second);
    }

    return false;
}
scp_result printEl(scp_operand *param)
{
    sc_addr element = param->addr;
    sc_addr addr2, addr3;
    sc_addr idtf;
    sc_iterator3 *it = nullptr;
    sc_uint32 out_c = 0, in_c = 0;
    if (SC_FALSE == sc_memory_is_element(param->addr))
    {
        return print_error("printEl", "Parameter has not value");
    }
    if (SC_RESULT_OK == sc_helper_get_system_identifier(element, &idtf))
    {
        sc_stream *stream;
        sc_uint32 length = 0, read_length = 0;
        sc_char *data;
        sc_memory_get_link_content(idtf, &stream);
        sc_stream_get_length(stream, &length);
        data = calloc(length + 1, sizeof(sc_char));
        sc_stream_read_data(stream, data, length, &read_length);
        data[length] = '\0';
        printf("\nPrint element: %s =\n", data);
        sc_stream_free(stream);
        free(data);
    }
    else
    {
        printf("\nPrint element: %u|%u =\n", element.seg, element.offset);
    }

    it = sc_iterator3_a_a_f_new(0, 0, element);
    if (it == 0)
    {
        return SCP_RESULT_ERROR;
    }
    printf("Input arcs:\n");
    while (SC_TRUE == sc_iterator3_next(it))
    {
        in_c++;
        addr2 = sc_iterator3_value(it, 0);
        addr3 = sc_iterator3_value(it, 1);

        if (SC_RESULT_OK == sc_helper_get_system_identifier(addr3, &idtf))
        {
            sc_stream *stream;
            sc_uint32 length = 0, read_length = 0;
            sc_char *data;
            sc_memory_get_link_content(idtf, &stream);
            sc_stream_get_length(stream, &length);
            data = calloc(length + 1, sizeof(sc_char));
            sc_stream_read_data(stream, data, length, &read_length);
            data[length] = '\0';
            if (SCP_RESULT_TRUE == check_type(addr3, scp_type_arc_access))
            {
                printf("\t%s <- ", data);
            }
            else
            {
                printf("\t%s <= ", data);
            }
            sc_stream_free(stream);
            free(data);
        }
        else
        {
            if (SCP_RESULT_TRUE == check_type(addr3, scp_type_arc_access))
            {
                printf("\t%u|%u <- ", addr3.seg, addr3.offset);
            }
            else
            {
                printf("\t%u|%u <= ", addr3.seg, addr3.offset);
            }
        }
        if (SC_RESULT_OK == sc_helper_get_system_identifier(addr2, &idtf))
        {
            sc_stream *stream;
            sc_uint32 length = 0, read_length = 0;
            sc_char *data;
            sc_memory_get_link_content(idtf, &stream);
            sc_stream_get_length(stream, &length);
            data = calloc(length + 1, sizeof(sc_char));
            sc_stream_read_data(stream, data, length, &read_length);
            data[length] = '\0';
            printf("%s;\n", data);
            sc_stream_free(stream);
            free(data);
        }
        else
        {
            printf("%u|%u;\n", addr2.seg, addr2.offset);
        }
    }
    sc_iterator3_free(it);
    printf("Total input arcs: %d\n", in_c);

    it = sc_iterator3_f_a_a_new(element, 0, 0);
    if (it == 0)
    {
        return SCP_RESULT_ERROR;
    }
    printf("Output arcs:\n");
    while (SC_TRUE == sc_iterator3_next(it))
    {
        out_c++;
        addr2 = sc_iterator3_value(it, 1);
        addr3 = sc_iterator3_value(it, 2);

        if (SC_RESULT_OK == sc_helper_get_system_identifier(addr2, &idtf))
        {
            sc_stream *stream;
            sc_uint32 length = 0, read_length = 0;
            sc_char *data;
            sc_memory_get_link_content(idtf, &stream);
            sc_stream_get_length(stream, &length);
            data = calloc(length + 1, sizeof(sc_char));
            sc_stream_read_data(stream, data, length, &read_length);
            data[length] = '\0';
            if (SCP_RESULT_TRUE == check_type(addr2, scp_type_arc_access))
            {
                printf("\t%s -> ", data);
            }
            else
            {
                printf("\t%s => ", data);
            }
            sc_stream_free(stream);
            free(data);
        }
        else
        {
            if (SCP_RESULT_TRUE == check_type(addr2, scp_type_arc_access))
            {
                printf("\t%u|%u -> ", addr2.seg, addr2.offset);
            }
            else
            {
                printf("\t%u|%u => ", addr2.seg, addr2.offset);
            }

        }
        if (SC_RESULT_OK == sc_helper_get_system_identifier(addr3, &idtf))
        {
            sc_stream *stream;
            sc_uint32 length = 0, read_length = 0;
            sc_char *data;
            sc_memory_get_link_content(idtf, &stream);
            sc_stream_get_length(stream, &length);
            data = calloc(length + 1, sizeof(sc_char));
            sc_stream_read_data(stream, data, length, &read_length);
            data[length] = '\0';
            printf("%s;\n", data);
            sc_stream_free(stream);
            free(data);
        }
        else
        {
            printf("%u|%u;\n", addr3.seg, addr3.offset);
        }
    }
    sc_iterator3_free(it);
    printf("Total output arcs: %d\n", out_c);
    return SCP_RESULT_TRUE;
}