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

Example on using the StreamSource class

This page illustrates an example program which sends stream messages at a regular interval. Just as an illustration, it gives up if the queue of unacknowledged messages gets too long.

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

#include "RHexCom.hh"

#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_source <port> <dest_machine> <dest_port>\n");
    exit(-1);
  }

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

  // create the stream source
  StreamSource* source =
    mgr->createStreamSource(argv[2], atoi(argv[3]),
                            sizeof(StreamStruct), STREAM_ID);

  // send the message periodically
  StreamStruct ss;
  for (int i=1;;i++) {
    //  note we are using the less efficient
    // but more compact method for stuffing messages
    ss.a = i; ss.b = 2*i; ss.c = i % 2;
    Message* msg = source->createMsg();
    msg->setStruct<StreamStruct>(&ss);

    // send the message
    source->sendMsg(msg);
    printf("Sent %d (queuing %d for acknowledgement)\n", i,
           source->numToSend());

    // if too long with no acknowledgement, quit
    if (source->numToSend() > 10)
      break;
    usleep(500000);
  }

  delete mgr;  // clean up
}

RHexLib Reference Documentation