lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Alexey Mikhailov <karma@galois.botik.ru> [2009-12-25 15:20:11]:

> Hello, LUA fondlers!

Hi,

> So basically I want to call something like this in LUA. (code in 
> pseudolanguage)
> 
> foreach packet that (satisfies)CONDITION in [time1, time2] 
> 	summary_param += packet.size 
> done

well, basically you can expose some C function which will return Lua table
with packets to Lua or you can do it using some kind of custom iterator
function similar to Lua ipairs() or using some simple next() function which
will return next packet each time is called. I would do something like this in
Lua/C:

-- get_packets() is C function exposed to Lua
packets = get_packets(time1, time2) -- returns Lua table with packets which
                                    -- match time1 and time2 conditions

-- process packets, using standard ipairs() Lua iterator
for index, packet in ipairs(packets) do
	-- process_condition() could be Lua or C function
	if process_condition(index, packet) == true then
		do_something_with_packet(index, packet)
	end
end

> (receive query in LUA -> call C function to query -> C callback will
> be called and information will be parsed -> call LUA code to process
> data)

I don't understand clearly all this, but basicaly you can have some Lua
function, event handler, called on each received packet:

-- this Lua function is called from C every time there's packet available
function event_packet_received(packet)
	if not process_packet(packet) then
		return false
	end

	return true
end

-- ynezz