G2A Many GEOs
SHARE
TWEET

atupd

kssr3951 Dec 23rd, 2015 (edited) 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- ============================================
  2. -- == Utilities
  3. -- ============================================
  4. local function fileReadAll(filePath)
  5.   local hFile = fs.open(filePath, "r")
  6.   if nil == hFile then
  7.     return nill
  8.   end
  9.   local txt = hFile.readAll()
  10.   hFile.close()
  11.   return txt
  12. end
  13. local function fileOverwrite(filePath, text)
  14.   local hFile = fs.open(filePath, "w")
  15.   hFile.writeLine(text)
  16.   hFile.close()
  17. end
  18. local function saveFile(filePath, data)
  19.   fileOverwrite(filePath, textutils.serialize(data))
  20. end
  21. local function loadFile(filePath)
  22.   local dat = fileReadAll(filePath)
  23.   if nil == dat then
  24.     return nil
  25.   end
  26.   return textutils.unserialize(dat)
  27. end
  28. local function deleteWhenExists(filePath)
  29.   if fs.exists(filePath) then
  30.     fs.delete(filePath)
  31.   end
  32. end
  33. local function startsWith(subject, keyword)
  34.   if nil == subject then
  35.     return false
  36.   end
  37.   if nil == keyword then
  38.     return false
  39.   end
  40.   if string.len(subject) < string.len(keyword) then
  41.     return false
  42.   end
  43.   if string.sub(subject, 1, string.len(keyword)) == keyword then
  44.     return true
  45.   else
  46.     return false
  47.   end
  48. end
  49. -- ============================================
  50. -- == Configuration
  51. -- ============================================
  52. local APP_FILE_NAME  = "/atupd"
  53. local THE_CHANNEL    = 3951
  54. local MSG_SIGNATURE  = "kssr3951's network system"
  55. local BASE_DIRECTORY = "/network"
  56. local STATUS_DIRECTORY = BASE_DIRECTORY .. "/status"
  57. -- ============================================
  58. -- == Application Common
  59. -- ============================================
  60. local MSGTYPE_SYS_UPDATE = "MSGTYPE_SYS_UPDATE"
  61. local function MsgSysUpdateNew()
  62.   return {
  63.       senderId,
  64.       type = MSGTYPE_SYS_UPDATE,
  65.       timestamp,
  66.       appFileName,
  67.       source  
  68.     }
  69. end
  70. local function serializeMessage(data)
  71.   data.senderId = os.getComputerID()
  72.   return MSG_SIGNATURE .. textutils.serialize(data)
  73. end
  74. local function unserializeMessage(data)
  75.   if false == startsWith(data, MSG_SIGNATURE) then
  76.     return nil
  77.   else
  78.     return textutils.unserialize(string.sub(data, string.len(MSG_SIGNATURE) + 1))
  79.   end
  80. end
  81. -- ============================================
  82. -- == Application
  83. -- ============================================
  84. local SIDES = {"top", "bottom", "left", "right", "front", "back"}
  85. local modems = { }
  86. local function receiveSysUpdate(msg)
  87.   local message = unserializeMessage(msg)
  88.   if nil == message then
  89.     return
  90.   end
  91.   if nil ~= message and MSGTYPE_SYS_UPDATE == message.type then
  92.     print("sysUpdate")
  93.     local timestamp = loadFile(STATUS_DIRECTORY .. "/systemUpdateDate")
  94.     print("system update required. timestamp=[" .. message.timestamp .. "]")
  95.     if nil ~= timestamp and timestamp == message.timestamp then
  96.       print("  already latest. (no need to update.)")
  97.     else
  98.       print("  need to be updated.")
  99.       saveFile(STATUS_DIRECTORY .. "/updateMessage", message)
  100.       deleteWhenExists(message.appFileName)
  101.       fileOverwrite(message.appFileName, message.source)
  102.       fileOverwrite("/startup", "shell.run(\"" .. message.appFileName .. "\")")
  103.       saveFile(STATUS_DIRECTORY .. "/updateNetSlave", true)
  104.       os.reboot()
  105.     end
  106.   end
  107. end
  108. local function modemAction(modemSide, senderChannel, replyChannel, msg, senderDistance)
  109.   -- print("== =========================================")
  110.   -- print("I just received a message from: "..senderChannel)
  111.   -- print("I should apparently reply on: "..replyChannel)
  112.   -- print("The modem receiving this is located on the "..modemSide.." side")
  113.   -- print("The message was: "..msg)
  114.   -- print("The sender is: "..senderDistance.." blocks away from me.")
  115.   -- print("== =========================================")
  116.   if true then
  117.     receiveSysUpdate(msg)
  118.   end
  119.   print("waiting.")
  120. end
  121. -- ============================================
  122. -- == main
  123. -- ============================================
  124. fs.makeDir(STATUS_DIRECTORY)
  125. local args = { ... }
  126. if true then
  127.   local mdmCnt = 0
  128.   for _, side in ipairs(SIDES) do
  129.     if "modem" == peripheral.getType(side) then
  130.       modems[side] = peripheral.wrap(side)
  131.       modems[side].open(THE_CHANNEL)
  132.       mdmCnt = mdmCnt + 1
  133.     end
  134.   end
  135.   print("modemCount = " .. tostring(mdmCnt))
  136. end
  137. if 1 <= #args then
  138.   -- ----------------------------------------
  139.   -- net update (root)
  140.   -- ----------------------------------------
  141.   if "appUpdate" == args[1] then
  142.     local message = MsgSysUpdateNew()
  143.     message.timestamp   = os.day() .. "-" .. os.time()
  144.     message.appFileName = APP_FILE_NAME
  145.     message.source      = fileReadAll(APP_FILE_NAME)
  146.     saveFile(STATUS_DIRECTORY .. "/systemUpdateDate", message.timestamp)
  147.     for _, mdm in pairs(modems) do
  148.       print("transmit begin")
  149.       mdm.transmit(THE_CHANNEL, 10, serializeMessage(message))
  150.       print("    transmit end")
  151.     end
  152.   end
  153. end
  154. if true then
  155.   -- ----------------------------------------
  156.   -- net update (not root)
  157.   -- ----------------------------------------
  158.   local tmp = loadFile(STATUS_DIRECTORY .. "/updateNetSlave")
  159.   deleteWhenExists(STATUS_DIRECTORY .. "/updateNetSlave")
  160.   if nil ~= tmp and true == tmp then
  161.     local message = loadFile(STATUS_DIRECTORY .. "/updateMessage")
  162.     for _, mdm in pairs(modems) do
  163.       mdm.transmit(THE_CHANNEL, 10, serializeMessage(message))
  164.     end
  165.     saveFile(STATUS_DIRECTORY .. "/systemUpdateDate", message.timestamp)
  166.   end
  167. end
  168. local timestamp = loadFile(STATUS_DIRECTORY .. "/systemUpdateDate")
  169. print("== =========================================")
  170. print("== kssr3951's network component.")
  171. print("== last update : " .. tostring(timestamp))
  172. print("== computer ID : " .. os.getComputerID())
  173. print("== =========================================")
  174. print("waiting.")
  175. while true do
  176.   local event, r1, r2, r3, r4, r5 = os.pullEvent()
  177.   if "modem_message" == event then
  178.     print("modem_message!!")
  179.     modemAction(r1, r2, r3, r4, r5)
  180.   end
  181. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top