-- ##############################
-- msg_receiver
-- version 0.6
-- (a paired program: msg_sender 0.6, http://pastebin.com/yUgqzL98)
-- http://hevohevo.hatenablog.com/
rednet.open("right")
function myRefuelAll()
for i=1,16 do
turtle.select(i)
turtle.refuel()
end
turtle.select(1)
end
function printResults(sender_id, message, tbl_results)
local exist_status, run_status, err_msg = unpack(tbl_results)
if exist_status then -- found a function
if run_status then -- success for running
print("#",sender_id,": ",message)
else -- failed or doesn\'t reply
print("#",sender_id,": ",message)
if err_msg then
print(" (Error) ",err_msg)
else
print(" (No reply) ")
end
end
else -- failed with running by wrong string
print("#",sender_id,": ",message)
print(" (NotFound) ")
end
end
-- main
while true do
local sender_id, message, distance = rednet.receive()
-- when a message includes the plural functions, doesn\'t return values.
if not string.find(message,";") then
message = "return "..message
end
-- make a function and run
local func = loadstring(message)
setfenv(func, getfenv()) -- change func\'s environment(_G) to current one.
local results = {pcall(func)} -- table: 1) exist_status, 2) run_status, 3) err_msg
-- respond results to the sender
rednet.send(sender_id, textutils.serialize(results))
-- print results to the terminal display.
printResults(sender_id, message, results)
end