From a921f766d156a19417b32cd0c0ec69398b99be4e Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Sat, 23 Apr 2011 22:19:35 -0400 Subject: [PATCH] Async creation of child ages --- .../plNetClient/plNetLinkingMgr.cpp | 23 ++- .../PubUtilLib/plVault/plVaultClientApi.cpp | 144 ++++++++++++++++++ .../PubUtilLib/plVault/plVaultClientApi.h | 1 + 3 files changed, 161 insertions(+), 7 deletions(-) diff --git a/Sources/Plasma/PubUtilLib/plNetClient/plNetLinkingMgr.cpp b/Sources/Plasma/PubUtilLib/plNetClient/plNetLinkingMgr.cpp index 35ed3a5b..fe757c05 100644 --- a/Sources/Plasma/PubUtilLib/plNetClient/plNetLinkingMgr.cpp +++ b/Sources/Plasma/PubUtilLib/plNetClient/plNetLinkingMgr.cpp @@ -480,6 +480,7 @@ bool plNetLinkingMgr::IProcessVaultNotifyMsg(plVaultNotifyMsg* msg) RelVaultNode* cVaultLink = nil; switch (msg->GetType()) { + case plVaultNotifyMsg::kRegisteredChildAgeLink: case plVaultNotifyMsg::kRegisteredOwnedAge: case plVaultNotifyMsg::kRegisteredSubAgeLink: cVaultLink = VaultGetNodeIncRef(msg->GetArgs()->GetInt(plNetCommon::VaultTaskArgs::kAgeLinkNode)); @@ -1047,17 +1048,25 @@ UInt8 plNetLinkingMgr::IPreProcessLink(void) { plAgeLinkStruct childLink; wchar parentAgeName[MAX_PATH]; - if (link->HasParentAgeFilename()) { + if (link->HasParentAgeFilename()) StrToUnicode(parentAgeName, link->GetParentAgeFilename(), arrsize(parentAgeName)); - if(!VaultAgeFindOrCreateChildAgeLinkAndWait(parentAgeName, info, &childLink)) - success = kLinkFailed; - } - else { - if(!VaultAgeFindOrCreateChildAgeLinkAndWait(nil, info, &childLink)) + + switch(VaultAgeFindOrCreateChildAgeLink( + (link->HasParentAgeFilename() ? parentAgeName : nil), + info, + &childLink)) + { + case hsFail: success = kLinkFailed; + break; + case FALSE: + success = kLinkDeferred; + break; + case TRUE: + success = kLinkImmediately; } - if (success != kLinkFailed) + if (success == kLinkImmediately) info->CopyFrom(childLink.GetAgeInfo()); } break; diff --git a/Sources/Plasma/PubUtilLib/plVault/plVaultClientApi.cpp b/Sources/Plasma/PubUtilLib/plVault/plVaultClientApi.cpp index a9701dfd..a49a8b6b 100644 --- a/Sources/Plasma/PubUtilLib/plVault/plVaultClientApi.cpp +++ b/Sources/Plasma/PubUtilLib/plVault/plVaultClientApi.cpp @@ -4796,6 +4796,150 @@ bool VaultAgeFindOrCreateChildAgeLinkAndWait ( return true; } +//============================================================================ +namespace _VaultCreateChildAge { + struct _Params { + UInt32* fChildAgesFldr; + UInt32* fAgeInfoId; + + ~_Params() { + DEL(fChildAgesFldr); + DEL(fAgeInfoId); + } + }; + + void _CreateNodeCallback(ENetError result, void* state, void* param, RelVaultNode* node) { + if (IS_NET_ERROR(result)) { + LogMsg(kLogError, "CreateChildAge: Failed to create AgeLink (async)"); + DEL(param); + return; + } + + _Params* p = (_Params*)param; + + // Add the children to the right places + VaultAddChildNode(node->nodeId, *p->fAgeInfoId, 0, nil, nil); + VaultAddChildNode(*p->fChildAgesFldr, *p->fAgeInfoId, 0, nil, nil); + + // Send the VaultNotify that the plNetLinkingMgr wants... + plVaultNotifyMsg * msg = NEWZERO(plVaultNotifyMsg); + msg->SetType(plVaultNotifyMsg::kRegisteredChildAgeLink); + msg->SetResultCode(result); + msg->GetArgs()->AddInt(plNetCommon::VaultTaskArgs::kAgeLinkNode, node->nodeId); + msg->Send(); + + DEL(param); + } + + void _DownloadCallback(ENetError result, void* param) { + if (IS_NET_ERROR(result)) { + LogMsg(kLogError, "CreateChildAge: Failed to download age vault (async)"); + DEL(param); + return; + } + + // Create the AgeLink node + VaultCreateNode(plVault::kNodeType_AgeLink, + (FVaultCreateNodeCallback)_CreateNodeCallback, + nil, + param + ); + } + + void _InitAgeCallback(ENetError result, void* state, void* param, UInt32 ageVaultId, UInt32 ageInfoId) { + if (IS_NET_ERROR(result)) { + LogMsg(kLogError, "CreateChildAge: Failed to init age (async)"); + DEL(param); + return; + } + + _Params* p = (_Params*)param; + p->fAgeInfoId = TRACKED_NEW UInt32(ageInfoId); + + // Download age vault + VaultDownload(L"CreateChildAge", + ageInfoId, + (FVaultDownloadCallback)_DownloadCallback, + param, + nil, + nil + ); + } +}; // namespace _VaultCreateAge + +UInt8 VaultAgeFindOrCreateChildAgeLink( + const wchar parentAgeName[], + const plAgeInfoStruct* info, + plAgeLinkStruct* link) +{ + using namespace _VaultCreateChildAge; + + // First, try to find an already existing ChildAge + char name[MAX_PATH]; + StrToAnsi(name, parentAgeName, arrsize(parentAgeName)); + plAgeInfoStruct search; + search.SetAgeFilename(name); + + RelVaultNode* rvnParentInfo = nil; + if (RelVaultNode* rvnParentLink = VaultGetOwnedAgeLinkIncRef(&search)) { + rvnParentInfo = rvnParentLink->GetChildNodeIncRef(plVault::kNodeType_AgeInfo, 1); + rvnParentLink->DecRef(); + } else // Fallback to current age + rvnParentInfo = VaultGetAgeInfoNodeIncRef(); + + // Test to make sure nothing went horribly wrong... + if (rvnParentInfo == nil) { + LogMsg(kLogError, "CreateChildAge: Couldn't find the parent ageinfo (async)"); + return hsFail; + } + + // Still here? Try to find the Child Ages folder + UInt8 retval = hsFail; + if (RelVaultNode* rvnChildAges = rvnParentInfo->GetChildAgeInfoListNodeIncRef(plVault::kChildAgesFolder, 1)) { + const char* ageName = info->GetAgeFilename(); + wchar hack[MAX_PATH]; + StrToUnicode(hack, ageName, arrsize(ageName)); + + // Search for our age + NetVaultNode* temp = NEWZERO(NetVaultNode); + temp->SetNodeType(plVault::kNodeType_AgeInfo); + VaultAgeInfoNode theAge(temp); + theAge.SetAgeFilename(hack); + + if (RelVaultNode* rvnAgeInfo = rvnChildAges->GetChildNodeIncRef(temp, 2)) { + RelVaultNode* rvnAgeLink = rvnAgeInfo->GetParentAgeLinkIncRef(); + + VaultAgeLinkNode accAgeLink(rvnAgeLink); + accAgeLink.CopyTo(link); + VaultAgeInfoNode accAgeInfo(rvnAgeInfo); + accAgeInfo.CopyTo(link->GetAgeInfo()); + + rvnAgeLink->DecRef(); + rvnAgeInfo->DecRef(); + + retval = TRUE; + } else { + _Params* p = TRACKED_NEW _Params; + p->fChildAgesFldr = TRACKED_NEW UInt32(rvnChildAges->nodeId); + + VaultAgeInfoNode accParentInfo(rvnParentInfo); + VaultInitAge(info, + accParentInfo.ageInstUuid, + (FVaultInitAgeCallback)_InitAgeCallback, + nil, + p + ); + } + + temp->DecRef(); + rvnChildAges->DecRef(); + retval = FALSE; + } + + rvnParentInfo->DecRef(); + return retval; +} + /***************************************************************************** * diff --git a/Sources/Plasma/PubUtilLib/plVault/plVaultClientApi.h b/Sources/Plasma/PubUtilLib/plVault/plVaultClientApi.h index f83226fb..2887202b 100644 --- a/Sources/Plasma/PubUtilLib/plVault/plVaultClientApi.h +++ b/Sources/Plasma/PubUtilLib/plVault/plVaultClientApi.h @@ -424,6 +424,7 @@ bool VaultAgeFindOrCreateChildAgeLinkAndWait ( const plAgeInfoStruct * info, plAgeLinkStruct * link ); +UInt8 VaultAgeFindOrCreateChildAgeLink(const wchar parentAgeName[], const plAgeInfoStruct* info, plAgeLinkStruct* link);