This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Thief^'s profile picture

Daemon API

Started by Thief^, 19 February 2013 - 01:32 AM
Thief^ #1
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:

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
superaxander #2
Posted 21 February 2013 - 07:46 PM
Beautiful exactly what I need.
Under what circumstances may I use this?
Thief^ #3
Posted 21 February 2013 - 11:43 PM
This is the license, if that's what you're asking: http://creativecommo...s/by-nc-sa/3.0/
(In Dutch: http://creativecommons.org/licenses/by-nc-sa/3.0/deed.nl)

If you're asking what it's useful for, you can use it to run multiple programs at the same time by using "daemon add <program> <parameters>". I created it to run my networking/routing code in the background while still giving me a working shell and allowing me to run any other programs.
Spongy141 #4
Posted 10 March 2013 - 02:29 PM
So by using this API, is it possible to have an account information program running in the background giving the main program its information at the same time?
cogilv25 #5
Posted 08 April 2013 - 12:11 PM
don't mean to sound stupid but is there any chance of a short explanation to what this actually does and what a daemon is because it looks interesting but I don't understand. thanks :)/>
lieudusty #6
Posted 08 April 2013 - 12:19 PM
Very nice API
airtonix #7
Posted 04 July 2013 - 09:56 PM
@Theif^

Awesome. Should help the ComputerCraft community create some nice standards.

don't mean to sound stupid but is there any chance of a short explanation to what this actually does and what a daemon is because it looks interesting but I don't understand. thanks :)/>

http://en.wikipedia....mon_(computing)