#include <naming_client.h>
Vianney Lecroart
Nevrax France
Definition at line 58 of file naming_client.h.
Static Public Member Functions | |
void | connect (const CInetAddress &addr, CCallbackNetBase::TRecordingState rec, const std::vector< CInetAddress > &addresses) |
Connect to the naming service. | |
bool | connected () |
Return true if the connection to the Naming Service was done. | |
void | disconnect () |
Close the connection to the naming service. | |
void | displayRegisteredServices (NLMISC::CLog *log=NLMISC::DebugLog) |
const std::list< CServiceEntry > & | getRegisteredServices () |
std::string | info () |
Returns information about the naming connection (connected or not, which naming service and so on). | |
bool | lookup (TServiceId sid, CInetAddress &addr) |
Same as lookup(const string&, CInetAddress&, uint16&). | |
bool | lookup (const std::string &name, CInetAddress &addr) |
void | lookupAll (const std::string &name, std::vector< CInetAddress > &addresses) |
bool | lookupAlternate (const std::string &name, CInetAddress &addr) |
bool | lookupAndConnect (const std::string &name, CCallbackClient &sock) |
uint16 | queryServicePort () |
bool | registerService (const std::string &name, const std::vector< CInetAddress > &addr, TServiceId &sid) |
bool | registerServiceWithSId (const std::string &name, const std::vector< CInetAddress > &addr, TServiceId sid) |
void | resendRegisteration (const std::string &name, const std::vector< CInetAddress > &addr, TServiceId sid) |
void | setRegistrationBroadcastCallback (TBroadcastCallback cb) |
You can link a callback if you want to know when a new service is registered (NULL to disable callback). | |
void | setUnregistrationBroadcastCallback (TBroadcastCallback cb) |
You can link a callback if you want to know when a new service is unregistered (NULL to disable callback). | |
void | unregisterAllServices () |
Unregister all services registered by this client. You don't have to. | |
void | unregisterService (TServiceId sid) |
Unregister a service from the naming service, service identifier. | |
void | update () |
Call it evenly. | |
Static Protected Member Functions | |
void | checkThreadId () |
Static Protected Attributes | |
uint | _ThreadId = 0xFFFFFFFF |
Private Types | |
typedef std::map< TServiceId, std::string > | TRegServices |
Type of map of registered services. | |
Private Member Functions | |
CNamingClient () | |
Constructor. | |
~CNamingClient () | |
Destructor. | |
Static Private Member Functions | |
void | doReceiveLookupAnswer (const std::string &name, std::vector< CInetAddress > &addrs) |
void | find (TServiceId sid, std::vector< CInetAddress > &addrs) |
void | find (const std::string &name, std::vector< CInetAddress > &addrs) |
Static Private Attributes | |
CCallbackClient * | _Connection = NULL |
TServiceId | _MySId = 0 |
TRegServices | _RegisteredServices |
std::list< CServiceEntry > | RegisteredServices |
NLMISC::CMutex | RegisteredServicesMutex |
Friends | |
void | cbRegisterBroadcast (CMessage &msgin, TSockId from, CCallbackNetBase &netbase) |
void | cbUnregisterBroadcast (CMessage &msgin, TSockId from, CCallbackNetBase &netbase) |
|
Type of map of registered services.
Definition at line 194 of file naming_client.h. |
|
Constructor.
Definition at line 200 of file naming_client.h.
00200 {} |
|
Destructor.
Definition at line 203 of file naming_client.h.
00203 {} |
|
Definition at line 544 of file naming_client.cpp. References nlerror. Referenced by disconnect(), queryServicePort(), registerService(), registerServiceWithSId(), resendRegisteration(), unregisterAllServices(), unregisterService(), and update().
00545 { 00546 if (getThreadId () != _ThreadId) 00547 { 00548 nlerror ("You try to access to the CNamingClient with 2 differents thread (%d and %d)", _ThreadId, getThreadId()); 00549 } 00550 } |
|
Connect to the naming service.
Definition at line 239 of file naming_client.cpp. References NLNET::CCallbackNetBase::addCallbackArray(), addr, NLNET::CCallbackClient::connect(), NLNET::CCallbackClient::connected(), NLNET::NamingClientCallbackArray, and nlassert.
00240 { 00241 nlassert (_Connection == NULL || _Connection != NULL && !_Connection->connected ()); 00242 _ThreadId = getThreadId (); 00243 00244 if (_Connection == NULL) 00245 { 00246 _Connection = new CCallbackClient( rec, "naming_client.nmr" ); 00247 _Connection->addCallbackArray (NamingClientCallbackArray, sizeof (NamingClientCallbackArray) / sizeof (NamingClientCallbackArray[0])); 00248 } 00249 00250 _Connection->connect (addr); 00251 00252 /* // send the available addresses 00253 CMessage msgout (_Connection->getSIDA(), "RS"); 00254 msgout.serialCont (const_cast<vector<CInetAddress>&>(addresses)); 00255 _Connection->send (msgout); 00256 00257 // wait the message that contains all already connected services 00258 FirstRegisteredBroadcast = false; 00259 while (!FirstRegisteredBroadcast && _Connection->connected ()) 00260 { 00261 _Connection->update (-1); 00262 nlSleep (1); 00263 } 00264 */} |
|
Return true if the connection to the Naming Service was done.
Definition at line 79 of file naming_client.h. References NLNET::CCallbackClient::connected(). Referenced by info().
00079 { return _Connection != NULL && _Connection->connected (); } |
|
Close the connection to the naming service.
Definition at line 267 of file naming_client.cpp. References checkThreadId(), NLNET::CCallbackClient::connected(), and NLNET::CCallbackClient::disconnect().
00268 { 00269 checkThreadId (); 00270 00271 if (_Connection != NULL) 00272 { 00273 if (_Connection->connected ()) 00274 { 00275 _Connection->disconnect (); 00276 } 00277 delete _Connection; 00278 _Connection = NULL; 00279 } 00280 00281 // we don't call unregisterAllServices because when the naming service will see the disconnection, 00282 // it'll automatically unregister all services registered by this client. 00283 } |
|
Definition at line 172 of file naming_client.h. References RegisteredServices, RegisteredServicesMutex, uint, and uint16.
00173 { 00174 RegisteredServicesMutex.enter (); 00175 log->displayNL ("Display the %d registered services :", RegisteredServices.size()); 00176 for (std::list<CServiceEntry>::iterator it = RegisteredServices.begin(); it != RegisteredServices.end (); it++) 00177 { 00178 log->displayNL (" > %s-%hu %d addr", (*it).Name.c_str(), (uint16)(*it).SId, (*it).Addr.size()); 00179 for(uint i = 0; i < (*it).Addr.size(); i++) 00180 log->displayNL (" '%s'", (*it).Addr[i].asString().c_str()); 00181 } 00182 log->displayNL ("End of the list"); 00183 RegisteredServicesMutex.leave (); 00184 } |
|
|
|
Definition at line 220 of file naming_client.h. References RegisteredServices, RegisteredServicesMutex, and NLNET::TServiceId.
00221 { 00222 RegisteredServicesMutex.enter (); 00223 for (std::list<CServiceEntry>::iterator it = RegisteredServices.begin(); it != RegisteredServices.end (); it++) 00224 if (sid == (*it).SId) 00225 addrs.push_back ((*it).Addr[0]); 00226 RegisteredServicesMutex.leave (); 00227 } |
|
Definition at line 211 of file naming_client.h. References RegisteredServices, and RegisteredServicesMutex. Referenced by lookup(), lookupAll(), and lookupAlternate().
00212 { 00213 RegisteredServicesMutex.enter (); 00214 for (std::list<CServiceEntry>::iterator it = RegisteredServices.begin(); it != RegisteredServices.end (); it++) 00215 if (name == (*it).Name) 00216 addrs.push_back ((*it).Addr[0]); 00217 RegisteredServicesMutex.leave (); 00218 } |
|
Returns all registered services. Definition at line 147 of file naming_client.h. References RegisteredServices.
00147 { return RegisteredServices; } |
|
Returns information about the naming connection (connected or not, which naming service and so on).
Definition at line 285 of file naming_client.cpp. References NLNET::CInetAddress::asString(), connected(), NLNET::CBufClient::remoteAddress(), and res.
|
|
Same as lookup(const string&, CInetAddress&, uint16&).
Definition at line 448 of file naming_client.cpp. References addr, NLNET::CCallbackClient::connected(), find(), nlassert, and NLNET::TServiceId.
00449 { 00450 nlassert (_Connection != NULL && _Connection->connected ()); 00451 00452 vector<CInetAddress> addrs; 00453 find (sid, addrs); 00454 00455 if (addrs.size()==0) 00456 return false; 00457 00458 nlassert (addrs.size()==1); 00459 addr = addrs[0]; 00460 00461 return true; 00462 } |
|
Returns true and the address of the specified service if it is found, otherwise returns false
Definition at line 432 of file naming_client.cpp. References addr, NLNET::CCallbackClient::connected(), find(), and nlassert.
00433 { 00434 nlassert (_Connection != NULL && _Connection->connected ()); 00435 00436 vector<CInetAddress> addrs; 00437 find (name, addrs); 00438 00439 if (addrs.size()==0) 00440 return false; 00441 00442 nlassert (addrs.size()==1); 00443 addr = addrs[0]; 00444 00445 return true; 00446 } |
|
Returns all services corresponding to the specified short name. Ex: lookupAll ("AS", addresses); Definition at line 494 of file naming_client.cpp. References NLNET::CCallbackClient::connected(), find(), and nlassert.
00495 { 00496 nlassert (_Connection != NULL && _Connection->connected ()); 00497 00498 find (name, addrs); 00499 } |
|
Tells the Naming Service the specified address does not respond for the specified service, and returns true and another address for the service if available, otherwise returns false
Definition at line 465 of file naming_client.cpp. References addr, NLNET::CCallbackClient::connected(), find(), nlassert, RegisteredServices, and RegisteredServicesMutex.
00466 { 00467 nlassert (_Connection != NULL && _Connection->connected ()); 00468 00469 // remove it from his local list 00470 00471 RegisteredServicesMutex.enter (); 00472 for (std::list<CServiceEntry>::iterator it = RegisteredServices.begin(); it != RegisteredServices.end (); it++) 00473 { 00474 if ((*it).Addr[0] == addr) 00475 { 00476 RegisteredServices.erase (it); 00477 break; 00478 } 00479 } 00480 RegisteredServicesMutex.leave (); 00481 00482 vector<CInetAddress> addrs; 00483 find (name, addrs); 00484 00485 if (addrs.size()==0) 00486 return false; 00487 00488 nlassert (addrs.size()==1); 00489 addr = addrs[0]; 00490 00491 return true; 00492 } |
|
Obtains a socket connected to a service providing the service name. In case of failure to connect, the method informs the Naming Service and tries to get another service.
Definition at line 501 of file naming_client.cpp. References NLNET::CInetAddress::asString(), NLNET::CCallbackClient::connect(), NLNET::CCallbackClient::connected(), nlassert, and nldebug.
00502 { 00503 nlassert (_Connection != NULL && _Connection->connected ()); 00504 00505 // look up for service 00506 CInetAddress servaddr; 00507 00508 // if service not found, return false 00509 if (!CNamingClient::lookup (name, servaddr)) 00510 return false; 00511 00512 do 00513 { 00514 try 00515 { 00516 // try to connect to the server 00517 sock.connect (servaddr); 00518 00519 // connection succeeded 00520 return true; 00521 } 00522 catch (ESocketConnectionFailed &e) 00523 { 00524 nldebug( "NC: Connection to %s failed: %s, tring another service if available", servaddr.asString().c_str(), e.what() ); 00525 00526 // try another server and if service is not found, return false 00527 if (!CNamingClient::lookupAlternate (name, servaddr)) 00528 return false; 00529 } 00530 } 00531 while (true); 00532 } |
|
Requests the naming service to choose a port for the service
Definition at line 411 of file naming_client.cpp. References checkThreadId(), NLNET::CCallbackClient::connected(), NLNET::CCallbackNetBase::getSIDA(), nlassert, nlinfo, NLMISC::nlSleep(), NLNET::QueryPort, NLNET::QueryPortPort, NLNET::CCallbackClient::send(), uint16, and NLNET::CCallbackClient::update().
00412 { 00413 checkThreadId (); 00414 nlassert (_Connection != NULL && _Connection->connected ()); 00415 00416 CMessage msgout (_Connection->getSIDA(), "QP"); 00417 _Connection->send (msgout); 00418 00419 // wait the answer of the naming service "QP" 00420 QueryPort = false; 00421 while (!QueryPort) 00422 { 00423 _Connection->update (-1); 00424 nlSleep (1); 00425 } 00426 00427 nlinfo ("NC: Received the answer of the query port (%hu)", QueryPortPort); 00428 00429 return QueryPortPort; 00430 } |
|
Register a service within the naming service. Returns false if the registration failed. Definition at line 302 of file naming_client.cpp. References _MySId, _RegisteredServices, addr, checkThreadId(), NLNET::CCallbackClient::connected(), NLNET::CCallbackNetBase::getSIDA(), nlassert, nldebug, NLMISC::nlSleep(), nlwarning, NLNET::Reason, NLNET::Registered, NLNET::RegisteredSID, NLNET::RegisteredSuccess, NLNET::CCallbackClient::send(), NLMISC::CMemStream::serial(), NLMISC::CMemStream::serialCont(), NLNET::TServiceId, uint16, and NLNET::CCallbackClient::update().
00303 { 00304 checkThreadId (); 00305 nlassert (_Connection != NULL && _Connection->connected ()); 00306 00307 CMessage msgout (_Connection->getSIDA(), "RG"); 00308 msgout.serial (const_cast<std::string&>(name)); 00309 msgout.serialCont (const_cast<vector<CInetAddress>&>(addr)); 00310 sid = 0; 00311 msgout.serial (sid); 00312 _Connection->send (msgout); 00313 00314 // wait the answer of the naming service "RG" 00315 Registered = false; 00316 RegisteredSID = &sid; 00317 while (!Registered) 00318 { 00319 _Connection->update (-1); 00320 nlSleep (1); 00321 } 00322 if (RegisteredSuccess) 00323 { 00324 _MySId = sid; 00325 _RegisteredServices.insert (make_pair (*RegisteredSID, name)); 00326 nldebug ("NC: Registered service %s-%hu at %s", name.c_str(), (uint16)sid, addr[0].asString().c_str()); 00327 } 00328 else 00329 { 00330 nldebug ("NC: Naming service refused to register service %s at %s", name.c_str(), addr[0].asString().c_str()); 00331 nlwarning ("NC: Startup denied: %s", Reason.c_str()); 00332 Reason.clear(); 00333 } 00334 00335 RegisteredSID = NULL; 00336 00337 return RegisteredSuccess; 00338 } |
|
Register a service within the naming service, using a specified service identifier. Returns false if the service identifier is unavailable i.e. the registration failed. Definition at line 340 of file naming_client.cpp. References _MySId, _RegisteredServices, addr, checkThreadId(), NLNET::CCallbackClient::connected(), NLNET::CCallbackNetBase::getSIDA(), nlassert, nldebug, nlerror, NLMISC::nlSleep(), NLNET::Registered, NLNET::RegisteredSID, NLNET::RegisteredSuccess, NLNET::CCallbackClient::send(), NLMISC::CMemStream::serial(), NLMISC::CMemStream::serialCont(), NLNET::TServiceId, uint16, and NLNET::CCallbackClient::update().
00341 { 00342 checkThreadId (); 00343 nlassert (_Connection != NULL && _Connection->connected ()); 00344 00345 CMessage msgout (_Connection->getSIDA(), "RG"); 00346 msgout.serial (const_cast<std::string&>(name)); 00347 msgout.serialCont (const_cast<vector<CInetAddress>&>(addr)); 00348 msgout.serial (sid); 00349 _Connection->send (msgout); 00350 00351 // wait the answer of the naming service "RGI" 00352 Registered = false; 00353 RegisteredSID = &sid; 00354 while (!Registered) 00355 { 00356 _Connection->update (-1); 00357 nlSleep (1); 00358 } 00359 if (RegisteredSuccess) 00360 { 00361 _MySId = sid; 00362 _RegisteredServices.insert (make_pair (*RegisteredSID, name)); 00363 nldebug ("NC: Registered service with sid %s-%hu at %s", name.c_str(), (uint16)*RegisteredSID, addr[0].asString().c_str()); 00364 } 00365 else 00366 { 00367 nlerror ("NC: Naming service refused to register service with sid %s at %s", name.c_str(), addr[0].asString().c_str()); 00368 } 00369 00370 return RegisteredSuccess == 1; 00371 } |
|
If the NS is down and goes up, we have to send it again the registration. But in this case, the NS must not send a registration broadcast, so we have a special message Definition at line 373 of file naming_client.cpp. References addr, checkThreadId(), NLNET::CCallbackClient::connected(), NLNET::CCallbackNetBase::getSIDA(), nlassert, NLNET::CCallbackClient::send(), NLMISC::CMemStream::serial(), NLMISC::CMemStream::serialCont(), and NLNET::TServiceId.
00374 { 00375 checkThreadId (); 00376 nlassert (_Connection != NULL && _Connection->connected ()); 00377 00378 CMessage msgout (_Connection->getSIDA(), "RRG"); 00379 msgout.serial (const_cast<std::string&>(name)); 00380 msgout.serialCont (const_cast<vector<CInetAddress>&>(addr)); 00381 msgout.serial (sid); 00382 _Connection->send (msgout); 00383 } |
|
You can link a callback if you want to know when a new service is registered (NULL to disable callback).
Definition at line 66 of file naming_client.cpp. References NLNET::_RegistrationBroadcastCallback, and NLNET::TBroadcastCallback.
00067 { 00068 _RegistrationBroadcastCallback = cb; 00069 } |
|
You can link a callback if you want to know when a new service is unregistered (NULL to disable callback).
Definition at line 71 of file naming_client.cpp. References NLNET::_UnregistrationBroadcastCallback, and NLNET::TBroadcastCallback.
00072 { 00073 _UnregistrationBroadcastCallback = cb; 00074 } |
|
Unregister all services registered by this client. You don't have to.
Definition at line 398 of file naming_client.cpp. References _RegisteredServices, checkThreadId(), NLNET::CCallbackClient::connected(), nlassert, NLNET::TServiceId, and unregisterService().
00399 { 00400 checkThreadId (); 00401 nlassert (_Connection != NULL && _Connection->connected ()); 00402 00403 while (!_RegisteredServices.empty()) 00404 { 00405 TRegServices::iterator irs = _RegisteredServices.begin(); 00406 TServiceId sid = (*irs).first; 00407 unregisterService (sid); 00408 } 00409 } |
|
Unregister a service from the naming service, service identifier.
Definition at line 385 of file naming_client.cpp. References _RegisteredServices, checkThreadId(), NLNET::CCallbackClient::connected(), NLNET::CCallbackNetBase::getSIDA(), nlassert, nldebug, NLNET::CCallbackClient::send(), NLMISC::CMemStream::serial(), and NLNET::TServiceId. Referenced by unregisterAllServices().
00386 { 00387 checkThreadId (); 00388 nlassert (_Connection != NULL && _Connection->connected ()); 00389 00390 CMessage msgout (_Connection->getSIDA(), "UNI"); 00391 msgout.serial (sid); 00392 _Connection->send (msgout); 00393 00394 nldebug ("NC: Unregistering service %s-%hu", _RegisteredServices[sid].c_str(), sid); 00395 _RegisteredServices.erase (sid); 00396 } |
|
Call it evenly.
Definition at line 536 of file naming_client.cpp. References checkThreadId(), NLNET::CCallbackClient::connected(), and NLNET::CCallbackClient::update().
00537 { 00538 checkThreadId (); 00539 // get message for naming service (new registration for example) 00540 if (_Connection != NULL && _Connection->connected ()) 00541 _Connection->update (); 00542 } |
|
Definition at line 119 of file naming_client.cpp.
00120 { 00121 TServiceId size; 00122 string name; 00123 TServiceId sid; 00124 vector<CInetAddress> addr; 00125 00126 msgin.serial (size); 00127 00128 for (TServiceId i = 0; i < size; i++) 00129 { 00130 msgin.serial (name); 00131 msgin.serial (sid); 00132 msgin.serialCont (addr); 00133 00134 // add it in the list 00135 00136 std::vector<CInetAddress> addrs; 00137 CNamingClient::find (sid, addrs); 00138 00139 if (addrs.size() == 0) 00140 { 00141 CNamingClient::RegisteredServicesMutex.enter (); 00142 CNamingClient::RegisteredServices.push_back (CNamingClient::CServiceEntry (name, sid, addr)); 00143 CNamingClient::RegisteredServicesMutex.leave (); 00144 00145 nlinfo ("NC: Registration Broadcast of the service %s-%hu '%s'", name.c_str(), (uint16)sid, vectorCInetAddressToString(addr).c_str()); 00146 00147 if (_RegistrationBroadcastCallback != NULL) 00148 _RegistrationBroadcastCallback (name, sid, addr); 00149 } 00150 else if (addrs.size() == 1) 00151 { 00152 CNamingClient::RegisteredServicesMutex.enter (); 00153 for (std::list<CNamingClient::CServiceEntry>::iterator it = CNamingClient::RegisteredServices.begin(); it != CNamingClient::RegisteredServices.end (); it++) 00154 { 00155 if (sid == (*it).SId) 00156 { 00157 (*it).Name = name; 00158 (*it).Addr = addr; 00159 break; 00160 } 00161 } 00162 CNamingClient::RegisteredServicesMutex.leave (); 00163 nlinfo ("NC: Registration Broadcast (update) of the service %s-%hu '%s'", name.c_str(), (uint16)sid, addr[0].asString().c_str()); 00164 } 00165 else 00166 { 00167 nlstop; 00168 } 00169 } 00170 00171 // FirstRegisteredBroadcast = true; 00172 00173 //CNamingClient::displayRegisteredServices (); 00174 } |
|
Definition at line 178 of file naming_client.cpp.
00179 { 00180 string name; 00181 TServiceId sid; 00182 vector<CInetAddress> addrs; 00183 00184 msgin.serial (name); 00185 msgin.serial (sid); 00186 00187 // remove it in the list, if the service is not found, ignore it 00188 00189 CNamingClient::RegisteredServicesMutex.enter (); 00190 for (std::list<CNamingClient::CServiceEntry>::iterator it = CNamingClient::RegisteredServices.begin(); it != CNamingClient::RegisteredServices.end (); it++) 00191 { 00192 if ((*it).SId == sid) 00193 { 00194 // check the structure 00195 nlassertex ((*it).Name == name, ("%s %s",(*it).Name.c_str(), name.c_str())); 00196 00197 addrs = (*it).Addr; 00198 00199 CNamingClient::RegisteredServices.erase (it); 00200 break; 00201 } 00202 } 00203 CNamingClient::RegisteredServicesMutex.leave (); 00204 00205 nlinfo ("NC: Unregistration Broadcast of the service %s-%hu", name.c_str(), (uint16)sid); 00206 00207 // send the ACK to the NS 00208 00209 CMessage msgout (CNamingClient::_Connection->getSIDA(), "ACK_UNI"); 00210 msgout.serial (sid); 00211 CNamingClient::_Connection->send (msgout); 00212 00213 // oh my god, it s my sid! but i m alive, why this f*cking naming service want to kill me? ok, i leave it alone! 00214 if(CNamingClient::_MySId == sid) 00215 { 00216 nlwarning ("NC: Naming Service asked me to leave, I leave!"); 00217 IService::getInstance()->exit(); 00218 return; 00219 } 00220 00221 if (_UnregistrationBroadcastCallback != NULL) 00222 _UnregistrationBroadcastCallback (name, sid, addrs); 00223 00224 //CNamingClient::displayRegisteredServices (); 00225 } |
|
Definition at line 52 of file naming_client.cpp. |
|
Definition at line 60 of file naming_client.cpp. Referenced by registerService(), and registerServiceWithSId(). |
|
Definition at line 53 of file naming_client.cpp. Referenced by registerService(), registerServiceWithSId(), unregisterAllServices(), and unregisterService(). |
|
Definition at line 58 of file naming_client.cpp. |
|
Definition at line 63 of file naming_client.cpp. Referenced by displayRegisteredServices(), find(), getRegisteredServices(), and lookupAlternate(). |
|
Referenced by displayRegisteredServices(), find(), and lookupAlternate(). |