Reviving topic, new problem, updated code… but don't feel like making another post in ask-a-pro
Anyway, my test program is now freezing up, and won't get past using wrap("left", 204, 204). I believe the problem is in the api, considering I *tried* to add some code to prevent random messages from messing it up(not that that was likely anyway, as the data was exchanged within… idk how short of a time XD). I know the slave is sending the table, since I added 'print' into the code as a bonus debug feature (i will enable/disable this later).
api
local modem = peripheral.wrap("back")
local oldos = {}
for k, v in pairs(os) do
oldos[k] = v
end
local function test(event)
if event.type == "peripheral" or event.type == "terminal" then
return true
elseif event.type == "turtle" or event.type == "fs" then
return true
else
return false
end
end
function os.pullEventRaw(...)
local args = {...}
local event = {oldos.pullEventRaw(unpack(args))}
if event[1] == "modem_message" and type(event[5]) == "table" and test(event[5]) then
event[5].info[1] = event[3].."_"..event[5].info[1]
return unpack(event[5].info)
else
return unpack(event)
end
end
function setModem(side)
if peripheral.getType(side) == "modem" then
modem = peripheral.wrap(side)
else
error("Modem expected, got "..peripheral.getType(side).."!", 2)
end
if not modem.isWireless() then
modem = peripheral.wrap("back")
error("Expected Wireless Modem, got Wired Modem!", 2)
end
end
function wrap(side, channela, channelb)
if not tonumber(channela) then
error("Expected Number, got ".. type(channela).."!", 2)
end
if not tonumber(channelb) then
error("Expected Number, got ".. type(channela).."!", 2)
end
local per, call = {
side = side,
channela = channela,
channelb = channelb,
}, {}
modem.open(channela)
modem.transmit(channela, channelb, {side = side, type = "wrap_peripheral"})
repeat
local msg = {os.pullEvent("modem_message")}
if type(msg[5]) == "table" and msg[5].type == "peripheral" then
for k, v in pairs(msg[5].info) do
call[v] = function(...)
modem.transmit(channela, channelb, {type = "call", side = side, call = v, params = {...}})
repeat
local event = {os.pullEvent("modem_message")}
if event[5].type == "peripheral" then
return unpack(event[5].info)
end
until(event[5].type == "peripheral")
end
end
end
until(msg[5].type == "peripheral")
setmetatable(per, {__index = call})
return per
end
function call(side, channela, channelb, method, ...)
if not tonumber(channela) then
error("Expected number, got "..type(channela).."!", 2)
end
if not tonumber(channelb) then
error("Expected number, got "..type(channelb).."!", 2)
end
if not tostring(method) then
error("Expected string, got "..type(method).."!", 2)
end
modem.open(channela)
modem.transmit(channela, channelb, {type = "call", side = side, call = method, params = {...}})
while true do
local event = {os.pullEvent("modem_message")}
if event[5].type == "peripheral" then
return(unpack(event[5].info))
end
end
end
function term(channela, channelb)
local per, call = {
channela = channela,
channelb = channelb,
}, {}
modem.open(channela)
modem.transmit(channela, channelb, {type = "wrap_term"})
repeat
local msg = {os.pullEvent("modem_message")}
if type(msg[5]) == "table" and msg[5].type == "terminal" then
for k, v in pairs(msg[5].info) do
call[k] = function(...)
modem.transmit(channela, channelb, {type = "terminal_call", call = k, params = {...}})
repeat
local event = {os.pullEvent("modem_message")}
if event[5].type == "terminal" then
return unpack(event[5].info)
end
until(event[5].type == "terminal")
end
end
end
until(msg[5].type == "terminal")
setmetatable(per, {__index = call})
return per
end
function turtle(channela, channelb)
local per, call = {
channela = channela,
channelb = channelb,
}, {}
modem.open(channela)
modem.transmit(channela, channelb, {type = "wrap_turtle"})
repeat
local msg = {os.pullEvent("modem_message")}
if type(msg[5]) == "table" and msg[5].type == "turtle" then
for k, v in pairs(msg[5].info) do
call[k] = function(...)
modem.transmit(channela, channelb, {type = "turtle_call", call = k, params = {...}})
repeat
local event = {os.pullEvent("modem_message")}
if event[5].type == "turtle" then
return unpack(event[5].info)
end
until(event[5].type == "turtle")
end
end
end
until(event[5].type == "turtle")
setmetatable(per, {__index = call})
return per
end
function fs(channela, channelb)
local per, call = {
channela = channel,
channelb = channelb,
}, {}
modem.open(channela)
modem.transmit(channela, channelb, {type = "wrap_fs"})
repeat
local msg = {os.pullEvent("modem_message")}
if type(msg[5]) == "table" and msg[5].type == "fs" then
for k, v in pairs(msg[5].info) do
call[k] = function(...)
modem.transmit(channela, channelb, {type = "fs_call", call = k, params = {...}})
repeat
local event = {os.pullEvent("modem_message")}
if event[5].type == "fs" then
return unpack(event[5].info)
end
until(event[5].type == "fs")
end
end
end
until(event[5].type == "fs")
setmetatable(per, {__index = call})
return per
end
slave
local args = {...}
if #args < 3 then
error("Incomplete arguments", 0)
elseif #args > 3 then
error("Too many arguments", 0)
end
assert(tonumber(args[1]), args[1].." isn't a number!")
assert(tonumber(args[2]), args[2].." isn't a number!")
args[1] = tonumber(args[1])
args[2] = tonumber(args[2])
local valid = false
if not peripheral.getType(args[3]) == "modem" then
error("Side is not valid", 0)
end
local modem = peripheral.wrap(args[3])
if not modem.isWireless() then
error("Modem isn't wireless")
else
modem.open(args[2])
end
local function reply(info)
modem.transmit(args[1], args[2], {info = info, type = "peripheral"})
end
while true do
event = {os.pullEvent()}
if event[1] == "modem_message" and type(event[5]) == "table" and event[5].type == "call" then
local response = {peripheral.call(event[5].side, event[5].call, unpack(event[5].params))}
reply(response)
term.setTextColor(colors.blue)
print("peripheral response sent")
elseif event[1] == "modem_message" and type(event[5]) == "table" and event[5].type == "wrap_peripheral" then
local methods = peripheral.getMethods(event[5].side)
reply(methods)
term.setTextColor(colors.red)
print("peripheral table sent")
elseif event[1] == "modem_message" and type(event[5]) == "table" and event[5].type == "wrap_term" then
local t = term.native()
modem.transmit(args[1], args[2], {info = t, type = "term"})
term.setTextColor(colors.lime)
print("terminal table sent")
elseif event[1] == "modem_message" and type(event[5]) == "table" and event[5].type == "wrap_turtle" then
if turtle then
modem.transmit(args[1], args[2] {info = turtle, type = "turtle"})
term.setTextColor(colors.orange)
print("turtle table sent")
else
modem.transmit(args[1], args[2], {info = nil, type = "turtle"})
term.setTextColor(colors.orange)
print("turtle does not exist")
end
elseif event[1] == "modem_message" and type(event[5]) == "table" and event[5].type == "terminal" then
local response = term.native()[event[5].info](unpack(event[5].params))
modem.transmit(args[1], args[2], {info = response, type = "terminal"})
term.setTextColor(colors.cyan)
print("terminal response sent")
elseif event[1] == "modem_message" and type(event[5]) == "table" and event[5].type == "turtle" then
local response = turtle[event[5].info](unpack(event[5].params))
term.setTextColor(colors.green)
print("turtle response sent")
else
reply(event)
term.setTextColor(colors.magenta)
print("peripheral event sent")
end
end