Truxton message get file

From truxwiki.com
Jump to navigation Jump to search

This will give you a barely functioning file object from the message.

Syntax

uint64_t truxton_message_get_file( uint64_t message_handle );

Parameters

message_handle

The handle created by the truxton_message_create or truxton_etl_get_message call.

Return value

The a file handle suitable for reading contents.

Remarks

The purpose of this API is to get the ETL author the fastest access to the file's contents. It pulls the information out of the message object and partially populates a file object with it. The database is not called so not all data items of the file object are there. The following API's are valid on a file object created in this way.

Sample

#include <TruxtonCAPI.h>

void main( void )
{
    char file_id[ 65 ];

    uint64_t message = 0;
    uint64_t file_handle = 0;
    uint64_t app = truxton_etl_create();

    truxton_etl_set_application_name( app, "Sample C ETL" );
    truxton_etl_set_stage_number( app, 4 );
    truxton_etl_add_desired_file_type( app, Type_JPEG );
    truxton_etl_set_queue_name( app, "SampleC" );

    truxton_etl_set_description( app, "A sample of how to write an ETL");

    // Pause here until we get a message from the "SampleC" message queue
    message = truxton_etl_get_message( app );

    while( message != 0 )
    {
        // Do something with the message
        memset( file_id, 0x00, sizeof( file_id ) );
        truxton_message_get_file_id( message, file_id, sizeof( file_id ) );

        printf( "Received file id %s\n", file_id );

        file_handle = truxton_message_get_file( message );

        if ( file_handle != 0 )
        {
            if ( truxton_file_get_is_eliminated( file_handle ) == 0 )
            {
               // We've got some contents to play with (DANGER! file could be bigger than RAM!)
               uint64_t file_length = truxton_file_get_length( file_handle );
               uint8_t * buffer = malloc( file_length );

               if ( buffer != NULL )
               {
                   if ( truxton_file_read( file_handle, buffer, file_length ) == file_length )
                   {
                      printf( "We successfully read %" PRIu64 " bytes\n", file_length );
                   }

                   free(buffer);
               }               
            }

            truxton_file_free( file_handle );
        }

        truxton_message_destroy( message );

        // Pause here until we get another message from the "SampleC" message queue
        message = truxton_etl_get_message( app );
    }
 
    truxton_etl_destroy( app );
}

The PRIu64 in the sample code above is a standard way of formatting a 64-bit unsigned integer in C. Over the years, different compilers on different operating systems used different format specifiers for things, these PRI macros, along with some tricky string concatenation the compilers perform for you, allow you to maintain a single code base without a bunch of macro magic.