This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
XxLegendxX98's profile picture

Listening for a rednet message being sent

Started by XxLegendxX98, 28 May 2013 - 06:58 PM
XxLegendxX98 #1
Posted 28 May 2013 - 08:58 PM
I'm attempting to display the in/out packets to a computer, just to monitor when things are connecting to it or if it's connecting to something. However, I've run into a problem monitoring packets being sent out. I've looked through several APIs and looked but there was nothing I could do with os.pullEvent to listen for the modem sending a message. So are there any events I could listen for to do this that I overlooked? Or would I have to implement it in every external program that sends rednet messages in the computer?
Bomb Bloke #2
Posted 28 May 2013 - 10:01 PM
You'll indeed require your own listener, best I can make out.

I guess a really simple way to do this would be to implement a global function and a global variable in your monitoring program. Have your other programs call the function whenever they want to send something and let that send it, while incrementing the variable.
Grim Reaper #3
Posted 29 May 2013 - 01:18 AM
I guess a really simple way to do this would be to implement a global function and a global variable in your monitoring program. Have your other programs call the function whenever they want to send something and let that send it, while incrementing the variable.

+1 I agree for this situation. You could override peripheral.wrap to check if the peripheral being wrapped is a modem and, from there, override the transmit function which is in the table returned by peripheral.wrap when wrapping a modem.

Here's a code example:

_G.lastTransmitCallData = {} -- All of the parameters to the last call to the wrapped transmit will be put here.
local oldWrap           = _G.peripheral.wrap -- The 'true' peripheral.wrap function.

_G.peripheral.wrap = function (side)
    -- If the peripheral being wrapped is a modem then make some changes
    -- to the modem handle before returning it.
    if peripheral.getType (side) == "modem" then
        local modemHandle = oldWrap (side)
        local oldTransmit = modemHandle.transmit

        -- Override the transmit function for the modem which has been wrapped.
        modemHandle.transmit = function ( ... )
            _G.lastTransmitCallData = { ... }
            oldTransmit ( ... )
        end

        -- Return the modified modem handle.
        return modemHandle
    end

    -- If the peripheral wasn't a modem then behave normally.
    return oldWrap (side)
end