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

GraphItem.cpp

Go to the documentation of this file.
00001 //
00002 // GraphItem.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 "Statistics.h"
00026 #include "GraphItem.h"
00027 #include "Skin.h"
00028 
00029 #include "Network.h"
00030 #include "Datagrams.h"
00031 #include "HostCache.h"
00032 #include "Neighbours.h"
00033 #include "Neighbour.h"
00034 #include "Transfers.h"
00035 #include "Downloads.h"
00036 #include "Uploads.h"
00037 
00038 #ifdef _DEBUG
00039 #undef THIS_FILE
00040 static char THIS_FILE[]=__FILE__;
00041 #define new DEBUG_NEW
00042 #endif
00043 
00044 
00046 // CGraphItem construction
00047 
00048 CGraphItem::CGraphItem(DWORD nCode, DWORD nParam, COLORREF nColour)
00049 {
00050         m_nCode         = nCode;
00051         m_nParam        = nParam;
00052         m_nColour       = nColour;
00053 
00054         m_nData         = 64;
00055         m_pData         = new DWORD[ m_nData ];
00056         m_nLength       = 0;
00057         m_nPosition     = 0;
00058 
00059         if ( m_nCode ) SetCode( m_nCode );
00060 }
00061 
00062 CGraphItem::~CGraphItem()
00063 {
00064         if ( m_pData ) delete [] m_pData;
00065 }
00066 
00068 // CGraphItem code
00069 
00070 void CGraphItem::SetCode(DWORD nCode)
00071 {
00072         if ( m_nCode != nCode )
00073         {
00074                 m_nCode = nCode;
00075                 Clear();
00076         }
00077 
00078         GRAPHITEM* pDesc = GetItemDesc( m_nCode );
00079         if ( pDesc ) Skin.LoadString( m_sName, pDesc->m_nStringID );
00080 }
00081 
00083 // CGraphItem clear
00084 
00085 void CGraphItem::Clear()
00086 {
00087         m_nLength       = 0;
00088         m_nPosition     = 0;
00089 }
00090 
00092 // CGraphItem add
00093 
00094 DWORD CGraphItem::Add(DWORD nValue)
00095 {
00096         m_pData[ m_nPosition++ ] = nValue;
00097         m_nLength = max( m_nLength, m_nPosition );
00098         if ( m_nPosition >= m_nData ) m_nPosition = 0;
00099         return nValue;
00100 }
00101 
00103 // CGraphItem value
00104 
00105 DWORD CGraphItem::GetValueAt(DWORD nPosition) const
00106 {
00107         if ( nPosition >= m_nData ) return 0;
00108         DWORD nPos = m_nData + m_nPosition - nPosition - 1;
00109         if ( nPos >= m_nData ) nPos -= m_nData;
00110         return m_pData[ nPos ];
00111 }
00112 
00113 DWORD CGraphItem::GetMaximum() const
00114 {
00115         DWORD nMaximum = 0;
00116 
00117         for ( DWORD nPosition = 0 ; nPosition < m_nLength ; nPosition++ )
00118         {
00119                 DWORD nPos = m_nData + m_nPosition - nPosition - 1;
00120                 if ( nPos >= m_nData ) nPos -= m_nData;
00121                 nMaximum = max( nMaximum, m_pData[ nPos ] );
00122         }
00123 
00124         return nMaximum;
00125 }
00126 
00128 // CGraphItem set history
00129 
00130 void CGraphItem::SetHistory(DWORD nSize, BOOL bMax)
00131 {
00132         if ( bMax && m_nData >= nSize ) return;
00133         else if ( ! bMax && m_nData == nSize ) return;
00134 
00135         DWORD* pOldData         = m_pData;
00136         DWORD nOldTotal         = m_nData;
00137         DWORD nOldLength        = min( m_nLength, nSize );
00138         DWORD nOldPosition      = m_nPosition;
00139 
00140         m_nData         = nSize;
00141         m_pData         = new DWORD[ m_nData ];
00142         m_nPosition     = 0;
00143         m_nLength       = 0;
00144 
00145         if ( pOldData == NULL ) return;
00146 
00147         for ( DWORD nPosition = 0 ; nPosition < nOldLength ; nPosition++ )
00148         {
00149                 DWORD nPos = nOldTotal + nOldPosition - nOldLength + nPosition;
00150                 if ( nPos >= nOldTotal ) nPos -= nOldTotal;
00151                 Add( pOldData[ nPos ] );
00152         }
00153 
00154         delete [] pOldData;
00155 }
00156 
00158 // CGraphItem update
00159 
00160 DWORD CGraphItem::Update()
00161 {
00162         return Add( (DWORD)GetValue( m_nCode, m_nParam ) );
00163 }
00164 
00166 // CGraphItem serialize
00167 
00168 void CGraphItem::Serialize(CArchive& ar)
00169 {
00170         if ( ar.IsStoring() )
00171         {
00172                 ar << m_nCode;
00173                 ar << m_nParam;
00174                 ar << m_nColour;
00175         }
00176         else
00177         {
00178                 ar >> m_nCode;
00179                 ar >> m_nParam;
00180                 ar >> m_nColour;
00181 
00182                 SetCode( m_nCode );
00183         }
00184 }
00185 
00187 // CGraphItem make gradient
00188 
00189 void CGraphItem::MakeGradient(COLORREF crBack)
00190 {
00191         if ( m_cPen[0] != m_nColour && m_pPen[0].m_hObject != NULL )
00192                 m_pPen[0].DeleteObject();
00193         if ( m_pPen[0].m_hObject == NULL )
00194                 m_pPen[0].CreatePen( PS_SOLID, 1, m_cPen[0] = m_nColour );
00195 
00196         int nOffset = ( crBack != RGB( 0, 0, 0 ) ) ? 1 : 0;
00197 
00198         for ( int nLayer = 1 ; nLayer < 4 ; nLayer++ )
00199         {
00200                 int nAlpha      = ( nLayer + nOffset ) * 255 / 5;
00201                 int nRed        = GetRValue( m_nColour ) * ( 255 - nAlpha ) / 255 + GetRValue( crBack ) * nAlpha / 255;
00202                 int nGreen      = GetGValue( m_nColour ) * ( 255 - nAlpha ) / 255 + GetGValue( crBack ) * nAlpha / 255;
00203                 int nBlue       = GetBValue( m_nColour ) * ( 255 - nAlpha ) / 255 + GetBValue( crBack ) * nAlpha / 255;
00204 
00205                 COLORREF cr = RGB( nRed, nGreen, nBlue );
00206 
00207                 if ( m_cPen[ nLayer ] != cr && m_pPen[ nLayer ].m_hObject != NULL )
00208                         m_pPen[ nLayer ].DeleteObject();
00209                 if ( m_pPen[ nLayer ].m_hObject == NULL )
00210                         m_pPen[ nLayer ].CreatePen( PS_SOLID, 1, m_cPen[ nLayer ] = cr );
00211         }
00212 }
00213 
00215 // CGraphItem value retreival
00216 
00217 QWORD CGraphItem::GetValue(DWORD nCode, DWORD nParam)
00218 {
00219         QWORD nValue = 0;
00220 
00221         switch ( nCode )
00222         {
00223         case GRC_RANDOM:
00224                 nValue = rand() % 100;
00225                 break;
00226 
00227         case GRC_TOTAL_BANDWIDTH_IN:
00228                 nValue = GetValue( GRC_GNUTELLA_BANDWIDTH_IN, 0 ) + GetValue( GRC_DOWNLOADS_BANDWIDTH, 0 ) + Datagrams.m_nInBandwidth * 8;
00229                 break;
00230         case GRC_TOTAL_BANDWIDTH_OUT:
00231                 nValue = GetValue( GRC_GNUTELLA_BANDWIDTH_OUT, 0 ) + GetValue( GRC_UPLOADS_BANDWIDTH, 0 ) + Datagrams.m_nOutBandwidth * 8;
00232                 break;
00233         case GRC_TOTAL_BANDWIDTH_TCP_IN:
00234                 nValue = GetValue( GRC_GNUTELLA_BANDWIDTH_IN, 0 ) + GetValue( GRC_DOWNLOADS_BANDWIDTH, 0 );
00235                 break;
00236         case GRC_TOTAL_BANDWIDTH_TCP_OUT:
00237                 nValue = GetValue( GRC_GNUTELLA_BANDWIDTH_OUT, 0 ) + GetValue( GRC_UPLOADS_BANDWIDTH, 0 );
00238                 break;
00239         case GRC_TOTAL_BANDWIDTH_UDP_IN:
00240                 nValue = Datagrams.m_nInBandwidth * 8;
00241                 break;
00242         case GRC_TOTAL_BANDWIDTH_UDP_OUT:
00243                 nValue = Datagrams.m_nOutBandwidth * 8;
00244                 break;
00245 
00246         case GRC_GNUTELLA_CONNECTIONS:
00247                 if ( ! Network.m_pSection.Lock( 20 ) ) break;
00248                 nValue = Neighbours.GetCount( -1, nrsConnected, -1 );
00249                 Network.m_pSection.Unlock();
00250                 break;
00251         case GRC_GNUTELLA_CONNECTIONS_ALL:
00252                 if ( ! Network.m_pSection.Lock( 20 ) ) break;
00253                 nValue = Neighbours.GetCount( -1, -1, -1 );
00254                 Network.m_pSection.Unlock();
00255                 break;
00256         case GRC_GNUTELLA_BANDWIDTH_IN:
00257                 nValue = Neighbours.m_nBandwidthIn * 8;
00258                 break;
00259         case GRC_GNUTELLA_BANDWIDTH_OUT:
00260                 nValue = Neighbours.m_nBandwidthOut * 8;
00261                 break;
00262         case GRC_GNUTELLA_PACKETS_IN:
00263                 if ( ! Network.m_pSection.Lock( 20 ) ) break;
00264                 Statistics.Update();
00265                 nValue = Statistics.Last.Gnutella1.Incoming + Statistics.Last.Gnutella2.Incoming;
00266                 Network.m_pSection.Unlock();
00267                 break;
00268         case GRC_GNUTELLA_PACKETS_OUT:
00269                 if ( ! Network.m_pSection.Lock( 20 ) ) break;
00270                 Statistics.Update();
00271                 nValue = Statistics.Last.Gnutella1.Outgoing + Statistics.Last.Gnutella2.Outgoing;
00272                 Network.m_pSection.Unlock();
00273                 break;
00274 
00275         case GRC_DOWNLOADS_FILES:
00276                 if ( ! Transfers.m_pSection.Lock( 20 ) ) break;
00277                 nValue = Downloads.GetCount( TRUE );
00278                 Transfers.m_pSection.Unlock();
00279                 break;
00280         case GRC_DOWNLOADS_TRANSFERS:
00281                 nValue = Downloads.m_nTransfers;
00282                 break;
00283         case GRC_DOWNLOADS_BANDWIDTH:
00284                 nValue = Downloads.m_nBandwidth * 8;
00285                 break;
00286 
00287         case GRC_UPLOADS_TRANSFERS:
00288                 nValue = Uploads.m_nCount;
00289                 break;
00290         case GRC_UPLOADS_BANDWIDTH:
00291                 nValue = Uploads.m_nBandwidth * 8;
00292                 break;
00293 
00294         case GRC_GNUTELLA_ROUTED:
00295                 if ( ! Network.m_pSection.Lock( 20 ) ) break;
00296                 Statistics.Update();
00297                 nValue = Statistics.Last.Gnutella1.Routed + Statistics.Last.Gnutella2.Routed;
00298                 Network.m_pSection.Unlock();
00299                 break;
00300         case GRC_GNUTELLA_DROPPED:
00301                 if ( ! Network.m_pSection.Lock( 20 ) ) break;
00302                 Statistics.Update();
00303                 nValue = Statistics.Last.Gnutella1.Dropped + Statistics.Last.Gnutella2.Dropped;
00304                 Network.m_pSection.Unlock();
00305                 break;
00306         case GRC_GNUTELLA_LOST:
00307                 if ( ! Network.m_pSection.Lock( 20 ) ) break;
00308                 Statistics.Update();
00309                 nValue = Statistics.Last.Gnutella1.Lost + Statistics.Last.Gnutella2.Lost;
00310                 Network.m_pSection.Unlock();
00311                 break;
00312         case GRC_GNUTELLA_QUERIES:
00313                 if ( ! Network.m_pSection.Lock( 20 ) ) break;
00314                 Statistics.Update();
00315                 nValue = Statistics.Last.Gnutella1.Queries + Statistics.Last.Gnutella2.Queries;
00316                 Network.m_pSection.Unlock();
00317                 break;
00318 
00319         };
00320 
00321         return nValue;
00322 }
00323 
00325 // CGraphItem item names
00326 
00327 GRAPHITEM CGraphItem::m_pItemDesc[] =
00328 {
00329         { 0, 0, 0 },
00330 
00331         { GRC_TOTAL_BANDWIDTH_IN, IDS_GRAPH_TOTAL_BANDWIDTH_IN, 1 },
00332         { GRC_TOTAL_BANDWIDTH_OUT, IDS_GRAPH_TOTAL_BANDWIDTH_OUT, 1 },
00333         { GRC_TOTAL_BANDWIDTH_TCP_IN, IDS_GRAPH_TOTAL_BANDWIDTH_TCP_IN, 1 },
00334         { GRC_TOTAL_BANDWIDTH_TCP_OUT, IDS_GRAPH_TOTAL_BANDWIDTH_TCP_OUT, 1 },
00335         { GRC_TOTAL_BANDWIDTH_UDP_IN, IDS_GRAPH_TOTAL_BANDWIDTH_UDP_IN, 1 },
00336         { GRC_TOTAL_BANDWIDTH_UDP_OUT, IDS_GRAPH_TOTAL_BANDWIDTH_UDP_OUT, 1 },
00337 
00338         { GRC_GNUTELLA_CONNECTIONS, IDS_GRAPH_GNUTELLA_CONNECTIONS, 0 },
00339         { GRC_GNUTELLA_CONNECTIONS_ALL, IDS_GRAPH_GNUTELLA_CONNECTIONS_ALL, 0 },
00340         { GRC_GNUTELLA_BANDWIDTH_IN, IDS_GRAPH_GNUTELLA_BANDWIDTH_IN, 1 },
00341         { GRC_GNUTELLA_BANDWIDTH_OUT, IDS_GRAPH_GNUTELLA_BANDWIDTH_OUT, 1 },
00342         { GRC_GNUTELLA_PACKETS_IN, IDS_GRAPH_GNUTELLA_PACKETS_IN, 0 },
00343         { GRC_GNUTELLA_PACKETS_OUT, IDS_GRAPH_GNUTELLA_PACKETS_OUT, 0 },
00344 
00345         { GRC_DOWNLOADS_FILES, IDS_GRAPH_DOWNLOADS_FILES, 0 },
00346         { GRC_DOWNLOADS_TRANSFERS, IDS_GRAPH_DOWNLOADS_TRANSFERS, 0 },
00347         { GRC_DOWNLOADS_BANDWIDTH, IDS_GRAPH_DOWNLOADS_BANDWIDTH, 1 },
00348 
00349         { GRC_UPLOADS_TRANSFERS, IDS_GRAPH_UPLOADS_TRANSFERS, 0 },
00350         { GRC_UPLOADS_BANDWIDTH, IDS_GRAPH_UPLOADS_BANDWIDTH, 1 },
00351 
00352         { GRC_GNUTELLA_ROUTED, IDS_GRAPH_GNUTELLA_ROUTED, 0 },
00353         { GRC_GNUTELLA_DROPPED, IDS_GRAPH_GNUTELLA_DROPPED, 0 },
00354         { GRC_GNUTELLA_LOST, IDS_GRAPH_GNUTELLA_LOST, 0 },
00355         { GRC_GNUTELLA_QUERIES, IDS_GRAPH_GNUTELLA_QUERIES, 0 },
00356 
00357         { 0, 0, 0 }
00358 };
00359 
00360 GRAPHITEM* CGraphItem::GetItemDesc(DWORD nCode)
00361 {
00362         if ( ! nCode ) return NULL;
00363 
00364         for ( int nItem = 1 ; m_pItemDesc[ nItem ].m_nCode ; nItem++ )
00365         {
00366                 if ( m_pItemDesc[ nItem ].m_nCode == nCode ) return &m_pItemDesc[ nItem ];
00367         }
00368 
00369         return NULL;
00370 }

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