Skip to content

Instantly share code, notes, and snippets.

@MEXAHOTABOP
Created July 10, 2018 19:40
Show Gist options
  • Save MEXAHOTABOP/f9c325b252f7b97654fdc15163bef5a6 to your computer and use it in GitHub Desktop.
Save MEXAHOTABOP/f9c325b252f7b97654fdc15163bef5a6 to your computer and use it in GitHub Desktop.
io = require("io")
event = require("event")
comp = require("component")
computer = require("computer")
wifi = comp.modem
data = comp.data
filename,port,compress = ...
wifi.open(port)
--TODO заменить все broadcast на будующую функцию для ретрансляции
handle,err = io.open(filename)
if handle == nil then
wifi.broadcast(666,"updateserver: cannot open " .. filename .. " " .. err)
end
if compress == nil or compress == true then
filecontent = data.deflate(handle:read("*a"))
else
filecontent = handle:read("*a")
end
maxsize = wifi.maxPacketSize()
num = math.ceil(filecontent:len() / maxsize)
timeout = computer.uptime() + 10
while true do
_,_,_,_,_,payload = event.pull(1,"modem_message")
if payload == "gotit" then break end
if computer.uptime() >= timeout then --если не получаем ответа то завершаем программу
wifi.broadcast(666,"updateserver: wait on port " .. port .. " timedout")
os.exit()
end
end
for i=1,num do
wifi.broadcast(port,filecontent:sub(maxsize * (i-1), maxsize * i))
end
wifi.broadcast(port,"end")
wifi.broadcast(666,"updateserver: file sended",false,port)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment