tclmsgque Tutorial
INTRODUCTION
BUILD
SERVER
CLIENT
FILTER
tclmsgque is the TCL language binding of the
libmsgque library.
The library was designed to simplify the client/server development using the following design rules:
- programming language independent
- operating system independent
- processor hardware independent
The TCL language binding provide the following basic components:
The build is different between
UNIX and
WINDOWS. On
UNIX the
automake (info automake) build environment is used and on
WINDOWS the
VisualExpress tools from
Microsoft are used. The
automake build on
WINDOWS is possible but not supported.
The example from the tutorial is using the
automake build-environment from the
tclmsgque tool. An example
automake configuration file is available in:
example/TCL/Makefile.am
no build necessary
The
VisualExpress build environment is located in the directory
win.
A
tclmsgque server requires the following setup:
- file:
example/TCL/MyServer.tcl - an instance of the abstract class MqS
- the interface IServerSetup and or IServerCleanup
- the interface IFactory to create a new application instance
The
minimal server looks like:
package require TclMsgque
proc MyFirstService {ctx} {
$ctx SendSTART
$ctx SendC "Hello World"
$ctx SendRETURN
}
proc ServerSetup {ctx} {
$ctx ServiceCreate "HLWO" MyFirstService
}
tclmsgque Main {
set srv [tclmsgque MqS]
$srv ConfigSetFactory
$srv ConfigSetServerSetup ServerSetup
if {[catch {
$srv LinkCreate {*}$argv
$srv ProcessEvent -wait FOREVER
}]} {
$srv ErrorSet
}
$srv Exit
The server is started as network visible TCP server listen on PORT 2345 using a THREAD for every new connection request:
> tclsh MyServer.tcl --tcp --port 2345 --thread
If you are using UNIX and if you want to setup a high-performance local server then use the build-in UDS (Unix-Domain-Sockets) capability to listen on the FILE /path/to/any/file.uds instead on a network port:
> tclsh MyServer.tcl --uds --file /path/to/any/file.uds --thread
Three things are important:
- the send style of functions
- the $ctx ServiceCreate token service function
- a connected context of type MqS
Sending data is done using a minimum of 2 steps:
- First: start a data package with $ctx SendSTART
- Last: submit the a data package to the link target using one of:
The first three
SendEND... functions are used to
call a remote service and the last one is used to answer an incoming service call. In-between $ctx SendSTART and $ctx SendEND token ... other
SEND DATA style commands are available to fill the data package with data.
Services are created with the $ctx ServiceCreate token service function. The first parameter is a 4 byte Token as public name. 4 byte is required because this string is mapped to a 4 byte integer for speed reason. The second parameter is an object providing the SERVICE CALLBACK interface.
The SERVICE CALLBACK function has one parameter of type MqS. This parameter refer to the original object (in our case MyServer). If the service object (in our case MyFirstService) is a subclass of the original MqS object class, the both statements SendSTART() and ctx.sendSTART() are identical.
A
tclmsgque client requires the following setup:
- an instance of the abstract class MqS
The
minimal client looks like:
package require TclMsgque
set ctx [tclmsgque MqS]
$ctx ConfigSetName "MyClient"
$ctx LinkCreate {*}$argv
$ctx SendSTART
$ctx SendEND_AND_WAIT "HLWO"
puts [$ctx ReadC]
$ctx Exit
To call a network visible TCP server listen on PORT 2345 use:
> tclsh MyClient.tcl --tcp --port 2345
> Hello World
To call a network visible UDP server listen on FILE /path/to/any/file.uds use:
> tclsh MyClient.tcl --udp --file /path/to/any/file.uds
> Hello World
To call a local server started by the client using PIPE communication use:
> tclsh MyClient.tcl @ tclsh MyServer.tcl
> Hello World
A filter has to implement the
IFilterFTR or the
IFilterEOF interface which requires the following functions:
- IFilterFTR, a service function to act on filter data rows. Every filter input data is a list of filter data rows and every row is a list of filter data columns. Every row is send from one filter-command to the following filter-command as FTR service request
- IFilterEOF, a service function to act on End-Of-Filter data. This service is called after the whole filter data was send. Sometimes the filter data can not be served as filter data rows (example: sorting of the input rows need to read all rows before the data can be send to the next filter command) and the EOF function is used to continue send filter data rows
The
minimal filter looks like: