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

Calling a program through rednet?

Started by marceloclp, 30 April 2014 - 07:04 PM
marceloclp #1
Posted 30 April 2014 - 09:04 PM
Hi. I am new to the ComputerCraft APIs (and LUA). Basically, I want to create a server between all my turtles and computers, where I'll have a main computer where I can call the turtles to execute programs.

My question is: is there any way I can create a program that allows me to call other programs? Like this:

I execute the main program on my main computer. All my turtles have the rednet on, but they aren't running any program at the moment. So I type, for example, the label of the turtle and name of the program, on my main computer, and the turtle will run that program.

And, since I already created this topic, allow me to ask another question: is there any way to make a program execute automatically when you load the chunk or log in to the game?d
CometWolf #2
Posted 30 April 2014 - 09:51 PM
Are your programs stored on the turtles already, or only on the main computer? It get's a little more involved depending on your answer.
You can make a computer run any program when it boots, by naming that program startup and putting it in the root folder. Usually most computers that were running when they were unloaded, will essentially reboot whenever they're reloaded.
electrodude512 #3
Posted 30 April 2014 - 10:40 PM
You can have a process running in the background using multishell or some daemon thingy that receives programs over rednet and then runs them (optionally saving them to disk).
CometWolf #4
Posted 30 April 2014 - 11:05 PM
Nothing fancy like that is required, just a simple override of pullEvent loaded at startup will do
Agoldfish #5
Posted 01 May 2014 - 12:12 AM
(and LUA).
It's Lua.
marceloclp #6
Posted 01 May 2014 - 12:43 AM
Are your programs stored on the turtles already, or only on the main computer? It get's a little more involved depending on your answer.
You can make a computer run any program when it boots, by naming that program startup and putting it in the root folder. Usually most computers that were running when they were unloaded, will essentially reboot whenever they're reloaded.
They are stored on the turtle (but not on the main computer), but I can easily download them if needed to. Ok, startup, got it, and how do I move programs to the root folder?

You can have a process running in the background using multishell or some daemon thingy that receives programs over rednet and then runs them (optionally saving them to disk).
I'll take a look at multishell, then. Seems interesting.

Nothing fancy like that is required, just a simple override of pullEvent loaded at startup will do
Hm, could you explain? :P/>

Ok.
CometWolf #7
Posted 01 May 2014 - 01:53 AM
The root folder is just "/". If you don't know much of computers, you can think of it like the top folder. It's where the rom folder is stored. You just use the built in move functions to move your files around.
Type this into the shell, subsituting folder/file with your program path obviously.

mv folder/file /startup

This will be simpler if your programs are already on the turtles, so don't worry about downloading them to the main computer. The turtles will just need to use shell.run on whatever they receive through rednet.

local id,message = rednet.receive()
shell.run(message)
Then you could just send the name of your program, or whatever you type into the shell when you run it normally.

Now to override the original pullEvent, so this function is always active, would be something like this

local oldPull = _G.os.pullEventRaw --create a local backup of the original
local newPull --forward declaration of the new pull function, this is done so we can reference the function itself from within itself
newPull = function(sString) --create our own pullEvent functon
  local tEvent = {oldPull(sFilter)} --use the original to get events
  if tEvent[1] == "rednet_message" then --check if the event was a rednet message
	_G.os.pullEventRaw = oldPull --restore the old pullEvent before running the program
	shell.run(tEvent[3])
	_G.os.pullEventRaw = newPull --override pullEvent again after the program is done
  end
  return unpack(tEvent) --return the events, so any other uses of pullEvent work like normal
end
_G.os.pullEventRaw = newPull --bootup override
This is untested code, so it might not work :P/> But ask if you have any questions.
Edited on 01 May 2014 - 12:23 AM
marceloclp #8
Posted 01 May 2014 - 06:58 AM
To override the pullEvent, I have to create a new program with the code you wrote and execute it, right?
Edited on 01 May 2014 - 04:58 AM
Bomb Bloke #9
Posted 01 May 2014 - 08:03 AM
It can either be your current script, or a different one. Once that code executes, the override will persist until you either manually undo it (by pointing _G.os.pullEventRaw back at the original version of the function), or until the computer restarts.
CoLDarkness #10
Posted 01 May 2014 - 01:13 PM
Well, also in addition to Comet's post I would like to remind you to use an encryption atleast to block incoming attacker packets.