Last active
July 10, 2018 19:39
-
-
Save MEXAHOTABOP/b7a7a2116eb4499498c889be2fe734e6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| os = require("os") | |
| comp = require("component") | |
| event = require("event") | |
| io = require("io") | |
| computer = require("computer") | |
| wifi = comp.modem | |
| if comp.isAvailable("data") then | |
| data = comp.data | |
| compress = true | |
| else | |
| data = {} | |
| data.md5 = function(...) return 0 end --all checks broken and every time download | |
| compress=false | |
| end | |
| program = "/root/sendfile.lua" | |
| if comp.isAvailable("robot") then | |
| name = require("robot").name() | |
| else | |
| name = computer.address() | |
| end | |
| --TODO заменить все broadcast на будующую функцию для ретрансляции | |
| function update() | |
| handle,err = io.open(program,"r") | |
| if handle == nil then | |
| wifi.broadcast(666,name .. ": error open " .. program .. " with error " .. err) | |
| return | |
| end | |
| content,err=handle:read("*a") | |
| if content == nil then | |
| wifi.broadcast(666,name .. ": error read " .. program .. " with error " ..err) | |
| end | |
| md5 = data.md5(handle:read("*a")) | |
| handle:close() | |
| wifi.open(667) | |
| wifi.broadcast(667,"check",computer.address(),program, md5) | |
| timeout = computer.uptime() + 5 --5 сек | |
| updatePort=nil | |
| md5 = nil | |
| while true do | |
| _,_,_,_,_,toAddress,updatePort,md5 = event.pull(1,"modem_message") --если требуется обновление получаем порт сервера обновлений | |
| if toAddress == computer.address() and updatePort ~= nil and md5 ~= nil then break end | |
| if computer.uptime() >= timeout then --если ни кто не отвечает или ответ не валидный то делать больше нечего | |
| wifi.broadcast(666,name .. ": port wait timeout") | |
| return | |
| end | |
| end | |
| wifi.close(667) | |
| wifi.open(tonumber(updatePort)) --порт выбраный сервером из диапозона 15000 .. 15100 просто для дебагинга | |
| wifi.broadcast(updatePort,"gotit") | |
| wifi.broadcast(666,name .. ": update port " .. updatePort .. " to " .. md5, true , updatePort) | |
| handle,err = io.open(program,"w") | |
| if handle == nil then | |
| wifi.broadcast(666,name .. ": error open " .. program .. " with error " .. err) | |
| return | |
| end | |
| timeout = computer.uptime() + 20 --20 сек | |
| filestr = "" | |
| while true do | |
| _,_,_,port,_,payload = event.pull(1,"modem_message") | |
| if port == updatePort then --дополнительная проверка на случай если с 667 пришли ещё пакеты перед закрытием | |
| if payload == "end" then | |
| if compress then filestr = data.inflate(filestr) end | |
| if data.md5(filestr) == md5 or compress == false then --dont check if no data card | |
| res,err = handle:write(filestr) | |
| handle:close() | |
| if res == nil then | |
| wifi.broadcast(666,name .. ": error write " .. program .. " with error " .. err,false, updatePort) | |
| return | |
| end | |
| wifi.broadcast(666,name .. ": update complite",false, updatePort) | |
| else | |
| wifi.broadcast(666,name .. ": update failed expected " .. md5 .. " got " .. data.md5(filestr),false, updatePort) | |
| end | |
| return | |
| end | |
| filestr = filestr .. payload | |
| timeout = timeout + 2 -- добавляем пару секунд за каждый полученый пакет на случай если пакет проходит много ретрансляторов | |
| end | |
| if computer.uptime() >= timeout then --если сервер не отвечает то отменяем обновление | |
| wifi.broadcast(666,name .. ": update wait timeout",false,updatePort) | |
| return | |
| end | |
| end | |
| end | |
| --while true do | |
| update() | |
| -- dofile(program) | |
| --end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment