MOOS学习笔记——多线程

/*
* 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 ) ;
bool OnConnect(void ∗pParam) {
    CMOOSCommClient ∗pC = reinterpret_ cast<CMOOSCommClient∗>(pParam) ;
    pC−>Register ( "X" , 0.0 ) ;
    pC−>Register ( "Y" , 0.0 ) ;
    pC−>Register ( "Z" , 0.0 ) ;
    retu rn true ;
}
bool OnMail (void ∗pParam){
CMOOSCommClient *pC = reinterpret _ cast<CMOOSCommClient∗>(pParam );
MOOSMSG_LIST M ; // get the mail
pC−>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 ;
}
bool funcX ( CMOOSMsg & M , void ∗ TheParameterYouSaidtoPassOnToCallback )
{
gPrinter . SimplyPrintTimeAndMessage ( " c a l l back f o r X" , MOOS : : ThreadPrint : : -
CYAN ) ;
r etu rn t ru e ;
}
bool funcY ( 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 ( int argc , char ∗ argv [] ) {
// unde r stand the command l i n e
MOOS : : 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 comms
MOOS : : 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
// invoked
Comms.AddMessageCallback ( " callback_X " , "X" , funcX , NULL ) ;
Comms.AddMessageCallback ( " callback_Y " , "Y" , funcY , NULL ) ;
// start the comms running
Comms . Run ( db_host , db_port , my_name ) ;
// f o r ever loop sending data
std : : vector<unsigned char> X (1000) ;
f o r ( ; ; ) {
MOOSPause (10) ;
Comms.Notify ( "X",X) ; // for callback_X
Comms.Notify ( "Y","This is Y"); // f o r callback_Y
Comms.Notify ( "Z",7.0); //no callb a c k
}
return 0 ;
}

猜你喜欢

转载自www.cnblogs.com/jingshikongming/p/8971959.html