26.02.2014 Views

Getting Started with QNX Neutrino - QNX Software Systems

Getting Started with QNX Neutrino - QNX Software Systems

Getting Started with QNX Neutrino - QNX Software Systems

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

© 2009, <strong>QNX</strong> <strong>Software</strong> <strong>Systems</strong> GmbH & Co. KG. Using timers<br />

if (timer_create (CLOCK_REALTIME, &event, &timerid) == -1) {<br />

fprintf (stderr, "%s: couldn’t create a timer, errno %d\n",<br />

progname, errno);<br />

perror (NULL);<br />

exit (EXIT_FAILURE);<br />

}<br />

// setup the timer (1s delay, 1s reload)<br />

timer.it_value.tv_sec = 1;<br />

timer.it_value.tv_nsec = 0;<br />

timer.it_interval.tv_sec = 1;<br />

timer.it_interval.tv_nsec = 0;<br />

}<br />

// and start it!<br />

timer_settime (timerid, 0, &timer, NULL);<br />

gotAPulse()<br />

In gotAPulse(), you can see how we’ve implemented the server’s ability to timeout a<br />

client. We walk down the list of clients, and since we know that the pulse is being<br />

triggered once per second, we simply decrement the number of seconds that the client<br />

has left before a timeout. If this value reaches zero, we reply back to that client <strong>with</strong> a<br />

message saying, “Sorry, timed out” (the MT_TIMEDOUT message type). You’ll notice<br />

that we prepare this message ahead of time (outside the for loop), and then send it as<br />

needed. This is just a style/usage issue — if you expect to be doing a lot of replies,<br />

then it might make sense to incur the setup overhead once. If you don’t expect to do a<br />

lot of replies, then it might make more sense to set it up as needed.<br />

If the timeout value hasn’t yet reached zero, we don’t do anything about it — the client<br />

is still blocked, waiting for a message to show up.<br />

/*<br />

* gotAPulse<br />

*<br />

* This routine is responsible for handling the fact that a<br />

* timeout has occurred. It runs through the list of clients<br />

* to see which client has timed out, and replies to it <strong>with</strong><br />

* a timed-out response.<br />

*/<br />

void<br />

gotAPulse (void)<br />

{<br />

ClientMessageT msg;<br />

int i;<br />

if (debug) {<br />

time_t now;<br />

}<br />

time (&now);<br />

printf ("Got a Pulse at %s", ctime (&now));<br />

// prepare a response message<br />

msg.messageType = MT_TIMEDOUT;<br />

// walk down list of clients<br />

April 30, 2009 Chapter 3 • Clocks, Timers, and <strong>Getting</strong> a Kick Every So Often 153

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!