Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -------------------------------------------------------------------------
  2. -- Receives messages from around the network and routes to destination
  3. -------------------------------------------------------------------------
  4.  
  5. -- Define routing table
  6. tRoute =
  7. {
  8. {dest="CMD", address="643bec2f-3776-4de0 895e-43a40a677a36", via="MODEM", desc="Command Line Server"},
  9. {dest="VOM", address="9a405bde-647a-4593-acb5-9a5d8251f8e1", via="TUNNEL", desc="Void Ore Miner"},
  10. {dest="VRM", address="9a405bde-647a-4593-acb5-9a5d8251f8e1", via="TUNNEL", desc="Void Resource Miner"}
  11. }
  12.  
  13. -- Constants
  14. ciPort=1001 -- Port used globally on my network
  15.  
  16. -- Open modem and tunnel
  17. local oComponent = require("component")
  18. local oEvent = require("event")
  19. local oModem = oComponent.modem
  20. local oTunnel = oComponent.tunnel
  21. local oTerm = require("term")
  22. local oSerialization = require("serialization")
  23.  
  24. oModem.open(ciPort)
  25. -- Do not have to open linked card - always open
  26.  
  27. if not oModem.isOpen(ciPort) then
  28. print("Modem failed to open on port " .. ciPort .. ". Exiting...")
  29. os.exit()
  30. end
  31.  
  32. -------------------------------------------------------------------------
  33. -- Functions
  34.  
  35. function FindDest(psDest, ptRoute)
  36. local lsAddress = ""
  37. local lsVia = ""
  38. local lsDesc = ""
  39.  
  40. -- Check the routing table and find the details of the destination
  41. for i, row in ipairs(ptRoute) do
  42. if row.dest == psDest then
  43. lsAddress = row.address
  44. lsVia = row.via
  45. lsDesc = row.desc
  46. end
  47. end
  48.  
  49. -- Check for dest unknown
  50. if lsAddress == "" then
  51. lsDesc = "Unknown Address"
  52. end
  53.  
  54. -- Return values to calling function
  55. return lsAddress, lsVia, lsDesc
  56. end
  57.  
  58. function ParseMsg(psMsgRaw)
  59. local loS = require("serialization")
  60.  
  61. -- Parse the raw message into a destination and a command
  62.  
  63. local ltMsg = loS.unserialize(tostring(psMsgRaw))
  64. local lsDest = ltMsg.dest
  65. local lsCmd = ltMsg.cmd
  66.  
  67. -- Return values to calling function
  68. return lsDest, lsCmd
  69. end
  70.  
  71. -------------------------------------------------------------------------
  72. -- Main Program
  73.  
  74. local fContinue = true
  75. local fLog = true
  76. local lsDest = ""
  77. local lsMsg = ""
  78.  
  79. -- Clear the screen
  80. oTerm.clear()
  81. print("Initialising message routing...")
  82.  
  83. -- Enter Loop
  84. while fContinue do
  85. -- Wait for an inbound message
  86. _, _, _, _, _, sMsgRaw = oEvent.pull("modem_message")
  87.  
  88. -- Parse message
  89. lsDest, lsCmd = ParseMsg(sMsgRaw)
  90.  
  91. -- Check for debug/local command
  92. if lsDest == "LCL" then
  93. -- Check to log
  94. if fLog then print("CMD " .. sMsgRaw) end
  95. -- Check for program quit
  96. if lsCmd == "EXIT" then fContinue = false end
  97. if lsCmd == "LOG" then
  98. fLog = true
  99. print("CMD Enable Logging")
  100. end
  101. if lsCmd == "NOLOG" then
  102. fLog = false
  103. print("CMD Disable Logging")
  104. end
  105. if lsCmd == "LIST" then
  106. lsCmd = oSerialization.serialize(tRoute)
  107. lsDest = "CMD"
  108. end
  109. end
  110. -- Message to process/send
  111. if lsDest ~= "LCL" or (lsDest == "LCL" and lsCmd == "LIST") then
  112. -- Get the destination etc
  113. lsAddress, lsVia, lsDesc = FindDest(lsDest, tRoute)
  114.  
  115. -- Check to log
  116. -- if fLog then print("Sending command " .. lsCmd .. " via " .. lsVia .. " to " .. lsDest .. ":" .. lsDesc) end
  117.  
  118. -- Check for unknown destination
  119. if lsDesc == "Unknown Address" then
  120. -- Print the details
  121. print("Unknown Address")
  122. -- Send a message to the control computer
  123.  
  124. else
  125. -- Check path
  126. if lsVia == "MODEM" then -- Route via modem
  127. oModem.send(lsAddress, ciPort, lsCmd)
  128. else -- Route via linked card
  129. oTunnel.send(lsCmd)
  130. end
  131. end
  132. end -- If local/forward
  133.  
  134. -- Yield for a little bit
  135. os.sleep(0.1)
  136. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement