Posted 19 February 2013 - 02:32 AM
APIs/daemon: http://pastebin.com/vmTnxGS9
daemon Program: http://pastebin.com/F89eWbtp
This is my attempt at a daemon API, made by hooking coroutine.resume() and using it to resume daemons routines.
Previous attempts at daemon APIs have suffered various flaws:
Running a new shell inside the daemon manager: I inject the daemon manager above the shell, so I don't need to nest shells and I don't break startup scripts
Not getting all events: this api injects itself in a way which should forward all events to daemons
Passing duplicate events to daemons: This is caused by hooking a function like os.pullEvent and not caring about who called it. In CC 1.5 there are parallel routines running rednet and the main shell, and both use pullEvent. If you don't filter by the running routine you get all events at least twice.
Not handling "terminate": I forward terminate to the main routine first, and only forward it to daemons if the main routine exits. This prevents daemons terminating unexpectedly.
Not handling daemons throwing errors: I print a message and terminate only the daemon that error'd, not the entire shell
Usage:
First initialise the daemon manager:
Adding a daemon:
Killing a daemon:
Sending an event to a daemon:
(runs the daemon immediately with the new event, unless it used os.pullEvent("filter") to ignore it)
Getting the status of a daemon:
(returns nil if not running, "active" otherwise)
Getting a list of all daemons:
I look forward to seeing what people think!
TODO:
License
CC BY-NC-SA 3.0
daemon Program: http://pastebin.com/F89eWbtp
This is my attempt at a daemon API, made by hooking coroutine.resume() and using it to resume daemons routines.
Previous attempts at daemon APIs have suffered various flaws:
Running a new shell inside the daemon manager: I inject the daemon manager above the shell, so I don't need to nest shells and I don't break startup scripts
Not getting all events: this api injects itself in a way which should forward all events to daemons
Passing duplicate events to daemons: This is caused by hooking a function like os.pullEvent and not caring about who called it. In CC 1.5 there are parallel routines running rednet and the main shell, and both use pullEvent. If you don't filter by the running routine you get all events at least twice.
Not handling "terminate": I forward terminate to the main routine first, and only forward it to daemons if the main routine exits. This prevents daemons terminating unexpectedly.
Not handling daemons throwing errors: I print a message and terminate only the daemon that error'd, not the entire shell
Usage:
First initialise the daemon manager:
if not daemon then os.loadAPI("APIs/daemon") end
if not daemon.isInstalled() then daemon.install() end
or use the daemon program:daemon install
Adding a daemon:
function daemon_function()
while true do
print "tick"
sleep(1)
print "tock"
sleep(1)
end
end
daemon.add("ticktock", daemon_function)
or use the daemon program:daemon add <program> <parameters>
Killing a daemon:
daemon.kill("ticktock")
or use the daemon program:daemon kill <name>
Sending an event to a daemon:
(runs the daemon immediately with the new event, unless it used os.pullEvent("filter") to ignore it)
daemon.sendEvent("ticktock", "terminate")
Getting the status of a daemon:
(returns nil if not running, "active" otherwise)
print(daemon.getStatus("ticktock"))
if not daemon.getStatus("ticktock") then
daemon.add("ticktock", daemon_function)
end
Getting a list of all daemons:
for key, value in daemon.list() do
print key, value.status
end
or use the daemon program:daemon list
I look forward to seeing what people think!
TODO:
- Blocking certain events from daemons (e.g. char)
- Per-daemon output redirect support
License
CC BY-NC-SA 3.0