Typed Data Transfer (TDT) User's Guide

Ciaron Linstead <linstead@pik-potsdam.de>

Revision History:

04/03/2002 CL - First version.

12/09/2003 CL - Second version, extensive revision

1 Introduction

The Typed Data Transfer (TDT) Library provides a simple, consistent interface for the transmission of data between programs in a platform- and language-independent way. It moves the complexities of handling data types and data sources into a self-contained library of functions.

The purpose of this document is to explain the use of the TDT function library when writing programs in C or Fortran. Some example programs are included and will be explained.

The TDT functions are written in C, and are provided with Fortran interface functions for using the library in Fortran programs. Opening and closing of sockets and files are handled by TDT functions, and data is written or read by means of a call to the appropriate TDT function.

Apart from adding function calls the code, a programmer must also provide an XML (eXtensible Markup Language) description of the data to be transferred and a configuration file for each program, also in XML. Each data structure being transferred needs its own XML description, each of which may be in separate XML files, or in just one.

2 Using the TDT in C

2.1 Prerequisites

2.1.1 "#include"s

To include the TDT library, simply

#include "tdt.h"

2.1.2 Declarations

It is necessary to declare a variable of type TDTState. This variable serves as a unique identifier for each data item being transferred, by bundling an XML description and a descriptor for the communication channel.

A variable of type TDTConfig is also required. This is used to refer to the data which will be read from a configuration file.

Examples:

TDTState ts;

TDTConfig tc;

2.2 User functions

2.2.1 tdt_configure()

Purpose

Read and parse the specified configuration file.

Input parameter

String config_filename /* the name of the configuration file */

                       /* to be parsed */

Output parameter

TDTConfig tc /* a parsed copy of the configuration file */
Example:

tc = tdt_configure (``config.xml'');

2.2.2 tdt_open()

Purpose

tdt_open() opens the communication channel specified by the connection name

Input parameters

TDTState ts            /* the TDTState variable */

TDTConfig tc           /* the TDTConfig variable */

String connection_name /* the name of the connection to open */

Output parameter

TDTState ts     /* a completed TDTState variable */
Example:

ts = tdt_open (ts, tc, "client_to_server");

2.2.3 tdt_read()

Purpose

To read the data specified by the given XML identifier, from the connection given in the TDTState parameter.

Input parameters

void *value /* the location at which to store the incoming data */

String name /* the name of the data to be read, */

            /* as it appears in the XML description */

TDTState ts /* The identifier for this data transfer */

Output parameters

None.

Example

tdt_read (&astruct, "astruct", ts);

2.2.4 tdt_write()

Purpose

To write data to the connection given by the TDTState parameter as per the XML identifer string (parameter "name").

Input parameters

void *value /* pointer to the data being written */

String name /* the name of the data to be written, */

            /* as it appears in the XML description */

TDTState ts /* The identifier for this data transfer */

Output parameters

None.

Example

tdt_write (&astruct, "astruct", ts);

2.2.5 tdt_close()

Purpose

Closes the connection or open files and frees TDT-allocated memory.

Input parameters

TDTState ts /*The identifier of the connection to be closed */
Output parameters

None.

Example

tdt_close(ts);

2.2.6 tdt_end()

Purpose

Frees memory allocated by the TDT for the configuration information.

Input parameters

TDTConfig tc  /* The identifier of the */

              /* configuration data */

Output parameters

None.

Example

tdt_close(tc);

2.3 Compiling and linking

2.3.1 Compiling the library

The TDT can be compiled as a library, libtdt.a. This must be in the library path of the development environment, or a directory specified by the -L flag in gcc. A makefile is included with the source files. Running 'make lib' from within the tdt source directory will rebuild the TDT library.

2.3.2 Linking with your own programs

First build the library libtdt.a as explained above.

Compile your application...

gcc -c -I<location of tdt.h> -Wall <program>.c
... and link:

gcc <program>.o -L<location of TDT library> -ltdt -lexpat -o<program>
where <program>.o is the output from the compilation of <program>.c

This assumes that the Expat XML parser library is available system-wide on your machine. If it is not, and you want to use the Expat library supplied with the TDT, use the following link command:

gcc <program>.o -L<location of TDT library> /

         -ltdt -L<location of Expat library> -lexpat -o<program>

