LUA and UDP

Hey, I’m working on a device for Vera that needs to listen to a UDP broadcast and react based on the message that is received. I haven’t been able to find any information on LUA and UDP and so I’m looking for help and code samples.

Thanks!
-Nick

Vera includes the LuaSocket library ([url=http://www.tecgraf.puc-rio.br/~diego/professional/luasocket/]http://www.tecgraf.puc-rio.br/~diego/professional/luasocket/[/url]. In current firmware, it’s missing some high-level functions (already fixed for next versions); but it does include all the UDP parts ([url=http://www.tecgraf.puc-rio.br/~diego/professional/luasocket/udp.html]http://www.tecgraf.puc-rio.br/~diego/professional/luasocket/udp.html[/url]).

I haven’t used it; but the code should be something like this:

require "socket"
local udpport = assert (socket.udp())
assert (udpport:setsockname ('*', yourlocalport))
udpport:settimeout (nil)                  -- wait indefinitely
local message = assert (udpport:receive())

Fantastic! Thank you, Javier, here is the code I’m working with, it listens on my UDP port and prints registers the events. I’m just trying to bootstrap my way into doing something cool, XBMC integration.

-- Utility functions provided by MCV, here for local testing
function SetUPnPState(variable_name, variable_value)
    -- Would in reality register the variables
    print("Registered " .. variable_name .. " as " .. variable_value)
end


-- My Script
require "socket"

-- Opens the UDP port
--   Should be trying to receive from the non-forwarding IP 255.255.255.255 but * seems to work
--   Should open the port 8278 which is XBMC's default UDP port
function startup()
    udpport = assert (socket.udp())
    assert (udpport:setsockname ('*', 8278))
    udpport:settimeout (nil)                  -- wait indefinitely
    local message, host = assert (udpport:receivefrom(1024))
    --print("Message " .. message)
    print("Host " .. host)
    incomingData(message)
end

function incomingData(message)
    if (string.find(message, "OnPlayBackStarted") ~= nil) then
        SetUPnPState("PlaybackStatus", "Playing")
    elseif (string.find(message, "OnPlayBackPaused") ~= nil) then
        SetUPnPState("PlaybackStatus", "Paused")
    else
        print("Message " .. message)
    end
end    

startup()

Something I don’t understand is where does the

local message, host = assert (udpport:receivefrom(1024))

go? It doesn’t seem to belong in the startup block. I need to wait and receive these message indefinitely and pass the result to the incomingData function. Also what does the incomingData function actually receive? I need to grab the IP address and the message as this device is really the top level device for sub devices.

Thanks for any help or tips.

Sincerely,
Nick

Any progress with this plugin?