Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Examples  

cluster::ClusterNetwork Class Reference

#include <ClusterNetwork.h>

List of all members.

Public Methods

 ClusterNetwork ()
virtual ~ClusterNetwork ()
void handlePacket (Packet *packet, ClusterNode *node)
bool isClusterNetworkReady ()
void updateNewConnections ()
void lockClusterNodes ()
 Locks the list of ClusterNodes. More...

void unlockClusterNodes ()
 Unlocks the list of ClusterNodes. More...

cluster::ClusterNodegetClusterNodeByHostname (const std::string &host_name)
 Returns the ClusterNode with the given hostname If no ClusterNode with this hostname exists, NULL is returned. More...

cluster::ClusterNodegetClusterNodeByName (const std::string &node_name)
 Returns the ClusterNode with the given name If no ClusterNode with this name exists, NULL is returned. More...

int getNumClusterNodes ()
 Get the number of nodes in the current cluster configuration. More...

void debugDumpClusterNodes (int debug_level)
 Print out debug information abour all nodes in the current cluster configuration. More...

std::vector< cluster::ClusterNode
* >::iterator 
getClusterNodesBegin ()
 Get an iterator to the beginning of the ClusterNodes std::vector. More...

std::vector< cluster::ClusterNode
* >::iterator 
getClusterNodesEnd ()
 Get an iterator to the end of the ClusterNodes std::vector. More...

void lockPendingNodes ()
 Locks the list of PendingNodes. More...

void unlockPendingNodes ()
 Unlocks the list of PendingNodes. More...

void addPendingNode (ClusterNode *node)
 Adds the given ClusterNode to a list of pending nodes that are still trying to gain a connection. More...

void removePendingNode (std::string hostname)
 Removes the ClusterNode with the given hostname from the list of PendingNodes. More...

ClusterNodegetPendingNode (std::string host_name)
 Returns the Pending node with the given hostname. More...

bool recognizeClusterMachineConfig (jccl::ConfigElementPtr element)
 Determine if the given jccl::ConfigElement is a MachineSpecific element. More...

bool configCanHandle (jccl::ConfigElementPtr element)
 Determine if we can handle the given jccl::ConfigElement. More...

bool configAdd (jccl::ConfigElementPtr element)
 Configure the given jccl::ConfigElement because it was just added to the active configuration. More...

bool configRemove (jccl::ConfigElementPtr element)
 Remove the given jccl::ConfigElement from the active configuration . More...


Static Public Methods

bool isLocalHost (const std::string &test_host_name)
 Determine if the given hostname matches the local machine's hostname. More...

std::string getMachineSpecificElementType ()
 Return the element type for MachineSpecific element that we configure here. More...


Constructor & Destructor Documentation

cluster::ClusterNetwork::ClusterNetwork  
 

Definition at line 57 of file ClusterNetwork.cpp.

00058    {
00059       mAcceptThread = NULL;
00060       vpr::InetAddr local_addr;
00061       vpr::InetAddr::getLocalHost(local_addr);
00062    }

cluster::ClusterNetwork::~ClusterNetwork   [virtual]
 

Definition at line 64 of file ClusterNetwork.cpp.

00065    {
00066       shutdown();
00067    }


Member Function Documentation

void cluster::ClusterNetwork::handlePacket Packet   packet,
ClusterNode   node
 

Definition at line 145 of file ClusterNetwork.cpp.

References gadgetDBG_RIM.

00146    {
00147       // If the ClusterNetwork should handle this packet, then do so.
00148       if (packet->getPacketType() == Header::RIM_CONNECTION_REQ)
00149       {
00150          ConnectionRequest* temp_connection_request = dynamic_cast<ConnectionRequest*>(packet);
00151          vprASSERT(NULL != temp_connection_request && "Dynamic cast failed!");
00152          vprASSERT(NULL != node && "We can not handle a packet if we do not know which node it is coming from");
00153 
00154          std::string host_name   = temp_connection_request->getHostname();
00155          vpr::Uint16 port        = temp_connection_request->getPort();
00156 
00157          // If difference warn us
00158          node->setHostname(host_name);
00159          node->setPort(port);
00160 
00161 
00162          // Send back a responce
00163          vprDEBUG(gadgetDBG_RIM,vprDBG_CONFIG_LVL)
00164                         << clrOutBOLD(clrMAGENTA,"[ClusterNetwork]")
00165                         << "Creating a new ConnectionAck Packet" << std::endl << vprDEBUG_FLUSH;
00166 
00167          // Get the localhost name.
00168          vpr::InetAddr local;
00169          vpr::InetAddr::getLocalHost(local);
00170 
00171          ConnectionAck* responce_packet = new ConnectionAck(local.getHostname(),
00172                                                             mListenAddr.getPort(),true);
00173 
00174          // Try to send a responce packet
00175          try
00176          {
00177             vprASSERT(NULL != node && "Node is null");
00178             vprASSERT(NULL != responce_packet && "Responce Packet is null");
00179             vprASSERT(NULL != node->getSockStream() && "SocketStream is null");
00180 
00181             // Send the responce
00182             responce_packet->printData(vprDBG_CONFIG_LVL);
00183             node->send(responce_packet);
00184          }
00185          catch(cluster::ClusterException cluster_exception)
00186          {
00187             vprDEBUG(gadgetDBG_RIM,vprDBG_CONFIG_LVL)
00188                << clrOutBOLD(clrRED,"ERROR:")
00189                << "ClusterNetwork::acceptLoop() Could not send responce packet: "
00190                << cluster_exception.getMessage() << std::endl << vprDEBUG_FLUSH;
00191 
00192             node->setConnected(ClusterNode::DISCONNECTED);
00193             node->setSockStream(NULL);
00194          }
00195 
00196          delete responce_packet;
00197          responce_packet = NULL;
00198 
00199          // Start updating the ClusterNode
00200          node->start();
00201 
00202          return;
00203       }
00204       else if (packet->getPacketType() == Header::RIM_CONNECTION_ACK)
00205       {
00206          ConnectionAck* temp_connection_ack = dynamic_cast<ConnectionAck*>(packet);
00207          vprASSERT(NULL != temp_connection_ack && "Dynamic cast failed!");
00208 
00209          if (node == NULL)
00210          {
00211             return;
00212          }
00213 
00214          if ( temp_connection_ack->getAck() )
00215          {
00216             std::string host_name   = temp_connection_ack->getHostname();
00217 
00218             //node->setConnected(ClusterNode::CONNECTED);
00219             node->setConnected(ClusterNode::NEWCONNECTION);
00220             node->setHostname(host_name);
00221 
00222             // Since we have gained a new connection we should start updating
00223             node->start();
00224          }
00225          else
00226          {
00227             //node->setConnected(ClusterNode::DISCONNECTED);
00228          }
00229       }
00230    }

bool cluster::ClusterNetwork::isClusterNetworkReady  
 

Definition at line 731 of file ClusterNetwork.cpp.

00732    {
00733       // ClusterNetwork is ready if it has
00734       // no pending connection requests
00735       return (0 == mPendingNodes.size());
00736    }

void cluster::ClusterNetwork::updateNewConnections  
 

Definition at line 698 of file ClusterNetwork.cpp.

References getClusterNodesBegin, and getClusterNodesEnd.

00699    {
00700       if (getNumPendingNodes() > 0)
00701       {
00702          if(attemptPendingNodes())
00703          {
00704             // -If the pending list is stale
00705             //   - refresh the list
00706             if(jccl::ConfigManager::instance()->isPendingStale())
00707             {
00708                jccl::ConfigManager::instance()->refreshPendingList();
00709             }
00710          }
00711       }
00712 
00713       // This fuction is needed in order to gurantee that all cluster
00714       // nodes become active at the same time, the beginning of the frame
00715       //
00716       // -Loop over all ClusterNodes checking for new connections
00717       // -If ClusterNode is a new connection
00718       //   - Set it to connected
00719 
00720       vpr::Guard<vpr::Mutex> guard(mClusterNodesLock);
00721 
00722       for (std::vector<cluster::ClusterNode*>::iterator i = getClusterNodesBegin();
00723             i != getClusterNodesEnd() ; i++)
00724       {
00725          if ((*i)->getConnected() == ClusterNode::NEWCONNECTION)
00726          {
00727             (*i)->setConnected(ClusterNode::CONNECTED);
00728          }
00729       }
00730    }

void cluster::ClusterNetwork::lockClusterNodes   [inline]
 

Locks the list of ClusterNodes.

This function blocks until it can lock the std::vector of ClusterNodes.

The caller of this method must call unlockActive() when it is finished viewing/modifying the active list.

Definition at line 78 of file ClusterNetwork.h.

00079    { mClusterNodesLock.acquire(); }

void cluster::ClusterNetwork::unlockClusterNodes   [inline]
 

Unlocks the list of ClusterNodes.

The method releases the lock on the std::map.of ClusterNodes

The caller of this method must have previously locked the ClusterNodes list with lockClusterNodes().

Definition at line 89 of file ClusterNetwork.h.

00090    { mClusterNodesLock.release(); }

ClusterNode * cluster::ClusterNetwork::getClusterNodeByHostname const std::string &    host_name
 

Returns the ClusterNode with the given hostname If no ClusterNode with this hostname exists, NULL is returned.

Definition at line 275 of file ClusterNetwork.cpp.

00276    {
00277       vpr::Guard<vpr::Mutex> guard(mClusterNodesLock);
00278 
00279       vpr::InetAddr searching_for_node;
00280       searching_for_node.setAddress(host_name, 0);
00281 
00282       // -Find ClusterNode with given hostname and return a pointer to it.
00283       // -If we do not find one, return NULL
00284       for (std::vector<cluster::ClusterNode*>::iterator i = mClusterNodes.begin();
00285            i != mClusterNodes.end() ; i++)
00286       {
00287          vpr::InetAddr testing_node;
00288          testing_node.setAddress((*i)->getHostname(),0);
00289 
00290          if (searching_for_node.getAddressString() == testing_node.getAddressString())
00291          {
00292             return(*i);
00293          }
00294       }
00295       return(NULL);
00296    }

ClusterNode * cluster::ClusterNetwork::getClusterNodeByName const std::string &    node_name
 

Returns the ClusterNode with the given name If no ClusterNode with this name exists, NULL is returned.

Definition at line 298 of file ClusterNetwork.cpp.

Referenced by cluster::ClusterDepChecker::depSatisfied.

00299    {
00300       vpr::Guard<vpr::Mutex> guard(mClusterNodesLock);
00301 
00302       // -Find ClusterNode with given name and return a pointer to it.
00303       // -If we do not find one, return NULL
00304       for (std::vector<cluster::ClusterNode*>::iterator i = mClusterNodes.begin();
00305            i != mClusterNodes.end() ; i++)
00306       {
00307          if ((*i)->getName() == node_name)
00308          {
00309             return(*i);
00310          }
00311       }
00312       return(NULL);
00313    }

int cluster::ClusterNetwork::getNumClusterNodes   [inline]
 

Get the number of nodes in the current cluster configuration.

Definition at line 130 of file ClusterNetwork.h.

00131    { return mClusterNodes.size(); }

void cluster::ClusterNetwork::debugDumpClusterNodes int    debug_level
 

Print out debug information abour all nodes in the current cluster configuration.

Precondition:
The caller of this method must NOT have locked the pending list.

Definition at line 605 of file ClusterNetwork.cpp.

References gadgetDBG_RIM.

00606    {
00607       vpr::Guard<vpr::Mutex> guard(mClusterNodesLock);
00608       vpr::DebugOutputGuard dbg_output(gadgetDBG_RIM,debug_level,
00609          std::string("-------------- Cluster Network --------------\n"),
00610          std::string("---------------------------------------------\n"));
00611       for(std::vector<ClusterNode*>::iterator j = mClusterNodes.begin(); j != mClusterNodes.end(); j++)
00612       {
00613          (*j)->debugDump(debug_level);
00614       }
00615    }

std::vector<cluster::ClusterNode*>::iterator cluster::ClusterNetwork::getClusterNodesBegin   [inline]
 

Get an iterator to the beginning of the ClusterNodes std::vector.

The caller of this method must have locked the ClusterNodes list.

Definition at line 143 of file ClusterNetwork.h.

Referenced by updateNewConnections.

00144    {
00145       vprASSERT(1 == mClusterNodesLock.test());
00146       return mClusterNodes.begin();
00147    }

std::vector<cluster::ClusterNode*>::iterator cluster::ClusterNetwork::getClusterNodesEnd   [inline]
 

Get an iterator to the end of the ClusterNodes std::vector.

The caller of this method must have locked the ClusterNodes list.

Definition at line 152 of file ClusterNetwork.h.

Referenced by updateNewConnections.

00153    {
00154       vprASSERT(1 == mClusterNodesLock.test());
00155       return mClusterNodes.end();
00156    }

void cluster::ClusterNetwork::lockPendingNodes   [inline]
 

Locks the list of PendingNodes.

This function blocks until it can lock the std::vector of PendingNodes.

The caller of this method must call unlockActive() when it is finished viewing/modifying the active list.

Definition at line 167 of file ClusterNetwork.h.

00168    { mPendingNodesLock.acquire(); }

void cluster::ClusterNetwork::unlockPendingNodes   [inline]
 

Unlocks the list of PendingNodes.

The method releases the lock on the std::map.of PendingNodes

The caller of this method must have previously locked the PendingNodes list with lockPendingNodes().

Definition at line 178 of file ClusterNetwork.h.

00179    { mPendingNodesLock.release(); }

void cluster::ClusterNetwork::addPendingNode ClusterNode   node
 

Adds the given ClusterNode to a list of pending nodes that are still trying to gain a connection.

Definition at line 315 of file ClusterNetwork.cpp.

References gadgetDBG_RIM, and getPendingNode.

00316    {
00317       if (getPendingNode(node->getHostname()) == NULL)
00318       {
00319          vpr::Guard<vpr::Mutex> guard(mPendingNodesLock);
00320          vprDEBUG(gadgetDBG_RIM,vprDBG_VERB_LVL)
00321             << "[ClusterNetwork] Adding Pending ClusterNode: " << node->getHostname()
00322             << "\n"<< vprDEBUG_FLUSH;
00323          mPendingNodes.push_back(node);
00324       }
00325    }

void cluster::ClusterNetwork::removePendingNode std::string    hostname
 

Removes the ClusterNode with the given hostname from the list of PendingNodes.

Definition at line 347 of file ClusterNetwork.cpp.

References gadgetDBG_RIM.

00348    {
00349       vpr::Guard<vpr::Mutex> guard(mPendingNodesLock);
00350       vprDEBUG(gadgetDBG_RIM,vprDBG_VERB_LVL)
00351          << "[ClusterNetwork] Removing Pending Host: " << host_name << "\n"<< vprDEBUG_FLUSH;
00352 
00353       for (std::vector<cluster::ClusterNode*>::iterator i = mPendingNodes.begin();
00354            i != mPendingNodes.end() ; i++)
00355       {
00356          if ((*i)->getHostname() == host_name)
00357          {
00358             mPendingNodes.erase(i);
00359             return;
00360          }
00361       }
00362    }

ClusterNode * cluster::ClusterNetwork::getPendingNode std::string    host_name
 

Returns the Pending node with the given hostname.

If the is not a node with the given hotname, NULL is returned.

Definition at line 327 of file ClusterNetwork.cpp.

Referenced by addPendingNode.

00328    {
00329       // -If we can get a pending node with the given hostname
00330       //  return a pointer to the node.
00331       // -Else return NULL
00332 
00333       vpr::Guard<vpr::Mutex> guard(mPendingNodesLock);
00334 
00335       for (std::vector<cluster::ClusterNode*>::iterator i = mPendingNodes.begin();
00336            i != mPendingNodes.end() ; i++)
00337       {
00338          if ((*i)->getHostname() == host_name)
00339          {
00340             return(*i);
00341          }
00342       }
00343       return NULL;
00344    }

bool cluster::ClusterNetwork::recognizeClusterMachineConfig jccl::ConfigElementPtr    element
 

Determine if the given jccl::ConfigElement is a MachineSpecific element.

Definition at line 618 of file ClusterNetwork.cpp.

Referenced by configAdd, configCanHandle, and configRemove.

00619    {
00620       return(element->getID() == ClusterNetwork::getMachineSpecificElementType());
00621    }

bool cluster::ClusterNetwork::configCanHandle jccl::ConfigElementPtr    element
 

Determine if we can handle the given jccl::ConfigElement.

Definition at line 623 of file ClusterNetwork.cpp.

References recognizeClusterMachineConfig.

00624    {
00625        return( recognizeClusterMachineConfig(element) );
00626    }

bool cluster::ClusterNetwork::configAdd jccl::ConfigElementPtr    element
 

Configure the given jccl::ConfigElement because it was just added to the active configuration.

Returns:
true If we successfully configured the given MachineSpecific element. false If we failed to configure the given MachineSpecific element.

Definition at line 628 of file ClusterNetwork.cpp.

References gadgetDBG_RIM, isLocalHost, and recognizeClusterMachineConfig.

00629    {
00630       if (recognizeClusterMachineConfig(element))
00631       {
00632          // -If local machine element
00633          //   -Add machine specific ConfigElements to the pending list.
00634          //   -Start Listening thread
00635          // -Else
00636          //   -Add Node to ClusterNetwork
00637 
00638          if (isLocalHost(element->getProperty<std::string>("host_name")))
00639          {
00640             // NOTE: Add all machine dependent ConfigElementPtr's here
00641             vprASSERT(element->getNum("display_system") == 1 && "A Cluster System element must have exactly 1 display_system element");
00642 
00643             std::vector<jccl::ConfigElementPtr> machine_specific_elements = element->getChildElements();
00644 
00645             for (std::vector<jccl::ConfigElementPtr>::iterator i = machine_specific_elements.begin();
00646                  i != machine_specific_elements.end();
00647                  i++)
00648             {
00649                jccl::ConfigManager::instance()->addConfigElement(*i, jccl::ConfigManager::PendingElement::ADD);
00650 
00651                vprDEBUG(vprDBG_ALL,vprDBG_CONFIG_LVL) << clrSetBOLD(clrCYAN)
00652                   << "[ClusterNetwork] Adding Machine specific ConfigElement: "
00653                   << (*i)->getName() << clrRESET << std::endl << vprDEBUG_FLUSH;
00654             }
00655 
00656             const int listen_port = element->getProperty<int>("listen_port");
00657             startListening(listen_port);
00658          }
00659          else
00660          {
00661             vprDEBUG(gadgetDBG_RIM,vprDBG_CONFIG_LVL) << clrOutBOLD(clrCYAN,"[ClusterNetwork] ")
00662                << "Adding Node: " << element->getName()
00663                << " to the Cluster Network\n" << vprDEBUG_FLUSH;
00664 
00665             std::string    name        = element->getName();
00666             std::string    host_name   = element->getProperty<std::string>("host_name");
00667             vpr::Uint16    listen_port = element->getProperty<int>("listen_port");
00668 
00669             addClusterNode(name, host_name, listen_port);
00670          }
00671          return true;
00672       }
00673       return false;
00674    }

bool cluster::ClusterNetwork::configRemove jccl::ConfigElementPtr    element
 

Remove the given jccl::ConfigElement from the active configuration .

Returns:
true If we successfully removed the given MachineSpecific element. false If we failed to removed the given MachineSpecific element.

Definition at line 676 of file ClusterNetwork.cpp.

References gadgetDBG_RIM, and recognizeClusterMachineConfig.

00677    {
00678      if (recognizeClusterMachineConfig(element))
00679      {
00680         vprDEBUG(gadgetDBG_RIM,vprDBG_CONFIG_LVL)
00681            << "[ClusterNetwork] Removing the Node: " << element->getName()
00682            << " from the Cluster Network\n" << vprDEBUG_FLUSH;
00683         return(true);
00684      }
00685      else
00686      {
00687          vprDEBUG(gadgetDBG_RIM,vprDBG_CONFIG_LVL) << "[ClusterNetwork::configRemove] ERROR, Something is seriously wrong, we should never get here\n"
00688          << vprDEBUG_FLUSH;
00689          return(false);
00690      }
00691    }

bool cluster::ClusterNetwork::isLocalHost const std::string &    test_host_name [static]
 

Determine if the given hostname matches the local machine's hostname.

Definition at line 72 of file ClusterNetwork.cpp.

References gadgetDBG_RIM.

Referenced by configAdd.

00073    {
00074       vpr::InetAddr local;
00075       vpr::InetAddr test;
00076 
00077       // Get the hostname to check against.
00078       test.setAddress(test_host_name, 0);
00079 
00080       // Get the localhost name.
00081       vpr::InetAddr::getLocalHost(local);
00082 
00083       vprDEBUG(gadgetDBG_RIM,vprDBG_CONFIG_LVL)
00084          << "===== Test Hostname =====" << std::endl << vprDEBUG_FLUSH;
00085       vprDEBUG(gadgetDBG_RIM,vprDBG_CONFIG_LVL)
00086          << test.getHostname() << std::endl << vprDEBUG_FLUSH;
00087       vprDEBUG(gadgetDBG_RIM,vprDBG_CONFIG_LVL)
00088          << test.getAddressString() << std::endl << vprDEBUG_FLUSH;
00089 
00090       vprDEBUG(gadgetDBG_RIM,vprDBG_CONFIG_LVL)
00091          << "+================= Local Hostnames ==================+"
00092          << std::endl << vprDEBUG_FLUSH;
00093       std::vector<std::string> names = local.getHostnames();
00094       for(std::vector<std::string>::iterator itr = names.begin() ; itr != names.end() ; ++itr)
00095       {
00096          vpr::InetAddr temp;
00097          temp.setAddress((*itr), 0);
00098          vprDEBUG(gadgetDBG_RIM,vprDBG_CONFIG_LVL)
00099             << "| Name: " << std::setw(25) << (*itr) << " | "
00100             << std::setw(16) << temp.getAddressString() << " | "
00101             << std::endl << vprDEBUG_FLUSH;
00102       }
00103 
00104       vprDEBUG(gadgetDBG_RIM,vprDBG_CONFIG_LVL)
00105          << "+================= Test Hostnames ===================+"
00106          << std::endl << vprDEBUG_FLUSH;
00107       std::vector<std::string> test_names = test.getHostnames();
00108       for(std::vector<std::string>::iterator itr = test_names.begin() ; itr != test_names.end() ; ++itr)
00109       {
00110          vpr::InetAddr temp;
00111          temp.setAddress((*itr), 0);
00112          vprDEBUG(gadgetDBG_RIM,vprDBG_CONFIG_LVL)
00113             << "| Name: " << std::setw(25) << (*itr) << " | "
00114             << std::setw(16) << temp.getAddressString() << " | "
00115             << std::endl << vprDEBUG_FLUSH;
00116       }
00117 
00118       vprDEBUG(gadgetDBG_RIM,vprDBG_CONFIG_LVL)
00119          << "+====================================================+"
00120          << std::endl << vprDEBUG_FLUSH;
00121 
00122       bool result = false;
00123       if(local.getAddressString() == test.getAddressString())
00124       {
00125          vprDEBUG(gadgetDBG_RIM,vprDBG_CONFIG_LVL)
00126             << "| We have a match.                                   |"
00127             << std::endl << vprDEBUG_FLUSH;
00128             result = true;
00129       }
00130       else
00131       {
00132          vprDEBUG(gadgetDBG_RIM,vprDBG_CONFIG_LVL)
00133             << "| NO match.                                          |"
00134             << std::endl << vprDEBUG_FLUSH;
00135             result = false;
00136       }
00137       vprDEBUG(gadgetDBG_RIM,vprDBG_CONFIG_LVL)
00138          << "+====================================================+"
00139          << std::endl << vprDEBUG_FLUSH;
00140 
00141       return result;
00142    }

std::string cluster::ClusterNetwork::getMachineSpecificElementType   [static]
 

Return the element type for MachineSpecific element that we configure here.

Definition at line 693 of file ClusterNetwork.cpp.

00694    {
00695       return "machine_specific";
00696    }


The documentation for this class was generated from the following files:
Generated on Sun May 2 14:26:43 2004 for Gadgeteer by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002