3 Using the TDT in Fortran

3.1 Prerequisites

3.1.1 Declarations

The following declarations are needed:

INTEGER tdtstate

INTEGER tdtconfig

3.2 User functions

3.2.1 tdt_fconfigure()

Purpose

Read and parse the specified configuration file.

Input parameter

C the name of the configuration file

configfilename

Output parameter

C a parsed copy of the configuration information

tdtconf

Example

CALL tdt_fconfigure(tdtconf, 'config.xml');

3.2.2 tdt_fopen()

Purpose

tdt_open() opens the communication channel specified by the connection name.

Input parameters

C a TDTState variable

tdtstate

C a TDTConfig variable

tdtconf

C the connection name

connection

Output parameter

C a completed TDTState variable

tdtstate

Examples:

CALL tdt_fopen_socket (tdtstate, tdtconf, 'client_to_server')

3.2.3 tdt_fwrite()

Purpose

To write data to the connection given by the TDTState parameter as per the XML identifer string (parameter "name").

Input parameters

C The identifier for this data transfer

tdtstate

C the data being written

val

C a string containing the name of the data to be written, 

C as it appears in the XML description 

name

Output parameter

None.

Example

CALL tdt_fwrite (tdtstate, astruct, 'astruct');

3.2.4 tdt_fread()

Purpose

To read data from the connection given by the TDTState parameter as per the XML identifer string (parameter "name").

Input parameters

C The identifier for this data transfer

tdtstate

C the data being read

val 

C a string containing the name of the data to be read, 

C as it appears in the XML description

name 

Output parameter

None.

Example

CALL tdt_fread (tdtstate, astruct, 'astruct');

3.2.5 tdt_fclose()

Purpose

Closes the connection or open files and frees TDT-allocated memory.

Input parameters

C The identifier of the connection to be closed

ts 

Output parameter

None.

Example

CALL tdt_fclose(tdtstate);

3.2.6 tdt_fend()

Purpose

Frees memory allocated by the TDT for the configuration information.

Input parameters

C The identifier of the stored configuration data

tc 

Output parameter

None.

Example

CALL tdt_fend(tdtconf);

3.3 Compiling and linking

3.3.1 Compiling the library

The TDT can be compiled as a library, libtdt.a. This must be in the library path of the development environment, or a directory specified by the -L flag in gcc. A makefile is included with the source files. Running 'make lib' from within the tdt source directory will rebuild the TDT library.

3.3.2 Linking with your own programs

First build the library libtdt.a as explained above.

Compile your application...

xlf -c -Wall <program>.f
... and link:

xlf <program>.o -L../tdt -o <program> -ltdt -lexpat
where <program>.o is the output from the compilation of <program>.f

This assumes that the Expat XML parser library is available system-wide on your machine. If it is not, and you want to use the Expat library supplied with the TDT, use the following link command:

xlf <program>.o -L<location of TDT library> /

         -ltdt -L<location of Expat library> -lexpat

3.4 Error conditions

The user functions of the TDT will crash, returning with error code 1, if an error occurs. It will also output a debugging message to stderr.

4 TDT Data descriptions in XML

This section will not explain XML syntax. For a short introduction to XML, see here:

http://www.w3schools.com/xml/default.asp

4.1 <data_desc> tags

This is the root element (or document element) required by XML.

Attributes : none.

4.2 <decl> tag

Each XML description of a model is made up of one or more variable declarations, which are identified by <decl> tags.

Attributes : name="abcde" where abcde is one or more alphanumeric characters.

4.3 <struct> tag

The <struct> and </struct> tags surround one or more declarations, analogous to a struct declaration in C.

Attributes : none.

4.4 <array> tag

The <array> and </array> tags surround a text element indicating the primitive type, or a declaration if the array is made up of composite types.

Attributes : size="n" where n is an integer.

4.5 <addr> tag

The <addr> and </addr> tags surround declarations, indicating that this is a pointer to a value, not the value itself.

Attributes : none.

4.6 Text elements

Text elements are the value which appear between start and end tags. For TDT XML, text elements are used to indicate the primitive data type of a declaration (<decl>) or an array (<array>).

The allowed values are "int", "double", "float", and "char".

4.7 Example

The following XML

