Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^4: Timed event within a script

by bajangerry (Sexton)
on Mar 09, 2017 at 20:18 UTC ( [id://1184085]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Timed event within a script
in thread Timed event within a script

Thanks for your input. I have been trying to wrap my head around the best way to do this. The problem I have is that even though there has not been a message from the device for hours it doesn't mean there is actually a problem, it is more likely that there is NOT a problem in that case. It would be nice if there was a periodic "keep alive" type message from the device but I am not that lucky.

Simply put, the device shoots out a message on a IP port whenever there is an error event and only then. It does not care if there is 1 or 100 devices monitoring the port either

Replies are listed 'Best First'.
Re^5: Timed event within a script
by Marshall (Canon) on Mar 10, 2017 at 21:29 UTC
    Geez. One can only hope that this "emergency message" doesn't mean something like "reactor coolant low"!

    To make this into a single process, you will need to determine whether or not the socket for the device has a line for you to process in it or not. I think that you will need IO::Socket. This sort of thing would be done in what is called a "select server". Don't read from the socket unless you know that it has something to say.

    I think this psuedo code would be fine in your application.

    # open file handles for reading and reporting # set $since_report_secs (seconds) = 0 while (sleep (1)) { my $emergency = check_for_emergency(); if ($emergency) { do something; } report_awake_status(); } sub check_for_emergency { while (device port ready to read) { read line and check it for "emergency" return 1 if emergency; } return 0; # "no error"; } sub report_awake_status { $last_report_time++; if (more than 30 minutes) { send awake message, set counter back to zero } }
    The above code essentially sets up a "polling loop". This is not as efficient as it "could be". But the logic for a simple single process is simple and I think that you will find that code like this doesn't take much CPU time.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1184085]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-04-26 06:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found