X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=src%2Fmessaging%2Fmessenger.cpp;h=4e5a278c4dca1a67dfafaf1d1f50c5ec6ab0266f;hb=refs%2Ftags%2F1.1.0;hp=6951f7d1d1bc8e10fdadef11fda92ef1f48d71ac;hpb=4e4fb5021cc9aa67390f6641a060b85c077a1880;p=ric-plt%2Fxapp-frame-cpp.git diff --git a/src/messaging/messenger.cpp b/src/messaging/messenger.cpp index 6951f7d..4e5a278 100644 --- a/src/messaging/messenger.cpp +++ b/src/messaging/messenger.cpp @@ -60,13 +60,15 @@ const int Messenger::DEFAULT_CALLBACK = -1; If port is nil, then the default port is used (4560). */ -Messenger::Messenger( char* port, bool wait4table ) { - if( port == NULL ) { - port = (char *) "4560"; +Messenger::Messenger( const char* uport, bool wait4table ) { + + if( uport == NULL ) { + listen_port = strdup( "4560" ); + } else { + listen_port = strdup( uport ); } gate = new std::mutex(); - listen_port = strdup( port ); mrc = rmr_init( listen_port, Messenger::MAX_PAYLOAD, 0 ); if( wait4table ) { @@ -78,6 +80,52 @@ Messenger::Messenger( char* port, bool wait4table ) { ok_2_run = true; } +/* + Move support. We DO allow the instance to be moved as only one copy + remains following the move. + Given a source object instance (soi) we move the information to + the new object, and then DELETE what was moved so that when the + user frees the soi, it doesn't destroy what we snarfed. +*/ +Messenger::Messenger( Messenger&& soi ) { + mrc = soi.mrc; + listen_port = soi.listen_port; + ok_2_run = soi.ok_2_run; + gate = soi.gate; + cb_hash = soi.cb_hash; // this seems dodgy + + soi.gate = NULL; + soi.listen_port = NULL; + soi.mrc = NULL; +} + +/* + Move operator. Given a source object instance, movee it's contents + to this insance. We must first clean up this instance. +*/ +Messenger& Messenger::operator=( Messenger&& soi ) { + if( this != &soi ) { // cannot move onto ourself + if( mrc != NULL ) { + rmr_close( mrc ); + } + if( listen_port != NULL ) { + free( listen_port ); + } + + mrc = soi.mrc; + listen_port = soi.listen_port; + ok_2_run = soi.ok_2_run; + gate = soi.gate; + cb_hash = soi.cb_hash; // this seems dodgy + + soi.gate = NULL; + soi.listen_port = NULL; + soi.mrc = NULL; + } + + return *this; +} + /* Destroyer. */ @@ -86,7 +134,9 @@ Messenger::~Messenger() { rmr_close( mrc ); } - free( listen_port ); + if( listen_port != NULL ) { + free( listen_port ); + } } /* @@ -165,18 +215,16 @@ void Messenger::Listen( ) { */ std::unique_ptr Messenger::Receive( int timeout ) { rmr_mbuf_t* mbuf = NULL; - //std::unique_ptr m; + std::unique_ptr m = NULL; - if( mrc == NULL ) { - return NULL; - } - - mbuf = rmr_torcv_msg( mrc, mbuf, timeout ); // future: do we want to reuse the mbuf here? - if( mbuf != NULL ) { - return std::unique_ptr( new Message( mbuf, mrc ) ); + if( mrc != NULL ) { + mbuf = rmr_torcv_msg( mrc, mbuf, timeout ); // future: do we want to reuse the mbuf here? + if( mbuf != NULL ) { + m = std::unique_ptr( new Message( mbuf, mrc ) ); + } } - return NULL; + return m; } /*