<data_desc>

  <decl name="astruct">

    <struct>

      <decl name="anarray">

        <array size="2">int</array></decl>

        <decl name="adouble">double</decl>

    </struct>

  </decl>

</data_desc>

represents the C declaration:

struct {

  int anarray[2];

  double adouble;

} astruct;

5 TDT Configuration files

The configuration files used by TDT are also written in XML.

5.1 <program> tag

The document-level tag for a configuration file is the <program> tag.

Attributes: name=''abcde'', where ``abcde'' is an alphanumeric string.

5.2 <input_channels> tag

An <input_channel> tag is made up of zero or more <channel> tags which, as the name suggests, are the channels on which the program will read data.

Attributes: none

5.3 <output_channels> tag

This tag is made up of zero or more <channel> tags, and groups together the channels on which output data will be sent.

Attributes: none

5.4 <channel> tag

The <channel> tag specifies all the information required to establish a TDT connection and send or receive the data in the correct format.

Attributes:

name : the name by which the channel will be referenced in the code.

type : the type of the connection: "socket" | "file"

host : if type is "socket", the hostname to use

port : if type is "socket", the port to use

filename : if type is "file", the filename to read/write from/to

datadesc : the name of the datadesc XML which describes the data that will pass on this channel

5.5 Example configuration file

<program name="clnt">

    <input_channels>

        <!- mode is implicitly READ ->

    </input_channels>

    <output_channels>

        <!- mode is implicitly WRITE ->

        <channel name="clnt_to_serv" 

                 host="localhost" 

                 port="2222" 

                 type="socket" 

                 datadesc="example.xml">

        </channel> 

    </output_channels>

</program> 

6 Sample applications

The sample applications are provided with Makefiles in order to compile and link the programs.

6.1 C Examples

The sample C programs (testclnt.c and testserv.c in the tests subdirectory of the TDT source directory) demonstrate writing data from a client application (testclnt) to a server application (testserv). testclnt writes three variables (a double, a struct containing an array of int and a double, and an int) to testserv. In this case, only one TDTState (for one XML description and one communication channel) is required.

Building the examples

Type "make all" from within the tdt/tests directory.

In tdt/tests this will create the programs "serv" and "clnt".

6.2 Fortran examples

The sample Fortran applications are two programs (prog1.f and prog2.f in the ftdt/tests subdirectory) which read and write data to/from each other. prog1 writes a 2x5 matrix of INTEGERs for prog2 to read and reads a 5x2 matrix of doubles (REAL*8) which is written by prog2.

Two XML files are provided ("example.xml" and "example2.xml"). These describe the two matrices. In these examples the XML files are expected to be in the same directory as the programs reading them.

Two TDTState variables are used by each program (tdtstate1 and tdtstate2). Each TDTState contains information about one matrix and the channel by which that data will be transferred.

Building the examples

Type "make all" from within the ftdt/tests directory.

In ftdt/tests, the programs will be called "prog1" and "prog2".

7 "make" parameters

The following parameters can be passed to make:

FF : specifies the name of the Fortran compiler (default is xlf).

EXPAT : specifies the location of the Expat libraries (default is ../../expat).

OS : specifies the operating system (Linux or AIX). This is used to select the appropriate Expat library (default is Linux).

Example:

make FF=g77 EXPAT=/expat_1.95.2 OS=AIX all
"make clean" will remove old object (*.o) files and executable programs from the tdt/tests and tdt directories (or ftdt/tests and ftdt).

8 Further information

If you need further information, have comments or requests for enhancements, or you've found a bug, you can contact the authors:

Ciaron Linstead <linstead@pik-potsdam.de>

Cezar Ionescu <ionescu@pik-potsdam.de>

Dan Beli <beli@pik-potsdam.de>

About this document ...

Typed Data Transfer (TDT) User's Guide

This document was generated using the LaTeX2HTML translator Version 2002 (1.62)

Copyright © 1993, 1994, 1995, 1996, Nikos Drakos, Computer Based Learning Unit, University of Leeds.
Copyright © 1997, 1998, 1999, Ross Moore, Mathematics Department, Macquarie University, Sydney.

The command line arguments were:
latex2html -split 0 -nonavigation -show_section_numbers user_guide-v2.tex

The translation was initiated by Ciaron Linstead on 2003-09-13


Ciaron Linstead 2003-09-13