LWMsgStatus
lwmsg_archive_read_message_fd(
    LWMsgArchive* archive,
    LWMsgMessage* message
    )
{
    LWMsgStatus status = LWMSG_STATUS_SUCCESS;
    readinfo info;
    LWMsgBuffer buffer = {0};
    LWMsgTypeSpec* type = NULL;
    size_t message_size = 0;

    BAIL_ON_ERROR(status = lwmsg_archive_read_message_header(archive, message, &message_size));
    BAIL_ON_ERROR(status = lwmsg_protocol_get_message_type(archive->base.prot, message->tag, &type));

    info.archive = archive;
    info.remaining = message_size;
    buffer.base = info.data;
    buffer.end = buffer.base;
    buffer.cursor = buffer.base;
    buffer.wrap = lwmsg_archive_read_message_wrap_fd;
    buffer.data = &info;

    /* Unmarshal the message payload */
    BAIL_ON_ERROR(status = lwmsg_data_unmarshal(archive->data_context, type, &buffer, &message->data));

error:

    return status;
}
Exemple #2
0
LWMsgStatus
lwmsg_assoc_print_message_alloc(
    LWMsgAssoc* assoc,
    LWMsgMessage* message,
    char** result
    )
{
    LWMsgStatus status = LWMSG_STATUS_SUCCESS;
    LWMsgDataContext* context = NULL;
    LWMsgTypeSpec* type = NULL;
    LWMsgBuffer buffer = {0};
    const char* tag_name = NULL;
    unsigned char nul = 0;

    buffer.wrap = realloc_wrap;
    buffer.data = (void*) &assoc->context;
    
    BAIL_ON_ERROR(status = lwmsg_data_context_new(&assoc->context, &context));
    BAIL_ON_ERROR(status = lwmsg_protocol_get_message_name(assoc->prot, message->tag, &tag_name));
    BAIL_ON_ERROR(status = lwmsg_protocol_get_message_type(assoc->prot, message->tag, &type));
    
    if (type)
    {
        BAIL_ON_ERROR(status = lwmsg_buffer_print(&buffer, "%s: ", tag_name));
        BAIL_ON_ERROR(status = lwmsg_data_print_graph(
                          context,
                          type,
                          message->data,
                          4,
                          &buffer));

        BAIL_ON_ERROR(status = lwmsg_buffer_write(&buffer, &nul, 1));
    }
    else
    {
        BAIL_ON_ERROR(status = lwmsg_buffer_print(&buffer, "%s", tag_name));
    }
    
    *result = (char*) buffer.base;

cleanup:

    if (context)
    {
        lwmsg_data_context_delete(context);
    }

    return status;

error:

    *result = NULL;

    if (buffer.base)
    {
        lwmsg_context_free(&assoc->context, buffer.base);
    }

    goto cleanup;
}
Exemple #3
0
LWMsgStatus
lwmsg_assoc_destroy_message(
    LWMsgAssoc* assoc,
    LWMsgMessage* message
    )
{
    LWMsgStatus status = LWMSG_STATUS_SUCCESS;
    LWMsgTypeSpec* type = NULL;
    LWMsgDataContext* context = NULL;

    if (message->tag != -1)
    {
        BAIL_ON_ERROR(status = lwmsg_protocol_get_message_type(assoc->prot, message->tag, &type));

        if (type != NULL)
        {
            BAIL_ON_ERROR(status = lwmsg_data_context_new(&assoc->context, &context));
            BAIL_ON_ERROR(status = lwmsg_data_free_graph(context, type, message->data));
        }

        message->tag = -1;
        message->data = NULL;
    }

error:

    if (context)
    {
        lwmsg_data_context_delete(context);
    }

    return status;
}
LWMsgStatus
lwmsg_archive_write_message_fd(
    LWMsgArchive* archive,
    LWMsgMessage* message
    )
{
    LWMsgStatus status = LWMSG_STATUS_SUCCESS;
    unsigned char data[2048];
    ArchiveMessage header = {0};
    LWMsgBuffer buffer = {0};
    LWMsgTypeSpec* type = NULL;
    off_t header_offset = archive->offset;
    off_t end_offset = 0;

    BAIL_ON_ERROR(status = lwmsg_protocol_get_message_type(archive->base.prot, message->tag, &type));

    buffer.base = data;
    buffer.end = data + sizeof(data);
    buffer.cursor = buffer.base;
    buffer.wrap = lwmsg_archive_write_message_wrap_fd;
    buffer.data = archive;

    /* Leave room to write the header in later */
    BAIL_ON_ERROR(status = lwmsg_archive_seek_fd(archive, header_offset + sizeof(header)));

    /* Write the marshaled data payload */
    BAIL_ON_ERROR(status = lwmsg_data_marshal(archive->data_context, type, message->data, &buffer));

    end_offset = archive->offset;

    /* Go back and write the header */
    BAIL_ON_ERROR(status = lwmsg_archive_seek_fd(archive, header_offset));
    lwmsg_archive_populate_message_header(archive, message, end_offset - header_offset - sizeof(header), &header);
    BAIL_ON_ERROR(status = lwmsg_archive_write_fd(archive, &header, sizeof(header)));

    /* Seek back to end of message stream */
    BAIL_ON_ERROR(status = lwmsg_archive_seek_fd(archive, end_offset));

error:

    return status;
}
Exemple #5
0
LWMsgStatus
lwmsg_assoc_free_graph(
    LWMsgAssoc* assoc,
    LWMsgTag mtype,
    void* object
    )
{
    LWMsgStatus status = LWMSG_STATUS_SUCCESS;
    LWMsgTypeSpec* type = NULL;
    LWMsgDataContext* context = NULL;

    BAIL_ON_ERROR(status = lwmsg_protocol_get_message_type(assoc->prot, mtype, &type));
    BAIL_ON_ERROR(status = lwmsg_data_context_new(&assoc->context, &context));
    BAIL_ON_ERROR(status = lwmsg_data_free_graph(context, type, object));

error:

    if (context)
    {
        lwmsg_data_context_delete(context);
    }

    return status;
}