Skip to content

Commit

Permalink
Make VaultAgeSetDeviceInbox non-blocking
Browse files Browse the repository at this point in the history
  • Loading branch information
dgelessus committed Sep 16, 2023
1 parent 9cce806 commit a141185
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 33 deletions.
13 changes: 8 additions & 5 deletions Sources/Plasma/FeatureLib/pfPython/pyAgeVault.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,17 +210,20 @@ PyObject * pyAgeVault::GetDevice(const ST::string& deviceName)
PYTHON_RETURN_NONE;
}

static void _SetDeviceInboxCallback(ENetError result, hsRef<RelVaultNode> inbox, void* param)
{
auto cb = static_cast<pyVaultNode::pyVaultNodeOperationCallback*>(param);
cb->SetNode(std::move(inbox));
cb->VaultOperationComplete(result);
}

// Sets the inbox associated with a device.
void pyAgeVault::SetDeviceInbox(const ST::string& deviceName, const ST::string& inboxName, PyObject * cbObject, uint32_t cbContext)
{
pyVaultNode::pyVaultNodeOperationCallback * cb = new pyVaultNode::pyVaultNodeOperationCallback( cbObject );
cb->VaultOperationStarted( cbContext );

if (hsRef<RelVaultNode> rvn = VaultAgeSetDeviceInboxAndWait(deviceName, inboxName))
cb->SetNode(rvn);

// cb deletes itself here.
cb->VaultOperationComplete(cbContext, cb->GetNode() ? kNetSuccess : kNetErrInternalError);
VaultAgeSetDeviceInbox(deviceName, inboxName, _SetDeviceInboxCallback, cb);
}

PyObject * pyAgeVault::GetDeviceInbox(const ST::string& deviceName)
Expand Down
13 changes: 8 additions & 5 deletions Sources/Plasma/FeatureLib/pfPython/pyVaultTextNoteNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ PyObject * pyVaultTextNoteNode::GetDeviceInbox() const
PYTHON_RETURN_NONE;
}

static void _SetDeviceInboxCallback(ENetError result, hsRef<RelVaultNode> inbox, void* param)
{
auto cb = static_cast<pyVaultNode::pyVaultNodeOperationCallback*>(param);
cb->SetNode(std::move(inbox));
cb->VaultOperationComplete(result);
}

void pyVaultTextNoteNode::SetDeviceInbox(const ST::string& devName, PyObject * cbObject, uint32_t cbContext)
{
if (!fNode)
Expand All @@ -152,9 +159,5 @@ void pyVaultTextNoteNode::SetDeviceInbox(const ST::string& devName, PyObject * c
pyVaultNode::pyVaultNodeOperationCallback * cb = new pyVaultNode::pyVaultNodeOperationCallback( cbObject );
cb->VaultOperationStarted( cbContext );

if (hsRef<RelVaultNode> rvn = VaultAgeSetDeviceInboxAndWait(devName, DEFAULT_DEVICE_INBOX))
cb->SetNode(rvn);

// cb deletes itself here.
cb->VaultOperationComplete(cbContext, cb->GetNode() ? kNetSuccess : kNetErrInternalError);
VaultAgeSetDeviceInbox(devName, DEFAULT_DEVICE_INBOX, _SetDeviceInboxCallback, cb);
}
74 changes: 52 additions & 22 deletions Sources/Plasma/PubUtilLib/plVault/plVaultClientApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3740,36 +3740,66 @@ hsRef<RelVaultNode> VaultAgeGetDevice (const ST::string& deviceName) {
}

//============================================================================
hsRef<RelVaultNode> VaultAgeSetDeviceInboxAndWait (const ST::string& deviceName, const ST::string& inboxName) {
namespace _VaultAgeSetDeviceInbox
{
struct _Params
{
ST::string inboxName;
FVaultAgeSetDeviceInboxCallback callback;
void* param;
hsRef<RelVaultNode> device;
hsRef<RelVaultNode> inbox;
};

static void _AddDeviceInboxChildCallback(ENetError result, void* param)
{
_Params* p = static_cast<_Params*>(param);
p->callback(result, IS_NET_SUCCESS(result) ? std::move(p->inbox) : nullptr, p->param);
delete p;
}

static void _CreateDefaultInboxCallback(ENetError result, void* state, void* param, hsWeakRef<RelVaultNode> node)
{
_Params* p = static_cast<_Params*>(param);
if (IS_NET_SUCCESS(result)) {
VaultFolderNode access(node);
access.SetFolderName(p->inboxName);
access.SetFolderType(plVault::kDeviceInboxFolder);

p->inbox = node;
VaultAddChildNode(p->device->GetNodeId(), node->GetNodeId(), 0, _AddDeviceInboxChildCallback, p);
} else {
p->callback(result, nullptr, p->param);
delete p;
}
}
}

void VaultAgeSetDeviceInbox(const ST::string& deviceName, const ST::string& inboxName, FVaultAgeSetDeviceInboxCallback callback, void* param)
{
using namespace _VaultAgeSetDeviceInbox;

s_ageDeviceInboxes[deviceName] = inboxName;

// if we found the inbox or its a global inbox then return here, otherwise if its the default inbox and
// it wasn't found then continue on and create the inbox
hsRef<RelVaultNode> existing = VaultAgeGetDeviceInbox(deviceName);
if (existing || inboxName != DEFAULT_DEVICE_INBOX)
return existing;

hsRef<RelVaultNode> device, inbox;

for (;;) {
device = VaultAgeGetDevice(deviceName);
if (!device)
break;

ENetError result;
inbox = VaultCreateNodeAndWait(plVault::kNodeType_Folder, &result);
if (!inbox)
break;

VaultFolderNode access(inbox);
access.SetFolderName(inboxName);
access.SetFolderType(plVault::kDeviceInboxFolder);
if (existing) {
callback(kNetSuccess, std::move(existing), param);
return;
} else if (inboxName != DEFAULT_DEVICE_INBOX) {
callback(kNetErrVaultNodeNotFound, nullptr, param);
return;
}

VaultAddChildNodeAndWait(device->GetNodeId(), inbox->GetNodeId(), 0);
break;
hsRef<RelVaultNode> device = VaultAgeGetDevice(deviceName);
if (!device) {
callback(kNetErrVaultNodeNotFound, nullptr, param);
return;
}

return inbox;
_Params* p = new _Params {inboxName, callback, param, std::move(device)};
VaultCreateNode(plVault::kNodeType_Folder, _CreateDefaultInboxCallback, nullptr, p);
}

//============================================================================
Expand Down
3 changes: 2 additions & 1 deletion Sources/Plasma/PubUtilLib/plVault/plVaultClientApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,8 @@ hsRef<RelVaultNode> VaultAgeAddDeviceAndWait(const ST::string& deviceName); //
void VaultAgeRemoveDevice (const ST::string& deviceName);
bool VaultAgeHasDevice (const ST::string& deviceName);
hsRef<RelVaultNode> VaultAgeGetDevice(const ST::string& deviceName);
hsRef<RelVaultNode> VaultAgeSetDeviceInboxAndWait(const ST::string& deviceName, const ST::string& inboxName); // blocks until completion
typedef void (*FVaultAgeSetDeviceInboxCallback)(ENetError result, hsRef<RelVaultNode> inbox, void* param);
void VaultAgeSetDeviceInbox(const ST::string& deviceName, const ST::string& inboxName, FVaultAgeSetDeviceInboxCallback callback, void* param);
hsRef<RelVaultNode> VaultAgeGetDeviceInbox(const ST::string& deviceName);
void VaultClearDeviceInboxMap ();

Expand Down

0 comments on commit a141185

Please sign in to comment.