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

HubHorizon.cpp

Go to the documentation of this file.
00001 //
00002 // HubHorizon.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 "HubHorizon.h"
00026 #include "G2Packet.h"
00027 
00028 #ifdef _DEBUG
00029 #undef THIS_FILE
00030 static char THIS_FILE[]=__FILE__;
00031 #define new DEBUG_NEW
00032 #endif
00033 
00034 CHubHorizonPool HubHorizonPool;
00035 
00036 
00038 // CHubHorizonPool construction
00039 
00040 CHubHorizonPool::CHubHorizonPool()
00041 {
00042         m_pBuffer       = NULL;
00043         m_nBuffer       = 0;
00044         m_pFree         = NULL;
00045         m_pActive       = NULL;
00046         m_nActive       = 0;
00047 }
00048 
00049 CHubHorizonPool::~CHubHorizonPool()
00050 {
00051         if ( m_pBuffer != NULL ) delete [] m_pBuffer;
00052 }
00053 
00055 // CHubHorizonPool setup
00056 
00057 void CHubHorizonPool::Setup()
00058 {
00059         if ( m_pBuffer != NULL ) delete [] m_pBuffer;
00060 
00061         m_nBuffer       = Settings.Gnutella2.HubHorizonSize;
00062         m_pBuffer       = new CHubHorizonHub[ m_nBuffer ];
00063         m_pActive       = NULL;
00064         m_nActive       = 0;
00065         m_pFree         = m_pBuffer;
00066 
00067         for ( DWORD nItem = 0 ; nItem < m_nBuffer ; nItem++ )
00068         {
00069                 m_pBuffer[ nItem ].m_pNext      = ( nItem < m_nBuffer - 1 )
00070                                                                         ? &m_pBuffer[ nItem + 1 ] : NULL;
00071         }
00072 }
00073 
00075 // CHubHorizonPool clear
00076 
00077 void CHubHorizonPool::Clear()
00078 {
00079         m_pActive       = NULL;
00080         m_nActive       = 0;
00081         m_pFree         = m_pBuffer;
00082 
00083         for ( DWORD nItem = 0 ; nItem < m_nBuffer ; nItem++ )
00084         {
00085                 m_pBuffer[ nItem ].m_pNext      = ( nItem < m_nBuffer - 1 )
00086                                                                         ? &m_pBuffer[ nItem + 1 ] : NULL;
00087         }
00088 }
00089 
00091 // CHubHorizonPool add
00092 
00093 CHubHorizonHub* CHubHorizonPool::Add(IN_ADDR* pAddress, WORD nPort)
00094 {
00095     CHubHorizonHub* pHub = m_pActive;
00096         for ( ; pHub ; pHub = pHub->m_pNext )
00097         {
00098                 if ( pHub->m_pAddress.S_un.S_addr == pAddress->S_un.S_addr )
00099                 {
00100                         pHub->m_nPort = nPort;
00101                         pHub->m_nReference ++;
00102                         return pHub;
00103                 }
00104         }
00105 
00106         if ( m_nActive == m_nBuffer || m_pFree == NULL ) return FALSE;
00107 
00108         pHub = m_pFree;
00109         m_pFree = m_pFree->m_pNext;
00110 
00111         pHub->m_pNext = m_pActive;
00112         m_pActive = pHub;
00113         m_nActive ++;
00114 
00115         pHub->m_pAddress        = *pAddress;
00116         pHub->m_nPort           = nPort;
00117         pHub->m_nReference      = 1;
00118 
00119         return pHub;
00120 }
00121 
00123 // CHubHorizonPool remove
00124 
00125 void CHubHorizonPool::Remove(CHubHorizonHub* pHub)
00126 {
00127         CHubHorizonHub** ppPrev = &m_pActive;
00128 
00129         for ( CHubHorizonHub* pSeek = *ppPrev ; pSeek ; pSeek = pSeek->m_pNext )
00130         {
00131                 if ( pHub == pSeek )
00132                 {
00133                         *ppPrev = pHub->m_pNext;
00134                         pHub->m_pNext = m_pFree;
00135                         m_pFree = pHub;
00136                         m_nActive --;
00137                         break;
00138                 }
00139 
00140                 ppPrev = &pSeek->m_pNext;
00141         }
00142 }
00143 
00145 // CHubHorizonPool find
00146 
00147 CHubHorizonHub* CHubHorizonPool::Find(IN_ADDR* pAddress)
00148 {
00149         for ( CHubHorizonHub* pHub = m_pActive ; pHub ; pHub = pHub->m_pNext )
00150         {
00151                 if ( pHub->m_pAddress.S_un.S_addr == pAddress->S_un.S_addr )
00152                 {
00153                         return pHub;
00154                 }
00155         }
00156 
00157         return NULL;
00158 }
00159 
00161 // CHubHorizonPool add hubs to packet
00162 
00163 int CHubHorizonPool::AddHorizonHubs(CG2Packet* pPacket)
00164 {
00165         int nCount = 0;
00166 
00167         for ( CHubHorizonHub* pHub = m_pActive ; pHub ; pHub = pHub->m_pNext )
00168         {
00169                 pPacket->WritePacket( "S", 6 );
00170                 pPacket->WriteLongLE( pHub->m_pAddress.S_un.S_addr );
00171                 pPacket->WriteShortBE( pHub->m_nPort );
00172 
00173                 theApp.Message( MSG_DEBUG, _T("  Try horizon %s"),
00174                         (LPCTSTR)CString( inet_ntoa( pHub->m_pAddress ) ) );
00175 
00176                 nCount++;
00177         }
00178 
00179         return nCount;
00180 }
00181 
00183 // CHubHorizonGroup construction
00184 
00185 CHubHorizonGroup::CHubHorizonGroup()
00186 {
00187         m_pList         = NULL;
00188         m_nCount        = 0;
00189         m_nBuffer       = 0;
00190 }
00191 
00192 CHubHorizonGroup::~CHubHorizonGroup()
00193 {
00194         Clear();
00195         if ( m_pList != NULL ) delete [] m_pList;
00196 }
00197 
00199 // CHubHorizonGroup add
00200 
00201 void CHubHorizonGroup::Add(IN_ADDR* pAddress, WORD nPort)
00202 {
00203         CHubHorizonHub** ppHub = m_pList;
00204 
00205         for ( DWORD nCount = m_nCount ; nCount ; nCount--, ppHub++ )
00206         {
00207                 if ( (*ppHub)->m_pAddress.S_un.S_addr == pAddress->S_un.S_addr )
00208                 {
00209                         (*ppHub)->m_nPort = nPort;
00210                         return;
00211                 }
00212         }
00213 
00214         CHubHorizonHub* pHub = HubHorizonPool.Add( pAddress, nPort );
00215         if ( pHub == NULL ) return;
00216 
00217         if ( m_nCount == m_nBuffer )
00218         {
00219                 m_nBuffer += 8;
00220                 CHubHorizonHub** pList = new CHubHorizonHub*[ m_nBuffer ];
00221                 if ( m_nCount ) CopyMemory( pList, m_pList, sizeof(CHubHorizonHub*) * m_nCount );
00222                 if ( m_pList ) delete [] m_pList;
00223                 m_pList = pList;
00224         }
00225 
00226         m_pList[ m_nCount++ ] = pHub;
00227 }
00228 
00230 // CHubHorizonGroup clear
00231 
00232 void CHubHorizonGroup::Clear()
00233 {
00234         CHubHorizonHub** ppHub = m_pList;
00235 
00236         for ( DWORD nCount = m_nCount ; nCount ; nCount--, ppHub++ )
00237         {
00238                 if ( -- ( (*ppHub)->m_nReference ) == 0 )
00239                 {
00240                         HubHorizonPool.Remove( *ppHub );
00241                 }
00242         }
00243 
00244         m_nCount = 0;
00245 }
00246 

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