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

Example on using the StreamSink class

This page illustrates an example program which sits on a stream and waits for new messages to come in.

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

#include "RHexCom.hh"

// the following must be consistent with the previous example
#define STREAM_ID 100
struct StreamStruct {
  int a, b;
  unsigned char c;
};

int main(int argc, char** argv)
{
  if (argc != 4) {
    fprintf(stderr,"Usage: stream_sink <port>\n");
    exit(-1);
  }

  // create communications manager
  unsigned short port = atoi(argv[1]);
  CommManager* mgr = new CommManager(port);

  // create the stream sing
  StreamSink* sink = mgr->createStreamSink(sizeof(int), STREAM_ID, 10);
  Message* msg;
  StreamStruct ss;
  while (1) {
    bool received = false;
    // while there are messages to be processed
    while ((msg = sink->pollData())) {
      // use the more "compact" means for processing data structure
      msg->getStruct<StreamStruct>(&ss);
      printf("Received %d %d\n", msg->getSize(), ss.a);
      sink->releaseMsg(msg);       // release the message back to the sink
      received = true;
    }
    if (received)
      printf("\n");
    usleep(1000000);
  }

  delete mgr; // clean everything up
}

RHexLib Reference Documentation