Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --iNet 1.0
- local net = {
- send = function( self, message )
- if type( self ) ~= "table" then
- error( 'Incorrect notation, use ":" instead of "."', 2 )
- end
- local packet = { proctal = self.proctal, sender = os.getComputerLabel() or os.getComputerID() }
- if self.encrypt then
- packet.message = self.encrypt( textutils.serialize( message ) )
- packet.encrypt = true
- else
- packet.message = message
- end
- self.modem.transmit( self.channel, self.replyChannel, textutils.serialize( packet ) )
- end,
- reply = function( self, message )
- if type( self ) ~= "table" then
- error( 'Incorrect notation, use ":" instead of "."', 2 )
- end
- local packet = { proctal = self.proctal, sender = os.getComputerLabel() or os.getComputerID() }
- if self.encrypt then
- packet.message = self.encrypt( textutils.serialize( message ) )
- packet.encrypt = true
- else
- packet.message = message
- end
- self.modem.transmit( self.last, self.replyChannel, textutils.serialize( packet ) )
- end,
- receive = function( self )
- if type( self ) ~= "table" then
- error( 'Incorrect notation, use ":" instead of "."', 2 )
- end
- while true do
- local event, side, channel, replyChannel, message, distance = os.pullEvent( "modem_message" )
- local info = textutils.unserialize( message )
- local _proctal = not self.proctal or self.proctal == info.proctal
- if channel == self.replyChannel and _proctal then
- self.last = replyChannel
- if info.encrypt and self.decrypt then
- if info.fileName then
- local file = fs.open( info.fileName, "w" )
- file.write( self.decrypt( info.message ) )
- file.close()
- return info.fileName, distance, info.sender
- else
- return self.decrypt( info.message ), distance, info.sender
- end
- elseif info.encrypt then
- error( "Message could not be decrypted", 2 )
- else
- if info.fileName then
- local file = fs.open( info.fileName, "w" )
- file.write( info.message )
- file.close()
- return info.fileName, distance, info.sender
- else
- return info.message, distance, info.sender
- end
- end
- end
- end
- end,
- ["repeat"] = function( self )
- if type( self ) ~= "table" then
- error( 'Incorrect notation, use ":", instead of "."', 2 )
- end
- while true do
- local event, side, channel, replyChannel, message, distance = os.pullEvent( "modem_message" )
- local info = textutils.unserialize( message )
- if not info["repeat"] then
- info["repeat"] = { os.getComputerID() }
- elseif not info["repeat"][ os.getComputerID() ] then
- info["repeat"][ os.getComputerID() ] = true
- modem.transmit( channel, replyChannel, textutils.serialize( info ) )
- end
- end
- end,
- setProctal = function( self, proctal )
- if type( self ) ~= "table" then
- error( 'Incorrect notation, use ":", instead of "."', 2 )
- elseif type( proctal ) ~= "string" then
- error( 'Expected string, got ' .. type( proctal ), 2 )
- end
- self.proctal = proctal
- end,
- setEncryption = function( self, en )
- if type( self ) ~= "table" then
- error( 'Incorrect notation, use ":", instead of "."', 2 )
- elseif type( en ) ~= "function" then
- error( 'Expected function, got ' .. type( en ), 2 )
- end
- self.encrypt = en
- end,
- setDecryption = function( self, de )
- if type( self ) ~= "table" then
- error( 'Incorrect notation, use ":", instead of "."', 2 )
- elseif type( de ) ~= "function" then
- error( 'Expected function, got ' .. type( de ), 2 )
- end
- self.decrypt = de
- end,
- removeEncryption = function( self )
- self.encrypt = nil
- end,
- removeDecryption = function( self )
- self.decrypt = nil
- end,
- sendFile = function( self, fileName )
- if type( self ) ~= "table" then
- error( 'Incorrect notation, use ":" instead of "."', 2 )
- end
- local file = fs.open( fileName, "r" )
- local message = file.readAll()
- file.close()
- local packet = { proctal = self.proctal, sender = os.getComputerLabel() or os.getComputerID(), fileName = fileName }
- if self.encrypt then
- packet.message = self.encrypt( textutils.serialize( message ) )
- packet.encrypt = true
- else
- packet.message = message
- end
- self.modem.transmit( self.channel, self.replyChannel, textutils.serialize( packet ) )
- end,
- }
- function open( channel, replyChannel, side )
- if not peripheral.isPresent( side ) or peripheral.getType( side ) ~= "modem" then
- error( "No modem connected on " .. side, 2 )
- end
- local modem = peripheral.wrap( side )
- if not modem.isOpen( replyChannel ) then
- modem.open( replyChannel )
- end
- local t = { modem = modem, channel = channel, replyChannel = replyChannel }
- setmetatable( t, { __index = net } )
- return t
- end
- function waitForAny( ... )
- local get = {}
- local value
- for k, v in ipairs( { ... } ) do
- get[ k ] = function() value = { v:receive() } end
- end
- parallel.waitForAny( unpack( get ) )
- return unpack( value )
- end
- function waitForAll( ... )
- local get = {}
- local value = {}
- for k, v in ipairs( { ... } ) do
- get[ k ] = function() value[ k ] = { v:receive() } end
- end
- parallel.waitForAll( unpack( get ) )
- local toReturn = {}
- for k, v in ipairs( value ) do
- for x, y in ipairs( v ) do
- toReturn[ #toReturn + 1 ] = y
- end
- end
- return unpack( toReturn )
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement