Advertisement
Doob

file_sender

Jul 31st, 2015
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.61 KB | None | 0 0
  1. local port = 12345
  2. local component = require('component')
  3. local modem = component.modem
  4. local ser = require('serialization')
  5. local endfile = '|||END FILE|||'
  6.  
  7. local function writeFile(modem_type, filename)
  8.   local event = require('event')
  9.   if modem_type == 'modem' then
  10.     modem.open(port)
  11.   end
  12.  
  13.   while true do
  14.     local event, receiver, sender, port, distance, message = event.pull()
  15.  
  16.     if event == 'modem_message' then
  17.       if not filename then
  18.         filename = ser.unserialize(message)[1]
  19.       end
  20.       str = ser.unserialize(message)[2]
  21.  
  22.       if str == endfile then
  23.         break
  24.       else
  25.       print(filename)
  26.         local file = io.open(filename, 'a')
  27.         file:write(str)
  28.         file:close()
  29.       end
  30.     end
  31.   end
  32.  
  33.   if modem_type == 'modem' then
  34.     modem.close(port)
  35.   end
  36.  
  37.   print('File: '..filename..' received')
  38. end
  39.  
  40. local function sendFile(modem_type, filename)
  41.   file = io.open(filename, 'r')
  42.   if not file then
  43.     print('Error: File not exist')
  44.   os.exit()
  45.   else
  46.     sFile = file:read('*a')
  47.     if modem_type == 'modem' then
  48.       if #sFile > modem.maxPacketSize()+#filename then
  49.         for i = 1, ( math.floor(#sFile) / ( (modem.maxPacketSize() - #filename) - 16 ) ) do
  50.           modem.broadcast(port, ser.serialize({filename, string.sub(sFile, i, i+modem.maxPacketSize)}))
  51.         end
  52.         modem.broadcast(port, ser.serialize({filename, endfile}))
  53.       else
  54.         modem.broadcast(port, ser.serialize({filename, sFile}))
  55.         os.sleep(0.1)
  56.         modem.broadcast(port, ser.serialize({filename, endfile}))
  57.       end
  58.     elseif modem_type == 'tunnel' then
  59.       local tunnel = component.tunnel
  60.       if #sFile > tunnel.maxPacketSize()+#filename then
  61.         for i = 1, ( math.floor(#sFile) / ( (tunnel.maxPacketSize() - #filename) - 16 ) ) do
  62.           tunnel.send(ser.serialize({filename, string.sub(sFile, i, i+tunnel.maxPacketSize)}))
  63.         end
  64.         tunnel.send(ser.serialize({filename, endfile}))
  65.       else
  66.         tunnel.send(ser.serialize({filename, sFile}))
  67.         os.sleep(0.1)
  68.         tunnel.send(ser.serialize({filename, endfile}))
  69.       end
  70.     end
  71.   end
  72.   file:close()
  73.   print('File: '..filename..' sended')
  74. end
  75.  
  76. local function checkType()
  77.   print('Enter type of modem (modem/tunnel): ')
  78.   return io.read()
  79. end
  80.  
  81. tArgs = {...}
  82.  
  83. if tArgs[1] == 'send' then
  84.   sendFile(checkType(), tArgs[2])
  85. elseif tArgs[1] == 'receive' then
  86.   writeFile(checkType())
  87. elseif #tArgs == 3 then
  88.   writeFile(checkType(), tArgs[3])
  89. elseif #tArgs > 3 or #tArgs == 0 then
  90.   print('Usage:\nfile_sender <send> <filename>\nfile_sender <receive>')
  91. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement