00001 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 #include <pthread.h>
00036 #include <string.h>
00037 #include <stdint.h>
00038 #include "discovery_service.h"
00039 #include "discovery.h"
00040 #include "stacktrace.h"
00041 
00042 using namespace sensix::discovery;
00043 
00044 extern pthread_mutex_t ancestor_mutex;
00045 extern uint8_t ancestors_running;
00046 
00047 
00049 
00050 
00051 AncestorsImpl::AncestorsImpl() : Ancestors() {}
00052 
00053 
00054 CORBA::Boolean AncestorsImpl::findNode(sensix::NodeId id)
00055 {
00056   return (CORBA::Boolean)ancestors_findNode(id);
00057 }
00058 
00059 
00060 NodeList *AncestorsImpl::nodes()
00061 {
00062   size_t size = 0;
00063   ancestors_nodes(NULL, &size);
00064   uint32_t *nodes = new uint32_t[size];
00065   ancestors_nodes(nodes, &size);
00066   NodeList *nlist = new NodeList(size);
00067   for (CORBA::ULong i = 0; i < nlist->length(); i++)
00068     (*nlist)[i] = nodes[i];
00069   delete nodes;
00070 
00071   return nlist;
00072 }
00073 
00074 
00076 
00077 
00078 SiblingsImpl::SiblingsImpl(CORBA::ORB_ptr ptr) : Siblings()
00079 {
00080   orb = ptr;
00081 }
00082 
00083 
00084 SiblingsImpl::SiblingsImpl() : Siblings() {}
00085 
00086 
00087 CORBA::Boolean SiblingsImpl::findNode(sensix::NodeId id)
00088 {
00089   return (CORBA::Boolean)siblings_findNode(id);
00090 }
00091 
00092 
00093 NodeList *SiblingsImpl::nodes()
00094 {
00095   size_t size = 0;
00096   siblings_nodes(NULL, &size);
00097   uint32_t *nodes = new uint32_t[size];
00098   siblings_nodes(nodes, &size);
00099   NodeList *nlist = new NodeList(size);
00100   for (CORBA::ULong i = 0; i < nlist->length(); i++)
00101     (*nlist)[i] = nodes[i];
00102   delete nodes;
00103 
00104   return nlist;
00105 }
00106 
00107 
00108 CapabilityList *SiblingsImpl::capabilities()
00109 {
00110   size_t size = 0;
00111   siblings_capabilities(NULL, &size);
00112   uint8_t *caps = new uint8_t[size];
00113   siblings_capabilities(caps, &size);
00114   CapabilityList *clist = new CapabilityList(size);
00115   for (CORBA::ULong i = 0; i < clist->length(); i++)
00116     (*clist)[i] = caps[i];
00117   delete caps;
00118 
00119   return clist;
00120 }
00121 
00122 
00123 RequestList *SiblingsImpl::queryNetwork(CORBA::Octet capability)
00124 {
00125   int err = 0;
00126   size_t size = 0;
00127   siblings_queryNetwork(NULL, &size, capability);
00128   if (size == 0)
00129     return NULL;
00130 
00131   object_reference_t *refs = new object_reference_t[size];
00132   err = siblings_queryNetwork(refs, &size, capability);
00133   if (err < 0) {
00134     if (err == NO_CAPABILITY)
00135       throw DiscoveryException(NoCapability, "No capable sibling");
00136     else if (err == TOO_SMALL) {
00137       log_error("%s: Internal error at siblings_queryNetwork", APP);
00138       printStackTrace("Capability", __FILE__, __LINE__);
00139     }
00140   }
00141 
00142   RequestList *rlist = new RequestList(size);
00143   for (CORBA::ULong i = 0; i < rlist->length(); i++) {
00144     CORBA::Object_ptr obj = orb->string_to_object(refs[i].ior);
00145     (*rlist)[i] = sensix::Request::_narrow(obj);
00146   }
00147   delete refs;
00148   
00149 
00150   return rlist;
00151 }
00152 
00153 
00154 sensix::Request_ptr SiblingsImpl::queryNode(sensix::NodeId id,
00155                                             CORBA::Octet capability)
00156 {
00157   object_reference_t ref;
00158   int err = siblings_queryNode(&ref, id, capability);
00159   if (err < 0) {
00160     if (err == NO_CAPABILITY)
00161       throw DiscoveryException(NoCapability, "Sibling is not capable");
00162     else if (err == NO_NODE)
00163       throw DiscoveryException(NoNode, "Sibling does not exist");
00164   }
00165 
00166   return sensix::Request::_narrow(orb->string_to_object(ref.ior));
00167 }
00168 
00169 
00171 
00172 
00173 DescendantsImpl::DescendantsImpl(CORBA::ORB_ptr ptr) : Descendants()
00174 {
00175   orb = ptr;
00176 }
00177 
00178 
00179 DescendantsImpl::DescendantsImpl() : Descendants() {}
00180 
00181 
00182 CORBA::Boolean DescendantsImpl::findNode(sensix::NodeId id)
00183 {
00184   return (CORBA::Boolean)descendants_findNode(id);
00185 }
00186 
00187 
00188 NodeList *DescendantsImpl::nodes()
00189 {
00190   size_t size = 0;
00191   descendants_nodes(NULL, &size);
00192   uint32_t *nodes = new uint32_t[size];
00193   descendants_nodes(nodes, &size);
00194   NodeList *nlist = new NodeList(size);
00195   for (CORBA::ULong i = 0; i < nlist->length(); i++)
00196     (*nlist)[i] = nodes[i];
00197   delete nodes;
00198 
00199   return nlist;
00200 }
00201 
00202 
00203 CapabilityList *DescendantsImpl::capabilities()
00204 {
00205   size_t size = 0;
00206   descendants_capabilities(NULL, &size);
00207   uint8_t *caps = new uint8_t[size];
00208   descendants_capabilities(caps, &size);
00209   CapabilityList *clist = new CapabilityList(size);
00210   for (CORBA::ULong i = 0; i < clist->length(); i++)
00211     (*clist)[i] = caps[i];
00212   delete caps;
00213 
00214   return clist;
00215 }
00216 
00217 
00218 RequestList *DescendantsImpl::queryNetwork(CORBA::Octet capability)
00219 {
00220   int err = 0;
00221   size_t size = 0;
00222   descendants_queryNetwork(NULL, &size, capability);
00223   if (size == 0)
00224     return NULL;
00225 
00226   object_reference_t *refs = new object_reference_t[size];
00227   err = descendants_queryNetwork(refs, &size, capability);
00228   if (err < 0) {
00229     if (err == NO_CAPABILITY)
00230       throw DiscoveryException(NoCapability, "No capable descendant");
00231     else if (err == TOO_SMALL) {
00232       log_error("%s: Internal error at descendants_queryNetwork", APP);
00233       printStackTrace("Capability", __FILE__, __LINE__);
00234     }
00235   }
00236 
00237   RequestList *rlist = new RequestList(size);
00238   for (CORBA::ULong i = 0; i < rlist->length(); i++) {
00239     CORBA::Object_ptr obj = orb->string_to_object(refs[i].ior);
00240     (*rlist)[i] = sensix::Request::_narrow(obj);
00241   }
00242   delete refs;
00243   
00244 
00245   return rlist;
00246 }
00247 
00248 
00249 sensix::Request_ptr DescendantsImpl::queryNode(sensix::NodeId id,
00250                                                CORBA::Octet capability)
00251 {
00252   object_reference_t ref;
00253   int err = descendants_queryNode(&ref, id, capability);
00254   if (err < 0) {
00255     if (err == NO_CAPABILITY)
00256       throw DiscoveryException(NoCapability, "Descendant is not capable");
00257     else if (err == NO_NODE)
00258       throw DiscoveryException(NoNode, "Descendant does not exist");
00259   }
00260 
00261   return sensix::Request::_narrow(orb->string_to_object(ref.ior));
00262 }
00263 
00264 
00266 
00267 
00268 OthersImpl::OthersImpl(CORBA::ORB_ptr ptr) : Others()
00269 {
00270   orb = ptr;
00271   children = new DescendantsImpl(ptr);
00272   sisters = new SiblingsImpl(ptr);
00273   parents = new AncestorsImpl();
00274 }
00275 
00276 
00277 Ancestors_ptr OthersImpl::ancestors()
00278 {
00279   return (Ancestors_ptr)parents;
00280 }
00281 
00282 Siblings_ptr OthersImpl::siblings()
00283 {
00284   return (Siblings_ptr)sisters;
00285 }
00286 
00287 Descendants_ptr OthersImpl::descendants()
00288 {
00289   return (Descendants_ptr)children;
00290 }
00291 
00292 
00293 CORBA::Boolean OthersImpl::findNode(sensix::NodeId id)
00294 {
00295   return (CORBA::Boolean)family_findNode(id);
00296 }
00297 
00298 
00299 RequestList *OthersImpl::queryNetwork(CORBA::Octet capability)
00300 {
00301   int err = 0;
00302   size_t size = 0;
00303   family_queryNetwork(NULL, &size, capability);
00304   if (size == 0)
00305     return NULL;
00306 
00307   object_reference_t *refs = new object_reference_t[size];
00308   err = family_queryNetwork(refs, &size, capability);
00309   if (err < 0) {
00310     if (err == NO_CAPABILITY)
00311       throw DiscoveryException(NoCapability,
00312                                "No capable sibling or descendant");
00313     else if (err == TOO_SMALL) {
00314       log_error("%s: Internal error at family_queryNetwork", APP);
00315       printStackTrace("Capability", __FILE__, __LINE__);
00316     }
00317   }
00318 
00319   RequestList *rlist = new RequestList(size);
00320   for (CORBA::ULong i = 0; i < rlist->length(); i++) {
00321     CORBA::Object_ptr obj = orb->string_to_object(refs[i].ior);
00322     (*rlist)[i] = sensix::Request::_narrow(obj);
00323   }
00324   delete refs;
00325   
00326 
00327   return rlist;
00328 }
00329 
00330 
00331 sensix::Request_ptr OthersImpl::queryNode(sensix::NodeId id,
00332                                           CORBA::Octet capability)
00333 {
00334   object_reference_t ref;
00335   int err = family_queryNode(&ref, id, capability);
00336   if (err < 0) {
00337     if (err == NO_CAPABILITY)
00338       throw DiscoveryException(NoCapability, "Peer node is not capable");
00339     else if (err == NO_NODE)
00340       throw DiscoveryException(NoNode, "Peer node does not exist");
00341   }
00342 
00343   return sensix::Request::_narrow(orb->string_to_object(ref.ior));
00344 }
00345 
00346 
00348 
00349 
00350 DiscoveryService::DiscoveryService(CORBA::ORB_ptr ptr,
00351                                    uint32_t id, uint8_t lvl,
00352                                    int port) : Self()
00353 {
00354   discovery_args_t args;
00355   args.node_id = id;
00356   args.h_level = lvl;
00357   args.service_port = port;
00358 
00359   if (pthread_create(&ds, NULL, discovery_service, &args) != 0)
00360       throw DiscoveryException(NoNetwork, "Discovery service failed to start");
00361 
00362   orb = ptr;
00363   family = new OthersImpl(orb);
00364 }
00365 
00366 
00367 DiscoveryService::~DiscoveryService()
00368 {
00369   shutdown();
00370 }
00371 
00372 
00373 void DiscoveryService::shutdown()
00374 {
00375   pthread_mutex_lock(&ancestor_mutex);
00376   ancestors_running = 0;
00377   pthread_mutex_unlock(&ancestor_mutex);
00378 
00379   pthread_join(ds, NULL);
00380 }
00381 
00382 
00383 
00384 CORBA::Octet DiscoveryService::level()
00385 {
00386   return (CORBA::Octet)self_level();
00387 }
00388 
00389 
00390 sensix::NodeId DiscoveryService::identifier()
00391 {
00392   return (sensix::NodeId)self_identifier();
00393 }
00394 
00395 
00396 CapabilityList *DiscoveryService::capabilities()
00397 {
00398   size_t size = 0;
00399   self_capabilities(NULL, &size);
00400   uint8_t *caps = new uint8_t[size];
00401   self_capabilities(caps, &size);
00402   CapabilityList *clist = new CapabilityList(size);
00403   for (CORBA::ULong i = 0; i < clist->length(); i++)
00404     (*clist)[i] = caps[i];
00405   delete caps;
00406 
00407   return clist;
00408 }
00409 
00410 
00411 void DiscoveryService::registerObject(CORBA::Octet capability,
00412                                       sensix::Request_var obj)
00413 {
00414   int err = 0;
00415   object_reference_t ref;
00416   char* objref = orb->object_to_string(obj.in());
00417 
00418   ref.type = capability;
00419   strncpy(ref.ior, objref, OBJ_REF_SIZE);
00420   ref.valid = 1;
00421 
00422   err = self_register_object((uint8_t)capability, &ref, 0);
00423   if (err == REGISTRY_FULL)
00424     throw new DiscoveryException(RegistryFull, "");
00425   if (err == ALREADY_REGISTERED)
00426     throw new DiscoveryException(AlreadyRegistered, "");
00427 }
00428 
00429 
00430 CORBA::Boolean DiscoveryService::findNodeInFamily(sensix::NodeId id) {
00431   return family->findNode(id);
00432 }
00433 
00434 RequestList *DiscoveryService::queryFamilyNetwork(CORBA::Octet capability) {
00435   return family->queryNetwork(capability);
00436 }
00437 
00438 sensix::Request_ptr DiscoveryService::queryFamilyNode(sensix::NodeId id,
00439                                                       CORBA::Octet capability) {
00440   return family->queryNode(id, capability);
00441 }
00442 
00443 
00444 CORBA::Boolean DiscoveryService::findNodeInDescendants(sensix::NodeId id) {
00445   return family->descendants()->findNode(id);
00446 }
00447 
00448 RequestList *DiscoveryService::queryDescendantNetwork(CORBA::Octet capability) {
00449   return family->descendants()->queryNetwork(capability);
00450 }
00451 
00452 sensix::Request_ptr DiscoveryService::queryDescendantNode(sensix::NodeId id,
00453                                                       CORBA::Octet capability) {
00454   return family->descendants()->queryNode(id, capability);
00455 }
00456 
00457 CapabilityList *DiscoveryService::descendantCapabilities() {
00458   return family->descendants()->capabilities();
00459 }
00460 
00461 NodeList *DiscoveryService::descendantNodes() {
00462   return family->descendants()->nodes();
00463 }
00464 
00465 
00466 CORBA::Boolean DiscoveryService::findNodeInSiblings(sensix::NodeId id) {
00467   return family->siblings()->findNode(id);
00468 }
00469 
00470 RequestList *DiscoveryService::querySiblingNetwork(CORBA::Octet capability) {
00471   return family->siblings()->queryNetwork(capability);
00472 }
00473 
00474 sensix::Request_ptr DiscoveryService::querySiblingNode(sensix::NodeId id,
00475                                                       CORBA::Octet capability) {
00476   return family->siblings()->queryNode(id, capability);
00477 }
00478 
00479 CapabilityList *DiscoveryService::siblingCapabilities() {
00480   return family->siblings()->capabilities();
00481 }
00482 
00483 NodeList *DiscoveryService::siblingNodes() {
00484   return family->siblings()->nodes();
00485 }
00486 
00487 
00488 CORBA::Boolean DiscoveryService::findNodeInAncestors(sensix::NodeId id) {
00489   return family->ancestors()->findNode(id);
00490 }
00491 
00492 NodeList *DiscoveryService::ancestorNodes() {
00493   return family->ancestors()->nodes();
00494 }
00495 
00496 
00497 
00498 
00500 
00501 
00502 
00503 
00504 
00505 
00506 
00507 
00508 
00509 
00510 
00511 
00512 
00513 
00514 
00515 
00516 
00517 
00518 
00519 
00520 
00521 
00522 
00523 
00524 
00525 
00526 
00527 
00528 
00529 
00530 
00531 
00532 
00533