Advertisement
King0fGamesYami

iNet

Aug 7th, 2014
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.58 KB | None | 0 0
  1. --iNet 1.0
  2. local net = {
  3.   send = function( self, message )
  4.     if type( self ) ~= "table" then
  5.       error( 'Incorrect notation, use ":" instead of "."', 2 )
  6.     end
  7.     local packet = { proctal = self.proctal, sender = os.getComputerLabel() or os.getComputerID() }
  8.     if self.encrypt then
  9.       packet.message = self.encrypt( textutils.serialize( message ) )
  10.       packet.encrypt = true
  11.     else
  12.       packet.message = message
  13.     end
  14.     self.modem.transmit( self.channel, self.replyChannel, textutils.serialize( packet ) )
  15.   end,
  16.   reply = function( self, message )
  17.     if type( self ) ~= "table" then
  18.       error( 'Incorrect notation, use ":" instead of "."', 2 )
  19.     end
  20.     local packet = { proctal = self.proctal, sender = os.getComputerLabel() or os.getComputerID() }
  21.     if self.encrypt then
  22.       packet.message = self.encrypt( textutils.serialize( message ) )
  23.       packet.encrypt = true
  24.     else
  25.       packet.message = message
  26.     end
  27.     self.modem.transmit( self.last, self.replyChannel, textutils.serialize( packet ) )
  28.   end,
  29.   receive = function( self )
  30.     if type( self ) ~= "table" then
  31.       error( 'Incorrect notation, use ":" instead of "."', 2 )
  32.     end
  33.     while true do
  34.       local event, side, channel, replyChannel, message, distance = os.pullEvent( "modem_message" )
  35.       local info = textutils.unserialize( message )
  36.       local _proctal = not self.proctal or self.proctal == info.proctal
  37.       if channel == self.replyChannel and _proctal 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 ) )
  43.             file.close()
  44.             return info.fileName, distance, info.sender
  45.           else
  46.             return self.decrypt( info.message ), 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( 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.         modem.transmit( channel, replyChannel, textutils.serialize( info ) )
  75.       end
  76.     end
  77.   end,
  78.   setProctal = function( self, proctal )
  79.     if type( self ) ~= "table" then
  80.       error( 'Incorrect notation, use ":", instead of "."', 2 )
  81.     elseif type( proctal ) ~= "string" then
  82.       error( 'Expected string, got ' .. type( proctal ), 2 )
  83.     end
  84.     self.proctal = proctal
  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 = { proctal = self.proctal, sender = os.getComputerLabel() or os.getComputerID(), fileName = fileName }
  116.     if self.encrypt then
  117.       packet.message = self.encrypt( textutils.serialize( message ) )
  118.       packet.encrypt = true
  119.     else
  120.       packet.message = message
  121.     end
  122.     self.modem.transmit( self.channel, self.replyChannel, textutils.serialize( packet ) )
  123.   end,
  124. }
  125.  
  126. function open( channel, replyChannel, side )
  127.   if not peripheral.isPresent( side ) or peripheral.getType( side ) ~= "modem" then
  128.     error( "No modem connected on " .. side, 2 )
  129.   end
  130.   local modem  = peripheral.wrap( side )
  131.   if not modem.isOpen( replyChannel ) then
  132.     modem.open( replyChannel )
  133.   end
  134.   local t = { modem = modem, channel = channel, replyChannel = replyChannel }
  135.   setmetatable( t, { __index = net } )
  136.   return t
  137. end
  138.  
  139. function waitForAny( ... )
  140.   local get = {}
  141.   local value
  142.   for k, v in ipairs( { ... } ) do
  143.     get[ k ] = function() value = { v:receive() } end
  144.   end
  145.   parallel.waitForAny( unpack( get ) )
  146.   return unpack( value )
  147. end
  148.  
  149. function waitForAll( ... )
  150.   local get = {}
  151.   local value = {}
  152.   for k, v in ipairs( { ... } ) do
  153.     get[ k ] = function() value[ k ] = { v:receive() } end
  154.   end
  155.   parallel.waitForAll( unpack( get ) )
  156.   local toReturn = {}
  157.   for k, v in ipairs( value ) do
  158.     for x, y in ipairs( v ) do
  159.       toReturn[ #toReturn + 1 ] = y
  160.     end
  161.   end
  162.   return unpack( toReturn )
  163. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement