Message Queue Endpoints

Message Queue Client Example

/***************************************************************************
 *   Copyright (C) 2007 by Rick L. Vinyard, Jr.                            *
 *   rvinyard@cs.nmsu.edu                                                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Lesser General Public License as        *
 *   published by the Free Software Foundation version 2.1.                *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Lesser General Public      *
 *   License along with this library; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA              *
 ***************************************************************************/
#include <conexus.h>
#include <iostream>

int main( int argc, char *argv[] )
{
  size_t written;

  if ( argc < 2 ) {
    std::cerr << "No data to send" << std::endl;
    return 1;
  }

  Conexus::init();

  Conexus::MessageQueue::pointer mq = Conexus::MessageQueue::create("/test_queue");

  written = mq->write( argv[1], strlen(argv[1]) );

  std::cout << "Wrote " << written << " to queue" << std::endl;

  return 0;
}

Message Queue Server Example

/***************************************************************************
 *   Copyright (C) 2007 by Rick L. Vinyard, Jr.                            *
 *   rvinyard@cs.nmsu.edu                                                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Lesser General Public License as        *
 *   published by the Free Software Foundation version 2.1.                *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Lesser General Public      *
 *   License along with this library; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA              *
 ***************************************************************************/
#include <conexus.h>
#include <iostream>

void print_data( const Conexus::Data d );

int main( int argc, char *argv[] )
{
  int run_time = 20;

  Conexus::init();

  Conexus::MessageQueue::pointer mq = Conexus::MessageQueue::create("/test_queue");

  mq->set_unlink_on_destruction();

  mq->signal_data().connect(sigc::ptr_fun(&print_data));

  mq->start();

  std::cout << "Main thread pid: " << pthread_self() << std::endl;
  // Set up a loop that will run and print the time every 5
  // seconds. Since the server is threaded, the sleep(1) call will not effect
  // the servicing thread.
  std::cout << "Starting..." << std::endl;
  for (int i=0; i <= run_time; i++) {
    if (i%5 == 0)
      std::cout << "Time: " << i << std::endl;
    sleep(1);
  }
  std::cout << "Time: " << run_time << std::endl;

  // Stop the server and prepare for shutdown
  mq->stop();

  return 0;

}

void print_data( const Conexus::Data d )
{
  std::cout << "data: ";
  for ( unsigned i = 0; i < d.size(); i++ )
    std::cout << d[ i ];
  std::cout << std::endl;
}

Generated on Tue Mar 13 19:54:50 2007 by  doxygen 1.5.1