System Port Trap Example
[1/5] from: carl::s::rebol::com at: 12-Oct-2002 17:24
Here is a short script that uses the REBOL system-port to intercept
shutdown
types of interrupts from various operating systems.
Why? For example, you would want to do this if you had a REBOL server
process on Linux that has internal state. You may want the chance to
write out information to files before quitting. This is the code you need
to make it happen.
The code below will detect both CTRL-C and REBOL ESCAPE key.
enable-system-trap: does [
; Trap OS interrupts
if not system/ports/system [
if none? attempt [system/ports/system: open [scheme: 'system]][
print "NOTE: Missing System Port" exit
]
]
if find get-modes system/ports/system 'system-modes 'signal [
set-modes system/ports/system [
signal: intersect get-modes system/ports/system 'signal-names [
sigquit sigterm sigint sighup
]
]
]
system/console/break: 'signal
append system/ports/wait-list system/ports/system
]
check-system-trap: func [port /local msg] [
; Process OS interrupts
if not port? port [return none]
if any [port/scheme <> 'system port <> system/ports/system][return port]
if not system/ports/system [return none]
while [msg: pick system/ports/system 1] [
if find [signal escape] msg/1 [
print ["System Trap:" msg]
; (save files here)
quit
]
]
none
]
print "Starting..."
enable-system-trap
forever [
wake-port: wait 20 ; timeout period
check-system-trap wake-port
]
[2/5] from: rotenca:telvia:it at: 13-Oct-2002 10:39
This should be the test to do to know the endian type:
endian?: does [second get-modes system/ports/system [endian]]
endian?; == little
It don't open the port in a new shell, but it seems to work always.
---
Ciao
Romano
[3/5] from: rotenca:telvia:it at: 13-Oct-2002 10:21
Hi Carl,
> The code below will detect both CTRL-C and REBOL ESCAPE key.
On my 1.2.8.3.1 only escape.
And this expression:
get-modes system/ports/system 'system-modes
gives as result:
[window winmsg endian]
so the following code will be never executed:
if find get-modes system/ports/system 'system-modes 'signal [
set-modes system/ports/system [
signal: intersect get-modes system/ports/system 'signal-names [
sigquit sigterm sigint sighup
]
]
]
What it useful for?
---
Ciao
Romano
[4/5] from: andreas:bolka:gmx at: 13-Oct-2002 13:37
Sunday, October 13, 2002, 9:21:34 AM, Romano wrote:
>> The code below will detect both CTRL-C and REBOL ESCAPE key.
> On my 1.2.8.3.1 only escape.
<<quoted lines omitted: 4>>
> so the following code will be never executed:
> if find get-modes system/ports/system 'system-modes 'signal [
[...]
> What it useful for?
the result depends on the operating system you're working on. for
example on linux you'll get the following:
>> get-modes system/ports/system 'system-modes
== [signal read-fd write-fd except-fd signal-names endian]
as of course rebol will most likely not receive windows messages on
linux :)
--
Best regards,
Andreas mailto:[andreas--bolka--gmx--net]
[5/5] from: greggirwin:mindspring at: 13-Oct-2002 14:47
Thanks Carl!!
Of course, now I have something *else* to play with to keep me busy. :)
--Gregg
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted