CUploadFile类和CUploadFiles类

来源:互联网 发布:软件人员外包 编辑:程序博客网 时间:2024/05/17 08:09

//
// UploadFile.h
//
// Copyright (c) Shareaza Development Team, 2002-2005.
// This file is part of SHAREAZA (
www.shareaza.com)
//
// Shareaza is free software; you can redistribute it
// and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// Shareaza is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Shareaza; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//

#if !defined(AFX_UPLOADFILE_H__D2D194F0_8528_482F_A4DA_DE1D9C496335__INCLUDED_)
#define AFX_UPLOADFILE_H__D2D194F0_8528_482F_A4DA_DE1D9C496335__INCLUDED_

#pragma once

#include "FileFragments.hpp"

class CUploadTransfer;

/*

每个CUploadFile中有好多个UploadTranfer对象指针,

active的那个是正在上传的任务。

*/


class CUploadFile
{

// Construction

public:

 CUploadFile(CUploadTransfer* pUpload, SHA1* pSHA1,
  LPCTSTR pszName, LPCTSTR pszPath, QWORD nSize);

 virtual ~CUploadFile();

// Attributes

public:

 IN_ADDR   m_pAddress;

 BOOL   m_bSHA1;

 SHA1   m_pSHA1;
 
 CString   m_sName;
 
 CString   m_sPath;
 
 QWORD   m_nSize;

public:

 DWORD   m_nRequests;

 FF::SimpleFragmentList m_oFragments;

public:

 BOOL   m_bSelected;

protected: // 里面都是CUploadTranfser的指针

 CPtrList  m_pTransfers;

// Operations

public:

 void    Add(CUploadTransfer* pUpload);

 BOOL    Remove(CUploadTransfer* pUpload);

 CUploadTransfer* GetActive() const;

public:

 void    AddFragment(QWORD nOffset, QWORD nLength);

 void    Remove();

// Inlines

public:

 //UploadTransfer链表为空了则返回true

 inline BOOL IsEmpty() const
 {
  return m_pTransfers.GetCount() == 0;
 }

};

#endif // !defined(AFX_UPLOADFILE_H__D2D194F0_8528_482F_A4DA_DE1D9C496335__INCLUDED_)

//
// UploadFile.cpp
//
// Copyright (c) Shareaza Development Team, 2002-2005.
// This file is part of SHAREAZA (
www.shareaza.com)
//
// Shareaza is free software; you can redistribute it
// and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// Shareaza is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Shareaza; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//

#include "StdAfx.h"
#include "Shareaza.h"
#include "UploadFile.h"
#include "UploadTransfer.h"
#include "FragmentedFile.h"
#include "Statistics.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif


//////////////////////////////////////////////////////////////////////
// CUploadFile construction

//根据参数设置各个属性的值,创建当前CUploadFile对象

CUploadFile::CUploadFile(CUploadTransfer* pUpload, SHA1* pSHA1,
       LPCTSTR pszName, LPCTSTR pszPath, QWORD nSize)
: m_oFragments( nSize )
{
 m_pAddress = pUpload->m_pHost.sin_addr;

 m_sName  = pszName;

 m_sPath  = pszPath;

 m_nSize  = nSize;

 if ( m_bSHA1 = ( pSHA1 != NULL ) )
 {
  m_pSHA1 = *pSHA1;
 }

 m_nRequests  = 0;

 m_bSelected  = FALSE;

 m_pTransfers.AddTail( pUpload );
}

CUploadFile::~CUploadFile()
{
}

//////////////////////////////////////////////////////////////////////
// CUploadFile transfer operations

//将参数pUpload加入到m_pTransfers链表的末尾

void CUploadFile::Add(CUploadTransfer* pUpload)
{
 if ( m_pTransfers.Find( pUpload ) == NULL )
 {
  m_pTransfers.AddTail( pUpload );
 }
}

//在m_pTranfers链表中删除参数指定的uploadTransfer对象

//如果没有找到,返回false,如果找到了删除之,如果为空了返回true

BOOL CUploadFile::Remove(CUploadTransfer* pUpload)
{
 POSITION pos = m_pTransfers.Find( pUpload );

 if ( pos == NULL )
 {
  return FALSE;
 }

 m_pTransfers.RemoveAt( pos );

 return IsEmpty();
}

//找到当前UploadTranfer中的活跃Tranfser对象

CUploadTransfer* CUploadFile::GetActive() const
{
 if ( IsEmpty() )
 {
  return NULL;
 }

 for ( POSITION pos = m_pTransfers.GetHeadPosition() ; pos ; )
 {
  CUploadTransfer* pUpload =
   (CUploadTransfer*)m_pTransfers.GetNext( pos );

  //所谓活跃,值得就是CUploadTransfer的m_nState状态不是upsNull

  if ( pUpload->m_nState != upsNull )
  {
   return pUpload;
  }
 }

 return (CUploadTransfer*)m_pTransfers.GetTail();
}

//调用transfer链表中每个transfer的Remove方法完成删除工作

void CUploadFile::Remove()
{
 for ( POSITION pos = m_pTransfers.GetHeadPosition() ; pos ; )
 {
  CUploadTransfer* pUpload =
   (CUploadTransfer*)m_pTransfers.GetNext( pos );

  pUpload->Remove();
 }
}

//////////////////////////////////////////////////////////////////////
// CUploadFile fragments

//如果 m_oFragments为空了,说明上传一个文件结束了,完成文件数目增加1

//然后在 m_oFragments中增加一个片段,偏移量为nOffset,长度为nLength

void CUploadFile::AddFragment(QWORD nOffset, QWORD nLength)
{
 if ( m_oFragments.empty() )
 {
  Statistics.Current.Uploads.Files++;
 }

    m_oFragments.insert( FF::SimpleFragment( nOffset, nOffset + nLength ) );
}

//
// UploadFiles.h
//
// Copyright (c) Shareaza Development Team, 2002-2005.
// This file is part of SHAREAZA (
www.shareaza.com)
//
// Shareaza is free software; you can redistribute it
// and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// Shareaza is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Shareaza; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//

#if !defined(AFX_UPLOADFILES_H__0F70ABF7_91B7_436B_A235_C3D01DB0D7F6__INCLUDED_)
#define AFX_UPLOADFILES_H__0F70ABF7_91B7_436B_A235_C3D01DB0D7F6__INCLUDED_

#pragma once

class CUploadFile;

class CUploadTransfer;


/*

 CUploadFiles 管理一个CUploadFile对象构成的数组,

可以将参数CUploadTransfer所在的CUploadFile对象搬到开头,也可以搬到末尾,

还可以寻找和删除CUploadTransfer对应的CUploadFile对象。

*/


class CUploadFiles 
{

// Construction

public:

 CUploadFiles();

 virtual ~CUploadFiles();
 
// Attributes

protected:

 CPtrList m_pList;

// Operations

public:

 void   Clear();

 CUploadFile* GetFile(CUploadTransfer* pUpload, SHA1* pSHA1,
  LPCTSTR pszName, LPCTSTR pszPath, QWORD nSize);

 void   Remove(CUploadTransfer* pTransfer);

 void   MoveToHead(CUploadTransfer* pTransfer);

 void   MoveToTail(CUploadTransfer* pTransfer);
 
// List Access

public:

 //返回m_pList的头结点位置

 inline POSITION GetIterator() const
 {
  return m_pList.GetHeadPosition();
 }

 //返回参数pos位置的CUploadFile对象指针
 
 inline CUploadFile* GetNext(POSITION& pos) const
 {
  return (CUploadFile*)m_pList.GetNext( pos );
 }

 //返回m_pList中的CUploadFile对象个数
 
 inline int GetCount() const
 {
  return m_pList.GetCount();
 }

 //如果参数pFile在当前m_pList中存在,则返回true,否则返回false

 inline BOOL Check(CUploadFile* pFile) const
 {
  return m_pList.Find( pFile ) != NULL;
 }

};

extern CUploadFiles UploadFiles;

#endif // !defined(AFX_UPLOADFILES_H__0F70ABF7_91B7_436B_A235_C3D01DB0D7F6__INCLUDED_)

