Moos Learning notes 4--different callbacks for separate threads
/***@fn Independent thread different callback *@Version V1. 0*@author*/#include "moos/libmoos/comms/moosasynccommclient.h"#include "moos/libmoos/utils/commandlineparser.h"//control change the font color inside the class defines a number of colors#include "moos/libmoos/utils/consolecolours.h"#include "moos/libmoos/utils/threadprint.h"Moos::threadprint gprinter (std:: cout);BOOLOnConnect (void*pparam) {Cmooscommclient *pc =reinterpret_cast<CMOOSCommClient*> (Pparam); Pc->register ("X", 0.0) ; Pc->register ("Y", 0.0) ; Pc->register ("Z", 0.0) ;return true;}BOOLOnmail (void*pparam) {Cmooscommclient *pc =reinterpret_cast<CMOOSCommClient*> (Pparam); Moosmsg_list M;//Get the MailPc->fetch (M); Moosmsg_list::iterator Q;//Process it for(Q=m.begin (); Q!=m.end (); q++) {//getsource (): Returns the name of the process that issued the notification //simplyprinttimeandmessage (): Output thread-safe formatting information, the first parameter is the data to be output //The second parameter is a color, default is nullGprinter.simplyprinttimeandmessage ("Mail:"+Q->getsource (), moos::threadprint::green); }return true;}BOOLFuncx (Cmoosmsg &m,void*theparameteryousaidtopassontocallback) {Gprinter.simplyprinttimeandmessage ("Call back for X" , MOOS:: Threadprint::cyan);return true;}BOOLFuncy (Cmoosmsg &m,void*theparameteryousaidtopassontocallback) {Gprinter.simplyprinttimeandmessage ("Call back for Y" , Moos::threadprint::magenta);return true;}intMainintARGC,Char*ARGV []) {//Understand the commandlineMoos::commandlineparser P (ARGC,ARGV); std::string db_host="localhost" ; P.getvariable ("--moos_host" , db_host);intDb_port =9000; P.getvariable ("--moos_port" , Db_port); std::string my_name ="ex40" ; P.getvariable ("--moos_name" , my_name);//Configure the CommsMoos::moosasynccommclient Comms; Comms.setonmailcallback (Onmail, &comms); Comms.setonconnectcallback (OnConnect, &comms);/***@Name Addmessagecallback () *@FN Adds a callback function for each message *@Param[in] * The first one is the alias of the channel * The second is the variable that you want to pass when the callback occurs* @note Use this function to call the function specified, do not use the function to call the default Onmail ()*/Comms.addmessagecallback ("Callback_x","X",Funcx,null); Comms.addmessagecallback ("Callback_y","Y",Funcy,null);//Start the comms runningComms. Run (Db_host, Db_port, my_name);//Forever loop sending dataSTD:: vector<unsigned Char>x ( +) ; for( ; ; ) {Moospause ( +) ; Comms.notify ("X",X);//For callback_xComms.notify ("Y","This is Y");//For callback_yComms.notify ("Z",7.0);//no Callback}return 0;}
Moos Learning notes 4--different callbacks for separate threads