All examples in this introduction contain only code lines to explain a special aspect of the API. Important things like error handling are left out in favor of better readability.
The libTML-c API is a set of functions operating on handles of objects. Two fundamental modules are available. TML (The Missing Link) provides the messaging and network functions and SIDEX (Simple Data Exchange) provides the API to access message data. After installing the library you can include TML and SIDEX into your project.
The library does not need any kind of initialization. You can start creating your first TML enabled application.
To use TML messaging, an application creates a TMLCore object. The function tml_Core_Open()
is creating the TMLCore and returns a handle. All subsequent calls to TMLCore methods use the handle. A single TMLCore processes inbound and outbound traffic. To accept inbound traffic a listener is started.
This example shows how to create a TMLCore and a profile with a single function.
Basically a profile is an interface providing one or more commands with unique IDs. The profile is identified by a unique string. Any string can be used but it has to be unique and it is recommended to use a URN (Unified Resource Name). The first part of the URN should be a domain name registered by the creator of the interface to make sure it does not conflict with other interfaces. If no registered domain is available, select any string that is as unique as possible. Multiple profiles can be registered at a single TMLCore with tml_Profile_Register()
. There is no need for another listener if the application provides more than one interface.
This command handler function is printing a message on the console to signal an incoming call. The msg
parameter is the received command. The data
parameter is a pointer to a custom object registered with the command.
Multiple functions are registered by repeating the call to tml_Profile_Register_Cmd()
.
After the call to tml_Core_Set_ListenerEnabled()
incoming traffic is processed by the handler function in different threads.
If the handler function is not thread safe, the application may become unstable. For example accessing global data structures from multiple threads may lead to unpredictable results.
If the communication is no longer needed within the application, a call to tml_Core_Close()
disposes all related objects and the TMLCore.
Sending messages via TML is also accomplished using a TMLCore object. Applications already listening for incoming traffic don't need to instantiate another TMLCore but can send with the the present one.
Two methods of message calls are available:
This example is creating a message object with tml_Cmd_Create()
and assigns a command ID with tml_Cmd_Header_SetCommand()
. From the previous example 4711 is used. To call the remote function a call to tml_Send_SyncMessage()
sends the message and waits for a reply. If an error occurred on the wire an error code is added to the message header and is checked with tml_Cmd_Header_GetError()
. If the error code is TML_SUCCESS
, everything is OK.
The TML Messaging Suite handles all data with the SIDEX API. Calling the function tml_Cmd_Acquire_Sidex_Handle()
returns a reference to a SIDEX_HANDLE representing a SIDEX document and locks the access to it for other threads. TML is multithreaded and the lock is required for synchronization. After accessing the data the lock has to be removed with tml_Cmd_Release_Sidex_Handle()
.
A SIDEX document is organized in groups and keys, identified with strings. Unicode strings can be used to identify parameters in any language. A set of functions allows to add c-types directly to the document.
The functions
sidex_Variant_Write()
andsidex_Variant_Read()
are used to add SIDEX variant values. Container types like lists, dictionaries and tables are available to combine a set of values and allow hieararchical structures.
The following example shows how to add some parameters to a call and get the result. The interface function from the first section has to be modified to do something meaningful. The sum of two float values are calculated and returned as a result.
Calling the remote function and error handling is shown in the previous section. The code below shows how to extend the example to call the sum function with parameters.
TML and SIDEX API functions return error codes. If no error was found SIDEX_SUCCESS
or TML_SUCCESS
is returned. Any value different from SIDEX_SUCCESS
or TML_SUCCESS
needs special attention. The example shows a simple error handling.