Skip to content

Commit

Permalink
Support object subtype query in remote device
Browse files Browse the repository at this point in the history
  • Loading branch information
szellmann authored and jeffamstutz committed Jul 21, 2023
1 parent 2174ee2 commit 80d6dd9
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 1 deletion.
58 changes: 57 additions & 1 deletion libs/remote_device/Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,31 @@ int Device::getProperty(ANARIObject object,

const char ** Device::getObjectSubtypes(ANARIDataType objectType)
{
return nullptr;
auto it = std::find_if(objectSubtypes.begin(),
objectSubtypes.end(),
[objectType](const ObjectSubtypes &os) {
return os.objectType == objectType;
});
if (it != objectSubtypes.end()) {
return (const char **)it->value.data();
}

auto buf = std::make_shared<Buffer>();
buf->write((const char *)&remoteDevice, sizeof(remoteDevice));
buf->write((const char *)&objectType, sizeof(objectType));
write(MessageType::GetObjectSubtypes, buf);

std::unique_lock l(syncObjectSubtypes.mtx);
syncObjectSubtypes.cv.wait(l, [this, &it, objectType]() {
it = std::find_if(objectSubtypes.begin(),
objectSubtypes.end(),
[&it, objectType](const ObjectSubtypes &os) {
return os.objectType == objectType;
});
return it != objectSubtypes.end();
});

return (const char **)it->value.data();
}

const void* Device::getObjectInfo(ANARIDataType objectType,
Expand Down Expand Up @@ -621,6 +645,12 @@ Device::~Device()
delete[] stringListProperties[i].value[j];
}
}

for (size_t i = 0; i < objectSubtypes.size(); ++i) {
for (size_t j = 0; j < objectSubtypes[i].value.size(); ++j) {
delete[] objectSubtypes[i].value[j];
}
}
}

ANARIObject Device::registerNewObject(ANARIDataType type, std::string subtype)
Expand Down Expand Up @@ -896,6 +926,32 @@ void Device::handleMessage(async::connection::reason reason,
syncProperties.cv.notify_all();

// LOG(logging::Level::Info) << "Property: " << property.name;
} else if (message->type() == MessageType::ObjectSubtypes) {
std::unique_lock l(syncObjectSubtypes.mtx);

Buffer buf;
buf.write(message->data(), message->size());
buf.seek(0);

ObjectSubtypes os;

buf.read((char *)&os.objectType, sizeof(os.objectType));

while (!buf.eof()) {
uint64_t strLen;
buf.read((char *)&strLen, sizeof(strLen));

char *subtype = new char[strLen + 1];
buf.read(subtype, strLen);
subtype[strLen] = '\0';
os.value.push_back(subtype);
}
os.value.push_back(nullptr);

objectSubtypes.push_back(os);

l.unlock();
syncObjectSubtypes.cv.notify_all();
} else if (message->type() == MessageType::ChannelColor
|| message->type() == MessageType::ChannelDepth) {
size_t off = 0;
Expand Down
15 changes: 15 additions & 0 deletions libs/remote_device/Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@ struct Device : anari::DeviceImpl, helium::ParameterizedObject
size_t which;
} syncProperties;

struct
{
std::mutex mtx;
std::condition_variable cv;
} syncObjectSubtypes;

ANARIDevice remoteDevice{nullptr};
std::string remoteSubtype = "default";

Expand All @@ -237,6 +243,15 @@ struct Device : anari::DeviceImpl, helium::ParameterizedObject
};
std::vector<StringListProperty> stringListProperties;

// Store subtypes; if the subtype was already queried,
// simply return the cached subtype list
struct ObjectSubtypes
{
ANARIDataType objectType;
std::vector<char *> value;
};
std::vector<ObjectSubtypes> objectSubtypes;

std::map<ANARIObject, Frame> frames;
std::map<ANARIArray, std::vector<char>> arrays;

Expand Down
36 changes: 36 additions & 0 deletions libs/remote_device/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,42 @@ struct Server
outbuf->write((const char *)mem.data(), size);
}
write(MessageType::Property, outbuf);
} else if (message->type() == MessageType::GetObjectSubtypes) {
LOG(logging::Level::Info) << "Message: GetObjectSubtypes, message size: "
<< prettyBytes(message->size());

Buffer buf;
buf.write(message->data(), message->size());
buf.seek(0);

Handle deviceHandle;
buf.read((char *)&deviceHandle, sizeof(deviceHandle));

ANARIDataType objectType;
buf.read((char *)&objectType, sizeof(objectType));

ANARIDevice dev = resourceManager.getDevice(deviceHandle);

if (!dev) {
LOG(logging::Level::Error)
<< "Server: invalid device: " << deviceHandle;
// manager->stop(); // legal?
return;
}

auto outbuf = std::make_shared<Buffer>();
outbuf->write((const char *)&objectType, sizeof(objectType));

const char **subtypes = anariGetObjectSubtypes(dev, objectType);

if (subtypes != nullptr) {
while (const char *str = *subtypes++) {
uint64_t strLen = strlen(str);
outbuf->write((const char *)&strLen, sizeof(strLen));
outbuf->write(str, strLen);
}
}
write(MessageType::ObjectSubtypes, outbuf);
} else {
LOG(logging::Level::Warning)
<< "Unhandled message of size: " << message->size();
Expand Down
2 changes: 2 additions & 0 deletions libs/remote_device/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ struct MessageType
FrameIsReady,
GetProperty,
Property,
GetObjectSubtypes,
ObjectSubtypes,
ChannelColor,
ChannelDepth,
};
Expand Down

0 comments on commit 80d6dd9

Please sign in to comment.