Kouksi44

filenet - send files over rednet (computercraft)

Aug 13th, 2014
850
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.55 KB | None | 0 0
  1. -- API to send/receive files via rednet;
  2. -- version 1.1 (updated)
  3.  
  4.  
  5. --GLOBAL VARIABLES--
  6.  
  7. version="1.1"
  8.  
  9. restrictedIds={}
  10.  
  11. --GLOBAL VARIABLES--
  12.  
  13.  
  14.  
  15.  
  16.  
  17. -------------------------------------------------------UTILS START-------------------------------------------------------
  18. local function contains(table, element)
  19.   if type(table)~="table" then error("Expected table,element,got "..type(table)..","..type(element),0)
  20. else
  21.   for _, value in pairs(table) do
  22.     if value == element then
  23.       return true
  24.     end
  25.   end
  26.   return false
  27. end
  28. end
  29.  
  30. local function getKey(table,element)
  31. if type(table)~="table" then error("Expected table,element,got "..type(table)..","..type(element),0)
  32. else
  33. for key, value in pairs(table) do
  34.     if value == element then
  35.       return key
  36.     end
  37.   end
  38.   return
  39.   end
  40. end
  41.  
  42.  
  43. function openSide()                                                                                                                                            
  44.         for _,side in ipairs({"top", "bottom", "front", "left", "right", "back"}) do
  45.                 if peripheral.isPresent(side)
  46.                         then
  47.                                 if peripheral.getType(side)=="modem"
  48.                                         then
  49.                                                 modemside=side
  50.                                                 rednet.open(modemside)
  51.                                 end
  52.                 end
  53.         end
  54. end
  55.  
  56.  
  57.  
  58. function getVersion()
  59. return version
  60. end
  61.  
  62. function blacklist(id)
  63. if type(id) ~="number" then
  64. error("Expected number,got "..type(id),0)
  65. else
  66. if not contains(restrictedIds,id) then
  67. table.insert(restrictedIds,id)
  68. else return
  69. end
  70. end
  71. end
  72.  
  73. function removeBlacklist(id)
  74. if type(id) ~="number" then
  75. error("Expected number,got "..type(id),0)
  76. else
  77. table.remove(restrictedIds,getKey(restrictedIds,id))
  78. end
  79. end
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88. -------------------------------------------------------REDWORKS----------------------------------------------------------
  89.  
  90. local function timedReceive(path,timeout,ovr)
  91.                        
  92.                         senderID,message,protocol=rednet.receive("FileTransfer",timeout)
  93.                        
  94.                
  95.                        
  96.                         if message ~=nil then
  97.                              
  98.                                 if not ovr and fs.exists(path) then
  99.                                         error("File already exists.",0)
  100.                                 elseif ovr or not fs.exists(path) then
  101.                                      
  102.                                         local l=fs.open(path,"w")
  103.                                         l.write(message)
  104.                                         l.close()
  105.                                         return senderID
  106.                                 end
  107.                         else
  108.                          
  109.                         return
  110.                         end
  111.                
  112. end
  113.  
  114. local function INTreceiveFile(path,ovr)
  115.                         event,senderID,message,protocol=os.pullEvent("rednet_message")  
  116.                        
  117.                         if not ovr and fs.exists(path) then
  118.                         error("File already exists.",0)
  119.                         else
  120.                         local l=fs.open(path,"w")
  121.                         print(message)
  122.                         l.write(message)
  123.                         l.close()
  124.                         return senderID
  125.                         end
  126. end
  127.  
  128.  
  129. local oldPull=os.pullEventRaw
  130.  
  131.  
  132. function os.pullEventRaw( sFilter )
  133.   while true do
  134.         local event = { oldPull() }
  135.         if coFoo~=nil then
  136.           if coroutine.status( coFoo ) == "suspended"  then
  137.            if event[1]=="rednet_message" and not contains(restrictedIds,event[2]) and event[4]=="FileTransfer" then
  138.                         coroutine.resume( coFoo, unpack( event ) )
  139.             end
  140.           end
  141.         elseif TimedcoFoo~=nil then
  142.           if coroutine.status(TimedcoFoo) =="suspended" then
  143.             if event[1]=="rednet_message" and not contains(restrictedIds,event[2]) and event[4]=="FileTransfer" then
  144.                         coroutine.resume( TimedcoFoo, unpack( event ) )
  145.             end
  146.           end
  147.         end
  148.         if sFilter == event[ 1 ] or not sFilter and not contains(restrictedIds,event[2]) and event[4]=="FileTransfer" then
  149.           return unpack( event )
  150.  
  151.         end
  152.         if sFilter == event[ 1 ] or not sFilter then
  153.           return unpack( event )
  154.         end
  155.   end
  156. end
  157.  
  158.  
  159.  
  160. function sendFile(path,receiverID)
  161.  
  162.         if fs.exists(path) and type(receiverID)=="number"                                              
  163.                 then
  164.                          g=fs.open(path,"r")
  165.                          file=g.readAll()
  166.                          g.close()
  167.                          rednet.send(receiverID , file,"FileTransfer" )
  168.                
  169.                 else error("Expected valid path,number got invalid path or invalid number",0)
  170.  
  171.         end    
  172.  
  173. end
  174.  
  175. function broadcastFile(path)
  176. if fs.exists(path)                                            
  177.                 then
  178.                          g=fs.open(path,"r")
  179.                          file=g.readAll()
  180.                          g.close()
  181.                          rednet.broadcast(file,"FileTransfer" )
  182.                
  183.                 else error("Expected valid path",0)
  184.  
  185.         end    
  186. end
  187.  
  188. function ASyncReceiveFile(path,timeout,override)
  189. if type(timeout)=="boolean" and override==nil then coFoo=coroutine.create(INTreceiveFile) coroutine.resume(coFoo,path,override)
  190.        
  191. elseif type(timeout)~="boolean" and type(timeout)~="number" then error("Expected valid path,number,boolean,got path, "..type(timeout)..","..type(override),0)
  192. elseif override~=nil and type(override)~="boolean" then error("Expected valid path,number,boolean,got path, "..type(timeout)..","..type(override),0)
  193. else TimedcoFoo=coroutine.create(timedReceive) coroutine.resume(TimedcoFoo,path,timeout,override)
  194. end
  195. end
  196.  
  197. function receiveFile(path,timeout,override)
  198. if type(timeout)=="boolean" and override==nil then INTreceiveFile(path,timeout)
  199.        
  200. elseif type(timeout)~="boolean" and type(timeout)~="number" then error("Expected valid path,number,boolean,got paaaaath, "..type(timeout)..","..type(override),0)
  201. elseif override~=nil and type(override)~="boolean" then error("Expected valid path,number,boolean,got path, "..type(timeout)..","..type(override),0)
  202. else timedReceive(path,timeout,override)
  203. end
  204. end
Advertisement
Add Comment
Please, Sign In to add comment