gadget::Connector Class Reference

Simple connector type. More...

#include <gadget/Connector.h>

List of all members.

Public Member Functions

 Connector ()
virtual ~Connector ()
vpr::ReturnStatus attemptConnect (Node *node)
 Attempt to connect to the given node.


Detailed Description

Simple connector type.

Definition at line 51 of file Connector.h.


Constructor & Destructor Documentation

gadget::Connector::Connector (  )  [inline]

Definition at line 62 of file Connector.h.

00063    {;}

virtual gadget::Connector::~Connector (  )  [inline, virtual]

Definition at line 64 of file Connector.h.

00065    {;}


Member Function Documentation

vpr::ReturnStatus gadget::Connector::attemptConnect ( Node node  ) 

Attempt to connect to the given node.

Definition at line 46 of file Connector.cpp.

References gadgetDBG_NET_MGR(), cluster::ConnectionAck::getAck(), gadget::Node::getHostname(), gadget::Node::getName(), gadget::Node::getPort(), gadget::Node::getSockStream(), gadget::Node::getStatus(), gadget::Node::NEWCONNECTION, gadget::Node::PENDING, cluster::ConnectionAck::printData(), gadget::Node::recvPacket(), gadget::Node::setSockStream(), and gadget::Node::setStatus().

00047 {
00048    // - Try to connect to remote host
00049    // - If successful
00050    //
00051    //   - Lock pending list
00052    //     - If hostname exists in pending list
00053    //       - Remove Node from pending list
00054    //   - Unlock pending list
00055    //
00056    //   - If Node not connected
00057    //     - Set socket stream
00058    //     - Set NEWCONNECTION
00059    
00060    vprASSERT( Node::PENDING == node->getStatus() &&
00061               "Can not connect to a node that is not pending." );
00062 
00063    vpr::SocketStream* sock_stream;
00064    vpr::InetAddr inet_addr;
00065 
00066    vprDEBUG( gadgetDBG_NET_MGR, vprDBG_VERB_LVL )
00067       << clrOutBOLD( clrBLUE, "[Connector]" )
00068       << " Attempting to connect to: " << node->getName()
00069       << std::endl << vprDEBUG_FLUSH;
00070       
00071    // Set the address that we want to connect to
00072    if ( !inet_addr.setAddress( node->getHostname(), node->getPort() ).success() )
00073    {
00074       vprDEBUG( gadgetDBG_NET_MGR, vprDBG_CRITICAL_LVL )
00075          << clrOutBOLD( clrBLUE, "[Connector]" )
00076          << clrOutBOLD( clrRED, " ERROR:" )
00077          << " Failed to set address" << std::endl << vprDEBUG_FLUSH;
00078       return vpr::ReturnStatus::Fail;
00079    }
00080 
00081    // Create a new socket stream to this address
00082    sock_stream = new vpr::SocketStream( vpr::InetAddr::AnyAddr, inet_addr );
00083 
00084    // If we can successfully open the socket and connect to the server
00085    if ( sock_stream->open().success() &&
00086         sock_stream->connect().success() )
00087    {
00088       vprDEBUG( gadgetDBG_NET_MGR, vprDBG_CONFIG_STATUS_LVL )
00089          << clrOutBOLD( clrBLUE, "[Connector]" )
00090          << " Successfully connected to: "
00091          << node->getHostname() <<":"<< node->getPort()
00092          << std::endl << vprDEBUG_FLUSH;
00093          
00094       sock_stream->setNoDelay( true );
00095       vpr::SocketStream* old_stream = node->getSockStream();
00096       node->setSockStream( sock_stream );
00097 
00098       vprDEBUG( gadgetDBG_NET_MGR, vprDBG_CONFIG_STATUS_LVL )
00099          << clrOutBOLD( clrBLUE, "[Connector]" )
00100          << " Waiting for connection ACK/NACK"
00101          << std::endl << vprDEBUG_FLUSH;
00102       
00103       cluster::Packet* temp_packet = node->recvPacket();
00104       cluster::ConnectionAck* ack_packet = dynamic_cast<cluster::ConnectionAck*>( temp_packet );
00105       node->setSockStream( old_stream );
00106 
00107       vprDEBUG( gadgetDBG_NET_MGR, vprDBG_CONFIG_STATUS_LVL )
00108          << clrOutBOLD( clrBLUE,"[Connector]" )
00109          << " Recieved" << ( ack_packet->getAck() ? " an Ack" : " a NACK" )
00110          << std::endl << vprDEBUG_FLUSH;
00111      
00112       vprASSERT( NULL != ack_packet && "Dynamic cast failed, must not be a ConnectionAck packet." );
00113       
00114       ack_packet->printData( vprDBG_CONFIG_STATUS_LVL );
00115       
00116       if ( ack_packet->getAck() )
00117       {
00118          //   - Lock pending list
00119          //     - If hostname exists in pending list
00120          //       - Remove Node from pending list
00121          //   - Unlock pending list
00122       
00123          vprASSERT( Node::PENDING == node->getStatus() && "Trying to connect to a node that is not pending" );
00124          node->setStatus( Node::NEWCONNECTION );
00125 
00126          //   - If Node not connected
00127          //     - Set socket stream
00128          //     - Set NEWCONNECTION
00129 
00130          vprASSERT( Node::NEWCONNECTION == node->getStatus() && "Should be NEWCONNECTION." );
00131          
00132          node->setSockStream( sock_stream );
00133          node->setStatus( Node::NEWCONNECTION );
00134 
00135          // XXX: We need to fix this in the near future.
00136          //ClusterDelta cluster_delta;
00137          //vpr::Interval temp;
00138          //temp = cluster_delta.getClusterDelta( getSockStream() );
00139          //mDelta = temp.getBaseVal();
00140       }
00141       else
00142       {
00143          node->setSockStream( NULL );
00144          delete sock_stream; 
00145          return vpr::ReturnStatus::Fail;
00146       }
00147    }
00148    else
00149    {
00150       delete sock_stream;
00151       
00152       vprDEBUG( gadgetDBG_NET_MGR, vprDBG_VERB_LVL )
00153          << clrOutBOLD( clrBLUE, "[Connector]" )
00154          << clrOutBOLD( clrRED, " ERROR:" )
00155          << " Could not connect to Node: "
00156          << node->getHostname() << " : " << node->getPort()
00157          << std::endl << vprDEBUG_FLUSH;
00158          
00159       return vpr::ReturnStatus::Fail;
00160    }
00161    
00162    return vpr::ReturnStatus::Succeed;
00163 }


The documentation for this class was generated from the following files:
Generated on Thu Jan 4 10:44:31 2007 for Gadgeteer by  doxygen 1.5.1