Example #1
0
bool CGFile::Open( LPCTSTR pszFilename, UINT uModeFlags, void FAR * pExtra )
{
	// RETURN: true = success.
	// OF_BINARY | OF_WRITE
	if ( pszFilename == NULL )
	{
		if ( IsFileOpen())
			return( true );
	}
	else
	{
		Close();	// Make sure it's closed first.
	}

	if ( pszFilename == NULL )
		pszFilename = GetFilePath();
	else
		m_strFileName = pszFilename;

	if ( m_strFileName.IsEmpty())
		return( false );

	m_uMode = uModeFlags;
	if ( ! OpenBase( pExtra ))
		return( false );

	sm_iFilesOpen++;
	ASSERT(sm_iFilesOpen>=0);
	return( true );
}
Example #2
0
BOOL CDownload::OpenDownload()
{
	if ( m_sName.IsEmpty() )
		return TRUE;	// Download has no name yet, postponing

	if ( IsFileOpen() )
		return TRUE;	// Already opened

	SetModified();

	if ( ( IsTorrent() && ! ( m_oSHA1 || m_oTiger || m_oED2K || m_oMD5 ) ) ?
		 Open( m_pTorrent ) :
		 Open( this ) )
		return TRUE;

	if ( m_nSize != SIZE_UNKNOWN && ! Downloads.IsSpaceAvailable( m_nSize, Downloads.dlPathIncomplete ) )
	{
		CString strFileError;
		strFileError.Format( LoadString( IDS_DOWNLOAD_DISK_SPACE ), (LPCTSTR)m_sName, (LPCTSTR)Settings.SmartVolume( m_nSize ) );
		SetFileError( ERROR_DISK_FULL, strFileError );

		theApp.Message( MSG_ERROR, L"%s", (LPCTSTR)strFileError );
	}

	return FALSE;
}
Example #3
0
ReadScriptDescriptor::~ReadScriptDescriptor()
{
    if(IsFileOpen()) {
        IF_PRINT_WARNING(SCRIPT_DEBUG) << "destructor was called when file was still open: " << _filename << std::endl;
        CloseFile();
    }
}
ReadScriptDescriptor::~ReadScriptDescriptor() {
	if (IsFileOpen()) {
		IF_PRINT_WARNING(SCRIPT_DEBUG) << "destructor was called when file was still open: " << _filename << endl;
		CloseFile();
	}

	_filename = "";
	_access_mode = SCRIPT_CLOSED;
	_error_messages.clear();
	_open_tables.clear();
}
Example #5
0
bool CGFile::OpenBase( void * pExtra )
{
	UNREFERENCED_PARAMETER(pExtra);
#ifdef _WIN32
	OFSTRUCT ofs;
	m_hFile = ::OpenFile( GetFilePath(), &ofs, GetMode());
#else
	m_hFile = open( GetFilePath(), GetMode());
#endif
	return( IsFileOpen());
}
Example #6
0
void CGFile::Close()
{
	if ( ! IsFileOpen())
		return;

	sm_iFilesOpen--;
	ASSERT(sm_iFilesOpen>=0);

	CloseBase();
	m_hFile = HFILE_ERROR;
}
Example #7
0
bool TagExtractor::ExtractTag(char* tag_buffer, int* tag_buffer_len) {
  if (tag_buffer_len == NULL) {
    return false;
  }
  if (!IsFileOpen()) {
    return false;
  }

  return InternalExtractTag(static_cast<char*>(file_base_),
                            tag_buffer,
                            tag_buffer_len);
}
Example #8
0
ModifyScriptDescriptor::~ModifyScriptDescriptor() {
	if (IsFileOpen()) {
		if (SCRIPT_DEBUG)
			cerr << "SCRIPT WARNING: ModifyScriptDescriptor destructor was called when file was still open: "
				<< _filename << endl;
		CloseFile();
	}
	
	_filename = "";
	_access_mode = SCRIPT_CLOSED;
	_error_messages.clear();
	_open_tables.clear();
}
Example #9
0
void TagExtractor::CloseFile() {
  if (file_base_ != NULL) {
    UnmapViewOfFile(file_base_);
    file_base_ = NULL;
  }
  if (file_mapping_ != NULL) {
    CloseHandle(file_mapping_);
    file_mapping_ = NULL;
  }
  if (IsFileOpen()) {
    CloseHandle(file_handle_);
    file_handle_ = INVALID_HANDLE_VALUE;
  }
}
Example #10
0
void CDownload::Boost(BOOL bBoost)
{
	if ( ! IsFileOpen() || m_bBoosted == bBoost ) return;

	theApp.Message( MSG_NOTICE, IDS_DOWNLOAD_BOOST, (LPCTSTR)GetDisplayName() );

	for ( CDownloadTransfer* pTransfer = GetFirstTransfer(); pTransfer; pTransfer = pTransfer->m_pDlNext )
	{
		pTransfer->Boost( bBoost );
	}

	m_bBoosted = bBoost;
	SetModified();
}
Example #11
0
bool CResourceScript::Open( LPCTSTR pszFilename, UINT wFlags )
{
	ADDTOCALLSTACK("CResourceScript::Open");
	// Open the file if it is not already open for use.

	if ( !IsFileOpen() )
	{
		UINT	mode = 0;
		mode |= OF_SHARE_DENY_WRITE;

		if ( ! CScript::Open( pszFilename, wFlags|mode))	// OF_READ
			return( false );
		if ( ! ( wFlags & OF_READWRITE ) && CheckForChange())
		{
			//  what should we do about it ? reload it of course !
			g_Cfg.LoadResourcesOpen( this );
		}
	}

	m_iOpenCount++;
	ASSERT( IsFileOpen());
	return( true );
}
Example #12
0
bool ReadScriptDescriptor::RunScriptFunction(const std::string& function_name) {
	if (!IsFileOpen()) {
		PRINT_ERROR << "Can't call function " << function_name << "without opening a script file." << std::endl;
		return false;
	}

	try {
	    ScriptCallFunction<void>(GetLuaState(), function_name.c_str());
	} catch(luabind::error e) {
		PRINT_ERROR << "Error while loading :" << function_name << std::endl;
		ScriptManager->HandleLuaError(e);
		return false;
	}
	return true;
}
Example #13
0
bool WriteScriptDescriptor::SaveFile() {
	if (IsFileOpen() == false) {
		PRINT_ERROR << "could not save the file because it was not open" << std::endl;
		return false;
	}

	if (SCRIPT_DEBUG && IsErrorDetected()) {
		PRINT_WARNING << "SCRIPT WARNING: In WriteScriptDescriptor::CloseFile(), the file " << _filename
			<< " had error messages remaining. They are as follows:" << std::endl
			<< _error_messages.str() << std::endl;
	}

	_outfile.flush();
	return _outfile.bad();
}
Example #14
0
void CResourceScript::Close()
{
	ADDTOCALLSTACK("CResourceScript::Close");
	// Don't close the file yet.
	// Close it later when we know it has not been used for a bit.
	if ( ! IsFileOpen())
		return;
	m_iOpenCount--;

	if ( ! m_iOpenCount )
	{
		m_timeLastAccess = CServTime::GetCurrentTime();
		// Just leave it open for caching purposes
		//CloseForce();
	}
}
Example #15
0
void CDownload::Resume()
{
	if ( IsCompleted() && ! IsSeeding() )
		return;

	if ( ! IsPaused() )
	{
		StartTrying();
		return;
	}

	theApp.Message( MSG_NOTICE, IDS_DOWNLOAD_RESUMED, (LPCTSTR)GetDisplayName() );

	if ( IsFileOpen() )
	{
		for ( POSITION posSource = GetIterator(); posSource; )
		{
			CDownloadSource* pSource = GetNext( posSource );

			pSource->OnResume();
		}
	}

	m_bPaused				= FALSE;
	m_bTempPaused			= FALSE;
	m_bTorrentTrackerError	= FALSE;
	m_tReceived				= GetTickCount();

	// Try again
	ClearFileError();

	// Obsolete: Remove this
	//if ( IsTorrent() )
	//{
	//	if ( Downloads.GetTryingCount( true ) < Settings.BitTorrent.DownloadTorrents )
	//		SetStartTimer();
	//}
	//else
	//{
	//	if ( Downloads.GetTryingCount() < ( Settings.Downloads.MaxFiles + Settings.Downloads.MaxFileSearches ) )
	//		SetStartTimer();
	//}

	SetModified();
}
Example #16
0
bool ReadScriptDescriptor::ExecuteFunction(const string& function_name) {
	if (IsFileOpen() == false) {
		IF_PRINT_WARNING(SCRIPT_DEBUG) << "function invoked when the file was not open: " << _filename << endl;
		return false;
	}

	try {
	    ScriptCallFunction<void>(GetLuaState(), function_name.c_str());
	}
	catch (luabind::error e) {
		IF_PRINT_WARNING(SCRIPT_DEBUG) << "caught Luabind error while trying to execute function \"" << function_name
			<< "\" in file: " << _filename << endl;
		ScriptManager->HandleLuaError(e);
		return false;
	}

	return true;
}
Example #17
0
void ReadScriptDescriptor::CloseFile() {
	if (IsFileOpen() == false) {
		IF_PRINT_WARNING(SCRIPT_DEBUG) << "could not close the file because it was not open." << endl;
		return;
	}

	// Probably not needed. Script errors should be printed immediately.
	if (IsErrorDetected()) {
		IF_PRINT_WARNING(SCRIPT_DEBUG) << "the file " << _filename << " had the following error messages remaining:" << endl;
		cerr << _error_messages.str() << endl;
	}

	_lstack = NULL;
	_error_messages.clear();
	_open_tables.clear();
	_access_mode = SCRIPT_CLOSED;
	ScriptManager->_RemoveOpenFile(this);
}
Example #18
0
std::string ReadScriptDescriptor::OpenTablespace() {
	if (!IsFileOpen()) {
		PRINT_ERROR << "Can't open a table space without opening a script file." << std::endl;
		return std::string();
	}

	// file extension or path information (for example, 'dat/maps/demo.lua' has a tablespace name of 'demo').
	int32 period = _filename.find(".");
	int32 last_slash = _filename.find_last_of("/");
	std::string tablespace = _filename.substr(last_slash + 1, period - (last_slash + 1));

	if (tablespace.empty()) {
	    PRINT_ERROR << "The script filename is not valid to be used as tablespace name: " << _filename << std::endl;
		return std::string();
	}

	OpenTable(tablespace, true); // Open the tablespace from the global stack.
	return tablespace;
}
Example #19
0
std::string ReadScriptDescriptor::OpenTablespace()
{
    if(!IsFileOpen()) {
        PRINT_ERROR << "Can't open a table space without opening a script file." << std::endl;
        return std::string();
    }

    std::string tablespace = GetTableSpace();

    if(tablespace.empty()) {
        PRINT_ERROR << "The script filename is not valid to be used as tablespace name: " << _filename << std::endl;
        return std::string();
    }

    if (OpenTable(tablespace, true)) // Open the tablespace from the global stack.
        return tablespace;
    else
        return std::string();
}
Example #20
0
string ReadScriptDescriptor::OpenTablespace() {
	if (IsFileOpen() == false) {
		IF_PRINT_WARNING(SCRIPT_DEBUG) << "Function invoked when file was not open." << endl;
		return string();
	}

	// Extract the tablespace name from the filename. For example, 'lua/data/maps/demo.lua' has a tablespace name of 'demo')
	uint32 period = _filename.find(".");
	uint32 last_slash = _filename.find_last_of("/");
	string tablespace = _filename.substr(last_slash + 1, period - (last_slash + 1));

	if (tablespace.empty() == true) {
	    IF_PRINT_WARNING(SCRIPT_DEBUG) << "Failed to open tablespace for file: " << _filename << endl;
		return tablespace;
	}

	OpenTable(tablespace);
	return tablespace;
}
Example #21
0
void WriteScriptDescriptor::CloseFile() {
	if (IsFileOpen() == false) {
		IF_PRINT_WARNING(SCRIPT_DEBUG)
			<< "SCRIPT ERROR: in WriteScriptDescriptor::CloseFile(), could not close the "
			<< "file because it was not open." << std::endl;
		return;
	}

	if (SCRIPT_DEBUG && IsErrorDetected()) {
		PRINT_WARNING << "SCRIPT WARNING: In WriteScriptDescriptor::CloseFile(), the file " << _filename
			<< " had error messages remaining. They are as follows:" << std::endl
		    << _error_messages.str() << std::endl;
	}

	_outfile.close();
	_error_messages.clear();
	_open_tables.clear();
	_access_mode = SCRIPT_CLOSED;
	ScriptManager->_RemoveOpenFile(this);
}
Example #22
0
void ModifyScriptDescriptor::CloseFile() {
	if (IsFileOpen() == false) {
		if (SCRIPT_DEBUG)
			cerr << "SCRIPT ERROR: ModifyScriptDescriptor::CloseFile() could not close the "
				<< "file because it was not open." << endl;
		return;
	}

	if (SCRIPT_DEBUG && IsErrorDetected()) {
		cerr << "SCRIPT WARNING: In ModifyScriptDescriptor::CloseFile(), the file " << _filename
			<< " had error messages remaining. They are as follows:" << endl;
		cerr << _error_messages.str() << endl;
	}

	_lstack = NULL;
	_error_messages.clear();
	_open_tables.clear();
	_access_mode = SCRIPT_CLOSED;
	ScriptManager->_RemoveOpenFile(this);
}
Example #23
0
bool CGFile::CopyFileTo( LPCTSTR pszDstFileName )
{
	if ( ! IsFileOpen())
	{
		if ( ! Open())
			return( false );
	}
	else
	{
		SeekToBegin();
	}

	CGFile sDst;
	if ( ! sDst.Open( pszDstFileName, OF_WRITE|OF_CREATE|OF_BINARY ))
	{
		return( false );
	}

	BYTE * pData = new BYTE [ 32 * 1024 ];	// temporary buffer.
	ASSERT(pData);
	bool fSuccess = true;

	while ( true )
	{
		size_t iSize = Read( pData, 32 * 1024 );
		if ( iSize < 0 )
		{
			fSuccess = false;
			break;
		}
		if ( iSize == 0 )
			break;
		sDst.Write( pData, iSize );
		if ( iSize < 32 * 1024 )
			break;
	}

	delete [] pData;
	return( fSuccess );
}
Example #24
0
bool TagExtractor::OpenFile(const TCHAR* filename) {
  CloseFile();
  file_handle_ = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
    OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  if (IsFileOpen()) {
    file_mapping_ = CreateFileMapping(file_handle_, NULL, PAGE_READONLY,
      0, 0, NULL);
    if (file_mapping_ != NULL) {
      file_base_ = MapViewOfFile(file_mapping_, FILE_MAP_READ, 0, 0, 0);
      if (file_base_ != NULL) {
        MEMORY_BASIC_INFORMATION info = {0};
        if (::VirtualQuery(file_base_, &info, sizeof(info))) {
          file_length_ = info.RegionSize;
          return true;
        }
      }
      CloseHandle(file_mapping_);
    }
    CloseFile();
  }
  return false;
}
Example #25
0
bool ReadScriptDescriptor::ExecuteFunction(const ScriptObject& object) {
	if (IsFileOpen() == false) {
		IF_PRINT_WARNING(SCRIPT_DEBUG) << "function invoked when the file was not open: " << _filename << endl;
		return false;
	}

	if (object.is_valid() == false) {
		IF_PRINT_WARNING(SCRIPT_DEBUG) << "tried to execute function with an invalid object for file: " << _filename << endl;
		return false;
	}

	try {
	    ScriptCallFunction<void>(object);
	}
	catch(luabind::error e) {
		IF_PRINT_WARNING(SCRIPT_DEBUG) << "caught Luabind error while trying to execute script object in file: "
			<< _filename << endl;
		ScriptManager->HandleLuaError(e);
		return false;
	}

	return true;
}
void OutputManagerReference::Output(
	const Time & time
) {
	// Check for open file
	if (!IsFileOpen()) {
		_EXCEPTIONT("No file available for output");
	}

#ifdef TEMPEST_NETCDF
	// Get processor rank
	int nRank = 0;
#ifdef TEMPEST_MPIOMP
	MPI_Comm_rank(MPI_COMM_WORLD, &nRank);
#endif

	// Update reference grid
	CalculatePatchCoordinates();

	// Initial outputs to a new Output file
	if ((nRank == 0) && (m_fFreshOutputFile)) {

		// Output topography
		m_varTopography->put(
			&(m_dataTopography[0][0][0]),
			m_dYCoord.GetRows(),
			m_dXCoord.GetRows());
	}

	// Equation set
	const EquationSet & eqn = m_grid.GetModel().GetEquationSet();

	// User data metadata
	const UserDataMeta & metaUserData = m_grid.GetModel().GetUserDataMeta();

	// Add new time
	if (nRank == 0) {
#pragma message "FIX: Doesn't give correct count of days"
		double dTimeDays = (time - m_grid.GetModel().GetStartTime()) / 86400.0;
		m_varTime->set_cur(m_ixOutputTime);
		m_varTime->put(&dTimeDays, 1);
	}
/*
	// Vertically interpolate data to model levels
	if (m_fOutputAllVarsOnNodes) {
		for (int c = 0; c < eqn.GetComponents(); c++) {
			if (m_grid.GetVarLocation(c) == DataLocation_REdge) {
				m_grid.InterpolateREdgeToNode(c, 0);
			}
		}
	}
*/
	// Perform Interpolate / Reduction on state data
	m_dataStateNode.Zero();

	m_grid.ReduceInterpolate(
		DataType_State,
		m_dREtaCoord,
		m_dAlpha,
		m_dBeta,
		m_iPatch,
		m_dataStateNode,
		(m_fOutputAllVarsOnNodes)?(DataLocation_None):(DataLocation_Node),
		!m_fRemoveReferenceProfile);

	if (!m_fOutputAllVarsOnNodes) {
		m_dataStateREdge.Zero();

		m_grid.ReduceInterpolate(
			DataType_State,
			m_grid.GetREtaInterfaces(),
			m_dAlpha,
			m_dBeta,
			m_iPatch,
			m_dataStateREdge,
			DataLocation_REdge,
			!m_fRemoveReferenceProfile);
	}

	// Perform Interpolate / Reduction on tracers data
	if (m_grid.GetModel().GetEquationSet().GetTracers() != 0) {
		m_dataTracers.Zero();

		m_grid.ReduceInterpolate(
			DataType_Tracers,
			m_dREtaCoord,
			m_dAlpha,
			m_dBeta,
			m_iPatch,
			m_dataTracers,
			DataLocation_None,
			true);
	}

	// Perform Interpolate / Reduction on user data
	if (metaUserData.GetUserData2DItemCount() != 0) {
		m_dataUserData2D.Zero();

		m_grid.ReduceInterpolate(
			DataType_Auxiliary2D,
			m_dREtaSurface,
			m_dAlpha,
			m_dBeta,
			m_iPatch,
			m_dataUserData2D);
	}

	// Perform Interpolate / Reduction on computed vorticity
	if (m_fOutputVorticity || m_fOutputDivergence) {
		m_grid.ComputeVorticityDivergence(0);

		if (m_fOutputVorticity) {
			m_grid.ReduceInterpolate(
				DataType_Vorticity,
				m_dREtaCoord,
				m_dAlpha,
				m_dBeta,
				m_iPatch,
				m_dataVorticity);
		}
		if (m_fOutputDivergence) {
			m_grid.ReduceInterpolate(
				DataType_Divergence,
				m_dREtaCoord,
				m_dAlpha,
				m_dBeta,
				m_iPatch,
				m_dataDivergence);
		}
	}

	// Perform Interpolate / Reduction on temperature
	if (m_fOutputTemperature) {
		m_grid.ComputeTemperature(0);

		m_grid.ReduceInterpolate(
			DataType_Temperature,
			m_dREtaCoord,
			m_dAlpha,
			m_dBeta,
			m_iPatch,
			m_dataTemperature);
	}

	// Perform Interpolate / Reduction on temperature
	if (m_fOutputSurfacePressure) {
		m_grid.ComputeSurfacePressure(0);

		m_grid.ReduceInterpolate(
			DataType_SurfacePressure,
			m_dREtaSurface,
			m_dAlpha,
			m_dBeta,
			m_iPatch,
			m_dataSurfacePressure);
	}

	// Perform Interpolate / Reduction on Richardson number
	if (m_fOutputRichardson) {
		m_grid.ComputeRichardson(0);

		m_grid.ReduceInterpolate(
			DataType_Richardson,
			m_dREtaCoord,
			m_dAlpha,
			m_dBeta,
			m_iPatch,
			m_dataRichardson);
	}

	// Store state variable data
	if (nRank == 0) {
		for (int c = 0; c < eqn.GetComponents(); c++) {
			if ((m_fOutputAllVarsOnNodes) ||
				(m_grid.GetVarLocation(c) == DataLocation_Node)
			) {
				m_vecComponentVar[c]->set_cur(m_ixOutputTime, 0, 0, 0);
				m_vecComponentVar[c]->put(
					&(m_dataStateNode[c][0][0]),
					1,
					m_dataStateNode.GetColumns(),
					m_dYCoord.GetRows(),
					m_dXCoord.GetRows());

			} else {
				m_vecComponentVar[c]->set_cur(m_ixOutputTime, 0, 0, 0);
				m_vecComponentVar[c]->put(
					&(m_dataStateREdge[c][0][0]),
					1,
					m_dataStateREdge.GetColumns(),
					m_dYCoord.GetRows(),
					m_dXCoord.GetRows());
			}
		}

		// Store tracer variable data
		if (eqn.GetTracers() != 0) {
			for (int c = 0; c < eqn.GetTracers(); c++) {
				m_vecTracersVar[c]->set_cur(m_ixOutputTime, 0, 0, 0);
				m_vecTracersVar[c]->put(
					&(m_dataTracers[c][0][0]),
					1,
					m_dataTracers.GetColumns(),
					m_dYCoord.GetRows(),
					m_dXCoord.GetRows());
			}
		}

		// Store user data
		if (metaUserData.GetUserData2DItemCount() != 0) {
			for (int c = 0; c < metaUserData.GetUserData2DItemCount(); c++) {
				m_vecUserData2DVar[c]->set_cur(m_ixOutputTime, 0, 0);
				m_vecUserData2DVar[c]->put(
					&(m_dataUserData2D[c][0][0]),
					1,
					m_dYCoord.GetRows(),
					m_dXCoord.GetRows());
			}
		}

		// Store vorticity data
		if (m_fOutputVorticity) {
			m_varVorticity->set_cur(m_ixOutputTime, 0, 0, 0);
			m_varVorticity->put(
				&(m_dataVorticity[0][0][0]),
				1,
				m_dataVorticity.GetColumns(),
				m_dYCoord.GetRows(),
				m_dXCoord.GetRows());
		}

		// Store divergence data
		if (m_fOutputDivergence) {
			m_varDivergence->set_cur(m_ixOutputTime, 0, 0, 0);
			m_varDivergence->put(
				&(m_dataDivergence[0][0][0]),
				1,
				m_dataDivergence.GetColumns(),
				m_dYCoord.GetRows(),
				m_dXCoord.GetRows());
		}

		// Store temperature data
		if (m_fOutputTemperature) {
			m_varTemperature->set_cur(m_ixOutputTime, 0, 0, 0);
			m_varTemperature->put(
				&(m_dataTemperature[0][0][0]),
				1,
				m_dataTemperature.GetColumns(),
				m_dYCoord.GetRows(),
				m_dXCoord.GetRows());
		}

		// Store surface pressure data
		if (m_fOutputSurfacePressure) {
			m_varSurfacePressure->set_cur(m_ixOutputTime, 0, 0);
			m_varSurfacePressure->put(
				&(m_dataSurfacePressure[0][0][0]),
				1,
				m_dYCoord.GetRows(),
				m_dXCoord.GetRows());
		}

		// Store Richardson data
		if (m_fOutputRichardson) {
			m_varRichardson->set_cur(m_ixOutputTime, 0, 0, 0);
			m_varRichardson->put(
				&(m_dataRichardson[0][0][0]),
				1,
				m_dataRichardson.GetColumns(),
				m_dYCoord.GetRows(),
				m_dXCoord.GetRows());
		}
	}

	// No longer fresh file
	m_fFreshOutputFile = false;
#endif

#ifdef TEMPEST_MPIOMP
	// Barrier
	MPI_Barrier(MPI_COMM_WORLD);
#endif
}
Example #27
0
void CDownload::OnRun()
{
	// Set the currently downloading state
	// (Used to optimize display in Ctrl/Wnd functions)
	m_bDownloading = false;

	const DWORD tNow = GetTickCount();

	if ( ! IsPaused() )
	{
		if ( GetFileError() != ERROR_SUCCESS )
		{
			// File or disk errors
			Pause( FALSE );
		}
		else if ( IsMoving() )
		{
			if ( ! IsCompleted() && ! IsTasking() )
				OnDownloaded();
		}
		else if ( IsTrying() || IsSeeding() )
		{
			// This download is trying to download
			if ( HasHash() )		// Workaround for direct downloads stuck "verifying"
				OpenDownload();

			// Dead Download Check: if download appears dead, give up and allow another to start.
			// Incomplete, and trying for at least 3 hours:
			if ( ! IsCompleted() && tNow > GetStartTimer() + ( 3 * 60 * 60 * 1000 ) )
			{
				const DWORD tHoursToTry = min( ( GetEffectiveSourceCount() + 49u ) / 50u, 9lu ) + Settings.Downloads.StarveGiveUp;

				// No new data for 5-14 hours
				if ( tNow > m_tReceived + ( tHoursToTry * 60 * 60 * 1000 ) )
				{
					if ( IsTorrent() )
					{
						// Are there other torrents that should start?
						if ( Downloads.GetTryingCount( TRUE ) >= Settings.BitTorrent.DownloadTorrents )
						{
							StopTrying();		// Give up for now, try again later
							return;
						}
					}
					else	// Regular download
					{
						// Are there other downloads that should try?
						if ( Downloads.GetTryingCount() >= ( Settings.Downloads.MaxFiles + Settings.Downloads.MaxFileSearches ) )
						{
							StopTrying();		// Give up for now, try again later
							return;
						}
					}
				}
			}
			// End Dead Download Check

			// Run the download
			RunTorrent( tNow );
			RunSearch( tNow );
			RunValidation();

			if ( IsSeeding() )
			{
				// Mark as collapsed to get correct heights when dragging files
				if ( ! Settings.General.DebugBTSources && m_bExpanded )
					m_bExpanded = FALSE;
			}
			else // if ( ! IsMoving() )
			{
				if ( IsComplete() && IsFileOpen() )
				{
					if ( IsFullyVerified() )
						OnDownloaded();
				}
				else if ( CheckTorrentRatio() )
				{
					if ( ! Network.IsConnected() )
					{
						StopTrying();
						return;
					}

					StartTransfersIfNeeded( tNow );
				}
			}

			// Calculate current downloading state
			if ( HasActiveTransfers() )
				m_bDownloading = true;

			// Mutate regular download to torrent download
			if ( Settings.BitTorrent.EnablePromote && m_oBTH && ! IsTorrent() )
			{
				m_pTorrent.Clear();
				m_pTorrent.m_oMD5	= m_oMD5;
				m_pTorrent.m_oBTH	= m_oBTH;
				m_pTorrent.m_oSHA1	= m_oSHA1;
				m_pTorrent.m_oED2K	= m_oED2K;
				m_pTorrent.m_oTiger	= m_oTiger;
				m_pTorrent.m_sName	= m_sName;
				m_pTorrent.m_nSize	= m_nSize;
				SetTorrent();
			}
		}
		else if ( ! IsCompleted() && m_bVerify != TRI_TRUE )
		{
			// This is pending download
		//	// If this download isn't trying to download, see if it can try
		//	if ( IsDownloading() )
		//		SetStartTimer();	// This download was probably started by a push/etc

			if ( Network.IsConnected() )
			{
				// Have extra regular downloads 'trying' so when a new slot is ready,
				// a download has sources and is ready to go.
				if ( Downloads.GetTryingCount() < ( Settings.Downloads.MaxFiles + Settings.Downloads.MaxFileSearches ) )
				{
					if ( ! IsTorrent() ||
						Downloads.GetTryingCount( true ) < Settings.BitTorrent.DownloadTorrents )
					{
						// Torrents only try when 'ready to go'. (Reduce tracker load)
						Resume();
					}
				}
			}
		//	else
		//		ASSERT( ! IsTrying() );
		}
	}

	// Don't save Downloads with many sources too often, since it's slow
	if ( tNow >= m_tSaved + ( GetCount() > 20 ? 5 * Settings.Downloads.SaveInterval : Settings.Downloads.SaveInterval ) )
	{
		if ( IsModified() )
		{
			FlushFile();
			if ( Save() )
				m_tSaved = tNow;
		}
	}
}
Example #28
0
BOOL CDownload::SeedTorrent()
{
	if ( IsMoving() || IsCompleted() )
		return FALSE;

	ASSERT( IsFileOpen() == FALSE );
	if ( IsFileOpen() )
		return FALSE;

	ASSERT( m_pTorrent.GetCount() );

	augment::auto_ptr< CFragmentedFile > pFragmentedFile( new CFragmentedFile );
	if ( ! pFragmentedFile.get() )
		return FALSE;	// Out of memory

	if ( ! pFragmentedFile->Open( m_pTorrent, FALSE ) )
	{
		SetFileError( pFragmentedFile->GetFileError(), pFragmentedFile->GetFileErrorString() );
		return FALSE;
	}

	AttachFile( pFragmentedFile.release() );

	if ( IsSingleFileTorrent() )
	{
		// Refill missing hashes for single-file torrent
		const CBTInfo::CBTFile* pBTFile = m_pTorrent.m_pFiles.GetHead();
		if ( ! m_pTorrent.m_oSHA1 && pBTFile->m_oSHA1 )
			m_pTorrent.m_oSHA1 = pBTFile->m_oSHA1;
		if ( ! m_pTorrent.m_oTiger && pBTFile->m_oTiger )
			m_pTorrent.m_oTiger = pBTFile->m_oTiger;
		if ( ! m_pTorrent.m_oED2K && pBTFile->m_oED2K )
			m_pTorrent.m_oED2K = pBTFile->m_oED2K;
		if ( ! m_pTorrent.m_oMD5 && pBTFile->m_oMD5 )
			m_pTorrent.m_oMD5 = pBTFile->m_oMD5;

		// Refill missed hash for library file
		CQuickLock oLock( Library.m_pSection );
		if ( CLibraryFile* pLibraryFile = LibraryMaps.LookupFileByPath( pBTFile->FindFile() ) )
		{
			if ( ! pLibraryFile->m_oBTH && m_oBTH )
			{
				Library.RemoveFile( pLibraryFile );
				pLibraryFile->m_oBTH = m_oBTH;
				Library.AddFile( pLibraryFile );
			}
		}
	}

	// Refill missing hashes
	if ( ! m_oSHA1 && m_pTorrent.m_oSHA1 )
		m_oSHA1 = m_pTorrent.m_oSHA1;
	if ( ! m_oTiger && m_pTorrent.m_oTiger )
		 m_oTiger = m_pTorrent.m_oTiger;
	if ( ! m_oED2K && m_pTorrent.m_oED2K )
		m_oED2K = m_pTorrent.m_oED2K;
	if ( ! m_oMD5 && m_pTorrent.m_oMD5 )
		m_oMD5 = m_pTorrent.m_oMD5;

	GenerateTorrentDownloadID();

	m_bSeeding = TRUE;
	m_bComplete = true;
	m_tCompleted = GetTickCount();
	m_bVerify = TRI_TRUE;

	memset( m_pTorrentBlock, TRI_TRUE, m_nTorrentBlock );
	m_nTorrentSuccess = m_nTorrentBlock;

	MakeComplete();
	ResetVerification();

	return TRUE;
}
void OutputManagerComposite::Output(
    const Time & time
) {
#ifdef USE_MPI
    // Check for open file
    if (!IsFileOpen()) {
        _EXCEPTIONT("No file available for output");
    }

    // Verify that only one output has been performed
    if (m_ixOutputTime != 0) {
        _EXCEPTIONT("Only one Composite output allowed per file");
    }

    // Determine processor rank
    int nRank;
    MPI_Comm_rank(MPI_COMM_WORLD, &nRank);

    // Data types
    const int ExchangeDataType_Geometric = 0;
    const int ExchangeDataType_ActiveState = 1;
    const int ExchangeDataType_Count = 2;

    // Determine space allocation for each GridPatch
    m_vecGridPatchByteSize.Allocate(m_grid.GetPatchCount(), 2);

    for (int i = 0; i < m_grid.GetActivePatchCount(); i++) {
        const GridPatch * pPatch = m_grid.GetActivePatch(i);

        int iPatchIx = pPatch->GetPatchIndex();
        if (iPatchIx > m_grid.GetPatchCount()) {
            _EXCEPTION2("PatchIndex (%i) out of range [0,%i)",
                        iPatchIx, m_grid.GetPatchCount());
        }

        const DataContainer & dcGeometric =
            pPatch->GetDataContainerGeometric();
        m_vecGridPatchByteSize[iPatchIx][0] =
            dcGeometric.GetTotalByteSize();

        const DataContainer & dcActiveState =
            pPatch->GetDataContainerActiveState();
        m_vecGridPatchByteSize[iPatchIx][1] =
            dcActiveState.GetTotalByteSize();
    }

    // Reduce GridPatch byte size
    if (nRank != 0) {
        MPI_Reduce(
            &(m_vecGridPatchByteSize[0][0]),
            &(m_vecGridPatchByteSize[0][0]),
            m_vecGridPatchByteSize.GetTotalSize(),
            MPI_INT,
            MPI_MAX,
            0,
            MPI_COMM_WORLD);

        // Reduce GridPatch byte size and write Grid information
    } else if (nRank == 0) {

        // Reduce
        MPI_Reduce(
            MPI_IN_PLACE,
            &(m_vecGridPatchByteSize[0][0]),
            m_vecGridPatchByteSize.GetTotalSize(),
            MPI_INT,
            MPI_MAX,
            0,
            MPI_COMM_WORLD);

        // The active Model
        const Model & model = m_grid.GetModel();

        // Get maximum byte size for exchange
        int sMaxRecvBufferByteSize = 0;
        for (int i = 0; i < m_vecGridPatchByteSize.GetRows(); i++) {
            if (m_vecGridPatchByteSize[i][0] > sMaxRecvBufferByteSize) {
                sMaxRecvBufferByteSize = m_vecGridPatchByteSize[i][0];
            }
            if (m_vecGridPatchByteSize[i][1] > sMaxRecvBufferByteSize) {
                sMaxRecvBufferByteSize = m_vecGridPatchByteSize[i][1];
            }
        }

        // Allocate Recv buffer at root
        if (m_vecRecvBuffer.GetRows() < sMaxRecvBufferByteSize) {
            m_vecRecvBuffer.Allocate(sMaxRecvBufferByteSize);
        }

        // Initialize byte location for each GridPatch
        m_vecGridPatchByteLoc.Allocate(m_grid.GetPatchCount(), 2);
        m_vecGridPatchByteLoc[0][0] = 0;
        m_vecGridPatchByteLoc[0][1] = m_vecGridPatchByteSize[0][0];
        for (int i = 1; i < m_vecGridPatchByteSize.GetRows(); i++) {
            m_vecGridPatchByteLoc[i][0] =
                m_vecGridPatchByteLoc[i-1][1]
                + m_vecGridPatchByteSize[i-1][1];

            m_vecGridPatchByteLoc[i][1] =
                m_vecGridPatchByteLoc[i][0]
                + m_vecGridPatchByteSize[i][0];
        }

        // Write check bits
        m_ofsActiveOutput.write((const char *)(&m_iCheck), sizeof(int));

        // Write current time
        const Time & timeCurrent = model.GetCurrentTime();
        m_ofsActiveOutput.write((const char *)(&(timeCurrent)), sizeof(Time));

        // Write Grid information to file
        const DataContainer & dcGridParameters =
            m_grid.GetDataContainerParameters();
        int nGridParametersByteSize =
            dcGridParameters.GetTotalByteSize();
        const char * pGridParameters =
            (const char *)(dcGridParameters.GetPointer());

        m_ofsActiveOutput.write(
            pGridParameters, nGridParametersByteSize);

        const DataContainer & dcGridPatchData =
            m_grid.GetDataContainerPatchData();
        int nGridPatchDataByteSize =
            dcGridPatchData.GetTotalByteSize();
        const char * pGridPatchData =
            (const char *)(dcGridPatchData.GetPointer());

        m_ofsActiveOutput.write(
            pGridPatchData, nGridPatchDataByteSize);
    }

    // Send data from GridPatches to root
    if (nRank != 0) {

        int nActivePatches = m_grid.GetActivePatchCount();
        if (nActivePatches == 0) {
            _EXCEPTIONT("No GridPatches on processor");
        }

        DataArray1D<MPI_Request> vecSendReqGeo(nActivePatches);
        DataArray1D<MPI_Request> vecSendReqAcS(nActivePatches);

        for (int i = 0; i < nActivePatches; i++) {
            const GridPatch * pPatch = m_grid.GetActivePatch(i);

            const DataContainer & dcGeometric =
                pPatch->GetDataContainerGeometric();
            int nGeometricDataByteSize =
                dcGeometric.GetTotalByteSize();
            const unsigned char * pGeometricData =
                dcGeometric.GetPointer();

            MPI_Isend(
                const_cast<unsigned char *>(pGeometricData),
                nGeometricDataByteSize,
                MPI_BYTE,
                0,
                ExchangeDataType_Geometric,
                MPI_COMM_WORLD,
                &(vecSendReqGeo[i]));

            const DataContainer & dcActiveState =
                pPatch->GetDataContainerActiveState();
            int nActiveStateDataByteSize =
                dcActiveState.GetTotalByteSize();
            const unsigned char * pActiveStateData =
                dcActiveState.GetPointer();

            MPI_Isend(
                const_cast<unsigned char *>(pActiveStateData),
                nActiveStateDataByteSize,
                MPI_BYTE,
                0,
                ExchangeDataType_ActiveState,
                MPI_COMM_WORLD,
                &(vecSendReqAcS[i]));
            /*
            			printf("Send Geo %i %i\n", pPatch->GetPatchIndex(), nGeometricDataByteSize);
            			printf("Send AcS %i %i\n", pPatch->GetPatchIndex(), nActiveStateDataByteSize);
            */
        }

        int iWaitAllMsgGeo =
            MPI_Waitall(
                nActivePatches,
                &(vecSendReqGeo[0]),
                MPI_STATUSES_IGNORE);

        if (iWaitAllMsgGeo == MPI_ERR_IN_STATUS) {
            _EXCEPTIONT("MPI_Waitall returned MPI_ERR_IN_STATUS");
        }

        int iWaitAllMsgAcS =
            MPI_Waitall(
                nActivePatches,
                &(vecSendReqAcS[0]),
                MPI_STATUSES_IGNORE);

        if (iWaitAllMsgAcS == MPI_ERR_IN_STATUS) {
            _EXCEPTIONT("MPI_Waitall returned MPI_ERR_IN_STATUS");
        }

        //std::cout << "WAIT DONE" << std::endl;

        // Receive data and output to file
    } else {

        // Reference position
        std::streampos posRefFile = m_ofsActiveOutput.tellp();
        if (posRefFile == (-1)) {
            _EXCEPTIONT("ActiveOutput::tellp() fail");
        }

        // Write my GridPatch data to file
        for (int i = 0; i < m_grid.GetActivePatchCount(); i++) {
            const GridPatch * pPatch = m_grid.GetActivePatch(i);

            int iPatchIx = pPatch->GetPatchIndex();
            if (iPatchIx > m_grid.GetPatchCount()) {
                _EXCEPTION2("PatchIndex (%i) out of range [0,%i)",
                            iPatchIx, m_grid.GetPatchCount());
            }
            /*
            			printf("Write %i %i %i %i %i\n",
            				iPatchIx,
            				m_vecGridPatchByteLoc[iPatchIx][0],
            				m_vecGridPatchByteSize[iPatchIx][0],
            				m_vecGridPatchByteLoc[iPatchIx][1],
            				m_vecGridPatchByteSize[iPatchIx][1]);
            */
            const DataContainer & dcGeometric =
                pPatch->GetDataContainerGeometric();
            int nGeometricDataByteSize =
                dcGeometric.GetTotalByteSize();
            const char * pGeometricData =
                (const char *)(dcGeometric.GetPointer());

            m_ofsActiveOutput.seekp(
                posRefFile + m_vecGridPatchByteLoc[iPatchIx][0]);
            m_ofsActiveOutput.write(
                pGeometricData, m_vecGridPatchByteSize[iPatchIx][0]);

            const DataContainer & dcActiveState =
                pPatch->GetDataContainerActiveState();
            int nActiveStateDataByteSize =
                dcActiveState.GetTotalByteSize();
            const char * pActiveStateData =
                (const char *)(dcActiveState.GetPointer());

            m_ofsActiveOutput.seekp(
                posRefFile + m_vecGridPatchByteLoc[iPatchIx][1]);
            m_ofsActiveOutput.write(
                pActiveStateData, m_vecGridPatchByteSize[iPatchIx][1]);
        }

        // Recieve all data objects from neighbors
        int nRemainingMessages =
            2 * (m_grid.GetPatchCount() - m_grid.GetActivePatchCount());

        for (; nRemainingMessages > 0; nRemainingMessages--) {

            // Receive a consolidation message
            MPI_Status status;

            MPI_Recv(
                &(m_vecRecvBuffer[0]),
                m_vecRecvBuffer.GetRows(),
                MPI_CHAR,
                MPI_ANY_SOURCE,
                MPI_ANY_TAG,
                MPI_COMM_WORLD,
                &status);

            // Data type from TAG
            int iDataType = status.MPI_TAG;
            if ((iDataType < 0) || (iDataType >= ExchangeDataType_Count)) {
                _EXCEPTION1("MPI_TAG (%i) out of range", iDataType);
            }

            // Check patch index
            int iPatchIx = *((int*)(&(m_vecRecvBuffer[0])));

            if (iPatchIx > m_vecGridPatchByteLoc.GetRows()) {
                _EXCEPTION2("PatchIndex (%i) out of range [0,%i)",
                            iPatchIx, m_vecGridPatchByteLoc.GetRows());
            }
            /*
            			if (iDataType == 0) {
            				printf("Recv Geo %i %i\n",
            					pPatchIx[0],
            					m_vecGridPatchByteSize[pPatchIx[0]][iDataType]);
            			} else {
            				printf("Recv AcS %i %i\n",
            					pPatchIx[0],
            					m_vecGridPatchByteSize[pPatchIx[0]][iDataType]);
            			}
            */
            m_ofsActiveOutput.seekp(
                posRefFile + m_vecGridPatchByteLoc[iPatchIx][iDataType]);
            m_ofsActiveOutput.write(
                (const char *)(&(m_vecRecvBuffer[0])),
                m_vecGridPatchByteSize[iPatchIx][iDataType]);
        }
    }

    // Barrier
    MPI_Barrier(MPI_COMM_WORLD);

#else
    _EXCEPTIONT("Not implemented without USE_MPI");
#endif
}