Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | Related Pages | Examples

ColCOWS_Node.cc

00001 //===========================================================================//
00002 //    
00003 //    Description: ColCOWS Nodes
00004 //    Filename: colcows/ColCOWS_Node.cc
00005 //    Authors: Michael Dussere and Aurélien Esnard
00006 //    
00007 //===========================================================================//
00008 //    
00009 //    Copyright (C) 2003 INRIA and CNRS
00010 //    
00011 //    This library is free software; you can redistribute it and/or
00012 //    modify it under the terms of the GNU Lesser General Public
00013 //    License as published by the Free Software Foundation; either
00014 //    version 2.1 of the License, or (at your option) any later version.
00015 //    
00016 //    This library is distributed in the hope that it will be useful,
00017 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019 //    Lesser General Public License for more details.
00020 //    
00021 //    You should have received a copy of the GNU Lesser General Public
00022 //    License along with this library; if not, write to the Free Software
00023 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00024 //    
00025 //===========================================================================//
00026 
00027 
00028 #include "ColCOWS_Node.hh"
00029 #include "ColCOWS_Corba.hh"
00030 #include "ColCOWS_EventHandlerManager.hh" 
00031 #include "ColCOWS_EventHandler.hh" 
00032 #include "ColCOWS_NodeServant.hh" 
00033 
00034 using namespace std;
00035 
00036 __COLCOWS_BEGIN__
00037 
00038 //===========================================================================//
00039 
00040 unsigned long Node::calculNumFather(unsigned long min, unsigned long max, unsigned long num_child, unsigned long base) 
00041 {
00042   COLCOWS_IN_DEBUG();
00043 
00044   COLCOWS_DEBUG( dblAccessory,"min : " << min );
00045   COLCOWS_DEBUG( dblAccessory,"max : " << max );
00046   COLCOWS_DEBUG( dblAccessory,"num_child : " << num_child );
00047   COLCOWS_DEBUG( dblAccessory,"base : " << base );
00048   
00049   //  min is either num_child, his father or one of its ancestors
00050   //  max is the max descendant of min
00051   //  base is the max number of branches of one node of the tree
00052   
00053   
00054   if ((num_child <=  min) || (num_child > max))
00055     return 0;
00056   
00057   
00058   unsigned long nb_descendants = max - min;
00059   
00060   if ( nb_descendants <= base )
00061     return min;
00062   
00063   unsigned long sub_min = 0;
00064   unsigned long sub_max = min;
00065   
00066   for (unsigned long i = 0; i < base; i++ ){
00067     sub_min = sub_max + 1;
00068     
00069     if (i < nb_descendants%base )
00070       sub_max += nb_descendants/base + 1;
00071     else
00072       sub_max += nb_descendants/base;
00073     
00074     if (num_child == sub_min)
00075       return min;
00076     
00077     else if ((num_child > sub_min)&&(num_child <= sub_max))
00078       return calculNumFather(sub_min, sub_max, num_child, base);
00079   }
00080   
00081   COLCOWS_OUT_DEBUG();  
00082   return 0;
00083   
00084 }
00085 
00086 //===========================================================================//
00087 
00088 unsigned long Node::calculMaxDescend(unsigned long min, unsigned long max, unsigned long num_child, unsigned long base) 
00089 {
00090   COLCOWS_IN_DEBUG();
00091 
00092   COLCOWS_DEBUG( dblAccessory,"min : " << min );
00093   COLCOWS_DEBUG( dblAccessory,"max : " << max );
00094   COLCOWS_DEBUG( dblAccessory,"num_child : " << num_child );
00095   COLCOWS_DEBUG( dblAccessory,"base : " << base );
00096   
00097   //  min is either num_child, his father or one of its ancestors
00098   //  max is the max descendant of min
00099   //  base is the max number of branches of one node of the tree
00100   
00101   if ((num_child <  min) || (num_child > max))
00102     return 0;
00103   
00104   if (num_child ==  min)
00105     return max;
00106   
00107   unsigned long nb_descendants = max - min;
00108   if ( nb_descendants <= base )
00109     return num_child;
00110   
00111   unsigned long sub_min = 0;
00112   unsigned long sub_max = min;
00113   
00114   for (unsigned long i = 0; i < base; i++ ){
00115     sub_min = sub_max + 1;
00116     
00117     if (i < nb_descendants%base )
00118       sub_max += nb_descendants/base + 1;
00119     else
00120       sub_max += nb_descendants/base;
00121     
00122     if (num_child == sub_min)
00123       return sub_max;
00124     else if ((num_child > sub_min)&&(num_child <= sub_max))
00125       return calculMaxDescend(sub_min, sub_max, num_child, base);
00126     
00127   }
00128   
00129   COLCOWS_OUT_DEBUG();  
00130   return 0;
00131   
00132 }
00133 
00134 //===========================================================================//
00135 //                       collective node class                               //
00136 //===========================================================================//
00137 
00138 
00139 //===========================================================================//
00140 
00141 Node::Node()
00142 {
00143   COLCOWS_IN_DEBUG();
00144   
00145   _event_handler_manager  = new EventHandlerManager( );
00146   _event_handler_manager->setColCOWSNode(this);
00147 
00148   pthread_mutex_init(&_event_mut, NULL);
00149   pthread_cond_init (&_event_cond, NULL);
00150 
00151   _local_workspace = new WorkspaceDesc();
00152   _remote_workspace_manager = new RemoteWorkspaceManager();
00153 
00154   _fully_activated = false;
00155 
00156   _node_servant   = NULL;
00157   
00158 }
00159 
00160 //===========================================================================//
00161 
00162 Node::~Node()
00163 {
00164   COLCOWS_IN_DEBUG();
00165   
00166   delete _event_handler_manager;
00167   delete _local_workspace;
00168   delete _remote_workspace_manager;
00169 
00170   pthread_mutex_destroy(&_event_mut);
00171   pthread_cond_destroy(&_event_cond);
00172 
00173   if ( _node_servant != NULL ) {
00174     COLCOWS_DEBUG( dblTest, "delete _node_servant" );
00175     delete _node_servant;
00176   }
00177   
00178 }
00179 
00180 //===========================================================================//
00181 
00182 Node * Node::New(std::string ws_id, 
00183                  std::string ws_kind, 
00184                  std::string node_kind, 
00185                  unsigned long num_node, 
00186                  unsigned long max_descendant, 
00187                  unsigned long nb_nodes,
00188                  unsigned long num_father,
00189                  CORBA::ORB * orbp ) 
00190 {
00191   COLCOWS_IN_DEBUG();
00192   
00193   Node * new_object = NULL;
00194 
00195 __BEGIN_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00196     
00197   NodeServant * new_node_servant = NULL;
00198   
00199     new_object = new Node;
00200     new_object->_orbp = orbp;
00201 
00202     new_object->getLocalWorkspace()->setId(ws_id);
00203     new_object->getLocalWorkspace()->setKind(ws_kind);
00204     new_object->getLocalWorkspace()->setNbNodes(nb_nodes);
00205     new_object->getLocalWorkspace()->setActivated(false);
00206 
00207     new_object->setKind(node_kind);
00208     new_object->setNumNode(num_node);
00209     new_object->setMaxDescendant(max_descendant);
00210     new_object->setActivated(false);
00211 
00212     new_node_servant = NodeServant::New(ws_id.c_str(),  
00213                                         ws_kind.c_str(), 
00214                                         node_kind.c_str(), 
00215                                         num_node, 
00216                                         max_descendant, 
00217                                         nb_nodes,
00218                                         num_father,
00219                                         orbp );
00220     
00221     // set NodeServant pointer in Node members
00222     new_object->_node_servant = new_node_servant;
00223     
00224     // set Node pointer in NodeServant members
00225     new_node_servant->_colcows_node = new_object;
00226     
00227 __END_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00228   
00229   COLCOWS_OUT_DEBUG();  
00230   return new_object;
00231 }
00232 
00233 //===========================================================================//
00234 
00235 Node * Node::NewHierachical(std::string ws_id,
00236                             std::string ws_kind, 
00237                             unsigned long num_node, 
00238                             unsigned long nb_nodes,
00239                             unsigned long base,
00240                             CORBA::ORB * orbp )  
00241 {
00242   COLCOWS_IN_DEBUG();
00243   
00244   Node * new_object = NULL;
00245 
00246 __BEGIN_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00247     
00248   NodeServant * new_node_servant = NULL;
00249   
00250     new_object = new Node;
00251     new_object->_orbp = orbp;
00252 
00253     new_object->getLocalWorkspace()->setId(ws_id);
00254     new_object->getLocalWorkspace()->setKind(ws_kind);
00255     new_object->getLocalWorkspace()->setNbNodes(nb_nodes);
00256     new_object->getLocalWorkspace()->setActivated(false);
00257 
00258     new_object->setKind("Hierachical");
00259     new_object->setNumNode(num_node);
00260     new_object->setMaxDescendant(calculMaxDescend(0, nb_nodes-1, num_node, base));
00261     new_object->setActivated(false);
00262 
00263     new_node_servant = NodeServant::New(ws_id.c_str(), 
00264                                         ws_kind.c_str(), 
00265                                         "Hierachical",
00266                                         num_node,
00267                                         calculMaxDescend(0, nb_nodes-1, num_node, base),
00268                                         nb_nodes, 
00269                                         calculNumFather(0, nb_nodes-1, num_node, base),
00270                                         orbp );
00271     
00272     // set NodeServant pointer
00273     new_object->_node_servant = new_node_servant;
00274     
00275     // set Node pointer
00276     new_node_servant->_colcows_node = new_object;
00277     
00278 __END_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__   
00279 
00280   COLCOWS_OUT_DEBUG();  
00281   return new_object;
00282   
00283 }
00284 
00285 //===========================================================================//
00286 
00287 Node * Node::NewProxy(std::string ws_id,
00288                       std::string ws_kind, 
00289                       unsigned long nb_nodes,
00290                       CORBA::ORB * orbp )  
00291 {
00292   COLCOWS_IN_DEBUG();
00293   
00294   Node * new_object = NULL;
00295 
00296 __BEGIN_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00297 
00298   NodeServant * new_node_servant = NULL;
00299       
00300     new_object = new Node;
00301     new_object->_orbp = orbp;
00302 
00303     new_object->getLocalWorkspace()->setId(ws_id);
00304     new_object->getLocalWorkspace()->setKind(ws_kind);
00305     new_object->getLocalWorkspace()->setNbNodes(nb_nodes+1);
00306     new_object->getLocalWorkspace()->setActivated(false);
00307 
00308     new_object->setKind("Proxy");
00309     new_object->setNumNode(0);
00310     new_object->setMaxDescendant(nb_nodes);
00311     new_object->setActivated(false);
00312 
00313     new_node_servant = NodeServant::New(ws_id.c_str(), 
00314                                         ws_kind.c_str(), 
00315                                         "Proxy",
00316                                         0,
00317                                         nb_nodes,
00318                                         nb_nodes+1, 
00319                                         0,
00320                                         orbp );
00321     
00322     // set NodeServant pointer
00323     new_object->_node_servant = new_node_servant;
00324     
00325     // set Node pointer
00326     new_node_servant->_colcows_node = new_object;
00327     
00328 __END_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__   
00329 
00330   COLCOWS_OUT_DEBUG();  
00331   return new_object;
00332   
00333 }
00334 
00335 
00336 //===========================================================================//
00337 
00338 Node * Node::NewPort(std::string ws_id,
00339                      std::string ws_kind, 
00340                      unsigned long num_node, 
00341                      unsigned long nb_nodes,
00342                      CORBA::ORB * orbp)  
00343 {
00344   COLCOWS_IN_DEBUG();
00345   
00346   Node * new_object = NULL;
00347 
00348 __BEGIN_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00349     
00350   NodeServant * new_node_servant = NULL;
00351   
00352     new_object = new Node;
00353     new_object->_orbp = orbp;
00354     
00355     new_object->getLocalWorkspace()->setId(ws_id);
00356     new_object->getLocalWorkspace()->setKind(ws_kind);
00357     new_object->getLocalWorkspace()->setNbNodes(nb_nodes+1);
00358     new_object->getLocalWorkspace()->setActivated(false);
00359 
00360     new_object->setKind("Node");
00361     new_object->setNumNode(num_node+1);
00362     new_object->setMaxDescendant(num_node + 1);
00363     new_object->setActivated(false);
00364 
00365     new_node_servant = NodeServant::New(ws_id.c_str(), 
00366                                         ws_kind.c_str(),
00367                                         "Node",
00368                                         num_node + 1,
00369                                         num_node + 1,
00370                                         nb_nodes + 1, 
00371                                         0,
00372                                         orbp );
00373     
00374     // set NodeServant pointer
00375     new_object->_node_servant = new_node_servant;
00376     
00377     // set Node pointer
00378     new_node_servant->_colcows_node = new_object;
00379     
00380 __END_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__   
00381 
00382   COLCOWS_OUT_DEBUG();  
00383   return new_object;
00384   
00385 }
00386 
00387 //===========================================================================//
00388 
00389 Node * Node::NewNode(std::string ws_id,
00390                      std::string ws_kind, 
00391                      std::string node_kind, 
00392                      unsigned long num_node, 
00393                      unsigned long nb_nodes,
00394                      CORBA::ORB * orbp)  
00395 {
00396   COLCOWS_IN_DEBUG();
00397   
00398   Node * new_object = NULL;
00399 
00400 __BEGIN_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00401     
00402   NodeServant * new_node_servant = NULL;
00403   
00404     new_object = new Node;
00405     new_object->_orbp = orbp;
00406 
00407     new_object->getLocalWorkspace()->setId(ws_id);
00408     new_object->getLocalWorkspace()->setKind(ws_kind);
00409     new_object->getLocalWorkspace()->setNbNodes(nb_nodes);
00410     new_object->getLocalWorkspace()->setActivated(false);
00411 
00412     new_object->setKind(node_kind);
00413     new_object->setNumNode(num_node);
00414     new_object->setActivated(false);
00415 
00416     if (num_node == 0) {
00417       new_node_servant = NodeServant::New(ws_id.c_str(), 
00418                                           ws_kind.c_str(),
00419                                           node_kind.c_str(),
00420                                           num_node,
00421                                           nb_nodes - 1,
00422                                           nb_nodes, 
00423                                           0,
00424                                           orbp );
00425       new_object->setMaxDescendant(nb_nodes - 1);
00426     }
00427     else {
00428       new_node_servant = NodeServant::New(ws_id.c_str(), 
00429                                           ws_kind.c_str(),
00430                                           node_kind.c_str(),
00431                                           num_node,
00432                                           num_node,
00433                                           nb_nodes, 
00434                                           0,
00435                                           orbp );      
00436       new_object->setMaxDescendant(num_node);
00437     }
00438 
00439     // set NodeServant pointer
00440     new_object->_node_servant = new_node_servant;
00441     
00442     // set Node pointer
00443     new_node_servant->_colcows_node = new_object;
00444     
00445 __END_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__ 
00446   
00447   COLCOWS_OUT_DEBUG();  
00448   return new_object;
00449   
00450 }
00451 
00452 //===========================================================================//
00453 
00454 unsigned long Node::connect( const std::string & ws_id, const std::string & ws_kind, const std::string & nameservice_str) 
00455 {
00456   COLCOWS_IN_DEBUG();
00457   unsigned long is_connected;
00458 
00459 __BEGIN_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00460 
00461   is_connected = _node_servant->connect( ws_id , ws_kind, nameservice_str );
00462 
00463 __END_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__ 
00464   
00465   COLCOWS_OUT_DEBUG();  
00466   return is_connected;
00467 }
00468 
00469 //===========================================================================//
00470 
00471 //! disconnect a remote workspace
00472 void Node::disconnect( unsigned long num_connection ) 
00473 {
00474   COLCOWS_IN_DEBUG();
00475   
00476   COLCOWS_ASSERT( isConnected(num_connection) ,"No connection with num " << num_connection);
00477   
00478 __BEGIN_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00479 
00480   _node_servant->disconnect( num_connection );
00481 
00482 __END_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__ 
00483   
00484   COLCOWS_OUT_DEBUG();  
00485   return ;
00486   
00487 }
00488 
00489 //===========================================================================//
00490 
00491 //! disconnect a remote workspace
00492 void Node::disconnectAll( ) 
00493 {
00494   COLCOWS_IN_DEBUG();
00495  
00496 __BEGIN_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00497 
00498   _node_servant->disconnectAll( );
00499   
00500 __END_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00501 
00502   COLCOWS_OUT_DEBUG();  
00503   return ;
00504   
00505 }
00506 
00507 //===========================================================================//
00508 
00509 unsigned long Node::getRemoteNbNodes( unsigned long num_connection, std::string node_kind )
00510 {
00511   COLCOWS_IN_DEBUG();
00512 
00513   unsigned long nb_nodes = 0;
00514 
00515 __BEGIN_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00516 
00517   if ( _remote_workspace_manager->find(num_connection) ==  _remote_workspace_manager->end() ) {
00518     COLCOWS_DEBUG( dblWarning,"No connection with num " << num_connection );
00519   }
00520   else if ( node_kind.empty() ) {
00521     nb_nodes = (*_remote_workspace_manager)[num_connection]->getNbNodes( );        
00522   }
00523   else {
00524     nb_nodes = (*_remote_workspace_manager)[num_connection]->getNbNodes( node_kind );    
00525   }
00526 
00527 __END_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00528 
00529   COLCOWS_OUT_DEBUG();  
00530   return nb_nodes;
00531 }
00532 
00533 //===========================================================================//
00534 
00535 string Node::getRemoteWorkspaceID( unsigned long num_connection )
00536 {
00537   COLCOWS_IN_DEBUG();
00538 
00539   string remote_id;
00540 
00541 __BEGIN_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00542   
00543   if ( _remote_workspace_manager->find(num_connection) ==  _remote_workspace_manager->end() ) {
00544     COLCOWS_DEBUG( dblWarning,"No connection with num " << num_connection );
00545   }
00546   else{
00547     remote_id = (*_remote_workspace_manager)[num_connection]->getId();
00548   }
00549 
00550 __END_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00551  
00552   COLCOWS_OUT_DEBUG();  
00553   return remote_id;    
00554 }
00555 
00556 //===========================================================================//
00557 
00558 string Node::getRemoteWorkspaceKind( unsigned long num_connection )
00559 {
00560   COLCOWS_IN_DEBUG();
00561   
00562   string remote_kind;
00563 
00564 __BEGIN_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00565 
00566   if ( _remote_workspace_manager->find(num_connection) ==  _remote_workspace_manager->end() ) {
00567     COLCOWS_DEBUG( dblWarning,"No connection with num " << num_connection );
00568   }
00569   else{
00570     remote_kind = (*_remote_workspace_manager)[num_connection]->getKind();
00571   }
00572 
00573 __END_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00574 
00575   COLCOWS_OUT_DEBUG();  
00576   return remote_kind;    
00577 }
00578 
00579 //===========================================================================//
00580 
00581 unsigned long Node::getNumConnection( std::string ws_id , std::string ws_kind ) 
00582 {
00583   COLCOWS_IN_DEBUG();
00584 
00585   unsigned long num_connection = NOT_CONNECTED;
00586 
00587 __BEGIN_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00588 
00589   RemoteWorkspaceManager::const_iterator it, it_begin, it_end;
00590   it_begin = _remote_workspace_manager->begin();
00591   it_end = _remote_workspace_manager->end();
00592 
00593   for (it = it_begin; it != it_end; ++ it) {
00594     if ( ( it->second->getId() == ws_id ) && 
00595          ( it->second->getKind() == ws_kind ) ) {
00596       num_connection = it->second->getNumConnection();
00597       break;
00598     }
00599   }
00600   
00601 __END_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00602 
00603   COLCOWS_OUT_DEBUG();  
00604   return num_connection;  
00605 }
00606 
00607 //===========================================================================//
00608 
00609 void Node::getNumConnectionVec(std::vector<unsigned long> & num_connection_vec ,
00610                                std::string ws_id , 
00611                                std::string ws_kind ) 
00612 {
00613   COLCOWS_IN_DEBUG();
00614   
00615 __BEGIN_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00616 
00617   num_connection_vec.clear();
00618 
00619   RemoteWorkspaceManager::const_iterator it, it_begin, it_end;
00620   it_begin = _remote_workspace_manager->begin();
00621   it_end = _remote_workspace_manager->end();
00622 
00623   for (it = it_begin; it != it_end; ++ it) {
00624     if ( ( it->second->getId() == ws_id ) && 
00625          ( it->second->getKind() == ws_kind ) ) {
00626       num_connection_vec.push_back( it->second->getNumConnection() );
00627       break;
00628     }
00629   }
00630   
00631 __END_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00632 
00633   COLCOWS_OUT_DEBUG();  
00634   return;  
00635 }
00636 
00637 //===========================================================================//
00638 
00639 void Node::activate()
00640   
00641 {
00642   COLCOWS_IN_DEBUG();
00643   
00644 __BEGIN_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00645 
00646   _node_servant->activate();
00647   setActivated(true);
00648 
00649 __END_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00650   
00651   COLCOWS_OUT_DEBUG();
00652   return;
00653 }
00654 
00655 //===========================================================================//
00656 
00657 void Node::deactivate()
00658   
00659 {
00660   COLCOWS_IN_DEBUG();
00661   
00662 __BEGIN_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00663 
00664   setActivated(false);
00665   _node_servant->deactivate();
00666 
00667 __END_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00668 
00669   COLCOWS_OUT_DEBUG();
00670   return;
00671 }
00672 
00673 //===========================================================================//
00674 
00675 void Node::onActivation( const IdlWorkspaceDesc& ws_desc ) 
00676 {
00677   COLCOWS_IN_DEBUG();
00678   
00679 __BEGIN_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00680     
00681   COLCOWS_DEBUG(dblAccessory, "Activation of node " << _num_node << "(" << _node_kind << ")");
00682   
00683   pthread_mutex_lock(&_event_mut);
00684 
00685   _local_workspace->convertFromIdl( ws_desc );
00686 
00687   _event_handler_manager->onActivation( );
00688   
00689   _fully_activated = true;
00690   
00691   pthread_cond_broadcast(&_event_cond);
00692   pthread_mutex_unlock(&_event_mut);
00693     
00694 __END_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00695   
00696   COLCOWS_OUT_DEBUG();
00697   return;
00698 }
00699 
00700 
00701 //===========================================================================//
00702 
00703 void Node::onDeactivation( const IdlWorkspaceDesc& ws_desc )  
00704 {
00705   COLCOWS_IN_DEBUG();
00706   
00707 __BEGIN_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00708 
00709     pthread_mutex_lock(&_event_mut);
00710 
00711     _local_workspace->convertFromIdl( ws_desc );
00712 
00713     _event_handler_manager->onDeactivation( );
00714     
00715     _fully_activated = false;
00716 
00717     pthread_cond_broadcast(&_event_cond);
00718     pthread_mutex_unlock(&_event_mut);
00719     
00720 __END_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00721   
00722   COLCOWS_OUT_DEBUG();
00723   return;
00724 }
00725 
00726 //===========================================================================//
00727 
00728 void Node::onConnection( unsigned long num_connection, const IdlWorkspaceDesc& idl_ws_desc, bool initiator )  
00729 {
00730   COLCOWS_IN_DEBUG();
00731   
00732 __BEGIN_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00733 
00734     WorkspaceDesc * ws_desc = new WorkspaceDesc(idl_ws_desc);
00735     ws_desc->setNumConnection( num_connection );
00736 
00737     pthread_mutex_lock(&_event_mut);
00738     _remote_workspace_manager->addRemoteWorkspace( ws_desc );
00739 
00740     _event_handler_manager->onConnection( num_connection, initiator );
00741 
00742     pthread_cond_broadcast(&_event_cond);
00743     pthread_mutex_unlock(&_event_mut);
00744     
00745 __END_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00746   
00747   COLCOWS_OUT_DEBUG();
00748   return;
00749 }
00750 
00751 //===========================================================================//
00752 
00753 void Node::onDisconnection( unsigned long num_connection )  
00754 {
00755   COLCOWS_IN_DEBUG();
00756   
00757 __BEGIN_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00758 
00759     pthread_mutex_lock(&_event_mut);
00760     _remote_workspace_manager->removeRemoteWorkspace( num_connection );
00761 
00762     _event_handler_manager->onDisconnection( num_connection );
00763     
00764     pthread_cond_broadcast(&_event_cond);
00765     pthread_mutex_unlock(&_event_mut);
00766     
00767 __END_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00768 
00769   COLCOWS_OUT_DEBUG();
00770   return;
00771 }
00772 
00773 
00774 //===========================================================================//
00775 
00776 bool Node::waitActivation( bool use_timeout )  
00777 {
00778   COLCOWS_IN_DEBUG();
00779   
00780 __BEGIN_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00781 
00782   pthread_mutex_lock(&_event_mut);
00783 
00784   int keep_trying = WAITING_RETRY;
00785 
00786   while( !_fully_activated && keep_trying) {
00787     COLCOWS_DEBUG( dblAccessory, "Waiting for activation of node " << _num_node << "(" << _node_kind << ")" );
00788 
00789     if (use_timeout)
00790       {
00791         struct timeval now;
00792         struct timespec timeout;
00793         gettimeofday(&now,NULL);
00794         timeout.tv_sec = now.tv_sec + WAITING_TIMEOUT;
00795         timeout.tv_nsec = now.tv_usec * 1000;
00796         if(pthread_cond_timedwait(&_event_cond, &_event_mut, &timeout) == ETIMEDOUT) {
00797           --keep_trying;
00798           COLCOWS_DEBUG( dblAccessory, "activation timeout, nb retry = " << keep_trying );
00799         }
00800       }
00801     else 
00802       {
00803         pthread_cond_wait(&_event_cond, &_event_mut );
00804         keep_trying = 1;
00805       }
00806       
00807   }
00808   
00809   pthread_mutex_unlock(&_event_mut);
00810 
00811 __END_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00812 
00813   COLCOWS_OUT_DEBUG();
00814   return _fully_activated;
00815 }
00816 
00817 //===========================================================================//
00818 
00819 bool Node::waitDeactivation( bool use_timeout )  
00820 {
00821   COLCOWS_IN_DEBUG();
00822  
00823 __BEGIN_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00824 
00825   pthread_mutex_lock(&_event_mut);
00826 
00827   int keep_trying = WAITING_RETRY;
00828   while( _fully_activated && keep_trying) {
00829     COLCOWS_DEBUG( dblAccessory, "Waiting for deactivation." );
00830     
00831     if (use_timeout)
00832       {
00833         struct timeval now;
00834         struct timespec timeout;
00835         gettimeofday(&now,NULL);
00836         timeout.tv_sec = now.tv_sec + WAITING_TIMEOUT;
00837         timeout.tv_nsec = now.tv_usec * 1000;
00838         if(pthread_cond_timedwait(&_event_cond, &_event_mut, &timeout) == ETIMEDOUT) {
00839           --keep_trying;
00840           COLCOWS_DEBUG( dblAccessory, "deactivation timeout, nb retry = " << keep_trying );
00841         }
00842       }
00843     else 
00844       {
00845         pthread_cond_wait(&_event_cond, &_event_mut );
00846         keep_trying = 1;
00847       }
00848     
00849   }
00850   
00851   pthread_mutex_unlock(&_event_mut);
00852 
00853 __END_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00854 
00855   COLCOWS_OUT_DEBUG();
00856   return !_fully_activated;
00857 }
00858 
00859 //===========================================================================//
00860 
00861 unsigned long Node::waitConnection( std::string ws_id, std::string ws_kind, bool use_timeout ) 
00862 {
00863   COLCOWS_IN_DEBUG();
00864 
00865   unsigned long  is_connected;  
00866 
00867 __BEGIN_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00868 
00869   pthread_mutex_lock(&_event_mut);
00870 
00871   is_connected = isConnected(ws_id, ws_kind);
00872   
00873   int keep_trying = WAITING_RETRY;
00874   while(( is_connected == NOT_CONNECTED) && keep_trying) {
00875     COLCOWS_DEBUG( dblAccessory, "Waiting for connection." );
00876     
00877     if (use_timeout)
00878       {
00879         struct timeval now;
00880         struct timespec timeout;
00881         gettimeofday(&now,NULL);
00882         timeout.tv_sec = now.tv_sec + WAITING_TIMEOUT;
00883         timeout.tv_nsec = now.tv_usec * 1000;
00884         if(pthread_cond_timedwait(&_event_cond, &_event_mut, &timeout) == ETIMEDOUT) {
00885           --keep_trying;
00886           COLCOWS_DEBUG( dblAccessory, "connection timeout, nb retry = " << keep_trying );
00887         }
00888       }
00889     else 
00890       {
00891         pthread_cond_wait(&_event_cond, &_event_mut );
00892         keep_trying = 1;
00893       }
00894     
00895     is_connected = isConnected(ws_id, ws_kind);
00896   }
00897   
00898   pthread_mutex_unlock(&_event_mut);
00899 
00900 __END_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00901 
00902   COLCOWS_OUT_DEBUG();
00903   return is_connected;
00904 }
00905 
00906 //===========================================================================//
00907 
00908 unsigned long Node::waitAllDisconnection( bool use_timeout ) 
00909 {
00910   COLCOWS_IN_DEBUG();
00911 
00912 __BEGIN_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00913 
00914   pthread_mutex_lock(&_event_mut);
00915 
00916   int keep_trying = WAITING_RETRY;
00917   while( (!_remote_workspace_manager->empty()) && keep_trying) {
00918     COLCOWS_DEBUG( dblAccessory, "Waiting for all disconnection." );
00919     
00920     if (use_timeout)
00921       {
00922         struct timeval now;
00923         struct timespec timeout;
00924         gettimeofday(&now,NULL);
00925         timeout.tv_sec = now.tv_sec + WAITING_TIMEOUT;
00926         timeout.tv_nsec = now.tv_usec * 1000;
00927         if(pthread_cond_timedwait(&_event_cond, &_event_mut, &timeout) == ETIMEDOUT) {
00928           --keep_trying;
00929           COLCOWS_DEBUG( dblAccessory, "all disconnection timeout, nb retry = " << keep_trying );
00930         }
00931       }
00932     else 
00933       {
00934         pthread_cond_wait(&_event_cond, &_event_mut );
00935         keep_trying = 1;
00936       }
00937     
00938   }
00939   
00940   pthread_mutex_unlock(&_event_mut);
00941 
00942 __END_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00943 
00944   COLCOWS_OUT_DEBUG();
00945   return _remote_workspace_manager->empty();
00946 }
00947 
00948 //===========================================================================//
00949 
00950 unsigned long Node::waitDisconnection( std::string ws_id, std::string ws_kind, bool use_timeout )
00951 {
00952   COLCOWS_IN_DEBUG();
00953   
00954   unsigned long  is_connected;
00955 
00956 __BEGIN_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00957 
00958   pthread_mutex_lock(&_event_mut);
00959 
00960   is_connected = isConnected(ws_id, ws_kind);
00961     
00962   int keep_trying = WAITING_RETRY;
00963   while(( is_connected != NOT_CONNECTED ) && keep_trying) {
00964     COLCOWS_DEBUG( dblAccessory, "Waiting for connection." );      
00965     
00966     if (use_timeout)
00967       {
00968         struct timeval now;
00969         struct timespec timeout;
00970         gettimeofday(&now,NULL);
00971         timeout.tv_sec = now.tv_sec + WAITING_TIMEOUT;
00972         timeout.tv_nsec = now.tv_usec * 1000;
00973         if(pthread_cond_timedwait(&_event_cond, &_event_mut, &timeout) == ETIMEDOUT) {
00974           --keep_trying;
00975           COLCOWS_DEBUG( dblAccessory, "disconnection timeout, nb retry = " << keep_trying );
00976         }
00977       }
00978     else 
00979       {
00980         pthread_cond_wait(&_event_cond, &_event_mut );
00981         keep_trying = 1;
00982       }
00983       
00984     is_connected = isConnected(ws_id, ws_kind);
00985   }
00986   
00987   pthread_mutex_unlock(&_event_mut);
00988 
00989 __END_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
00990 
00991   COLCOWS_OUT_DEBUG();
00992   return !is_connected;
00993 }
00994 
00995 //===========================================================================//
00996 
00997 bool Node::isConnected(std::string ws_id, std::string ws_kind) const
00998 {
00999   COLCOWS_IN_DEBUG();
01000 
01001   bool is_connected = false;
01002 
01003 __BEGIN_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
01004 
01005   RemoteWorkspaceManager::const_iterator it, it_begin, it_end;
01006   it_begin = _remote_workspace_manager->begin();
01007   it_end = _remote_workspace_manager->end();
01008 
01009   for (it = it_begin; it != it_end; ++ it) {
01010     if ( ( it->second->getId() == ws_id ) && 
01011          ( it->second->getKind() == ws_kind ) ) {
01012       is_connected = true;
01013       break;
01014     }
01015   }
01016   
01017 __END_REDIRECT_ALL_EXCEPTIONS_TO_COLCOWS__
01018 
01019   COLCOWS_OUT_DEBUG();  
01020   return is_connected;  
01021 }
01022 
01023 //===========================================================================//
01024 
01025 void Node::setEventHandlerManager(EventHandlerManager * event_handler_manager) 
01026 {
01027   COLCOWS_IN_DEBUG();  
01028 
01029   event_handler_manager->_colcows_node = this;
01030   _event_handler_manager = event_handler_manager;   
01031 
01032   COLCOWS_OUT_DEBUG();  
01033   return;  
01034 }
01035 
01036 //===========================================================================//
01037 
01038 unsigned int Node::addEventHandler( EventHandler * event_handler ) 
01039 {
01040   COLCOWS_ASSERT((_event_handler_manager != NULL), "Event handler manager is null");
01041   return _event_handler_manager->addEventHandler( event_handler );
01042 }
01043 
01044 //===========================================================================//
01045 
01046 void Node::removeEventHandler( unsigned int key ) 
01047 { 
01048   _event_handler_manager->removeEventHandler( key ); 
01049 }
01050 
01051 //===========================================================================//
01052 
01053 void Node::removeEventHandler( EventHandler * event_handler ) 
01054 { 
01055   _event_handler_manager->removeEventHandler( event_handler->getKey() ); 
01056 }
01057 
01058 //===========================================================================//
01059 
01060 void Node::printself(ostream & stream, int indent) const 
01061 {
01062   NodeDesc::printself(stream, indent);  
01063   _local_workspace->printself(stream, indent+1);
01064 }
01065 
01066 //===========================================================================//
01067 
01068 ostream& operator <<(std::ostream& stream, const Node & _this)
01069 {
01070   stream << "Node {" << std::endl; 
01071   _this.printself(stream);
01072   stream << "}" << std::endl;
01073   return stream;
01074 }
01075 
01076 //===========================================================================//
01077 
01078 __COLCOWS_END__
01079 
01080 // EOF

Generated on Tue Jun 21 12:55:56 2005 for ColCOWS by  doxygen 1.4.1