/*
 *               Copyright (C) 2005, BroadOn Communications Corp.
 *
 *  These coded instructions, statements, and computer programs contain
 *  unpublished  proprietary information of BroadOn Communications Corp.,
 *  and  are protected by Federal copyright law. They may not be disclosed
 *  to  third  parties or copied or duplicated in any form, in whole or in
 *  part, without the prior written consent of BroadOn Communications Corp.
 *
 */

/* Register handler for "event" IOS_EVENT_MIN.
 * Handle NUM_EVENTS events.
 * De-register handler and exit.
 *
 * To test: 
 *   - build and install this program
 *   - run it program from iossh
 *   - send signal IOS_EVENT_MIN (printed by program) to the process
 *     iose
 */
#include <stdio.h>  /* For printf, sprintf */
#include <signal.h> /* For value of SIGRTMIN on Linux */

#include <ios.h>
#include <iostypes.h>
#include <ioslibc.h>

#define MSGQ_SIZE 8
#define NUM_EVENTS 5

IOSMessage msgQ[MSGQ_SIZE];

void
event()
{
  char msg[32];
  char *rmsg;
  IOSMessage msgQ[MSGQ_SIZE];
  IOSMessageQueueId qid;
  int i;

  sprintf(msg,"tst-event-%d", IOS_EVENT_MIN);
  qid = IOS_CreateMessageQueue(msgQ, MSGQ_SIZE);
  if ( qid < 0 ) {
    IOS_Perror("creating msg queue failed", qid);
  }
  IOS_HandleEvent(IOS_EVENT_MIN, qid, (IOSMessage) msg);
  printf("Event handler registered for %d\n", IOS_EVENT_MIN );
  for ( i = 0; i < 5; ++i ) {
    IOS_ReceiveMessage(qid, (IOSMessage *) &rmsg, IOS_MESSAGE_BLOCK);
    printf("reader read msg: %s\n", rmsg);
  }
  IOS_UnhandleEvent(IOS_EVENT_MIN);
  IOS_ThreadExit(0);
}
