Advertisement
King0fGamesYami

iNet

Aug 19th, 2014
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.14 KB | None | 0 0
  1. --iNet 1.2
  2. local b='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  3. local net = {
  4.   send = function( self, message )
  5.     if type( self ) ~= "table" then
  6.       error( 'Incorrect notation, use ":" instead of "."', 2 )
  7.     end
  8.     local packet = { protocol = self.protocol, sender = os.getComputerLabel() or os.getComputerID() }
  9.     if self.encrypt then
  10.       packet.message = self.encrypt( textutils.serialize( message ), self )
  11.       packet.encrypt = true
  12.     else
  13.       packet.message = message
  14.     end
  15.     self.modem.transmit( self.channel, self.replyChannel, self.b64enc( textutils.serialize( packet ) ) )
  16.   end,
  17.   reply = function( self, message )
  18.     if type( self ) ~= "table" then
  19.       error( 'Incorrect notation, use ":" instead of "."', 2 )
  20.     end
  21.     local packet = { protocol = self.protocol, sender = os.getComputerLabel() or os.getComputerID() }
  22.     if self.encrypt then
  23.       packet.message = self.encrypt( textutils.serialize( message ), self )
  24.       packet.encrypt = true
  25.     else
  26.       packet.message = message
  27.     end
  28.     self.modem.transmit( self.last, self.replyChannel, self.b64enc( textutils.serialize( packet ) ) )
  29.   end,
  30.   receive = function( self )
  31.     if type( self ) ~= "table" then
  32.       error( 'Incorrect notation, use ":" instead of "."', 2 )
  33.     end
  34.     while true do
  35.       local event, side, channel, replyChannel, message, distance = os.pullEvent( "modem_message" )
  36.       local info = textutils.unserialize( self.b64dec( message ) )
  37.       if channel == self.replyChannel and self.protocol == info.protocol then
  38.         self.last = replyChannel
  39.         if info.encrypt and self.decrypt then
  40.           if info.fileName then
  41.             local file = fs.open( info.fileName, "w" )
  42.             file.write( self.decrypt( info.message, self ) )
  43.             file.close()
  44.             return info.fileName, distance, info.sender
  45.           else
  46.             return self.decrypt( info.message, self ), distance, info.sender
  47.           end
  48.         elseif info.encrypt then
  49.           error( "Message could not be decrypted", 2 )
  50.         else
  51.           if info.fileName then
  52.             local file = fs.open( info.fileName, "w" )
  53.             file.write( info.message )
  54.             file.close()
  55.             return info.fileName, distance, info.sender
  56.           else
  57.            return info.message, distance, info.sender
  58.           end
  59.         end
  60.       end
  61.     end
  62.   end,
  63.   ["repeat"] = function( self )
  64.     if type( self ) ~= "table" then
  65.       error( 'Incorrect notation, use ":", instead of "."', 2 )
  66.     end
  67.     while true do
  68.       local event, side, channel, replyChannel, message, distance = os.pullEvent( "modem_message" )
  69.       local info = textutils.unserialize( self.b64dec( message ) )
  70.       if not info["repeat"] then
  71.         info["repeat"] = { os.getComputerID() }
  72.       elseif not info["repeat"][ os.getComputerID() ] then
  73.         info["repeat"][ os.getComputerID() ] = true
  74.         self.modem.transmit( channel, replyChannel, self.b64enc( textutils.serialize( info ) ) )
  75.       end
  76.     end
  77.   end,
  78.   setProtocol = function( self, protocol )
  79.     if type( self ) ~= "table" then
  80.       error( 'Incorrect notation, use ":", instead of "."', 2 )
  81.     elseif type( protocol ) ~= "string" then
  82.       error( 'Expected string, got ' .. type( protocol ), 2 )
  83.     end
  84.     self.protocol = protocol
  85.   end,
  86.   setEncryption = function( self, en )
  87.     if type( self ) ~= "table" then
  88.       error( 'Incorrect notation, use ":", instead of "."', 2 )
  89.     elseif type( en ) ~= "function" then
  90.       error( 'Expected function, got ' .. type( en ), 2 )
  91.     end
  92.     self.encrypt = en
  93.   end,
  94.   setDecryption = function( self, de )
  95.     if type( self ) ~= "table" then
  96.       error( 'Incorrect notation, use ":", instead of "."', 2 )
  97.     elseif type( de ) ~= "function" then
  98.       error( 'Expected function, got ' .. type( de ), 2 )
  99.     end
  100.     self.decrypt = de
  101.   end,
  102.   removeEncryption = function( self )
  103.     self.encrypt = nil
  104.   end,
  105.   removeDecryption = function( self )
  106.     self.decrypt = nil
  107.   end,
  108.   sendFile = function( self, fileName )
  109.     if type( self ) ~= "table" then
  110.       error( 'Incorrect notation, use ":" instead of "."', 2 )
  111.     end
  112.     local file = fs.open( fileName, "r" )
  113.     local message = file.readAll()
  114.     file.close()
  115.     local packet = { protocol = self.protocol, sender = os.getComputerLabel() or os.getComputerID(), fileName = fileName }
  116.     if self.encrypt then
  117.       packet.message = self.encrypt( textutils.serialize( message ), self )
  118.       packet.encrypt = true
  119.     else
  120.       packet.message = message
  121.     end
  122.     self.modem.transmit( self.channel, self.replyChannel, self.b64enc( textutils.serialize( packet ) ) )
  123.   end,
  124.   b64enc = function(data)
  125.     return ((data:gsub('.', function(x)
  126.         local r,b='',x:byte()
  127.         for i=8,1,-1 do r=r..(b%2^i-b%2^(i-1)>0 and '1' or '0') end
  128.         return r;
  129.     end)..'0000'):gsub('%d%d%d?%d?%d?%d?', function(x)
  130.         if (#x < 6) then return '' end
  131.         local c=0
  132.         for i=1,6 do c=c+(x:sub(i,i)=='1' and 2^(6-i) or 0) end
  133.         return b:sub(c+1,c+1)
  134.     end)..({ '', '==', '=' })[#data%3+1])
  135.   end,
  136.   b64dec = function(data)
  137.     data = string.gsub(data, '[^'..b..'=]', '')
  138.     return (data:gsub('.', function(x)
  139.         if (x == '=') then return '' end
  140.         local r,f='',(b:find(x)-1)
  141.         for i=6,1,-1 do r=r..(f%2^i-f%2^(i-1)>0 and '1' or '0') end
  142.         return r;
  143.     end):gsub('%d%d%d?%d?%d?%d?%d?%d?', function(x)
  144.         if (#x ~= 8) then return '' end
  145.         local c=0
  146.         for i=1,8 do c=c+(x:sub(i,i)=='1' and 2^(8-i) or 0) end
  147.         return string.char(c)
  148.     end))
  149.   end,
  150. }
  151.  
  152. function open( channel, replyChannel, side )
  153.   if type( side ) ~= "string" then
  154.     error( "Expected number, number, string, got " .. type( channel ) .. ", " .. type( replyChannel ) .. ", " .. type( side ), 2 )
  155.   if not peripheral.isPresent( side ) or peripheral.getType( side ) ~= "modem" then
  156.     error( "No modem connected on " .. side, 2 )
  157.   end
  158.   if type( channel ) ~= "number" or type( replyChannel ) ~= "number" then
  159.     error( "Expected number, number, side, got " .. type( channel ) .. ", " .. type( replyChannel ) .. ", " .. side, 2 )
  160.   end
  161.   local modem  = peripheral.wrap( side )
  162.   if not modem.isOpen( replyChannel ) then
  163.     modem.open( replyChannel )
  164.   end
  165.   if channel == replyChannel then
  166.     error( "channel and replyChannel cannot be the same value", 2 )
  167.   end
  168.   local t = { modem = modem, channel = channel, replyChannel = replyChannel }
  169.   setmetatable( t, { __index = net } )
  170.   return t
  171. end
  172.  
  173. function waitForAny( ... )
  174.   local get = {}
  175.   local value
  176.   for k, v in ipairs( { ... } ) do
  177.     get[ k ] = function() value = { v:receive(), k } end
  178.   end
  179.   parallel.waitForAny( unpack( get ) )
  180.   return unpack( value )
  181. end
  182.  
  183. function waitForAll( ... )
  184.   local get = {}
  185.   local value = {}
  186.   for k, v in ipairs( { ... } ) do
  187.     get[ k ] = function() value[ k ] = { v:receive() } end
  188.   end
  189.   parallel.waitForAll( unpack( get ) )
  190.   local toReturn = {}
  191.   for k, v in ipairs( value ) do
  192.     for x, y in ipairs( v ) do
  193.       toReturn[ #toReturn + 1 ] = y
  194.     end
  195.   end
  196.   return unpack( toReturn )
  197. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement