Posted 10 December 2012 - 10:29 PM
Or something like that. This is currently in it's infancy, but it will be very useful/feature packed when it's done.
Anyway, currently all this thing does is let you execute programs remotely and instantly on a virtually limitless number of clients, while at the same time keeping the clients protected from any errors that might occur in the code/program you send them. So if your program errors out, the wrapper will keep running allowing you to send it a new version or even a totally diffident program. Keep in mind that there is no write protection, so if you send it two programs with the same name, it will over-write the old one.
I plan to add several things to this over the next few days, including but not limited to:
- The ability to specify clients
- The ability to "link" clients to a host so that it always updates only those few clients
- The ability to remote-mange files on the clients (run, delete, execute, etc)
And now for the code
Host program:
client:
To use, simply run the client on all the machines you want to receive, then just follow this format when running the host command:
If you guys have any suggests/tips please tell me as I'm just starting out with this computercraft.
Anyway, currently all this thing does is let you execute programs remotely and instantly on a virtually limitless number of clients, while at the same time keeping the clients protected from any errors that might occur in the code/program you send them. So if your program errors out, the wrapper will keep running allowing you to send it a new version or even a totally diffident program. Keep in mind that there is no write protection, so if you send it two programs with the same name, it will over-write the old one.
I plan to add several things to this over the next few days, including but not limited to:
- The ability to specify clients
- The ability to "link" clients to a host so that it always updates only those few clients
- The ability to remote-mange files on the clients (run, delete, execute, etc)
And now for the code
Host program:
local args = {...}
function getPeripheral(thing)
local Sides = {
"left","right",
"top","bottom",
"front","back",
}
for i,v in pairs(Sides) do
if (peripheral.isPresent(v) and peripheral.getType(v) == thing) then
print("'"..thing.."' found on side '"..v.."'.")
return peripheral.wrap(v), v
end
end
print("ERROR: '"..thing.."' not found!")
return nil, nil
end
function string.explode(str, delim)
local stringParts = {}
local lastPos = 1
for i = 1, #str do
if (str:sub(i,i) == delim) then
table.insert(stringParts, str:sub(lastPos, i-1))
lastPos = i+1
end
end
table.insert(stringParts, str:sub(lastPos, #str))
return stringParts
end
local modem, side = getPeripheral("modem")
if (modem) then
rednet.open(side)
if (args[1] == "code") then
local name = string.explode(args[1], '/')
rednet.broadcast("receive RAW_CODE")
rednet.broadcast(args[2])
rednet.broadcast("run RAW_CODE")
elseif (fs.exists(args[1])) then
local file = io.open(args[1], "r")
local CONTENTS = ""
for line in file:lines() do
CONTENTS = CONTENTS..line.."\n"
end
file:close()
local name = string.explode(args[1], '/')
rednet.broadcast("receive "..name[#name])
rednet.broadcast(CONTENTS)
rednet.broadcast("run "..name[#name].." "..(args[2] or ""))
end
rednet.close(side)
end
client:
function getPeripheral(thing)
local Sides = {
"left","right",
"top","bottom",
"front","back",
}
for i,v in pairs(Sides) do
if (peripheral.isPresent(v) and peripheral.getType(v) == thing) then
print("'"..thing.."' found on side '"..v.."'.")
return peripheral.wrap(v), v
end
end
print("ERROR: '"..thing.."' not found!")
return nil, nil
end
function string.explode(str, delim)
local stringParts = {}
local lastPos = 1
for i = 1, #str do
if (str:sub(i,i) == delim) then
table.insert(stringParts, str:sub(lastPos, i-1))
lastPos = i+1
end
end
table.insert(stringParts, str:sub(lastPos, #str))
return stringParts
end
local function saveFile(filename, FILE_CONTENTS)
if (not fs.exists("received")) then
fs.makeDir("received")
end
local file = io.open("received/"..filename, "w")
if (file:write(FILE_CONTENTS)) then
file:close()
return 1
else
file:close()
return -2
end
end
local modem, side = getPeripheral("modem")
if (modem) then
local msg = {}
local receiveMode = "commands"
rednet.open(side)
while (msg ~= "stop") do
local id, tmpMsg, dist
local senderId = -1
local msgData = {}
id, msg, dist = rednet.receive()
msgData = string.explode(msg, ' ')
if (msgData[1] == "receive") then
senderId = id
local newId = -1
local FILE = ""
while (newId ~= senderId) do
newId, FILE = rednet.receive()
end
local filepath = string.explode(msgData[2], '/')
local err = saveFile(filepath[#filepath], FILE)
--if (err == -1) then
end
if (msgData[1] == "run") then
if ( not fs.exists("received/"..msgData[2])) then
rednet.send(id, "badname")
else
if (msgData[3]) then
if (not shell.run("received/"..msgData[2], unpack(string.explode(msgData[3],",")))) then
rednet.send(id, "badrun")
end
else
if (not shell.run("received/"..msgData[2])) then
rednet.send(id, "badrun")
end
end
end
end
end
rednet.close(side)
end
Since this is a very early version there are a few things to note: the client auto-executes everything you send it, when you send code it can only be one statement, there is virtually zero error checking, and the code is rather messyTo use, simply run the client on all the machines you want to receive, then just follow this format when running the host command:
"commandName" <absolute path or the word "code"> <the programs arguments or your code>
The path is delimited by '/' and the arguments are delimited by ','
Examples:
transmitPrograms disk/funstuff/disco square,100,10
transmitPrograms code turtle.dig()
If you guys have any suggests/tips please tell me as I'm just starting out with this computercraft.