| 1 |
|
| 2 |
<HTML> |
| 3 |
<HEAD> |
| 4 |
<TITLE>XPA/Tcl Interface</TITLE> |
| 5 |
</HEAD> |
| 6 |
<BODY> |
| 7 |
|
| 8 |
|
| 9 |
<H2><A NAME="xpatcl">XPATcl: the XPA Interface to the Tcl/Tk Environment</A></H2> |
| 10 |
|
| 11 |
|
| 12 |
<H2>Summary</H2> |
| 13 |
|
| 14 |
<P> |
| 15 |
Tcl/Tk programs can act as XPA clients and/or servers using the Tcl |
| 16 |
interface to XPA that is contained in the libtclxpa.so shared object. |
| 17 |
|
| 18 |
<H2>Server Routines</H2> |
| 19 |
|
| 20 |
<PRE> |
| 21 |
set xpa [xpanew class name help sproc sdata smode rproc rdata rmode] |
| 22 |
xpafree xpa |
| 23 |
set xpa [xpanew class name help iproc idata imode] |
| 24 |
set xpa [xpacmdnew class name] |
| 25 |
xpacmdadd xpa name help sproc sdata smode rproc rdata rmode |
| 26 |
xpacmddel xpa cmd |
| 27 |
set val [xparec xpa option] |
| 28 |
options: name, class, method, cmdfd, datafd, cmdchan, datachan |
| 29 |
xpasetbuf xpa buf len |
| 30 |
xpaerror xpa message |
| 31 |
xpamessage xpa message |
| 32 |
</PRE> |
| 33 |
|
| 34 |
<H2>Client Routines</H2> |
| 35 |
|
| 36 |
<PRE> |
| 37 |
set xpa [xpaopen mode] |
| 38 |
xpaclose xpa |
| 39 |
set got [xpaget xpa template paramlist mode bufs lens names errs n] |
| 40 |
set got [xpaget xpa template paramlist mode chans names errs n] |
| 41 |
set got [xpaset xpa template paramlist mode buf len names errs n] |
| 42 |
set got [xpasetfd xpa template paramlist mode chan names errs n] |
| 43 |
set got [xpainfo xpa template paramlist mode names errs n] |
| 44 |
# NB: 2.1 calling sequence change |
| 45 |
# set got [xpaaccess template type] (2.0.5) |
| 46 |
set got [xpaaccess xpa template paramlist mode names errs n] |
| 47 |
set got [xpanslookup template type classes names methods] |
| 48 |
</PRE> |
| 49 |
|
| 50 |
|
| 51 |
<H2>Description</H2> |
| 52 |
<P> |
| 53 |
You can call XPANew(), XPACmdNew(), or XPAInfoNew() within a C |
| 54 |
routine to add C-based XPA server callbacks to a TCL/Tk program that |
| 55 |
uses a Tcl/Tk event loop (either vwait() or the Tk event loop); |
| 56 |
Such a program does not need or want to use the XPA event loop. |
| 57 |
Therefore, in order to add XPA access points to the Tcl/Tk loop, the |
| 58 |
following routine should be called beforehand: |
| 59 |
<PRE> |
| 60 |
int XPATclAddInput(XPA xpa); |
| 61 |
</PRE> |
| 62 |
<P> |
| 63 |
Normally, the xpa argument is NULL, meaning that all current XPA |
| 64 |
access points are registered with the event loop. However, if a |
| 65 |
single XPA access point is to be added (i.e., after the event loop is |
| 66 |
started) then the handle of that XPA access point can be passed to |
| 67 |
this routine. |
| 68 |
|
| 69 |
<P> |
| 70 |
The significance of the XPA/TCL interface goes beyond the support for |
| 71 |
using XPA inside C code. The interface allows you to write XPA |
| 72 |
servers and to make calls to the XPA client interface within the Tcl |
| 73 |
environment using the Tcl language directly. The XPA/Tcl |
| 74 |
interface can be loaded using the following package command: |
| 75 |
<PRE> |
| 76 |
package require tclxpa 2.0 |
| 77 |
</PRE> |
| 78 |
Alternatively, you can load the shared object (called libtclxpa.so ) directly: |
| 79 |
<PRE> |
| 80 |
load .../libtclxpa.so tclxpa |
| 81 |
</PRE> |
| 82 |
<P> |
| 83 |
Once the tclxpa package is loaded, you can use Tcl versions of XPA |
| 84 |
routines to define XPA servers or make client XPA calls. The |
| 85 |
interface for these routines is designed to match the Unix XPA |
| 86 |
interface as nearly as possible. Please refer to |
| 87 |
<A HREF="./server.html">XPA Servers</A> |
| 88 |
and |
| 89 |
<A HREF="./client.html">XPA Clients</A> |
| 90 |
for general information about these routines. |
| 91 |
|
| 92 |
<P> |
| 93 |
The file test.tcl in the XPA source directory gives examples for using the |
| 94 |
XPA/Tcl interface. |
| 95 |
|
| 96 |
<P> |
| 97 |
The following notes describe the minor differences between the interfaces. |
| 98 |
|
| 99 |
<H2><A NAME="xpanew">XPANew</A></H2> |
| 100 |
<PRE> |
| 101 |
<B> |
| 102 |
set xpa [xpanew class name help sproc sdata smode rproc rdata rmode] |
| 103 |
</B> |
| 104 |
</PRE> |
| 105 |
<P> |
| 106 |
rproc and sproc routines are routines. The calling sequence of the |
| 107 |
rproc routine is identical to its C counterpart: |
| 108 |
<PRE> |
| 109 |
proc rec_cb { xpa client_data paramlist buf len } { ... } |
| 110 |
</PRE> |
| 111 |
<P> |
| 112 |
The sproc routine, however is slightly different from its C counterpart |
| 113 |
because of the difficulty of passing data back from the callback to C: |
| 114 |
<PRE> |
| 115 |
proc sendcb { xpa client_data paramlist } { ... } |
| 116 |
</PRE> |
| 117 |
<P> |
| 118 |
Note that the C-based server's char **buf and int *len arguments are |
| 119 |
missing from the Tcl callback. This is because we did not know how to |
| 120 |
fill buf with data and pass it back to the C routines for communication |
| 121 |
with the client. Instead, the Tcl server callback uses the following |
| 122 |
routine to set buf and len: |
| 123 |
<PRE> |
| 124 |
xpasetbuf xpa buf len |
| 125 |
</PRE> |
| 126 |
where: |
| 127 |
<PRE> |
| 128 |
arg explanation |
| 129 |
------ ----------- |
| 130 |
xpa the first argument of the server callback |
| 131 |
buf the data to be returned to the client |
| 132 |
len data length in bytes, (if absent, use length of the buf object) |
| 133 |
</PRE> |
| 134 |
<P> |
| 135 |
When this routine is called, a copy of buf is saved for transmission to |
| 136 |
the client. |
| 137 |
|
| 138 |
<P> |
| 139 |
The fact that buf is duplicated means that TCL server writers might wish to |
| 140 |
perform the I/O directly within the callback, rather than have XPA do it |
| 141 |
automatically at the end of the routine. To do this, set: |
| 142 |
<PRE> |
| 143 |
fillbuf=false |
| 144 |
</PRE> |
| 145 |
<P> |
| 146 |
in the xpanew smode and then perform I/O through the Tcl channel |
| 147 |
obtained from: |
| 148 |
<PRE> |
| 149 |
set dchan [xparec $xpa datachan] |
| 150 |
</PRE> |
| 151 |
<P> |
| 152 |
where: |
| 153 |
<PRE> |
| 154 |
arg explanation |
| 155 |
------ ----------- |
| 156 |
xpa the first argument of the server callback |
| 157 |
datachan literal string "datachan" that returns the data channel |
| 158 |
len data length in bytes, (if absent, use length of the buf object) |
| 159 |
</PRE> |
| 160 |
<P> |
| 161 |
<B> |
| 162 |
NB: datachan and cmdchan are not available under Windows. It is |
| 163 |
necessary to use the "raw" equivalents: datafd and cmdfd. |
| 164 |
</B> |
| 165 |
|
| 166 |
<P> |
| 167 |
The same considerations apply to the rproc for receive servers: a copy |
| 168 |
of the incoming data is generated to pass to the receive callback. This |
| 169 |
copy again can be avoided by using "fillbuf=false" in the rmode and then |
| 170 |
reading the incoming data from datachan. |
| 171 |
|
| 172 |
<P> |
| 173 |
The send and receive callback routines can use the xpaerror and xpamessage |
| 174 |
routines to send errors and messages back to the client. If you also |
| 175 |
want tcl itself to field an error condition, use the standard return call: |
| 176 |
<PRE> |
| 177 |
return ?-code c? ?-errorinfo i? ?-errorcode ec? string |
| 178 |
</PRE> |
| 179 |
<P> |
| 180 |
See the Tcl man page for more info. |
| 181 |
|
| 182 |
<H2><A NAME="xpanew">XPARec</A></H2> |
| 183 |
<P> |
| 184 |
The Tcl xparec procedure supplies server routines with access to information |
| 185 |
that is available via macros in the C interface: |
| 186 |
<PRE> |
| 187 |
set val [xparec xpa <option>] |
| 188 |
</PRE> |
| 189 |
<P> |
| 190 |
where option is: name, class, method, cmdfd, datafd, cmdchan, |
| 191 |
datachan. Note that two additional identifiers, cmdchan and datachan, |
| 192 |
have been added to to provide Tcl channels corresponding to datafd and |
| 193 |
cmdfd. (These latter might still be retrieved in Tcl and passed back |
| 194 |
to a C routines.) An additional option called "version" can be used to |
| 195 |
determine the XPA version used to build the Tcl interface. Note that |
| 196 |
the standard options require a valid XPA handle, but "version" does |
| 197 |
not (since it simply reports the value of the XPA_VERSION definition |
| 198 |
in the XPA source include file). |
| 199 |
|
| 200 |
<P> |
| 201 |
<B> |
| 202 |
NB: datachan and cmdchan are not available under Windows. It is |
| 203 |
necessary to use the "raw" equivalents: datafd and cmdfd. |
| 204 |
</B> |
| 205 |
<PRE> |
| 206 |
macro explanation |
| 207 |
------ ----------- |
| 208 |
class class of this xpa |
| 209 |
name name of this xpa |
| 210 |
method method string (inet or local connect info) |
| 211 |
cmdchan Tcl channel of command socket |
| 212 |
datachan Tcl channel of data socket |
| 213 |
cmdfd fd of command socket |
| 214 |
datafd fd of data socket |
| 215 |
sendian endian-ness of server ("little" or "big") |
| 216 |
cendian endian-ness of client ("little" or "big" |
| 217 |
version XPA version used to build this code |
| 218 |
</PRE> |
| 219 |
|
| 220 |
<p> |
| 221 |
Under Windows, the Tcl event handler cannot automatically sense when an |
| 222 |
XPA socket is ready for IO (i.e. Tcl_CreateFileHandler() is not available |
| 223 |
under Windows). The Windows Tcl event handler therefore must be awakened |
| 224 |
occasionally for check for XPA events. This is done using the standard |
| 225 |
Tcl_SetMaxBlockTime() call. The time parameter is defined in tclloop.c |
| 226 |
and is currently set to 1000 microseconds (1/1000 of a second). |
| 227 |
|
| 228 |
<P> |
| 229 |
The version option can be used to differentiate between source code versions. |
| 230 |
It was created to support legacy Tcl code that needs to maintain the 2.0.5 |
| 231 |
calling sequence for xpaaccess. You can use a version test such as: |
| 232 |
<PRE> |
| 233 |
if [catch { xparec "" version } version] { |
| 234 |
puts "pre-2.1.0e" |
| 235 |
} else { |
| 236 |
puts [split $version .] |
| 237 |
} |
| 238 |
<PRE> |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
<P> |
| 245 |
<A HREF="./help.html">Go to XPA Help Index</A> |
| 246 |
|
| 247 |
<H5>Last updated: September 10, 2003</H5> |
| 248 |
</BODY> |
| 249 |
</HTML> |