Main Page | Modules | Class Hierarchy | Compound List | File List | Compound Members | File Members | Related Pages

Example on using the Mailbox class

This page illustrates an example program which polls a mailbox at a regular interval.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "RHexCom.hh"

// note these must be consistent from the previous example
#define MAILBOX_ID 100
struct MailStruct {
  int a, b;
  unsigned char c;
};

int main(int argc, char** argv)
{
  if (argc != 2) {
    fprintf(stderr, "Usage: mailbox <port>\n");
    exit(-1);
  }
  
  // create the communications manager
  unsigned short port = atoi(argv[1]);
  CommManager* mgr = new CommManager(port);

  // create the mailbox
  Mailbox* box = mgr->createMailbox(sizeof(MailStruct), MAILBOX_ID);
  
  while (1) {
    bool new_data = box->newMail(); // check if there is new mail

    // poll the mail box
    Message* msg = box->getData();
    MailStruct* dest= (MailStruct*) msg->getData();
    printf("Received %d %d %d %d\n", (int) new_data,
           dest->a, dest->b, dest->c);

    // release the message back to the mailbox
    box->releaseMsg(msg);

    usleep(500000);
  }

  delete mgr;   // This is all we need for cleaning up
}

RHexLib Reference Documentation