| 1 |
|
| 2 |
<HTML> |
| 3 |
<HEAD> |
| 4 |
<TITLE>XPA Server API</TITLE> |
| 5 |
</HEAD> |
| 6 |
<BODY> |
| 7 |
|
| 8 |
|
| 9 |
<H2><A NAME="xpaserver">XPAServer: The XPA Server-side Programming Interface</A></H2> |
| 10 |
|
| 11 |
|
| 12 |
<H2>Summary</H2> |
| 13 |
A description of the XPA server-side programming interface. |
| 14 |
|
| 15 |
|
| 16 |
<H2><A NAME="intro">Introduction to XPA Server Programming</H2></A> |
| 17 |
<P> |
| 18 |
Creating an XPA server is easy: you generally only need to call the |
| 19 |
XPANew() subroutine to define a named XPA access point and set up the |
| 20 |
send and receive callback routines. You then enter an event loop such |
| 21 |
as XPAMainLoop() to field XPA requests. |
| 22 |
<PRE> |
| 23 |
#include <xpa.h> |
| 24 |
|
| 25 |
XPA <A HREF="./server.html#xpanew">XPANew</A>(char *class, char *name, char *help, |
| 26 |
int (*send_callback)(), void *send_data, char *send_mode, |
| 27 |
int (*rec_callback)(), void *rec_data, char *rec_mode); |
| 28 |
|
| 29 |
XPA <A HREF="./server.html#xpacmdnew">XPACmdNew</A>(char *class, char *name); |
| 30 |
|
| 31 |
XPACmd <A HREF="./server.html#xpacmdadd">XPACmdAdd</A>(XPA xpa, |
| 32 |
char *name, char *help, |
| 33 |
int (*send_callback)(), void *send_data, char *send_mode, |
| 34 |
int (*rec_callback)(), void *rec_data, char *rec_mode); |
| 35 |
|
| 36 |
void <A HREF="./server.html#xpacmddel">XPACmdDel</A>(XPA xpa, XPACmd cmd); |
| 37 |
|
| 38 |
XPA <A HREF="./server.html#xpainfonew">XPAInfoNew</A>(char *class, char *name, |
| 39 |
int (*info_callback)(), void *info_data, char *info_mode); |
| 40 |
|
| 41 |
int <A HREF="./server.html#xpafree">XPAFree</A>(XPA xpa); |
| 42 |
|
| 43 |
void <A HREF="./server.html#xpamainloop">XPAMainLoop</A>(void); |
| 44 |
|
| 45 |
int <A HREF="./server.html#xpapoll">XPAPoll</A>(int msec, int maxreq); |
| 46 |
|
| 47 |
void <A HREF="./server.html#xpaatexit">XPAAtExit</A>(void); |
| 48 |
|
| 49 |
void <A HREF="./server.html#xpacleanup">XPACleanup</A>(void); |
| 50 |
|
| 51 |
</PRE> |
| 52 |
|
| 53 |
<H2>Introduction</H2> |
| 54 |
|
| 55 |
To use the XPA application programming interface, a software developer |
| 56 |
generally will include the xpa.h definitions file: |
| 57 |
<PRE> |
| 58 |
#include <xpa.h> |
| 59 |
</PRE> |
| 60 |
in the software module that defines or accesses an XPA access point, and |
| 61 |
then will link against the libxpa.a library: |
| 62 |
<PRE> |
| 63 |
gcc -o foo foo.c libxpa.a |
| 64 |
</PRE> |
| 65 |
XPA has been compiled using both C and C++ compilers. |
| 66 |
|
| 67 |
<P> |
| 68 |
A server program generally defines an XPA access point by calling the |
| 69 |
XPANew() routine and specifies "send" and/or "receive" callback |
| 70 |
procedures to be executed by the program when an external process |
| 71 |
either sends data or commands to this access point or requests data or |
| 72 |
information from this access point. A program also can define several |
| 73 |
sub-commands for a single access point by calling XPACmdNew() and |
| 74 |
XPACmdAdd() instead. Having defined one or more public access points |
| 75 |
in this way, an XPA server program enters its usual event loop (or |
| 76 |
uses the standard XPA event loop). |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
<H2><A NAME="xpanew">XPANew: create a new XPA access point</A></H2> |
| 82 |
|
| 83 |
|
| 84 |
<B> |
| 85 |
<PRE> |
| 86 |
#include <xpa.h> |
| 87 |
|
| 88 |
XPA XPANew(char *class, char *name, char *help, |
| 89 |
int (*send_callback)(), |
| 90 |
void *send_data, char *send_mode, |
| 91 |
int (*rec_callback)(), |
| 92 |
void *rec_data, char *rec_mode); |
| 93 |
</PRE> |
| 94 |
</B> |
| 95 |
|
| 96 |
|
| 97 |
<P> |
| 98 |
Create a new XPA public access point with the class:name |
| 99 |
identifier <A HREF="./template.html">template</A> |
| 100 |
and enter this access point into the XPA name server, so that it |
| 101 |
can be accessed by external processes. XPANew() returns an XPA struct. |
| 102 |
Note that the length of the class and name designations must be less |
| 103 |
than or equal to 1024 characters each. |
| 104 |
|
| 105 |
<P> |
| 106 |
The XPA name server daemon, xpans, will be started automatically if it |
| 107 |
is not running already (assuming it can be found in the path). The |
| 108 |
program's ip address and listening port are specified by the |
| 109 |
environment variable XPA_NSINET, which takes the form <ip>:<port>. If |
| 110 |
no such environment variable exists, then xpans is started on the |
| 111 |
current machine listening on port 14285. It also uses 14286 as a |
| 112 |
known port for its public access point (so that routines do not have |
| 113 |
to go to the name server to find the name server ip and port!) |
| 114 |
As of XPA 2.1.1, version information is exchanged between the xpans |
| 115 |
process and the new access point. If the access point uses an XPA |
| 116 |
major/minor version newer than xpans, a warning is issued by both processes, |
| 117 |
since mixing of new servers and old xpa programs (xpaset, xpaget, |
| 118 |
xpans, etc.) is not likely to work. You can turn off the warning |
| 119 |
message by setting the XPA_VERSIONCHECK environment variable to "false". |
| 120 |
|
| 121 |
<P> |
| 122 |
The help string is meant to be returned by a request from xpaget: |
| 123 |
<PRE> |
| 124 |
xpaget class:name -help |
| 125 |
</PRE> |
| 126 |
<P> |
| 127 |
A send_callback and/or a receive_callback can be specified; at |
| 128 |
least one of them must be specified. |
| 129 |
|
| 130 |
<P> |
| 131 |
A send_callback can be specified that will be executed in response to |
| 132 |
an external request from the xpaget program, the XPAGet() routine, or |
| 133 |
XPAGetFd() routine. This callback is used to send data to the |
| 134 |
requesting client. |
| 135 |
|
| 136 |
<P> |
| 137 |
The calling sequence for send_callback() is: |
| 138 |
<PRE> |
| 139 |
int send_callback(void *send_data, void *call_data, |
| 140 |
char *paramlist, char **buf, size_t *len) |
| 141 |
{ |
| 142 |
XPA xpa = (XPA)call_data; |
| 143 |
... |
| 144 |
return(stat); |
| 145 |
} |
| 146 |
</PRE> |
| 147 |
<P> |
| 148 |
The send_mode string is of the form: "key1=value1,key2=value2,..." |
| 149 |
The following keywords are recognized: |
| 150 |
<PRE> |
| 151 |
key value default explanation |
| 152 |
------ -------- -------- ----------- |
| 153 |
acl true/false true enable access control |
| 154 |
freebuf true/false true free buf after callback completes |
| 155 |
</PRE> |
| 156 |
<P> |
| 157 |
The call_data should be recast to the XPA struct as shown. In |
| 158 |
addition, client-specific data can be passed to the callback in |
| 159 |
send_data. |
| 160 |
|
| 161 |
<P> |
| 162 |
The paramlist will be supplied by the client as qualifying parameters |
| 163 |
for the callback. There are two ways in which the send_callback() |
| 164 |
routine can send data back to the client: |
| 165 |
|
| 166 |
<P> |
| 167 |
1. The send_callback() routine can fill in a buffer and pass back a |
| 168 |
pointer to this buffer. An integer len also is returned to specify the |
| 169 |
number of bytes of data in buf. XPA will send this buffer to the |
| 170 |
client after the callback is complete. |
| 171 |
|
| 172 |
<P> |
| 173 |
2. The send_callback can send data directly to the client by writing |
| 174 |
to the fd pointed by the macro: |
| 175 |
<PRE> |
| 176 |
xpa_datafd(xpa) |
| 177 |
</PRE> |
| 178 |
<P> |
| 179 |
Note that this fd is of the kind returned by socket() or open(). |
| 180 |
|
| 181 |
<P> |
| 182 |
If a buf has been allocated by a standard malloc routine, filled, and |
| 183 |
returned to XPA, then freebuf generally is set so that the buffer will |
| 184 |
be freed automatically when the callback is completed and data has |
| 185 |
been sent to the client. If a static buf is returned, freebuf should |
| 186 |
be set to false to avoid a system error when freeing static storage. |
| 187 |
Note that default value for freebuf implies that the callback will |
| 188 |
allocate a buffer rather than use static storage. |
| 189 |
|
| 190 |
<P> |
| 191 |
On the other hand, if buf is dynamically allocated using a method |
| 192 |
other than a standard malloc/calloc/realloc routine (e.g. using Perl's |
| 193 |
memory allocation and garbage collection scheme), then it is necessary |
| 194 |
to tell XPA how to free the allocated buffer. To do this, use the |
| 195 |
XPASetFree() routine within your callback: |
| 196 |
<PRE> |
| 197 |
void XPASetFree(XPA xpa, void (*myfree)(void *), void *myfree_ptr); |
| 198 |
</PRE> |
| 199 |
The first argument is the usual XPA handle. The second argument is the |
| 200 |
special routine to call to free your allocated memory. The third |
| 201 |
argument is an optional pointer. If not NULL, the specified free |
| 202 |
routine is called with that pointer as its sole argument. If NULL, the |
| 203 |
free routine is called with the standard buf pointer as its sole |
| 204 |
argument. This is useful in cases where there is a mapping between the |
| 205 |
buffer pointer and the actual allocated memory location, and the |
| 206 |
special routine is expecting to be passed the former. |
| 207 |
|
| 208 |
<P> |
| 209 |
If, while the callback performs its processing, an error occurs that |
| 210 |
should be communicated to the client, then the routine XPAError should be |
| 211 |
called: |
| 212 |
<PRE> |
| 213 |
XPAError(XPA xpa, char *s); |
| 214 |
</PRE> |
| 215 |
<P> |
| 216 |
where s is an arbitrary error message. The returned error message |
| 217 |
string will be of the form: |
| 218 |
<PRE> |
| 219 |
XPA$ERROR [error] (class:name ip:port) |
| 220 |
</PRE> |
| 221 |
<P> |
| 222 |
If the callback wants to send a specific acknowledgment message back |
| 223 |
to the client, the routine XPAMessage can be called: |
| 224 |
<PRE> |
| 225 |
XPAMessage(XPA xpa, char *s); |
| 226 |
</PRE> |
| 227 |
<P> |
| 228 |
where s is an arbitrary error message. The returned error message |
| 229 |
string will be of the form: |
| 230 |
<PRE> |
| 231 |
XPA$MESSAGE [message] (class:name ip:port) |
| 232 |
</PRE> |
| 233 |
<P> |
| 234 |
Otherwise, a standard acknowledgment is sent back to the client |
| 235 |
after the callback is completed. |
| 236 |
|
| 237 |
<P> |
| 238 |
The callback routine should return 0 if no error occurs, or -1 to |
| 239 |
signal an error. |
| 240 |
|
| 241 |
<P> |
| 242 |
A receive_callback can be specified that will be executed in response |
| 243 |
to an external request from the xpaset program, or the XPASet (or |
| 244 |
XPASetFd()) routine. This callback is used to process data received |
| 245 |
from an external process. |
| 246 |
|
| 247 |
<P> |
| 248 |
The calling sequence for receive_callback is: |
| 249 |
<PRE> |
| 250 |
int receive_callback(void *receive_data, void *call_data, |
| 251 |
char *paramlist, char *buf, size_t len) |
| 252 |
{ |
| 253 |
XPA xpa = (XPA)call_data; |
| 254 |
... |
| 255 |
return(stat); |
| 256 |
} |
| 257 |
</PRE> |
| 258 |
<P> |
| 259 |
The mode string is of the form: "key1=value1,key2=value2,..." |
| 260 |
The following keywords are recognized: |
| 261 |
<PRE> |
| 262 |
key value default explanation |
| 263 |
------ -------- -------- ----------- |
| 264 |
acl true/false true enable access control |
| 265 |
buf true/false true server expects data bytes from client |
| 266 |
fillbuf true/false true read data into buf before executing callback |
| 267 |
freebuf true/false true free buf after callback completes |
| 268 |
</PRE> |
| 269 |
<P> |
| 270 |
The call_data should be recast to the XPA struct as shown. In |
| 271 |
addition, client-specific data can be passed to the callback in |
| 272 |
receive_data. |
| 273 |
|
| 274 |
<P> |
| 275 |
The paramlist will be supplied by the client. In addition, if the |
| 276 |
receive_mode keywords buf and fillbuf are true, then on entry into the |
| 277 |
receive_callback() routine, buf will contain the data sent by the |
| 278 |
client. If buf is true but fillbuf is false, it becomes the callback's |
| 279 |
responsibility to retrieve the data from the client, using the data fd |
| 280 |
pointed to by the macro xpa_datafd(xpa). If freebuf is true, then buf |
| 281 |
will be freed when the callback is complete. |
| 282 |
|
| 283 |
<P> |
| 284 |
If, while the callback is performing its processing, an error occurs |
| 285 |
that should be communicated to the client, then the routine XPAError |
| 286 |
can be called: |
| 287 |
<PRE> |
| 288 |
XPAError(XPA xpa, char *s); |
| 289 |
</PRE> |
| 290 |
<P> |
| 291 |
where s is an arbitrary error message. |
| 292 |
|
| 293 |
<P> |
| 294 |
The callback routine should return 0 if no error occurs, or -1 to |
| 295 |
signal an error. |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
<H2><A NAME="xpacmdnew">XPACmdNew: create a new XPA public access point for commands</A></H2> |
| 301 |
|
| 302 |
|
| 303 |
<B> |
| 304 |
<PRE> |
| 305 |
#include <xpa.h> |
| 306 |
|
| 307 |
XPA XPACmdNew(char *class, char *name); |
| 308 |
</PRE> |
| 309 |
</B> |
| 310 |
|
| 311 |
|
| 312 |
<P> |
| 313 |
Create a new XPA public access point for commands that will share a |
| 314 |
common identifier class:name. Enter this access point into the XPA |
| 315 |
name server, so that it can be accessed by external processes. |
| 316 |
XPACmdNew() returns an XPA struct. |
| 317 |
|
| 318 |
<P> |
| 319 |
It often is more convenient to have one public access point that can |
| 320 |
manage a number of commands, rather than having individual access |
| 321 |
points for each command. For example, it is easier to command the |
| 322 |
ds9 image display using: |
| 323 |
<PRE> |
| 324 |
echo "colormap I8" | xpaset ds9 |
| 325 |
echo "scale log" | xpaset ds9 |
| 326 |
echo "file foo.fits" | xpaset ds9 |
| 327 |
</PRE> |
| 328 |
<P> |
| 329 |
then to use: |
| 330 |
<PRE> |
| 331 |
echo "I8" | xpaset ds9_colormap |
| 332 |
echo "log" | xpaset ds9_scale |
| 333 |
echo "foo.fits" | xpaset ds9_file |
| 334 |
</PRE> |
| 335 |
<P> |
| 336 |
In the first case, the commands remain the same regardless of the |
| 337 |
target XPA name. In the second case, the command names must change |
| 338 |
for each instance of ds9. That is, if a second instance of ds9 |
| 339 |
called DS9 were running, it would be commanded either as: |
| 340 |
<PRE> |
| 341 |
echo "colormap I8" | xpaset DS9 |
| 342 |
echo "scale log" | xpaset DS9 |
| 343 |
echo "file foo.fits" | xpaset DS9 |
| 344 |
</PRE> |
| 345 |
<P> |
| 346 |
or as: |
| 347 |
<PRE> |
| 348 |
echo "I8" | xpaset DS9_colormap |
| 349 |
echo "log" | xpaset DS9_scale |
| 350 |
echo "foo.fits" | xpaset DS9_file |
| 351 |
</PRE> |
| 352 |
<P> |
| 353 |
Thus, in cases where a program is going to manage many commands, it |
| 354 |
generally is easier to define them as commands associated with the |
| 355 |
XPACmdNew() routine, rather than as separate access points using |
| 356 |
XPANew(). |
| 357 |
|
| 358 |
<P> |
| 359 |
When XPACmdNew() is called, only the class:name identifier is |
| 360 |
specified. Each sub-command is subsequently defined using the |
| 361 |
XPACmdAdd() routine. |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
<H2><A NAME="xpacmdadd">XPACmdAdd: add a command to an XPA command public access point</A></H2> |
| 367 |
|
| 368 |
|
| 369 |
<B> |
| 370 |
<PRE> |
| 371 |
#include <xpa.h> |
| 372 |
|
| 373 |
XPACmd XPACmdAdd(XPA xpa, char *name, char *help, |
| 374 |
int (*send_callback)(), |
| 375 |
void *send_data, char *send_mode, |
| 376 |
int (*rec_callback)(), |
| 377 |
void *rec_data, char *rec_mode); |
| 378 |
</PRE> |
| 379 |
</B> |
| 380 |
|
| 381 |
|
| 382 |
<P> |
| 383 |
Add a command to an XPA command access point. The XPA argument specifies the |
| 384 |
XPA struct returned by a call to XPANewCmd(). The name argument is the |
| 385 |
name of the command. The other arguments function identically to the |
| 386 |
arguments in the XPANew() command, i.e., the send_callback and rec_callback |
| 387 |
routines have identical calling sequences to their XPANew() counterparts, |
| 388 |
with the exceptions noted below. |
| 389 |
|
| 390 |
<P> |
| 391 |
When help is requested for a command access point using: |
| 392 |
<PRE> |
| 393 |
xpaget -h class:name |
| 394 |
</PRE> |
| 395 |
<P> |
| 396 |
all of the command help strings are listed. To get help for a given |
| 397 |
command, use: |
| 398 |
<PRE> |
| 399 |
xpaget -h class:name cmd |
| 400 |
</PRE> |
| 401 |
<P> |
| 402 |
Also, the acl keyword in the send_mode and receive_mode strings is |
| 403 |
global to the access point, not local to the command. Thus, the value |
| 404 |
for the acl mode should be the same in all send_mode (or receive_mode) |
| 405 |
strings for each command in a command access point. (The acl for |
| 406 |
send_mode need not be the same as the acl for receive_mode, though). |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
<H2><A NAME="xpacmddel">XPACmdDel: remove a command from an XPA command public access point</A></H2> |
| 412 |
|
| 413 |
|
| 414 |
<B> |
| 415 |
<PRE> |
| 416 |
#include <xpa.h> |
| 417 |
|
| 418 |
void XPACmdDel(XPA xpa, XPACmd cmd); |
| 419 |
</PRE> |
| 420 |
</B> |
| 421 |
|
| 422 |
|
| 423 |
<P> |
| 424 |
This routine removes a command from the list of available commands in |
| 425 |
a given XPA. That command will no longer be available for processing. |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
<H2><A NAME="xpainfonew">XPAInfoNew: define an XPA info public access point</A></H2> |
| 431 |
|
| 432 |
|
| 433 |
<B> |
| 434 |
<PRE> |
| 435 |
#include <xpa.h> |
| 436 |
|
| 437 |
XPA XPAInfoNew(char *class, char *name, |
| 438 |
int (*info_callback)(), |
| 439 |
void *info_data, char *info_mode); |
| 440 |
</PRE> |
| 441 |
</B> |
| 442 |
|
| 443 |
|
| 444 |
<P> |
| 445 |
[NB: this is an experimental interface, new to XPA 2.0, whose value |
| 446 |
and best use is evolving.] |
| 447 |
|
| 448 |
<P> |
| 449 |
A program can register interest in receiving a short message about a |
| 450 |
particular topic from any other process that cares to send such a |
| 451 |
message. Neither has to be an XPA server. For example, if a user |
| 452 |
starts to work with a new image file called new.fits, she might |
| 453 |
wish to alert interested programs about this new file by sending a |
| 454 |
short message using xpainfo: |
| 455 |
<PRE> |
| 456 |
xpainfo IMAGEFILE /data/new.fits |
| 457 |
</PRE> |
| 458 |
|
| 459 |
<P> |
| 460 |
In this example, each process that has used the XPAInfoNew() call to |
| 461 |
register interest in messages associated with the identifier IMAGEFILE |
| 462 |
will have its info_callback() executed with the following calling |
| 463 |
sequence: |
| 464 |
<PRE> |
| 465 |
int info_cb(void *info_data, void *call_data, char *paramlist) |
| 466 |
{ |
| 467 |
XPA xpa = (XPA)call_data; |
| 468 |
} |
| 469 |
</PRE> |
| 470 |
<P> |
| 471 |
The arguments passed to this routine are equivalent to those sent in |
| 472 |
the send_callback() routine. The main difference is that there is no |
| 473 |
buf sent to the info callback: this mechanism is meant for short |
| 474 |
announcement of messages of interest to many clients. |
| 475 |
|
| 476 |
<P> |
| 477 |
The mode string is of the form: "key1=value1,key2=value2,..." |
| 478 |
The following keywords are recognized: |
| 479 |
<PRE> |
| 480 |
key value default explanation |
| 481 |
------ -------- -------- ----------- |
| 482 |
acl true/false true enable access control |
| 483 |
</PRE> |
| 484 |
<P> |
| 485 |
Because no buf is passed to this callback, the usual buf-related keywords |
| 486 |
are not applicable here. |
| 487 |
|
| 488 |
<P> |
| 489 |
The information sent in the parameter list is arbitrary. However, we |
| 490 |
envision sending information such as file names or XPA access points |
| 491 |
from which to collect more data. Note that the xpainfo program and |
| 492 |
the XPAInfo() routine that cause the info_callback to execute do not |
| 493 |
wait for the callback to complete before returning. |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
<H2><A NAME="xpafree">XPAFree: remove an XPA public access point</A></H2> |
| 499 |
|
| 500 |
|
| 501 |
<PRE> |
| 502 |
<B> |
| 503 |
#include <xpa.h> |
| 504 |
|
| 505 |
int XPAFree(XPA xpa); |
| 506 |
</B> |
| 507 |
</PRE> |
| 508 |
|
| 509 |
|
| 510 |
<P> |
| 511 |
Remove the specified XPA public access point from the name server and |
| 512 |
free all associated storage. Note that removal from the name server |
| 513 |
happens automatically when the process terminates, so this call is not |
| 514 |
generally needed. It is used when public access points are being |
| 515 |
defined temporarily and then destroyed when no longer needed. For |
| 516 |
example, ds9 temporarily creates a public access point when it |
| 517 |
loads a new image for display and destroys it when the image is |
| 518 |
unloaded. |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
<H2><A NAME="xpamainloop">XPAMainLoop: optional main loop for XPA</A></H2> |
| 524 |
|
| 525 |
|
| 526 |
<B> |
| 527 |
<PRE> |
| 528 |
#include <xpa.h> |
| 529 |
|
| 530 |
void XPAMainLoop(); |
| 531 |
</PRE> |
| 532 |
</B> |
| 533 |
|
| 534 |
|
| 535 |
<P> |
| 536 |
Once XPA access points have been defined, a program must enter an |
| 537 |
event loop to watch for requests from external programs. This can be |
| 538 |
done in a variety of ways, depending on whether the event loop is |
| 539 |
processing events other than XPA events. In cases where there are no |
| 540 |
non-XPA events to be processed, the program can simply call the |
| 541 |
XPAMainLoop() event loop. This loop is implemented essentially as |
| 542 |
follows (error checking is simplified in this example): |
| 543 |
<PRE> |
| 544 |
FD_ZERO(&readfds); |
| 545 |
while( XPAAddSelect(NULL, &readfds) ){ |
| 546 |
if( sgot = select(swidth, &readfds, NULL, NULL, NULL) >0 ) |
| 547 |
XPAProcessSelect(&readfds, 0); |
| 548 |
else |
| 549 |
break; |
| 550 |
FD_ZERO(&readfds); |
| 551 |
} |
| 552 |
</PRE> |
| 553 |
<P> |
| 554 |
The XPAAddSelect() routine sets up the select() readfds variable so |
| 555 |
that select() will wait for I/O on all the active XPA channels. It |
| 556 |
returns the number of XPAs that are active; the loop will end when |
| 557 |
there are no active XPAs. The standard select() routine is called to |
| 558 |
wait for an external I/O request. Since no timeout struct is passed |
| 559 |
in argument 5, the select() call hangs until there is an external |
| 560 |
request. When an external I/O request is made, the XPAProcessSelect() |
| 561 |
routine is executed to process the pending requests. In this routine, |
| 562 |
the maxreq value determines how many requests will be processed: if |
| 563 |
maxreq <=0, then all currently pending requests will be processed. |
| 564 |
Otherwise, up to maxreq requests will be processed. (The most usual |
| 565 |
values for maxreq is 0 to process all requests.) |
| 566 |
|
| 567 |
<P> |
| 568 |
If a program has its own Unix select() loop, then XPA access points can |
| 569 |
be added to it by using a variation of the standard XPAMainLoop: |
| 570 |
<PRE> |
| 571 |
XPAAddSelect(xpa, &readfds); |
| 572 |
[app-specific ...] |
| 573 |
if( select(width, &readfds, ...) ){ |
| 574 |
XPAProcessSelect(&readfds, maxreq); |
| 575 |
[app-specific ...] |
| 576 |
FD_ZERO(&readfds); |
| 577 |
} |
| 578 |
</PRE> |
| 579 |
<P> |
| 580 |
XPAAddSelect() is called before select() to add the access points. |
| 581 |
If the first argument is NULL, then all active XPA access points |
| 582 |
are added. Otherwise only the specified access point is added. |
| 583 |
After select() is called, the XPAProcessSelect() routine can be called |
| 584 |
to process XPA requests. Once again, the maxreq value determines how |
| 585 |
many requests will be processed: if maxreq <=0, then all currently |
| 586 |
pending requests will be processed. Otherwise, up to maxreq requests |
| 587 |
will be processed. |
| 588 |
|
| 589 |
<P> |
| 590 |
XPA access points can be added to |
| 591 |
<A HREF="./xt.html">Xt event loops</A> (using XtAppMainLoop()) |
| 592 |
and |
| 593 |
<A HREF="./tcl.html">Tcl/Tk event loops</A> (using vwait and the Tk loop). |
| 594 |
When using XPA with these event loops, you only need to call: |
| 595 |
<PRE> |
| 596 |
int XPAXtAddInput(XtAppContext app, XPA xpa) |
| 597 |
</PRE> |
| 598 |
or |
| 599 |
<PRE> |
| 600 |
int XPATclAddInput(XPA xpa) |
| 601 |
</PRE> |
| 602 |
respectively before entering the loop. |
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
<H2><A NAME="xpapoll">XPAPoll: execute existing XPA requests</A></H2> |
| 608 |
|
| 609 |
|
| 610 |
<B> |
| 611 |
<PRE> |
| 612 |
#include <xpa.h> |
| 613 |
|
| 614 |
int XPAPoll(int msec, int maxreq); |
| 615 |
</PRE> |
| 616 |
</B> |
| 617 |
|
| 618 |
|
| 619 |
<P> |
| 620 |
It is sometimes desirable to implement a polling loop, i.e., where one |
| 621 |
checks for and processes XPA requests without blocking. For this |
| 622 |
situation, use the XPAPoll() routine: |
| 623 |
<PRE> |
| 624 |
XPAPoll(int msec, int maxreq); |
| 625 |
</PRE> |
| 626 |
<P> |
| 627 |
The XPAPoll() routine will perform XPAAddSelect() and select(), but with a |
| 628 |
timeout specified in millisecs by the msec argument. If one or more |
| 629 |
XPA requests are made before the timeout expires, the XPAProcessSelect() |
| 630 |
routine is called to process those requests. The maxreq value determines |
| 631 |
how many requests will be processed: if maxreq < 0, then no events are |
| 632 |
processed, but instead, the return value indicates the number of events |
| 633 |
that are pending. If maxreq == 0, then all currently pending requests |
| 634 |
will be processed. Otherwise, up to maxreq requests will be processed. |
| 635 |
(The most usual values for maxreq are 0 to process all requests and 1 |
| 636 |
to process one request). |
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
<H2><A NAME="xpaatexit">XPAAtExit: install exit handler</A></H2> |
| 642 |
|
| 643 |
|
| 644 |
<PRE> |
| 645 |
<B> |
| 646 |
#include <xpa.h> |
| 647 |
|
| 648 |
void XPAAtExit(void); |
| 649 |
</B> |
| 650 |
</PRE> |
| 651 |
|
| 652 |
|
| 653 |
<P> |
| 654 |
XPAAtExit() will install an exit handler using atexit() to run XPAFree on all |
| 655 |
XPA access points. This might be useful in cases where Unix sockets are being |
| 656 |
used: if an explicit call to XPAFree() is not made by the program, the Unix |
| 657 |
socket file will not be deleted immediately without an atexit handler. (NB: this |
| 658 |
call should not be made in a Tcl/Tk application. Accessing the Tcl native file |
| 659 |
system after Tcl has shut down all file systems causes the Tcl/Tl program to |
| 660 |
crash). |
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
<H2><A NAME="xpacleanup">XPACleanup: release reserved XPA memory</A></H2> |
| 666 |
|
| 667 |
|
| 668 |
<PRE> |
| 669 |
<B> |
| 670 |
#include <xpa.h> |
| 671 |
|
| 672 |
void XPACleanup(void); |
| 673 |
</B> |
| 674 |
</PRE> |
| 675 |
|
| 676 |
|
| 677 |
<P> |
| 678 |
When XPA is initialized, it allocates a small amount of memory for the |
| 679 |
access control list, temp directory path, and reserved commands. This |
| 680 |
memory is found by valgrind to be "still reachable", meaning that "your |
| 681 |
program didn't free some memory it could have". Calling the |
| 682 |
XPACleanup() routine before exiting the program will free this memory |
| 683 |
and make valgrind happy. |
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
<H2><A NAME="macros">XPA Server Callback Macros</A></H2> |
| 689 |
|
| 690 |
|
| 691 |
<B> |
| 692 |
<PRE> |
| 693 |
#include <xpa.h> |
| 694 |
|
| 695 |
xpa_class, xpa_name, xpa_method, xpa_cmdfd, xpa_datafd, |
| 696 |
xpa_sendian, xpa_cendian |
| 697 |
</PRE> |
| 698 |
</B> |
| 699 |
|
| 700 |
|
| 701 |
<P> |
| 702 |
Server routines have access to information about the XPA being called via |
| 703 |
the following macros (each of which takes the xpa handle as an argument): |
| 704 |
<PRE> |
| 705 |
macro explanation |
| 706 |
------ ----------- |
| 707 |
xpa_class class of this xpa |
| 708 |
xpa_name name of this xpa |
| 709 |
xpa_method method string (inet or local connect info) |
| 710 |
xpa_cmdfd fd of command socket |
| 711 |
xpa_datafd fd of data socket |
| 712 |
xpa_sendian endian-ness of server ("little" or "big") |
| 713 |
xpa_cendian endian-ness of client ("little" or "big" |
| 714 |
</PRE> |
| 715 |
<P> |
| 716 |
The argument to these macros is the call_data pointer that is passed |
| 717 |
to the server procedure. This pointer should be type case to XPA |
| 718 |
in the server routine: |
| 719 |
<PRE> |
| 720 |
XPA xpa = (XPA)call_data; |
| 721 |
</PRE> |
| 722 |
|
| 723 |
<P> |
| 724 |
The most important of these macros is xpa_datafd(). A server routine |
| 725 |
that sets "fillbuf=false" in receive_mode or send_mode can use this |
| 726 |
macro to perform I/O directly to/from the client, rather than using |
| 727 |
buf. |
| 728 |
|
| 729 |
<P> |
| 730 |
The xpa_cendian and xpa_sendian macros can be used together to determine |
| 731 |
if the data transferred from the client is byte swapped with respect |
| 732 |
to the server. Values for these macros are: "little", "big", or "?". |
| 733 |
In order to do a proper conversion, you still need to know the format |
| 734 |
of the data (i.e., byte swapping is dependent on the size of the data |
| 735 |
element being converted). |
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
<H2><A NAME="race">XPA Race Conditions</A></H2> |
| 741 |
|
| 742 |
|
| 743 |
Potential XPA race conditions and how to avoid them. |
| 744 |
|
| 745 |
|
| 746 |
<P> |
| 747 |
Currently, there is only one known circumstance in which XPA can get |
| 748 |
(temporarily) deadlocked in a race condition: if two or more XPA |
| 749 |
servers send messages to one another using an XPA client routine such |
| 750 |
as XPASet(), they can deadlock while each waits for the other server |
| 751 |
to respond. (This can happen if the servers call XPAPoll() with a |
| 752 |
time limit, and send messages in between the polling call.) The |
| 753 |
reason this happens is that both client routines send a string to the |
| 754 |
other server to establish the handshake and then wait for the server |
| 755 |
response. Since each client is waiting for a response, neither is able |
| 756 |
to enter its event-handling loop and respond to the other's |
| 757 |
request. This deadlock will continue until one of the timeout periods |
| 758 |
expire, at which point an error condition will be triggered and the |
| 759 |
timed-out server will return to its event loop. |
| 760 |
|
| 761 |
<P> |
| 762 |
Starting with version 2.1.6, this rare race condition can be |
| 763 |
avoided by setting the XPA_IOCALLSXPA environment variable for servers |
| 764 |
that will make client calls. Setting this variable causes all XPA |
| 765 |
socket IO calls to process outstanding XPA requests whenever the |
| 766 |
primary socket is not ready for IO. This means that a server making a |
| 767 |
client call will (recursively) process incoming server requests while |
| 768 |
waiting for client completion. It also means that a server callback |
| 769 |
routine can handle incoming XPA messages if it makes its own XPA call. |
| 770 |
The semi-public routine oldvalue=XPAIOCallsXPA(newvalue) can be used |
| 771 |
to turn this behavior off and on temporarily. Passing a 0 will turn |
| 772 |
off IO processing, 1 will turn it back on. The old value is returned |
| 773 |
by the call. |
| 774 |
|
| 775 |
<P> |
| 776 |
By default, the XPA_IOCALLSXPA option is turned off, because we judge |
| 777 |
that the added code complication and overhead involved will not be |
| 778 |
justified by the amount of its use. Moreover, processing XPA requests |
| 779 |
within socket IO can lead to non-intuitive results, since incoming |
| 780 |
server requests will not necessarily be processed to completion in the |
| 781 |
order in which they are received. |
| 782 |
|
| 783 |
<P> |
| 784 |
Aside from setting XPA_IOCALLSXPA, the simplest way to avoid this race |
| 785 |
condition is to multi-process: when you want to send a client message, |
| 786 |
simply start a separate process to call the client routine, so that |
| 787 |
the server is not stopped. It probably is fastest and easiest to use |
| 788 |
fork() and then have the child call the client routine and exit. But |
| 789 |
you also can use either the system() or popen() routine to start one |
| 790 |
of the command line programs and do the same thing. Alternatively, you |
| 791 |
can use XPA's internal launch() routine instead of system(). Based on |
| 792 |
fork() and exec(), this routine is more secure than system() because |
| 793 |
it does not call /bin/sh. |
| 794 |
|
| 795 |
<P> |
| 796 |
Starting with version 2.1.5, you also can send an XPAInfo() message with |
| 797 |
the mode string "ack=false". This will cause the client to send a message |
| 798 |
to the server and then exit without waiting for any return message from |
| 799 |
the server. This UDP-like behavior will avoid the server deadlock when |
| 800 |
sending short XPAInfo messages. |
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
<P> |
| 829 |
<A HREF="./help.html">Go to XPA Help Index</A> |
| 830 |
|
| 831 |
<H5>Last updated: September 10, 2003</H5> |
| 832 |
</BODY> |
| 833 |
</HTML> |