Receive system events


ProviewR transmits messages at certain events, e.g. when a soft restart proceeds or when
the runtime environment is stopped. An application can listen to these messages, for
example to terminate when ProviewR is terminated. The messages are received from Qcom. You
create a Qcom queue and bind this queue to the queue that submits the messages.

#include "rt_qcom.h"
#include "rt_ini_event.h"
#include "rt_qcom_msg.h"

qcom_sQid qid = qcom_cNQid;
qcom_sQid qini;
qcom_sQattr qAttr;

if ( !qcom_Init(&sts, 0, "ra_myappl")) {
   throw co_error(sts);

// Create a queue to receive stop and restart events
qAttr.type = qcom_eQtype_private;
qAttr.quota = 100;
if ( !qcom_CreateQ(&sts, &qid, &qAttr, "events"))
   throw co_error(sts);

// Bind to init event queue
qini = qcom_cQini;
if ( !qcom_Bind(&sts, qid, &qini))
   throw co_error(sts);

In each scan you read the queue with qcom_Get() to see if any messages have arrived. You
can also use the timeout in qcom_Get() to wait to next scan. In the example below, the
terminate event is handled, but also the oldPlcStop and swapDone events that indicates
the start and end of a soft restart. You only have to do this if you want the application
to discover new objects of new configurations after a soft restart.

int tmo = 1000;
char mp[2000];
qcom_sGet get;
int swap = 0;

for (;;) {
   get.maxSize = sizeof(mp);
   get.data = mp;
   qcom_Get( &sts, &qid, &get, tmo);
   if (sts == QCOM__TMO || sts == QCOM__QEMPTY) {
     if ( !swap)
       // Do the normal thing
       scan();
   }
   else {
     // Ini event received
     ini_mEvent new_event;
     qcom_sEvent *ep = (qcom_sEvent*) get.data;

     new_event.m = ep->mask;
     if (new_event.b.oldPlcStop && !swap) {
       errh_SetStatus( PWR__APPLRESTART);
       swap = 1;
       close();
     } else if (new_event.b.swapDone && swap) {
       swap = 0;
       open();
       errh_SetStatus( PWR__ARUN);
     } else if (new_event.b.terminate) {
       exit(0);
     }
   }
}

If you only are interested in stopping the process when ProviewR is taken down, there is
a more simple way to kill it. You can put a scripfile, pwrp_stop.sh, on $pwrp_exe where
you kill the process.

killall ra_myappl