Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("faketerm")
- local sides = {"front","back","left","right","top","bottom"}
- local modems = {} --sides containing modems
- local peripheral_sides = {} --list of sides containing a peripheral that is not a modem
- local peripheral_map = {} --local perhipheral_name -> side map
- local connections = {} --networked computer names
- local peripherals = {} --networked peripheral names that are not computers
- local upstream = nil
- local downstream = {} --sides with downstream modems
- local am_i_lost = true --our position on the network is unknown
- function table.find(t,val) for _, v in ipairs(t) do if v == val then return v end end return nil end
- function pack(...) return arg end
- function scan_sides()
- modems = {}
- peripheral_sides = {}
- peripheral_map = {}
- connections = {}
- peripherals = {}
- for _,v in ipairs(sides) do if peripheral.isPresent(v) then if peripheral.getType(v) == "modem" then table.insert(modems,v) else table.insert(peripheral_sides,v) end end end --populate modems, peripheral_sides
- for _,v in ipairs(modems) do peripheral.call(v,"open",1) end --listen on channel 1 on all modems
- local cur_side = nil
- for i,v in ipairs(peripheral.getNames()) do if table.find(sides,v) ~= nil then
- if strsub(v,1,8) == "computer" then table.insert(connections,v) else table.insert(peripherals,v) end end end --populate peripherals, connections
- for i,v in ipairs(peripheral_sides) do peripheral_map[peripheral.getType().."_"..os.computerID()] = v end --populate local peripherals
- end
- scan_sides()
- function make_packet(to,command,...)
- local packet = {}
- packet["message_id"] = os.clock()
- packet["sender_id"] = os.computerID()
- packet["destination_id"] = to
- packet["command"] = command
- packet["args_table"] = arg
- return packet
- end
- function set_upstream(side)
- upstream = side
- downstream = {}
- for _,v in ipairs(modems) do if v ~= side then table.insert(downstream,v) end end
- peripherals = {}
- peripheral_map = {}
- for _,v in ipairs(peripheral_sides) do if table.find(sides,v) == nil then table.insert(peripherals,os.computerID().."_"..peripheral.getType(v).."_") end end --add direct peripherals to table
- for _,v in ipairs(peripheral.getNames()) do
- local cur_side = nil;
- if table.find(sides,v) == nil then
- if cur_side ~= nil and cur_side ~= upstream and string.sub(v,1,9) ~= "computer_" then
- table.insert(peripherals,v) --add networked peripherals
- end
- else
- cur_side = v
- end
- end
- end
- while true do
- local event,side,freq,replyFreq,message,dist = os.pullEvent("modem_message")
- local msg = textutils.unserialize(message)
- local args
- if msg["command"] == "tracert" then --modify message
- args = msg["args_table"]
- table.insert(args[1],os.computerID().."_"..os.getComputerLabel())
- end
- if msg["destination_id"] == os.computerID() or msg["destination_id"] == 0 then
- args = msg["args_table"]
- if msg["command"] == "acknowledge" then --do some pending_queue stuff
- --TODO
- else --respond with acknowledge
- peripheral.call(side,"transmit",replyFreq,freq,textutils.serialize(make_packet(msg["sender_id"],"acknowledge",msg["message_id"])))
- end
- if msg["command"] == "ping" then --respond with connected_to
- peripheral.call(side,"transmit",replyFreq,freq,textutils.serialize(make_packet(msg["sender_id"],"connected_to",os.getComputerLabel(),connections,peripherals)))
- end
- if msg["command"] == "tracert" then --respond with tracert_response
- peripheral.call(side,"transmit",replyFreq,freq,textutils.serialize(make_packet(msg["sender_id"],"tracert_response",args[1])))
- end
- if msg["command"] == "tracert_response" then end --dummy does nothing
- if msg["command"] == "exec" then --run script
- for i,v in pairs(args) do write(i) write(": ") print(v) end
- local func = loadstring(args[1]) --load command string (does not run yet)
- local rval
- if args[2] == nil then --no response
- pcall(func()) --just run command
- end
- if args[2] == "vals" then --respond with report_return, return vals
- rval = {pcall(func)} --run command, save return val(s)
- peripheral.call(side,"transmit",replyFreq,freq,textutils.serialize(make_packet(msg["sender_id"],"report_return",msg["message_id"],rval)))
- end
- if args[2] == "term" then --respond with report_return, terminal output
- local ft
- if args[3] ~= nil then
- ft = faketerm.newFakeTerm(args[3].col,args[3].sx,args[3].sy,args[3].cx,args[3].cy)
- else
- ft = faketerm.newFakeTerm()
- end
- local ct = term.current()
- term.redirect(faketerm)
- success,err = pcall(func)
- if not success then print(err) end
- rval = faketerm.historyCopy()
- faketerm.deleteFakeTerm(ft)
- term.redirect(ct)
- peripheral.call(side,"transmit",replyFreq,freq,textutils.serialize(make_packet(msg["sender_id"],"report_return",msg["message_id"],rval)))
- end
- end
- if msg["command"] == "report_event" then end --dummy does nothing
- if msg["command"] == "report_return" then end --dummy does nothing
- if msg["command"] == "replace" then --update file
- file = fs.open(args[1],"w") --open file
- file.writeLine(args[2]) --write script
- file.close() --close file
- end
- if msg["command"] == "exception" then end --dummy does nothing
- if msg["command"] == "routing_reset" then --respond with register
- set_upstream(side)
- am_i_lost = false
- scan_sides()
- peripheral.call(side,"transmit",replyFreq,freq,textutils.serialize(make_packet(msg["sender_id"],"register",os.getComputerLabel(),am_i_lost)))
- end
- if msg["command"] == "register" then end --dummy does nothing
- if msg["command"] == "list_connections" then --respond with connected_to
- peripheral.call(side,"transmit",replyFreq,freq,textutils.serialize(make_packet(msg["sender_id"],"connected_to",os.getComputerLabel(),connections,peripherals)))
- end
- if msg["command"] == "connected_to" then end --dummy does nothing
- if msg["command"] == "assume_safe_state" then shell.run("safe_state") end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement