/** A simple example showing how to use A comms client*/#include "moos/libmoos/comms/moosasynccommclient.h"#include "moos/libmoos/utils/commandlineparser.h"#include "moos/libmoos/utils/consolecolours.h"#include "moos/libmoos/utils/threadprint.h"Moos::threadprint gprinter (std:: cout);BOOLOnConnect (voidPparam) {cmooscommclient PC = reinterpret_ cast<cmooscommclient?> (pparam); Pc?>register ("X",0.0) ; Pc?>register ("Y",0.0) ; Pc?>register ("Z",0.0) ; Retu RNtrue;}BOOLOnmail (void? pparam) {Cmooscommclient *pc = Reinterpret _ Cast<cmooscommclient?> (Pparam); Moosmsg_list M;//Get the MailPc?>fetch (M); Moosmsg_list:: Iterator Q;//P R o C e s s i t for(Q=m.begin (); Q!=m.end (); q++) {Gprinter. Simplyprinttimeandmessage ("Mail:"+q?>getsource (), MOOS::-threadprint:: GREEN);} R ETU RN T RU e;BOOLFuncx (Cmoosmsg & M,void? Theparameteryousaidtopassontocallback) {Gprinter. Simplyprinttimeandmessage ("c A l l back F o r X", MOOS:: threadprint::-cyan); r ETU rn T RU e;}BOOLFuncy (Cmoosmsg & M,void? Theparameteryousaidtopassontocallback) {Gprinter. Simplyprinttimeandmessage ("c A l l back F o r Y", MOOS:: threadprint::-magenta); r ETU rn T RU e;} I n T Main (intARGC,Char? argv []) {//Unde R stand the command L N eMOOS:: Commandlineparser P (argc, argv); std:: string db_host="L o c a l h o s T"; P. GetVariable ("?? Moos_host ", db_host); i n t db_port =9000; P. GetVariable ("?? Moos_port ", db_port); std:: string my_name ="ex40"; P. GetVariable ("?? Moos_name ", my_name);//C o n f i g u r e the commsMOOS:: Moosasynccommclient Comms; Comms. Setonmailcallback (Onmail, &comms); Comms. Setonconnectcallback (OnConnect, &comms);//Here we add per message C a l l B a C K s//FI R S t parameter i s the Channel Nick?name, then the f u n c t i o n//to C a l l, then a parameter we want passed when c a l l b a C K i s//invokedComms.addmessagecallback ("Callback_x","X", Funcx, NULL); Comms.addmessagecallback ("Callback_y","Y", Funcy, NULL);//Start the comms runningComms. Run (Db_host, Db_port, my_name);//F o r ever loop sending dataSTD:: vector<unsigned Char> X ( +); F o r (;;) {Moospause (Ten) ; Comms.notify ("X", X);//For callback_xComms.notify ("Y","This is Y");//F o r callback_yComms.notify ("Z",7.0);//no callb a C k}return 0;}
Moos Learning notes-multithreading