pairConnection.cpp

00001 
00002 /* Copyright (c) 2007-2008, Stefan Eilemann <eile@equalizergraphics.com> 
00003  *
00004  * This library is free software; you can redistribute it and/or modify it under
00005  * the terms of the GNU Lesser General Public License version 2.1 as published
00006  * by the Free Software Foundation.
00007  *  
00008  * This library is distributed in the hope that it will be useful, but WITHOUT
00009  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00010  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00011  * details.
00012  * 
00013  * You should have received a copy of the GNU Lesser General Public License
00014  * along with this library; if not, write to the Free Software Foundation, Inc.,
00015  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00016  */
00017 
00018 #include "pairConnection.h"
00019 
00020 using namespace std;
00021 
00022 namespace eq
00023 {
00024 namespace net
00025 {
00026 PairConnection::PairConnection( ConnectionPtr readConnection,
00027                                 ConnectionPtr writeConnection )
00028         : _readConnection( readConnection )
00029         , _writeConnection( writeConnection )
00030 {
00031     _sibling = new PairConnection( this );
00032     EQASSERT( readConnection->isClosed( ));
00033     EQASSERT( writeConnection->isClosed( ));
00034     EQINFO << "New PairConnection @" << (void*)this << endl;
00035 }
00036 
00037 PairConnection::PairConnection( PairConnection* sibling )
00038         : _readConnection( sibling->_writeConnection ),
00039           _writeConnection( sibling->_readConnection ),
00040           _sibling( sibling )
00041 {
00042     EQASSERT( _readConnection->isClosed( ));
00043     EQASSERT( _writeConnection->isClosed( ));
00044     EQINFO << "New PairConnection Sibling @" << (void*)this << endl;
00045 }
00046 
00047 PairConnection::~PairConnection()
00048 {
00049     _readConnection = 0;
00050     _writeConnection = 0;
00051     _sibling = 0;
00052 }
00053 
00054 ConnectionPtr PairConnection::getSibling()
00055 {
00056     return _sibling.get();
00057 }
00058 
00059 bool PairConnection::connect()
00060 {
00061     if( _state != STATE_CLOSED )
00062         return false;
00063 
00064     _state           = STATE_CONNECTING;
00065     _sibling->_state = STATE_CONNECTING;
00066 
00067     if( !_readConnection->connect( ))
00068     {
00069         _state           = STATE_CLOSED;
00070         _sibling->_state = STATE_CLOSED;
00071         return false;
00072     }
00073 
00074     if( !_writeConnection->connect( ))
00075     {
00076         _readConnection->close();
00077         _state           = STATE_CLOSED;
00078         _sibling->_state = STATE_CLOSED;
00079         return false;
00080     }
00081 
00082     _state           = STATE_CONNECTED;
00083     _sibling->_state = STATE_CONNECTED;
00084     return true;
00085 }
00086 
00087 void PairConnection::close()
00088 {
00089     if( _state != STATE_CONNECTED )
00090         return;
00091 
00092     _state           = STATE_CLOSED;
00093     _sibling->_state = STATE_CLOSED;
00094 
00095     _readConnection->close();
00096     _writeConnection->close();
00097 
00098     // Break circular RefPtr dependency to sibling. This prohibits reopening the
00099     // pair connection!
00100     _sibling->_sibling = 0;
00101 
00102     _readConnection    = 0;
00103     _writeConnection   = 0;
00104     _sibling           = 0;
00105 }
00106 
00107 }
00108 }
Generated on Mon Aug 10 18:58:40 2009 for Equalizer 0.9 by  doxygen 1.5.8