Advertisement
Guest User

dummy2

a guest
Mar 31st, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.13 KB | None | 0 0
  1. os.loadAPI("faketerm")
  2.  
  3. local sides = {"front","back","left","right","top","bottom"}
  4. local modems = {} --sides containing modems
  5. local peripheral_sides = {} --list of sides containing a peripheral that is not a modem
  6. local peripheral_map = {} --local perhipheral_name -> side map
  7. local connections = {} --networked computer names
  8. local peripherals = {} --networked peripheral names that are not computers
  9. local upstream = nil
  10. local downstream = {} --sides with downstream modems
  11. local am_i_lost = true --our position on the network is unknown
  12.  
  13. function table.find(t,val) for _, v in ipairs(t) do if v == val then return v end end return nil end
  14. function pack(...) return arg end
  15.  
  16. function scan_sides()
  17. modems = {}
  18. peripheral_sides = {}
  19. peripheral_map = {}
  20. connections = {}
  21. peripherals = {}
  22. 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
  23. for _,v in ipairs(modems) do peripheral.call(v,"open",1) end --listen on channel 1 on all modems
  24. local cur_side = nil
  25. for i,v in ipairs(peripheral.getNames()) do if table.find(sides,v) ~= nil then
  26. if strsub(v,1,8) == "computer" then table.insert(connections,v) else table.insert(peripherals,v) end end end --populate peripherals, connections
  27. for i,v in ipairs(peripheral_sides) do peripheral_map[peripheral.getType().."_"..os.computerID()] = v end --populate local peripherals
  28. end
  29. scan_sides()
  30.  
  31. function make_packet(to,command,...)
  32. local packet = {}
  33. packet["message_id"] = os.clock()
  34. packet["sender_id"] = os.computerID()
  35. packet["destination_id"] = to
  36. packet["command"] = command
  37. packet["args_table"] = arg
  38. return packet
  39. end
  40.  
  41. function set_upstream(side)
  42. upstream = side
  43. downstream = {}
  44. for _,v in ipairs(modems) do if v ~= side then table.insert(downstream,v) end end
  45. peripherals = {}
  46. peripheral_map = {}
  47. 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
  48. for _,v in ipairs(peripheral.getNames()) do
  49. local cur_side = nil;
  50. if table.find(sides,v) == nil then
  51. if cur_side ~= nil and cur_side ~= upstream and string.sub(v,1,9) ~= "computer_" then
  52. table.insert(peripherals,v) --add networked peripherals
  53. end
  54. else
  55. cur_side = v
  56. end
  57. end
  58. end
  59.  
  60. while true do
  61. local event,side,freq,replyFreq,message,dist = os.pullEvent("modem_message")
  62. local msg = textutils.unserialize(message)
  63. local args
  64. if msg["command"] == "tracert" then --modify message
  65. args = msg["args_table"]
  66. table.insert(args[1],os.computerID().."_"..os.getComputerLabel())
  67. end
  68.  
  69. if msg["destination_id"] == os.computerID() or msg["destination_id"] == 0 then
  70. args = msg["args_table"]
  71. if msg["command"] == "acknowledge" then --do some pending_queue stuff
  72. --TODO
  73. else --respond with acknowledge
  74. peripheral.call(side,"transmit",replyFreq,freq,textutils.serialize(make_packet(msg["sender_id"],"acknowledge",msg["message_id"])))
  75. end
  76.  
  77. if msg["command"] == "ping" then --respond with connected_to
  78. peripheral.call(side,"transmit",replyFreq,freq,textutils.serialize(make_packet(msg["sender_id"],"connected_to",os.getComputerLabel(),connections,peripherals)))
  79. end
  80. if msg["command"] == "tracert" then --respond with tracert_response
  81. peripheral.call(side,"transmit",replyFreq,freq,textutils.serialize(make_packet(msg["sender_id"],"tracert_response",args[1])))
  82. end
  83. if msg["command"] == "tracert_response" then end --dummy does nothing
  84. if msg["command"] == "exec" then --run script
  85. for i,v in pairs(args) do write(i) write(": ") print(v) end
  86. local func = loadstring(args[1]) --load command string (does not run yet)
  87. local rval
  88. if args[2] == nil then --no response
  89. pcall(func()) --just run command
  90. end
  91. if args[2] == "vals" then --respond with report_return, return vals
  92. rval = {pcall(func)} --run command, save return val(s)
  93. peripheral.call(side,"transmit",replyFreq,freq,textutils.serialize(make_packet(msg["sender_id"],"report_return",msg["message_id"],rval)))
  94. end
  95. if args[2] == "term" then --respond with report_return, terminal output
  96. local ft
  97. if args[3] ~= nil then
  98. ft = faketerm.newFakeTerm(args[3].col,args[3].sx,args[3].sy,args[3].cx,args[3].cy)
  99. else
  100. ft = faketerm.newFakeTerm()
  101. end
  102. local ct = term.current()
  103. term.redirect(faketerm)
  104. success,err = pcall(func)
  105. if not success then print(err) end
  106. rval = faketerm.historyCopy()
  107. faketerm.deleteFakeTerm(ft)
  108. term.redirect(ct)
  109. peripheral.call(side,"transmit",replyFreq,freq,textutils.serialize(make_packet(msg["sender_id"],"report_return",msg["message_id"],rval)))
  110. end
  111. end
  112. if msg["command"] == "report_event" then end --dummy does nothing
  113. if msg["command"] == "report_return" then end --dummy does nothing
  114. if msg["command"] == "replace" then --update file
  115. file = fs.open(args[1],"w") --open file
  116. file.writeLine(args[2]) --write script
  117. file.close() --close file
  118. end
  119. if msg["command"] == "exception" then end --dummy does nothing
  120. if msg["command"] == "routing_reset" then --respond with register
  121. set_upstream(side)
  122. am_i_lost = false
  123. scan_sides()
  124. peripheral.call(side,"transmit",replyFreq,freq,textutils.serialize(make_packet(msg["sender_id"],"register",os.getComputerLabel(),am_i_lost)))
  125. end
  126. if msg["command"] == "register" then end --dummy does nothing
  127. if msg["command"] == "list_connections" then --respond with connected_to
  128. peripheral.call(side,"transmit",replyFreq,freq,textutils.serialize(make_packet(msg["sender_id"],"connected_to",os.getComputerLabel(),connections,peripherals)))
  129. end
  130. if msg["command"] == "connected_to" then end --dummy does nothing
  131. if msg["command"] == "assume_safe_state" then shell.run("safe_state") end
  132. end
  133. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement