Chapter 2. Client Program

Table of Contents

2.1. Characteristics and Components of a Program
2.2. Development Environments and Tools
2.3. Program Flow
2.4. Compiling a Program
2.5. Starting and Terminating a Process
2.5.1. sdlc
2.5.2. fdlc

This chapter describes client program characteristics, components, flow, and development environment.

2.1. Characteristics and Components of a Program

Tmax provides functions for the communication network and buffer management, which enable easy development of application programs. These functions are offered as a library and compiled together with application programs. Developers can develop client programs without knowing the origin of services and servers.

Once a client program is coded, an executable file is created after a successful compilation. In order to compile a client program, the following are required: a client program written by developers, the Tmax client library, a struct file (if a struct buffer is used), and a field table definition file (if a field key buffer is used).

A client program consists of the following components:

  • Client program

    A client program written by developers.

  • Client library

    A library (libcli.a / libcli.so) provided by Tmax. It consists of function object codes used for developing client programs.

  • Struct file (.s)

    A struct file of <filename.s> is required to use structs (STRUCT, X_C_TYPE, or X_COMMON) in a client program. The struct file is precompiled by the sdlc command to create a binary file. The binary file contains information required to convert each struct data into a standard data type. This is used to send/receive data in standard format when a client program is run.

  • Field key file (.f)

    A field definition file of <filename.f> is required when a field key buffer is used. When the file is compiled by the fdlc command, the field key buffer file creates <field key buffer name_fdl.h> through key mapping and is used as a program. Unlike conventional struct files, users are allowed to modify the field values selectively to prevent resource waste. However, overhead is inevitable during key mapping.

2.2. Development Environments and Tools

Tmax client programs can be developed in the following environments using the listed tools:

  • Development environment

    UNIX, Windows NT, Windows 95/98/2000, Windows 3.1, MS-DOS, etc.

  • Development tool

    ANSI C, VC++, BC++, VB, VB .Net, C#, Delphi, PowerBuilder, Embedded VC, etc.

Note

For more information about program development using development tools, refer to "Tmax Programming Guide(4GL)".

2.3. Program Flow

A client program passes a request from a user to a server, and returns the results from the server to the user.

The following are the steps for writing a client program:

main()
{
        Allocate a buffer for tpstart.
        Initialize a buffer for tpstart.

        Connect to Tmax.
        Allocate a buffer for data transmission and reception
        while {
            Input user requests into the transmit message buffer.
            Send the transmit message buffer to a server (service request).
            Receive a response from the server with the receive message buffer.
            Show the receive message buffer to users.
        }
        Deallocate the transmit/receive message buffer.

        Disconnect from Tmax.
}

The following is the flow of a client program:

[Figure 2.1] Client Program Flow

Client Program Flow

The following is the process of each function in a client program:

[Figure 2.2] Process of Functions in a Client Program

Process of Functions in a Client Program


Note

For more information about functions related to communication networks and buffer management for application development, refer to "Chapter 9. Client API" or "Tmax Reference Guide".

The following describes the major functions of a client program.

  • Functions for Tmax communication environment

    FunctionDescription
    tmaxreadenv

    Defines Tmax environment variables that are referenced when executing a Tmax client program in a file.

    By specifying a file path and name when writing a program that calls an API, a file pointer to the specified file can be used to parse the file content when the client program is executed. Tmax environment variables, such as TMAX_HOST_ADDR and TMAX_HOST_PORT, are loaded into the allocated memory when a client program is executed and used to call related API functions.

  • Functions for connecting/disconnecting a client to/from a server

    FunctionDescription
    tpstartRequests a socket connection to CLL and passes the accepted connection to CLH. TMAX_HOST_ADDR and TMAX_HOST_PORT are used to call tpstart().
    tpendTerminates a socket connection using CLL process.
  • Functions for allocating and releasing a buffer

    FunctionDescription
    tpallocAllocates the required amount of memory for data transactions between clients and servers via Tmax. Dynamically allocated memory must be explicitly released.
    tpfreeReleases memory that was dynamically allocated using tpalloc(). If an allocated memory buffer is not returned, garbage (memory leakage) is created.
  • Functions for synchronous communication

    FunctionDescription
    tpcallSends a service request and transmitted data to CLH. CLH checks the requested service, and transfers it to a server application process. The client waits until it receives a response for the request.
  • Functions for asynchronous communication

    FunctionDescription
    tpacall

    Sends a service request and transmitted data to CLH. CLH checks the requested service, and transfers it to a server application process.

    The client executes the next logic immediately after calling tpacall() without waiting for a response.

    tpgetrply

    Requests response data from CLH using the value of tpcall() parameter (cd). If the response data for the corresponding client exists, the data is immediately sent to the client.

    According to the value in the flags parameter, tpgetrply waits until it receives the response data, or sends a reception error to a client.

