Skip to content

Commit

Permalink
Make VaultAgeAddDevice non-blocking
Browse files Browse the repository at this point in the history
  • Loading branch information
dgelessus committed Sep 16, 2023
1 parent a141185 commit c2ed4b0
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 26 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 @@ -176,18 +176,21 @@ void pyAgeVault::AddChronicleEntry( const ST::string& name, uint32_t type, const
VaultAddAgeChronicleEntry(name, type, value);
}

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

// AGE DEVICES. AKA IMAGERS, WHATEVER.
// Add a new device.
void pyAgeVault::AddDevice(const ST::string& deviceName, PyObject * cbObject, uint32_t cbContext)
{
pyVaultNode::pyVaultNodeOperationCallback * cb = new pyVaultNode::pyVaultNodeOperationCallback( cbObject );
cb->VaultOperationStarted( cbContext );

if (hsRef<RelVaultNode> rvn = VaultAgeAddDeviceAndWait(deviceName))
cb->SetNode(rvn);

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

// Remove a device.
Expand Down
67 changes: 47 additions & 20 deletions Sources/Plasma/PubUtilLib/plVault/plVaultClientApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3668,31 +3668,58 @@ void VaultAddAgeChronicleEntry (
}

//============================================================================
hsRef<RelVaultNode> VaultAgeAddDeviceAndWait (const ST::string& deviceName) {
if (hsRef<RelVaultNode> existing = VaultAgeGetDevice(deviceName))
return existing;
hsRef<RelVaultNode> device, folder;

for (;;) {
folder = VaultGetAgeDevicesFolder();
if (!folder)
break;
namespace _VaultAgeAddDevice
{
struct _Params
{
ST::string deviceName;
FVaultAgeAddDeviceCallback callback;
void* param;
hsRef<RelVaultNode> folder;
hsRef<RelVaultNode> device;
};

ENetError result;
device = VaultCreateNodeAndWait(plVault::kNodeType_TextNote, &result);
if (!device)
break;
static void _AddFolderDeviceChildCallback(ENetError result, void* param)
{
_Params* p = static_cast<_Params*>(param);
p->callback(result, IS_NET_SUCCESS(result) ? std::move(p->device) : nullptr, p->param);
delete p;
}

VaultTextNoteNode access(device);
access.SetNoteType(plVault::kNoteType_Device);
access.SetNoteTitle(deviceName);
static void _CreateDeviceCallback(ENetError result, void* state, void* param, hsWeakRef<RelVaultNode> node)
{
_Params* p = static_cast<_Params*>(param);
if (IS_NET_SUCCESS(result)) {
VaultTextNoteNode access(node);
access.SetNoteType(plVault::kNoteType_Device);
access.SetNoteTitle(p->deviceName);

VaultAddChildNodeAndWait(folder->GetNodeId(), device->GetNodeId(), 0);
break;
p->device = node;
VaultAddChildNode(p->folder->GetNodeId(), node->GetNodeId(), 0, _AddFolderDeviceChildCallback, p);
} else {
p->callback(result, nullptr, p->param);
delete p;
}
}
}

void VaultAgeAddDevice(const ST::string& deviceName, FVaultAgeAddDeviceCallback callback, void* param)
{
using namespace _VaultAgeAddDevice;

if (hsRef<RelVaultNode> existing = VaultAgeGetDevice(deviceName)) {
callback(kNetSuccess, std::move(existing), param);
return;
}

hsRef<RelVaultNode> folder = VaultGetAgeDevicesFolder();
if (!folder) {
callback(kNetErrVaultNodeNotFound, nullptr, param);
return;
}

return device;
_Params* p = new _Params {deviceName, callback, param, std::move(folder)};
VaultCreateNode(plVault::kNodeType_TextNote, _CreateDeviceCallback, 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 @@ -404,7 +404,8 @@ void VaultAddAgeChronicleEntry (
int entryType,
const ST::string& entryValue
);
hsRef<RelVaultNode> VaultAgeAddDeviceAndWait(const ST::string& deviceName); // blocks until completion
typedef void (*FVaultAgeAddDeviceCallback)(ENetError result, hsRef<RelVaultNode> device, void* param);
void VaultAgeAddDevice(const ST::string& deviceName, FVaultAgeAddDeviceCallback callback, void* param);
void VaultAgeRemoveDevice (const ST::string& deviceName);
bool VaultAgeHasDevice (const ST::string& deviceName);
hsRef<RelVaultNode> VaultAgeGetDevice(const ST::string& deviceName);
Expand Down

0 comments on commit c2ed4b0

Please sign in to comment.