Sensor poll/presence push loop
Now that we’ve set everything up, determined the initial coffee status, and connected to the Jabber server, we’re ready to start the main loop:
# Main loop: check Jabber and RCX
while (1) {
defined($jabber->Process(GRAIN)) or
die "The connection to the Jabber server was broken\n";
my $s = &set_status($rcx->Poll(9, SENSOR));
&set_presence($jabber, $s) if defined $s;
}
The while (1) loop is a bit of a giveaway. This script won’t
stop until you force it to by entering
Ctrl-C—but that’s essentially
what we want. In the loop, we call the Process()
method on the Jabber connection object in $jabber.
Process() is the equivalent of the Jabberpy’s
process() method in the Python scripts.
Process() waits around for up to the number of seconds
specified as the single argument (or not at all if no argument is specified)
for XML to appear on the stream connection from the Jabber server. If
complete fragments do appear, callbacks, defined in the connection
object, are called with the elements (<iq/>,
<message/>, and
<presence/>) that the fragments represent.
This is in the same way as, for example, callbacks are used in the Python
scripts using the Jabberpy library. The
setup_Jabber(), which will be discussed in the next section, is where the callback
definition is made.
Net::Jabber’s Process() method returns
undef
if the connection to the Jabber server is terminated while waiting for
XML. The undef value is dealt with appropriately by
ending the script.
The ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access