2.4. Compiling a Program

Compile a client program to create an object file.

The following shows the process of compiling a client program using a struct buffer or fielded buffer.

[Figure 2.3] Compiling a Client Program Using a Struct Buffer

Compiling a Client Program Using a Struct Buffer


[Figure 2.4] Compiling a Client Program Using a Fielded Buffer

Compiling a Client Program Using a Fielded Buffer


The following are the steps for compiling a client program.

  1. Create a UNIX configuration file.

    Set the environment variables needed to connect to Tmax. (.profile, .login, and .cshrc)

    TMAX_HOST_ADDR = Tmax address (=IP Address)
    TMAX_HOST_PORT = Port number (default : 8888)
  2. Write a client program (client.c).

    Develop a program that requests a service from a server and receives a response from the server.

  3. Compile the client program with the libraries (libcli.a / libcli.so).

  4. Compile the struct header file if a struct buffer is used. A dummy struct is required if STRUCT, X_C_TYPE, or X_COMMON buffer is used. Compile the header file with sdlc to convert it to a standard communication type. The created sdl file is required to execute the client program.

    If a field buffer is used, compile the field key file with fdlc.

The following is an example of a Makefile for a Tmax client program:

TARGET = <clientname> 
APOBJS = $(TARGET).o

TMAXLIBD = $(TMAXDIR)/lib

#TMAXLIBS is different according to OS.
#Solaris : TMAXLIBS = -lsocket -lnsl –lcli 
#Compac, HP, IBM, Linux : TMAXLIBS= -lcli

TMAXLIBS = -lcli 

#CFLAGS is different according to OS.
#Solaris 32bit, Compaq, Linux: CFLAGS = -O –I$(TMAXDIR)
#Solaris 64bit: CFLAGS = -xarch=v9 -O –I$(TMAXDIR)
#HP 32bit: CFLAGS = -Ae -O –I$(TMAXDIR)
#HP 64bit: CFLAGS = -Ae +DA2.0W +DD64 +DS2.0 -O –I$(TMAXDIR)
#IBM 32bit: CFLAGS = -q32 –brtl -O –I$(TMAXDIR
#IBM 64bit: CFLAGS = -q64 –brtl -O –I$(TMAXDIR

CFLAGS = -O –I$(TMAXDIR)

#
.SUFFIXES : .c

.c.o:
        $(CC) $(CFLAGS) -c $<

#
# client compile
#

$(TARGET): $(APOBJS)
         $(CC) $(CFLAGS) -L$(TMAXLIBD) -o $(TARGET) $(APOBJS) $(TMAXLIBS) 

#
clean:
        -rm -f *.o core $(TARGET)

The sample program provided by Tmax executes "make" using the shell script named "compile". To use the shell script, type the following into a command prompt. In "compile", enter the client program name without the extension into EXE of the Makefile.

compile c cli (=>client program name without its extension)

2.5. Starting and Terminating a Process

Set environment variables to start Tmax.

Since a client program gets necessary information from environment variables when connecting to Tmax, TMAX_HOST_ADDR and TMAX_HOST_PORT must be defined in the UNIX shell configuration file (c shell: .cshrc, korn shell: .profile, bash shell: .bash_profile, etc.). TMAX_BACKUP_ADDR and TMAX_BACKUP_PORT can also be defined to handle possible failures, and TMAX_CONNECT_TIMEOUT can be defined to handle possible network failures.

Client processes can be created after Tmax environment variables are set in the UNIX configuration file. The client processes are started and terminated in the method used to start and terminate other executable files.

The following describes each environment variable.

Environment VariableDescription
TMAX_HOST_ADDR

IP of the server where Tmax is installed. The definition of this variable is very important because connections are reset internally if the service requested by a client is not provided by the machine where the client connected to initially.

If the client requests for a transaction processing, the Tmax server that was connected to initially becomes the coordinator that oversee the 2PC protocol. Therefore, the host address should be set to the server with the most frequently requested services in order to decrease network traffic and reduce response time.

TMAX_HOST_PORTPort of the server where Tmax is installed. If TPORTNO is not defined in the Tmax configuration file, the default value (8888) is used.
TMAX_BACKUP_ADDR

When a client issues a request, a connection is established to a server specified by TMAX_HOST_ADDR.

If the server is unavailable, another connection is established to another server specified by TMAX_BACKUP_ADDR. If TMAX_BACKUP_ADDR is not set, the request fails.

TMAX_BACKUP_PORTTmax server port number specified by TMAX_BACKUP_ADDR.
TMAX_CONNECT_TIMEOUT

This value is set to handle network failures. A client waits for a connection until this timeout period expires.

If a connection is not established during this timeout period, tpstart() fails. If a network error occurs, tperrono is set to TPETIME.

If TMAX_BACKUP_ADDR/TMAX_BACKUP_PORT is set, tpstart() attempts to connect through the backup host for the timeout period.

SDLFILERequired for client programs using the struct buffer.
FDLFILERequired for client programs using the fielded buffer.

Note

For more information about the sdlc and fdlc commands, refer to "Tmax Reference Guide".

2.5.1. sdlc

The SDLFILE environment variable must be set for a client program that use the struct buffer. This variable must define the SDL file created by compiling a struct file used in a client program with sdlc. The client can communicate with Tmax server if the SDLFILE contains the struct file during struct communication.

The struct file can be compiled with the following command:

$ sdlc -c -i Struct file name [ -o sdl File name] [ -h Header file name]
OptionDescription
-i struct file name

Name of the struct file to compile. One or more file can be defined regardless of the extension. An asterisk (*) can be used as a wild card for any file name.

In a struct file, one or more structs must be defined using the "struct name { . . . };" format.

[ -o sdl file name]Name of SDL file to create. It is set to the SDLFILE variable.
[ -h header file name]Create the struct information in a header file format. If the [-h] option is omitted, <name_sdl.h> is created.

If [-o] option is omitted, the following default SDL file is created.

  • If there is only one struct file, the created file name is <struct file name without extension.sdl>.

    In the following case, the created file name is <demo.sdl>.

    sdlc -c -i demo.s  
  • If there are multiple struct files, the created file name is <the first struct file name without extension.sdl>.

    In the following case, the created file name is <stra.sdl>.

    sdlc -c -i stra.s bana.s orage.s 

    If the current directory contains a.s, b.s, and c.s files, the created file name is <a.sdl>.

    sdlc -c -i *.s 

2.5.2. fdlc

The FDLFILE environment variable must be set for a client program using the fielded buffer. This variable must define the FDL file created by compiling a fielded buffer file used in a client program with fdlc. The client can communicate with Tmax server if the FDLFILE contains the field information during fielded buffer communication.

The field buffer file can be compiled with the following command:

$ fdlc -c -i Field buffer file name [ -o fdl File name] [ -h Header file name]
OptionDescription
-i field buffer file nameName of a fielded buffer file to compile.
[ -o fdl file name]Name of FDL file to create. It is set to the FDLFILE variable. If the [-o] option is omitted, <tmax.fdl> is created by default.
[ -h header file name]Create the struct information into a header file format. If the [-h] option is omitted, <name_fdl.h> is created.