//
// UploadFiles.cpp
//
// Copyright (c) Shareaza Development Team, 2002-2005.
// This file is part of SHAREAZA (
www.shareaza.com)
//
// Shareaza is free software; you can redistribute it
// and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// Shareaza is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Shareaza; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//

#include "StdAfx.h"
#include "Shareaza.h"
#include "UploadFiles.h"
#include "UploadFile.h"
#include "UploadTransfer.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

CUploadFiles UploadFiles;


//////////////////////////////////////////////////////////////////////
// CUploadFiles construction

CUploadFiles::CUploadFiles()
{
}

CUploadFiles::~CUploadFiles()
{
 Clear();
}

//////////////////////////////////////////////////////////////////////
// CUploadFiles clear

//调用m_List中的每个CUploadFile对象的Remove方法完成清除操作

void CUploadFiles::Clear()
{
 for ( POSITION pos = GetIterator() ; pos ; )
 {
  GetNext( pos )->Remove();
 }
 
 ASSERT( GetCount() == 0 );
}

//////////////////////////////////////////////////////////////////////
// CUploadFiles file allocation

/*

遍历m_pList链表,如果有一项CUploadFile的地址和参数pUpload的地址相同

而且pFile->m_sPath和参数pszPath也相同,则在UploadFile中加入uploadTransfer

对象pUpload.如果没有找到,则根据参数创建一个新的CUploadFile对象加入到

m_plist的最后

*/

CUploadFile* CUploadFiles::GetFile(CUploadTransfer* pUpload,
           SHA1* pSHA1, LPCTSTR pszName,
           LPCTSTR pszPath, QWORD nSize)
{
 for ( POSITION pos = GetIterator() ; pos ; )
 {
  CUploadFile* pFile = GetNext( pos );

  if ( pFile->m_pAddress.S_un.S_addr
   == pUpload->m_pHost.sin_addr.S_un.S_addr )
  {
   if ( pFile->m_sPath == pszPath )
   {
    pFile->Add( pUpload );

    return pFile;
   }
  }
 }
 
 CUploadFile* pFile = new CUploadFile( pUpload, pSHA1,
  pszName, pszPath, nSize );

 m_pList.AddTail( pFile );
 
 return pFile;
}

//////////////////////////////////////////////////////////////////////
// CUploadFiles remove an upload trasnfer

/*

遍历m_pList链表,在pUploadFile中删除参数指定的pTransfer

对象,如果pUploadFile为空了,除了参数Transfer外没有别的Transfer了,

则在m_pList中删除pUploadTransfer对象

*/

void CUploadFiles::Remove(CUploadTransfer* pTransfer)
{
 for ( POSITION pos = GetIterator() ; pos ; )
 {
  POSITION posRemove = pos;

  CUploadFile* pFile = GetNext( pos );
  
  if ( pFile->Remove( pTransfer ) )
  {
   delete pFile;

   m_pList.RemoveAt( posRemove );
  }
 }
}

//////////////////////////////////////////////////////////////////////
// CUploadFiles move a file with this transfer to the head/tail. (Cheap BT sorting)

/*

遍历m_pList链表,如果

pUploadFile的active是参数Tranfer,则将其移动到m_pList的最前面

*/

void CUploadFiles::MoveToHead(CUploadTransfer* pTransfer)
{
 for ( POSITION pos = GetIterator() ; pos ; )
 {
  POSITION posThis = pos;

  CUploadFile* pFile = GetNext( pos );

  if ( pFile->GetActive() == pTransfer )
  {
   m_pList.RemoveAt( posThis );

   m_pList.AddHead( pFile );

   return;
  }
 }
}

/*

遍历m_pList链表,如果

pUploadFile的active是参数Tranfer,则将其移动到m_pList的最后面

*/


void CUploadFiles::MoveToTail(CUploadTransfer* pTransfer)
{
 for ( POSITION pos = GetIterator() ; pos ; )
 {
  POSITION posThis = pos;

  CUploadFile* pFile = GetNext( pos );

  if ( pFile->GetActive() == pTransfer )
  {
   m_pList.RemoveAt( posThis );

   m_pList.AddTail( pFile );

   return;
  }
 }
}

 

原创粉丝点击