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

Simple way to do Daemons?

Started by LDShadowLord, 06 April 2014 - 12:26 AM
LDShadowLord #1
Posted 06 April 2014 - 02:26 AM
I'm creating a program which is a modified router, while allows me to go all NSA and log everything, but i'd still like access to the shell so does anyone know of a way I could integrate a Daemon? Or link to me a way I could integrate a Daemon? I'd prefer to avoid coroutines as I don't understand them.
1lann #2
Posted 06 April 2014 - 02:34 AM
I'm creating a program which is a modified router, while allows me to go all NSA and log everything, but i'd still like access to the shell so does anyone know of a way I could integrate a Daemon? Or link to me a way I could integrate a Daemon? I'd prefer to avoid coroutines as I don't understand them.
A quick example:

local function daemon()
  -- This will run in the background
  -- I would not advise writing anything to the screen :P/>
end
parallel.waitForAny(daemon, function() shell.run("/rom/programs/shell") end)
Edited on 06 April 2014 - 12:34 AM
Dog #3
Posted 06 April 2014 - 02:40 AM
Essentially the same approach, but a bit different syntactically (it just breaks the shell down into its own function outside of the parallel call):

local function foregroundShell()
  shell.run("shell")
end

local function myMainFunction()
  <your main program loop>
  <this runs in the background>
  <do not write to the screen>
end

parallel.waitForAny(myMainFunction,foregroundShell)
Edited on 06 April 2014 - 12:51 AM
Bomb Bloke #4
Posted 06 April 2014 - 02:53 AM
One way is to write a modified version of os.pullEvent, and have it record whatever data you're interested in.

For eg, the default one looks something like this:

function os.pullEvent(eventType)
	myEvent = {os.pullEventRaw(eventType)}
	
	if myEvent[1] == "terminate" then error("Ctrl+T pressed") end
	
	return myEvent
end

You can see that it's basically a wrapper for os.pullEventRaw - it returns whatever that returns, unless it returns a terminate event, in which case it kills your script.

If you declared your own version of that function, which eg looked for key events and saved them into a hidden text file somewhere, then hey presto! Key logger. You literally just take something like the above code block, alter it to taste, run it on startup, then otherwise allow the computer to work "normally".

The code demonstrated here functions under this concept, though it may be a little complex for you to read at this stage.
Edited on 06 April 2014 - 12:54 AM
LDShadowLord #5
Posted 06 April 2014 - 02:53 AM
Excellent, thankyou very much, folks.