简体   繁体   中英

how to use a timer in Lua

I capture data with tshark and save certain data from the packet header to process them in order to detect some incedants in the network. I saved the data in a table in my lua program (which is running in the cmd with tshark using the command (-Xlua_script:)) and now i want to process the data of each minute alone while capturing is running. It's an online processing. Firstly:Any body knows if this could be implemented?Secondly I need a timer, I don't know how to do this, and i want a way that i can take the data in the tables to process them, reset the tables to get the new data of the next minute without losing any data. Any suggestions or ideas??

there isn't the concept of a 'timer' in lua like some other languages, where you can create one and set up an event handler and have your main program notified when the timer goes off... however you can periodically check os.clock() to determine how long its been since you've done some processing and if a minute has elapsed, go ahead and process the data.

something like this might be what you need:

lastTimeProcessed = os.clock()

function IsTimeToProcess(currentTime)
    span = currentTime - lastTimeProcessed
    if span >= 60 then
        lastTimeProcessed = currentTime
        return true
    end

    return false
end

while true do
    if IsTimeToProcess(os.clock()) then
        -- process some data here
    end
    -- otherwise do another round of whatever you're doing
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM