Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local port = 12345
- local component = require('component')
- local modem = component.modem
- local ser = require('serialization')
- local endfile = '|||END FILE|||'
- local function writeFile(modem_type, filename)
- local event = require('event')
- if modem_type == 'modem' then
- modem.open(port)
- end
- while true do
- local event, receiver, sender, port, distance, message = event.pull()
- if event == 'modem_message' then
- if not filename then
- filename = ser.unserialize(message)[1]
- end
- str = ser.unserialize(message)[2]
- if str == endfile then
- break
- else
- print(filename)
- local file = io.open(filename, 'a')
- file:write(str)
- file:close()
- end
- end
- end
- if modem_type == 'modem' then
- modem.close(port)
- end
- print('File: '..filename..' received')
- end
- local function sendFile(modem_type, filename)
- file = io.open(filename, 'r')
- if not file then
- print('Error: File not exist')
- os.exit()
- else
- sFile = file:read('*a')
- if modem_type == 'modem' then
- if #sFile > modem.maxPacketSize()+#filename then
- for i = 1, ( math.floor(#sFile) / ( (modem.maxPacketSize() - #filename) - 16 ) ) do
- modem.broadcast(port, ser.serialize({filename, string.sub(sFile, i, i+modem.maxPacketSize)}))
- end
- modem.broadcast(port, ser.serialize({filename, endfile}))
- else
- modem.broadcast(port, ser.serialize({filename, sFile}))
- os.sleep(0.1)
- modem.broadcast(port, ser.serialize({filename, endfile}))
- end
- elseif modem_type == 'tunnel' then
- local tunnel = component.tunnel
- if #sFile > tunnel.maxPacketSize()+#filename then
- for i = 1, ( math.floor(#sFile) / ( (tunnel.maxPacketSize() - #filename) - 16 ) ) do
- tunnel.send(ser.serialize({filename, string.sub(sFile, i, i+tunnel.maxPacketSize)}))
- end
- tunnel.send(ser.serialize({filename, endfile}))
- else
- tunnel.send(ser.serialize({filename, sFile}))
- os.sleep(0.1)
- tunnel.send(ser.serialize({filename, endfile}))
- end
- end
- end
- file:close()
- print('File: '..filename..' sended')
- end
- local function checkType()
- print('Enter type of modem (modem/tunnel): ')
- return io.read()
- end
- tArgs = {...}
- if tArgs[1] == 'send' then
- sendFile(checkType(), tArgs[2])
- elseif tArgs[1] == 'receive' then
- writeFile(checkType())
- elseif #tArgs == 3 then
- writeFile(checkType(), tArgs[3])
- elseif #tArgs > 3 or #tArgs == 0 then
- print('Usage:\nfile_sender <send> <filename>\nfile_sender <receive>')
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement