Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

NeighboursBase.cpp

Go to the documentation of this file.
00001 //
00002 // NeighboursBase.cpp
00003 //
00004 // Copyright (c) Shareaza Development Team, 2002-2005.
00005 // This file is part of SHAREAZA (www.shareaza.com)
00006 //
00007 // Shareaza is free software; you can redistribute it
00008 // and/or modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2 of
00010 // the License, or (at your option) any later version.
00011 //
00012 // Shareaza is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with Shareaza; if not, write to the Free Software
00019 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 //
00021 
00022 #include "StdAfx.h"
00023 #include "Shareaza.h"
00024 #include "Settings.h"
00025 #include "Network.h"
00026 #include "NeighboursBase.h"
00027 #include "Neighbour.h"
00028 #include "RouteCache.h"
00029 
00030 #ifdef _DEBUG
00031 #undef THIS_FILE
00032 static char THIS_FILE[]=__FILE__;
00033 #define new DEBUG_NEW
00034 #endif
00035 
00036 
00038 // CNeighboursBase construction
00039 
00040 CNeighboursBase::CNeighboursBase()
00041 {
00042         m_nUnique               = 2;
00043         m_nRunCookie    = 5;
00044         m_nStableCount  = 0;
00045         m_nLeafCount    = 0;
00046         m_nLeafContent  = 0;
00047         m_nBandwidthIn  = 0;
00048         m_nBandwidthOut = 0;
00049 }
00050 
00051 CNeighboursBase::~CNeighboursBase()
00052 {
00053         CNeighboursBase::Close();
00054 }
00055 
00057 // CNeighboursBase list access
00058 
00059 POSITION CNeighboursBase::GetIterator() const
00060 {
00061         return m_pUniques.GetStartPosition();
00062 }
00063 
00064 CNeighbour* CNeighboursBase::GetNext(POSITION& pos) const
00065 {
00066         LPVOID nUnique;
00067         CNeighbour* pNeighbour;
00068         m_pUniques.GetNextAssoc( pos, nUnique, (void*&)pNeighbour );
00069         return pNeighbour;
00070 }
00071 
00073 // CNeighboursBase lookup
00074 
00075 CNeighbour* CNeighboursBase::Get(DWORD nUnique) const
00076 {
00077         CNeighbour* pNeighbour = NULL;
00078         if ( ! m_pUniques.Lookup( (LPVOID)nUnique, (void*&)pNeighbour ) ) pNeighbour = NULL;
00079         return pNeighbour;
00080 }
00081 
00082 CNeighbour* CNeighboursBase::Get(IN_ADDR* pAddress) const
00083 {
00084         for ( POSITION pos = GetIterator() ; pos ; )
00085         {
00086                 CNeighbour* pNeighbour = GetNext( pos );
00087 
00088                 if ( pNeighbour->m_pHost.sin_addr.S_un.S_addr == pAddress->S_un.S_addr )
00089                         return pNeighbour;
00090         }
00091 
00092         return NULL;
00093 }
00094 
00096 // CNeighboursBase counting
00097 
00098 // Count the number of neighbours that match criteria
00099 int CNeighboursBase::GetCount(PROTOCOLID nProtocol, int nState, int nNodeType) const
00100 {
00101         int nCount = 0;
00102 
00103         if ( ( Network.m_pSection.Lock( 200 ) ) )
00104         {
00105                 for ( POSITION pos = m_pUniques.GetStartPosition() ; pos ; )
00106                 {
00107                         CNeighbour* pNeighbour;
00108                         LPVOID nUnique;
00109 
00110                         m_pUniques.GetNextAssoc( pos, nUnique, (void*&)pNeighbour );
00111 
00112                         if ( nProtocol < 0 || nProtocol == pNeighbour->m_nProtocol )
00113                         {
00114                                 if ( nState < 0 || nState == pNeighbour->m_nState )
00115                                 {
00116                                         if ( nNodeType < 0 || nNodeType == pNeighbour->m_nNodeType )
00117                                         {
00118                                                 nCount++;
00119                                         }
00120                                 }
00121                         }
00122                 }
00123                 Network.m_pSection.Unlock();
00124         }
00125 
00126         return nCount;
00127 }
00128 
00129 // Return true if any neighbour matches criteria (faster than above function if you just want yes/no)
00130 BOOL CNeighboursBase::NeighbourExists(PROTOCOLID nProtocol, int nState, int nNodeType) const
00131 {
00132         CSingleLock pLock( &Network.m_pSection );
00133 
00134         if ( pLock.Lock( 200 ) ) return FALSE;
00135 
00136         for ( POSITION pos = m_pUniques.GetStartPosition() ; pos ; )
00137         {
00138                 CNeighbour* pNeighbour;
00139                 LPVOID nUnique;
00140 
00141                 m_pUniques.GetNextAssoc( pos, nUnique, (void*&)pNeighbour );
00142 
00143                 if ( nProtocol < 0 || nProtocol == pNeighbour->m_nProtocol )
00144                 {
00145                         if ( nState < 0 || nState == pNeighbour->m_nState )
00146                         {
00147                                 if ( nNodeType < 0 || nNodeType == pNeighbour->m_nNodeType )
00148                                 {
00149                                         return TRUE;
00150                                 }
00151                         }
00152                 }
00153         }
00154 
00155         return FALSE;
00156 }
00157 
00159 // CNeighboursBase connect
00160 
00161 void CNeighboursBase::Connect()
00162 {
00163 }
00164 
00166 // CNeighboursBase close
00167 
00168 void CNeighboursBase::Close()
00169 {
00170         for ( POSITION pos = GetIterator() ; pos ; )
00171         {
00172                 GetNext( pos )->Close();
00173         }
00174 
00175         m_nStableCount  = 0;
00176         m_nLeafCount    = 0;
00177         m_nLeafContent  = 0;
00178         m_nBandwidthIn  = 0;
00179         m_nBandwidthOut = 0;
00180 }
00181 
00183 // CNeighboursBase run callback
00184 
00185 void CNeighboursBase::OnRun()
00186 {
00187         DWORD tNow                      = GetTickCount();
00188         DWORD tEstablish        = tNow - 1500;
00189 
00190         int nStableCount        = 0;
00191         int nLeafCount          = 0;
00192         DWORD nLeafContent      = 0;
00193         DWORD nBandwidthIn      = 0;
00194         DWORD nBandwidthOut     = 0;
00195 
00196         m_nRunCookie++;
00197 
00198         while ( TRUE )
00199         {
00200                 Network.m_pSection.Lock();
00201                 BOOL bWorked = FALSE;
00202 
00203                 for ( POSITION pos = GetIterator() ; pos ; )
00204                 {
00205                         CNeighbour* pNeighbour = GetNext( pos );
00206 
00207                         if ( pNeighbour->m_nRunCookie != m_nRunCookie )
00208                         {
00209                                 pNeighbour->m_nRunCookie = m_nRunCookie;
00210 
00211                                 if ( pNeighbour->m_nState == nrsConnected &&
00212                                          pNeighbour->m_tConnected < tEstablish )
00213                                          nStableCount ++;
00214 
00215                                 if ( pNeighbour->m_nNodeType == ntLeaf )
00216                                 {
00217                                         nLeafCount ++;
00218                                         nLeafContent += pNeighbour->m_nFileVolume;
00219                                 }
00220 
00221                                 pNeighbour->Measure();
00222 
00223                                 nBandwidthIn    += pNeighbour->m_mInput.nMeasure;
00224                                 nBandwidthOut   += pNeighbour->m_mOutput.nMeasure;
00225 
00226                                 pNeighbour->DoRun();
00227 
00228                                 bWorked = TRUE;
00229                                 break;
00230                         }
00231                 }
00232 
00233                 Network.m_pSection.Unlock();
00234 
00235                 if ( ! bWorked ) break;
00236         }
00237 
00238         m_nStableCount  = nStableCount;
00239         m_nLeafCount    = nLeafCount;
00240         m_nLeafContent  = nLeafContent;
00241         m_nBandwidthIn  = nBandwidthIn;
00242         m_nBandwidthOut = nBandwidthOut;
00243 }
00244 
00246 // CNeighboursBase add and remove
00247 
00248 void CNeighboursBase::Add(CNeighbour* pNeighbour, BOOL bAssignUnique)
00249 {
00250         if ( bAssignUnique )
00251         {
00252                 CNeighbour* pExisting = NULL;
00253 
00254                 do
00255                 {
00256                         pNeighbour->m_nUnique = m_nUnique++;
00257                 }
00258                 while ( pNeighbour->m_nUnique < 2 ||
00259                                 m_pUniques.Lookup( (LPVOID)pNeighbour->m_nUnique, (void*&)pExisting ) );
00260         }
00261 
00262         m_pUniques.SetAt( (LPVOID)pNeighbour->m_nUnique, pNeighbour );
00263 }
00264 
00265 void CNeighboursBase::Remove(CNeighbour* pNeighbour)
00266 {
00267         Network.QueryRoute->Remove( pNeighbour );
00268         Network.NodeRoute->Remove( pNeighbour );
00269 
00270         m_pUniques.RemoveKey( (LPVOID)pNeighbour->m_nUnique );
00271 }
00272 

Generated on Thu Dec 15 10:39:44 2005 for Shareaza 2.2.1.0 by  doxygen 1.4.2