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

Scheduler.cpp

Go to the documentation of this file.
00001 //
00002 // Scheduler.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 
00023 
00024 #include "StdAfx.h"
00025 #include "atltime.h"
00026 #include "Shareaza.h"
00027 #include "Settings.h"
00028 #include "DlgHelp.h"
00029 #include "Scheduler.h"
00030 #include "Network.h"
00031 
00032 CScheduler Schedule;
00033 
00035 // CScheduler construction
00036 
00037 CScheduler::CScheduler()
00038 {
00039         FillMemory( m_pSchedule , 7 * 24, SCHEDULE_FULL_SPEED );
00040         m_nCurrentHour = 0xFF;
00041         m_tLastCheck = 0;
00042 }
00043 
00044 CScheduler::~CScheduler()
00045 {
00046 }
00047 
00049 // CScheduler load and save
00050 
00051 BOOL CScheduler::Load()
00052 {
00053         CFile pFile;
00054         CString strFile = Settings.General.UserPath + _T("\\Data\\Schedule.dat");
00055 
00056         if ( ! pFile.Open( strFile, CFile::modeRead ) )
00057         {
00058                 theApp.Message( MSG_ERROR, _T("Failed to open Schedule.dat") );
00059                 return FALSE;
00060         }
00061 
00062         try
00063         {
00064                 CArchive ar( &pFile, CArchive::load );
00065                 Serialize( ar );
00066                 ar.Close();
00067         }
00068         catch ( CException* pException )
00069         {
00070                 pException->Delete();
00071         }
00072 
00073         pFile.Close();
00074 
00075         m_nCurrentHour = 0xFF;  // Reset the current hour so the scheduler updates now.
00076         return TRUE;
00077 }
00078 
00079 void CScheduler::Save()
00080 {
00081         CFile pFile;
00082         CString strFile = Settings.General.UserPath + _T("\\Data\\Schedule.dat");
00083 
00084         if ( pFile.Open( strFile, CFile::modeWrite|CFile::modeCreate ) )
00085         {
00086                 CArchive ar( &pFile, CArchive::store );
00087                 Serialize( ar );
00088                 ar.Close();
00089         }
00090 
00091         m_nCurrentHour = 0xFF;  // Reset the current hour so the scheduler updates now.
00092 }
00093 
00095 // CScheduler serialize
00096 
00097 void CScheduler::Serialize(CArchive& ar)
00098 {
00099         int nDay, nHour;
00100 
00101         if ( ar.IsStoring() )
00102         {
00103                 for ( nDay = 0 ; nDay < 7 ; nDay++ )
00104                 {
00105                         for ( nHour = 0 ; nHour < 24 ; nHour++ )
00106                         {
00107                                 ar << m_pSchedule[nDay][nHour];
00108                         }
00109                 }
00110         }
00111         else
00112         {
00113                 for ( nDay = 0 ; nDay < 7 ; nDay++ )
00114                 {
00115                         for ( nHour = 0 ; nHour < 24 ; nHour++ )
00116                         {
00117                                 ar >> m_pSchedule[nDay][nHour];
00118                         }
00119                 }
00120         }
00121 }
00122 
00124 // CScheduler Update
00125 void CScheduler::Update()
00126 {
00127         int nDay, nHour;
00128 
00129         if ( Settings.Scheduler.Enable )
00130         {
00131                 DWORD tTicks = GetTickCount();
00132 
00133                 if ( tTicks - m_tLastCheck < 20000 ) return; // Only run this once every 20 seconds since the CTime functions can be slow
00134                 m_tLastCheck = tTicks;
00135 
00136                 // Get the current time
00137                 CTime tTime = CTime::GetCurrentTime();
00138 
00139                 // Get current Day/Hour and check they are valid
00140                 nDay = tTime.GetDayOfWeek() - 1;
00141                 nHour = tTime.GetHour();
00142                 if ( ( nDay >= 7 ) || ( nDay < 0 ) || ( nHour >= 24 ) || ( nHour < 0 ) )
00143                 {
00144                         // Really Really Strange Error That Should Never Happen.
00145                         theApp.Message( MSG_ERROR, _T("Scheduler received invalid time") );
00146                         return;
00147                 }
00148 
00149                 // If the hour has changed, set the current status
00150                 if ( m_nCurrentHour != nHour )
00151                 {
00152                         SetVariables( m_pSchedule[nDay][nHour] );
00153                         m_nCurrentHour = nHour;
00154                 }
00155         }
00156 }
00157 
00159 // CScheduler set variables
00160 void CScheduler::SetVariables(BYTE nCurrentSettings)
00161 {
00162         switch ( nCurrentSettings )
00163         {
00164         case SCHEDULE_OFF:
00165                 theApp.Message( MSG_DEBUG, _T("Scheduler: Shutting down") );
00166                 Settings.Live.BandwidthScale = 0;
00167                 Settings.Gnutella2.EnableToday  = FALSE;
00168                 Settings.Gnutella1.EnableToday  = FALSE;
00169                 Settings.eDonkey.EnableToday    = FALSE;
00170                 if ( Network.IsConnected() ) Network.Disconnect();
00171                 break;
00172         case SCHEDULE_LIMITED_SPEED:
00173                 theApp.Message( MSG_DEBUG, _T("Scheduler: Limited speed") );
00174                 Settings.Live.BandwidthScale = Settings.Scheduler.LimitedBandwidth;
00175                 Settings.Gnutella2.EnableToday  = TRUE;
00176                 Settings.Gnutella1.EnableToday  = Settings.Scheduler.LimitedNetworks ? FALSE :Settings.Gnutella1.EnableAlways;
00177                 Settings.eDonkey.EnableToday    = Settings.Scheduler.LimitedNetworks ? FALSE :Settings.eDonkey.EnableAlways;
00178                 if ( ! Network.IsConnected() ) Network.Connect( TRUE );
00179                 break;
00180         case SCHEDULE_FULL_SPEED:
00181                 theApp.Message( MSG_DEBUG, _T("Scheduler: Full Speed") );
00182                 Settings.Live.BandwidthScale = 100;
00183                 Settings.Gnutella2.EnableToday  = TRUE;
00184                 Settings.Gnutella1.EnableToday  = Settings.Gnutella1.EnableAlways;
00185                 Settings.eDonkey.EnableToday    = Settings.eDonkey.EnableAlways;
00186                 if ( ! Network.IsConnected() ) Network.Connect( TRUE );
00187                 break;
00188         default: //Error
00189                 theApp.Message( MSG_ERROR, _T("Invalid value in schedule") );
00190                 break;
00191         }
00192 }

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