Example #1
0
// This constructor gets the string version of the ID passed in,
// and sets that string on this object. (For when you need a string
// version of an ID.)
OTString::OTString(const OTIdentifier & theValue) : m_lLength(0), m_lPosition(0), m_strBuffer(NULL)
{
//	Initialize();

    if (theValue.GetSize() > 0)
        theValue.GetString(*this);
}
Example #2
0
// This constructor gets the string version of the ID passed in,
// and sets that string on this object. (For when you need a string
// version of an ID.)
OTString::OTString(const OTIdentifier & theValue)
{
	Initialize();
	
	if (theValue.GetSize() > 0)
		theValue.GetString(*this);
}
Example #3
0
_SharedPtr<OTAccount> OTAcctList::GetOrCreateAccount(OTPseudonym			& theServerNym,
												   const OTIdentifier	& ACCOUNT_OWNER_ID,
												   const OTIdentifier	& ASSET_TYPE_ID,
												   const OTIdentifier	& SERVER_ID,
												   bool					& bWasAcctCreated, // this will be set to true if the acct is created here. Otherwise set to false;
												   const int64_t             lStashTransNum/*=0*/)
{
	_SharedPtr<OTAccount> pRetVal;
	bWasAcctCreated = false;
	// ------------------------------------------------
	if (OTAccount::stash == m_AcctType)
	{
		if (lStashTransNum <= 0)
		{
			OTLog::Error("OTAcctList::GetOrCreateAccount: Failed attempt to create stash account without cron item #.\n");
			return pRetVal;
		}
	}

	// ------------------------------------------------
	// First, we'll see if there's already an account ID available for the requested asset type ID.
	//
	const OTString strAssetTypeID(ASSET_TYPE_ID);
	const std::string str_asset_type_id = strAssetTypeID.Get();

	// ------------------------------------
	OTString strAcctType;
	TranslateAccountTypeToString(m_AcctType, strAcctType);
	// ----------------------------------------------------------------

	mapOfStrings::iterator it_acct_ids = m_mapAcctIDs.find(str_asset_type_id);
	if (m_mapAcctIDs.end() != it_acct_ids) // Account ID *IS* already there for this asset type...
	{
		const std::string			str_account_id	= (*it_acct_ids).second; // grab account ID
		mapOfWeakAccounts::iterator	it_weak			= m_mapWeakAccts.find(str_account_id); // Try to find account pointer...

		if (m_mapWeakAccts.end() != it_weak)  // FOUND the weak ptr to the account! Maybe it's already loaded
		{
//			bool bSuccess = true;

			_WeakPtr<OTAccount>	pWeak	= (*it_weak).second; // first is acct ID, second is weak_ptr to account.

			try
			{
				_SharedPtr<OTAccount>	pShared(pWeak);

				// If success, then we have a shared pointer. But it's worrying (TODO) because this should have
				// gone out of scope and been destroyed by whoever ELSE was using it. The fact that it's still here...
				// well I'm glad not to double-load it, but I wonder why it's still here? And we aren't walking on anyone's
				// toes, right? If this were multi-threaded, then I'd explicitly lock a mutex here, honestly. But since things
				// happen one at a time on OT, I'll settle for a warning for now. I'm assuming that if the account's loaded
				// already somewhere, it's just a pointer sitting there, and we're not walking on each other's toes.
				//
				if (pShared)
				{
					OTLog::vOutput(0, "OTAcctList::GetOrCreateAccount: Warning: account (%s) was already in memory so I gave you a "
								   "pointer to the existing one. (But who else has a copy of it?) \n", str_account_id.c_str());
					return pShared;
				}

			}
			catch (...)
			{

			}


			// Though the weak pointer was there, the resource must have since been destroyed,
			// because I cannot lock a new shared ptr onto it.   :-(
			//
			// Therefore remove it from the map, and RE-LOAD IT.
			//
			m_mapWeakAccts.erase(it_weak);
		}

		// DIDN'T find the acct pointer, even though we had the ID.
		// (Or it was there, but we couldn't lock a shared_ptr onto it, so we erased it...)
		//
		// So let's load it now. After all, the Account ID *does* exist...
		//
		const OTString strAcctID(str_account_id.c_str());
		const OTIdentifier theAccountID(strAcctID);

		// The Account ID exists, but we don't have the pointer to a loaded account for it.
		// Soo.... let's load it.
		//
		OTAccount * pAccount = OTAccount::LoadExistingAccount(theAccountID, SERVER_ID);

		if (NULL == pAccount)
			OTLog::vError("OTAcctList::GetOrCreateAccount: Failed trying to load %s account with account ID: %s\n",
						  strAcctType.Get(),strAcctID.Get());
		else if (!pAccount->VerifySignature(theServerNym))
			OTLog::vError("OTAcctList::GetOrCreateAccount: Failed verifying server's signature on %s account with account ID: %s\n",
						  strAcctType.Get(),strAcctID.Get());
		else if (!pAccount->VerifyOwnerByID(ACCOUNT_OWNER_ID))
		{
			const OTString strOwnerID(ACCOUNT_OWNER_ID);
			OTLog::vError("OTAcctList::GetOrCreateAccount: Failed verifying owner ID (%s) on %s account ID: %s\n",
						  strOwnerID.Get(), strAcctType.Get(),strAcctID.Get());
		}
		else // SUCCESS loading the account...
		{
			OTLog::vOutput(3, "Successfully loaded %s account ID: %s Asset Type ID: %s\n",
						   strAcctType.Get(), strAcctID.Get(), str_asset_type_id.c_str());

			pRetVal								= _SharedPtr<OTAccount>(pAccount); // Create a shared pointer to the account, so it will be cleaned up automatically.
			m_mapWeakAccts [strAcctID.Get()]	= _WeakPtr<OTAccount>(pRetVal); // save a weak pointer to the acct, so we'll never load it twice, but we'll also know if it's been deleted.
		}
		return pRetVal;
		//
	} // (Asset Type ID was found on the AcctID Map -- a corresponding Account ID is already there for that asset type.)

	// ******************************************************************************

	// Not found... There's no account ID yet for that asset type ID.
	// That means we can create it...
	//
	OTMessage theMessage; // Here we set up theMessage
	ACCOUNT_OWNER_ID.GetString(theMessage.m_strNymID);
	ASSET_TYPE_ID.GetString(theMessage.m_strAssetID);
	SERVER_ID.GetString(theMessage.m_strServerID);

	OTAccount * pAccount = OTAccount::GenerateNewAccount(ACCOUNT_OWNER_ID,	// theUserID
														 SERVER_ID,			// theServerID
														 theServerNym,		// theServerNym
														 theMessage,
														 m_AcctType,		// OTAccount::voucher is default.
														 lStashTransNum);
	// ------------------------------------------------------------------------------------------

	if (NULL == pAccount)
		OTLog::vError(" OTAcctList::GetOrCreateAccount: Failed trying to generate %s account with asset type ID: %s\n",
					  strAcctType.Get(), str_asset_type_id.c_str());
	else // SUCCESS creating the account...
	{
		OTString strAcctID;
		pAccount->GetIdentifier(strAcctID);

		OTLog::vOutput(0, "Successfully created %s account ID: %s Asset Type ID: %s\n",
					   strAcctType.Get(), strAcctID.Get(), str_asset_type_id.c_str());

		pRetVal = _SharedPtr<OTAccount>(pAccount); // Create a shared pointer to the account, so it will be cleaned up automatically.

		m_mapWeakAccts	[strAcctID.Get()]				= _WeakPtr<OTAccount>(pRetVal); // save a weak pointer to the acct, so we'll never load it twice, but we'll also know if it's been deleted.
		m_mapAcctIDs	[theMessage.m_strAssetID.Get()]	= strAcctID.Get(); // Save the new acct ID in a map, keyed by asset type ID.

		bWasAcctCreated = true;
	}

	return pRetVal;
}