Arbortext Command Language > Callbacks > channel_set_callback
  
channel_set_callback
channel_set_callback (ch, funcname[, data])
This function sets the specified function funcname as a callback to be invoked whenever certain events are detected on the network channel ch. ch is a channel identifier returned by open_connect or open_accept. The optional argument data is evaluated and its value is passed to the specified callback function.
Calling channel_set_callback automatically sets the channel ch to non-blocking mode.
The callback function must be declared as:
function funcname(ch, op[, data])
where ch is the channel identifier, data is the user data argument passed to channel_set_callback, and op is the integer specifying the type of notification.
0 — the channel was opened. This notification is actually given when data are first available for reading and allows the callback to initialize its state.
1 — the channel was closed. Note, that if read returns 0 when called on a read (op=2) notification, Arbortext Editor gives a close notification instead of returning 0 from read.
2 — the channel is ready for reading (that is, there is more incoming data).
The callback must call read (or getline if appropriate) on a read notification to read data from channel. The callback cannot be invoked again until after a read is performed. The callback can call read more than once on a single notification, but must be prepared to deal with a partial or incomplete read.
The callback is automatically removed when the channel is closed.
The sample function that follows uses open_connect and channel_set_callback to fetch an HTML document from an HTTP server (well known port 80). For clarity, it does minimal error checking and does not deal with the MIME headers that are normally transmitted preceding the actual document. Unlike the similar example given with open_connect, this uses channel_set_callback to read from channel in a non-blocking manner (that is, the user function ch_notify gets called only when data are available on the channel).
function http_async_get(host, path, lclfile) {
local of = open(lclfile, "wb")
if (of < 0) {
message "http_get: couldn't open $lclfile for write"
return 0;
}
local ch = open_connect(host, 80)
if (ch < 0) {
message "http_get: $main::api_error"
return 0;
}
channel_set_callback(ch, "ch_notify", of)
write(ch, "GET " . path . " HTTP/1.0\r\n\r\n")
return ch
}
function ch_notify(ch, what, of)
{
switch (what) {
case 0: # open
# could open the output file here
break;
case 1: #close
close(ch)
close(of)
break;
case 2: # read
local buf, len
len = read(ch, buf, 512)
switch (len) {
case -2: # read would block
return;
case -1: # unexpected error
message "ch_notify: $main::api_error"
# fall through
case 0: # connection was closed
# note, by closing CH here this callback
# gets removed so that we won't get a
# subsequent close notification (1).
close(of)
close(ch)
break
default:
write(of, buf, len)
}
}
}
Related Topics
getline built-in function
open_accept built-in function
open_connect built-in function
read built-in function