root/juggler/branches/2.2/modules/gadgeteer/gadget/Connector.cpp

Revision 19729, 5.8 kB (checked in by patrick, 2 years ago)

Copyright update.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /*************** <auto-copyright.pl BEGIN do not edit this line> **************
2  *
3  * VR Juggler is (C) Copyright 1998-2007 by Iowa State University
4  *
5  * Original Authors:
6  *   Allen Bierbaum, Christopher Just,
7  *   Patrick Hartling, Kevin Meinert,
8  *   Carolina Cruz-Neira, Albert Baker
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public
21  * License along with this library; if not, write to the
22  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23  * Boston, MA 02111-1307, USA.
24  *
25  *************** <auto-copyright.pl END do not edit this line> ***************/
26
27 #include <gadget/gadgetConfig.h>
28 #include <gadget/Connector.h>
29 #include <gadget/Node.h>
30
31 #include <vpr/IO/Socket/SocketStream.h>
32
33 #include <cluster/Packets/Packet.h>
34 #include <cluster/Packets/ConnectionAck.h>
35
36 namespace gadget
37 {
38
39 bool Connector::attemptConnect(Node* node)
40 {
41    // - Try to connect to remote host
42    // - If successful
43    //
44    //   - Lock pending list
45    //     - If hostname exists in pending list
46    //       - Remove Node from pending list
47    //   - Unlock pending list
48    //
49    //   - If Node not connected
50    //     - Set socket stream
51    //     - Set NEWCONNECTION
52   
53    vprASSERT( Node::PENDING == node->getStatus() &&
54               "Can not connect to a node that is not pending." );
55
56    vpr::SocketStream* sock_stream;
57    vpr::InetAddr inet_addr;
58
59    vprDEBUG( gadgetDBG_NET_MGR, vprDBG_VERB_LVL )
60       << clrOutBOLD( clrBLUE, "[Connector]" )
61       << " Attempting to connect to: " << node->getName()
62       << std::endl << vprDEBUG_FLUSH;
63      
64    try
65    {
66       // Set the address that we want to connect to
67       inet_addr.setAddress( node->getHostname(), node->getPort() );
68    }
69    catch (vpr::IOException&)
70    {
71       vprDEBUG( gadgetDBG_NET_MGR, vprDBG_CRITICAL_LVL )
72          << clrOutBOLD( clrBLUE, "[Connector]" )
73          << clrOutBOLD( clrRED, " ERROR:" )
74          << " Failed to set address" << std::endl << vprDEBUG_FLUSH;
75       return false;
76    }
77
78    // Create a new socket stream to this address
79    sock_stream = new vpr::SocketStream( vpr::InetAddr::AnyAddr, inet_addr );
80
81    try
82    {
83       // If we can successfully open the socket and connect to the server
84       sock_stream->open();
85       sock_stream->connect();
86
87       vprDEBUG( gadgetDBG_NET_MGR, vprDBG_CONFIG_STATUS_LVL )
88          << clrOutBOLD( clrBLUE, "[Connector]" )
89          << " Successfully connected to: "
90          << node->getHostname() <<":"<< node->getPort()
91          << std::endl << vprDEBUG_FLUSH;
92    }
93    catch (vpr::IOException&)
94    {
95       delete sock_stream;
96      
97       vprDEBUG( gadgetDBG_NET_MGR, vprDBG_VERB_LVL )
98          << clrOutBOLD( clrBLUE, "[Connector]" )
99          << clrOutBOLD( clrRED, " ERROR:" )
100          << " Could not connect to Node: "
101          << node->getHostname() << " : " << node->getPort()
102          << std::endl << vprDEBUG_FLUSH;
103          
104       return false;
105    }
106
107    try
108    {
109       sock_stream->setNoDelay( true );
110       vpr::SocketStream* old_stream = node->getSockStream();
111       node->setSockStream( sock_stream );
112
113       vprDEBUG( gadgetDBG_NET_MGR, vprDBG_CONFIG_STATUS_LVL )
114          << clrOutBOLD( clrBLUE, "[Connector]" )
115          << " Waiting for connection ACK/NACK"
116          << std::endl << vprDEBUG_FLUSH;
117      
118       cluster::Packet* temp_packet = node->recvPacket();
119       cluster::ConnectionAck* ack_packet = dynamic_cast<cluster::ConnectionAck*>( temp_packet );
120       node->setSockStream( old_stream );
121
122       vprDEBUG( gadgetDBG_NET_MGR, vprDBG_CONFIG_STATUS_LVL )
123          << clrOutBOLD( clrBLUE,"[Connector]" )
124          << " Recieved" << ( ack_packet->getAck() ? " an Ack" : " a NACK" )
125          << std::endl << vprDEBUG_FLUSH;
126      
127       vprASSERT( NULL != ack_packet && "Dynamic cast failed, must not be a ConnectionAck packet." );
128      
129       ack_packet->printData( vprDBG_CONFIG_STATUS_LVL );
130      
131       if ( ack_packet->getAck() )
132       {
133          //   - Lock pending list
134          //     - If hostname exists in pending list
135          //       - Remove Node from pending list
136          //   - Unlock pending list
137       
138          vprASSERT( Node::PENDING == node->getStatus() && "Trying to connect to a node that is not pending" );
139          node->setStatus( Node::NEWCONNECTION );
140
141          //   - If Node not connected
142          //     - Set socket stream
143          //     - Set NEWCONNECTION
144
145          vprASSERT( Node::NEWCONNECTION == node->getStatus() && "Should be NEWCONNECTION." );
146          
147          node->setSockStream( sock_stream );
148          node->setStatus( Node::NEWCONNECTION );
149
150          // XXX: We need to fix this in the near future.
151          //ClusterDelta cluster_delta;
152          //vpr::Interval temp;
153          //temp = cluster_delta.getClusterDelta( getSockStream() );
154          //mDelta = temp.getBaseVal();
155       }
156       else
157       {
158          node->setSockStream( NULL );
159          delete sock_stream;
160          return false;
161       }
162    }
163    catch (vpr::IOException& ex)
164    {
165       delete sock_stream;
166      
167       vprDEBUG( gadgetDBG_NET_MGR, vprDBG_CRITICAL_LVL )
168          << clrOutBOLD( clrBLUE, "[Connector]" )
169          << clrOutBOLD( clrRED, " ERROR:" )
170          << " reading ACK/NACK from: "
171          << node->getHostname() << " : " << node->getPort()
172          << std::endl << ex.what()
173          << std::endl << vprDEBUG_FLUSH;
174          
175       return false;
176    }
177    
178    return true;
179 }
180
181 } // end namespace gadget
182
Note: See TracBrowser for help on using the browser.