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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

time1.c<br />

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

}<br />

}<br />

}<br />

/*<br />

* gotAMessage<br />

*<br />

* This routine is called whenever a message arrives. We look at the<br />

* type of message (either a "wait for data" message, or a "here’s some<br />

* data" message), and act accordingly. For simplicity, we’ll assume<br />

* that there is never any data waiting. See the text for more discussion<br />

* about this.<br />

*/<br />

void<br />

gotAMessage (int rcvid, ClientMessageT *msg)<br />

{<br />

int i;<br />

// determine the kind of message that it is<br />

switch (msg -> messageType) {<br />

// client wants to wait for data<br />

case MT_WAIT_DATA:<br />

// see if we can find a blank spot in the client table<br />

for (i = 0; i < MAX_CLIENT; i++) {<br />

if (!clients [i].in_use) {<br />

}<br />

}<br />

// found one -- mark as in use, save rcvid, set timeout<br />

clients [i].in_use = 1;<br />

clients [i].rcvid = rcvid;<br />

clients [i].timeout = 5;<br />

return;<br />

fprintf (stderr, "Table full, message from rcvid %d ignored, "<br />

"client blocked\n", rcvid);<br />

break;<br />

// client <strong>with</strong> data<br />

case MT_SEND_DATA:<br />

// see if we can find another client to reply to <strong>with</strong> this<br />

// client’s data<br />

for (i = 0; i < MAX_CLIENT; i++) {<br />

if (clients [i].in_use) {<br />

// found one -- reuse the incoming message as an<br />

// outgoing message<br />

msg -> messageType = MT_OK;<br />

// reply to BOTH CLIENTS!<br />

MsgReply (clients [i].rcvid, EOK, msg, sizeof (*msg));<br />

MsgReply (rcvid, EOK, msg, sizeof (*msg));<br />

}<br />

}<br />

clients [i].in_use = 0;<br />

return;<br />

320 Appendix: C • Sample Programs April 30, 2009

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

Saved successfully!

Ooh no, something went wrong!