mm_pubsub.cpp
Go to the documentation of this file.
00001 
00006 /*****************************************************************************
00007 ** Includes
00008 *****************************************************************************/
00009 
00010 #include <ecl/command_line/xor_handler.hpp>
00011 #include <ecl/command_line/cmd_line.hpp>
00012 #include <ecl/command_line/value_arg.hpp>
00013 #include <ecl/command_line/switch_arg.hpp>
00014 #include <ecl/time.hpp>
00015 #include <mm_core_msgs/test_identifiers.hpp>
00016 #include <mm_core_msgs/string.hpp>
00017 #include <string>
00018 #include "../../include/mm_mux_demux/publisher.hpp"
00019 #include "../../include/mm_mux_demux/demux.hpp"
00020 #include "../../include/mm_mux_demux/subscriber.hpp"
00021 
00022 /*****************************************************************************
00023 ** Methods
00024 *****************************************************************************/
00025 
00026 void foo(std::string data) {
00027   std::cout << "foo is processing data: " << data << std::endl;
00028 }
00029 /*****************************************************************************
00030 ** Main
00031 *****************************************************************************/
00032 
00033 int main(int argc, char **argv)
00034 {
00035   /****************************************
00036    ** Args
00037    ****************************************/
00038   ecl::CmdLine cmd("Pub-sub testing with nanomsg.");
00039   ecl::SwitchArg pub("p", "pub", "Run the publisher only", false);
00040   ecl::SwitchArg sub("s", "sub", "Run the subscriber only", false);
00041   ecl::SwitchArg both("b", "both", "Run both the publisher and the subscriber", false);
00042   std::vector<ecl::Arg*> xorlist;
00043   xorlist.push_back(&pub);
00044   xorlist.push_back(&sub);
00045   xorlist.push_back(&both);
00046   cmd.xorAdd(xorlist);
00047   ecl::UnlabeledValueArg<std::string> ip("ip","Ip to use (e.g. ipc:///tmp/pubsub.ipc, tcp://192.168.1.3:5555)", false,"ipc:///tmp/pubsub.ipc","string", cmd);
00048   cmd.parse(argc, argv);
00049 
00050   //bool result = mm_messages::MessageRegistry::add<mm_core_msgs::TestStrings, std::string>("unused_description");
00051   if ( pub.isSet() ) {
00052     mm_mux_demux::MessageMux::start("dude", ip.getValue());
00053     std::cout << "Creating publisher" << std::endl;
00054     mm_mux_demux::Publisher publisher("dude");
00055     ecl::MilliSleep()(200); // let the connection establish itself
00056     while(true) {
00057       std::cout << "Publishing 'dude'" << std::endl;
00058       publisher.publish(mm_core_msgs::TestStrings, std::string("dude"));
00059       ecl::MilliSleep()(500);
00060     }
00061   } else if ( sub.isSet() ) {
00062     mm_mux_demux::MessageDemux::start("dude", ip.getValue());
00063     std::cout << "Creating demux"<< std::endl;
00064     mm_mux_demux::Subscriber<mm_core_msgs::TestStrings, std::string> subscriber("dude", foo);
00065     while(true) {
00066       ecl::MilliSleep()(200);
00067     }
00068     mm_mux_demux::MessageDemux::shutdown("dude");
00069   } else if ( both.isSet() ) {
00070     mm_mux_demux::MessageMux::start("dude", ip.getValue(), mm_messages::Verbosity::HIGH);
00071     mm_mux_demux::MessageDemux::start("dude", ip.getValue(), mm_messages::Verbosity::HIGH);
00072     mm_mux_demux::Subscriber<mm_core_msgs::TestStrings, std::string> subscriber("dude", foo);
00073     mm_mux_demux::Publisher publisher("dude");
00074     ecl::MilliSleep()(200); // let the connection establish itself
00075     publisher.publish(mm_core_msgs::TestStrings, std::string("dude"));
00076     ecl::MilliSleep()(500);
00077     mm_mux_demux::MessageDemux::shutdown("dude");
00078   } else {
00079     // should never get here because tclap will throw up the usage page.
00080   }
00081   return 0;
00082 }


mm_mux_demux
Author(s): Daniel Stonier
autogenerated on Thu Jun 6 2019 21:13:22