Advertisement
vishnukey

Router_master

Nov 30th, 2015
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.22 KB | None | 0 0
  1. comp = {} --table of all known terminals. "name":id
  2.  
  3. rednet.open("back")
  4. rednet.host("BASE", "master")
  5.  
  6. --sends the packets to their correct locations
  7. function route(source, packet)
  8.   if cotains(comp, packet.dest) then
  9.     rednet.send(comp[packet.dest], packet)
  10.   else
  11.     rednet.send(source, "that is not a valid adress")
  12.   end
  13. end
  14.  
  15. --handles what to do with recieved packets
  16. function recieve()
  17.   local id, message = rednet.receive()
  18.  
  19.   if message.message == "connect" then
  20.     update(id, message.id)
  21.     return
  22.   end
  23.  
  24.   if not contains(comp, message.id) then
  25.     udpade(id, message.id)
  26.     route(id, message)
  27.   else
  28.     route(message)
  29.   end
  30. end
  31.  
  32. --updates the list of known systems with a new entry
  33. function update(id, name)
  34.   if not contains(comp, name) then
  35.     comp[name] = id
  36.   end
  37. end
  38.  
  39. --checks if a key is in the specified table
  40. function contains(table, object)
  41.   for i in pairs(table) do
  42.     if i == object then
  43.       return true
  44.     else
  45.       return false
  46.     end
  47.   end
  48. end
  49.  
  50. --main program loop
  51. while true do
  52.   recieve()
  53.  
  54.   --debugging code
  55.   print("recieved")
  56.   for i, v in ipairs(comp) do
  57.     print(i)
  58.     print(v)
  59.   end
  60.   print("end")
  61.   --end debugging code
  62. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement