Posted 27 January 2013 - 06:30 AM
A daemon in computer science is a background task. Traditionally, Lua doesn't do this too well. But I think I've built a nice solution for this.
As soon as you run this program, a new shell will be started. This shell can be used normally. Now you can add daemons to the background.
daemon.loadDaemon(stringName, func) is how you load a daemon. You create a function that you want to run in the background, and load it. In the example above, I declared the function within the loadDaemon call, but you can create the function before hand also.
This is particularly useful for APIs that need tasks to keep running.
Now you can call MyApiFile.getLastMessage() to retrieve the most recent rednet message. This is obviously a very basic example but it works and it demonstrates just what the daemon server can do.
To stop the daemon server, run the program again with the argument -stop or call daemon.stopServer(). Also, running <filename> -list will list all running daemons by their IDs and names. Using <filename> -kill <ID> will kill the daemon with the specified ID. Using <filename> -s will launch the daemon server and run startup when the new shell starts.
Note: no matter what you name this file, the API for loadDaemon() is ALWAYS called daemon. So no matter what, the code is daemon.loadDaemon(stringName, func).
2nd Note: I strongly urge you to use os.pullEventRaw instead of os.pullEvent from within your daemons. Because of the way termination works, it's very tricky to predict how a daemon will react to this, if at all.
pastebin get MtkSVDWH startd
Changelog
As soon as you run this program, a new shell will be started. This shell can be used normally. Now you can add daemons to the background.
daemon.loadDaemon("My Daemon", function()
-- Code to do stuff.
end)
daemon.loadDaemon(stringName, func) is how you load a daemon. You create a function that you want to run in the background, and load it. In the example above, I declared the function within the loadDaemon call, but you can create the function before hand also.
This is particularly useful for APIs that need tasks to keep running.
--MyApiFile
if daemon then
local lastMessage
function getLastMessage()
return lastMessage
end
function myDaemon()
while true do
local id, msg = rednet.receive()
lastMessage = msg
end
end
daemon.loadDaemon("My Daemon", myDaemon)
else
print("This API requires the Daemon Server by ElvishJerricco")
end
Now you can call MyApiFile.getLastMessage() to retrieve the most recent rednet message. This is obviously a very basic example but it works and it demonstrates just what the daemon server can do.
To stop the daemon server, run the program again with the argument -stop or call daemon.stopServer(). Also, running <filename> -list will list all running daemons by their IDs and names. Using <filename> -kill <ID> will kill the daemon with the specified ID. Using <filename> -s will launch the daemon server and run startup when the new shell starts.
Note: no matter what you name this file, the API for loadDaemon() is ALWAYS called daemon. So no matter what, the code is daemon.loadDaemon(stringName, func).
2nd Note: I strongly urge you to use os.pullEventRaw instead of os.pullEvent from within your daemons. Because of the way termination works, it's very tricky to predict how a daemon will react to this, if at all.
pastebin get MtkSVDWH startd
Changelog
Spoiler
v1.2.1- Added -s option. Launches startup in the new shell.
- daemon.loadDaemon(stringName, func) now has an argument for the name of the daemon.
- Added -list option. Will list all daemons currently running.
- Added -kill option. Will kill the targeted daemon.
- Added daemon.getDaemonIDsNames(). Returns a table of daemon names with their IDs as keys.
- Added daemon.killDaemonByID(id). Kills the daemon with that ID.
- Modified the help option to scroll since it's now too long for one page.
- Ctr+T now works.
- Calling daemon.stopServer() will stop the server.
- Using the command argument -stop will stop the server.
- Initial release.