Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some smaller usability and compile fixes to the remote server app #134

Merged
merged 4 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libs/remote_device/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ target_link_libraries(anari-remote-server

## Installation ##

install(TARGETS ${PROJECT_NAME}
install(TARGETS ${PROJECT_NAME} anari-remote-server
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
Expand Down
1 change: 1 addition & 0 deletions libs/remote_device/Compression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <turbojpeg.h>
#endif
#include <map>
#include <cstring>
#include "Logging.h"

namespace remote {
Expand Down
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
41 changes: 40 additions & 1 deletion 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 All @@ -988,6 +1024,7 @@ static void printUsage()
{
std::cout << "./anari-remote-server [{--help|-h}]\n"
<< " [{--verbose|-v}]\n"
<< " [{--library|-l} <ANARI library>]\n"
<< " [{--port|-p} <N>]\n";
}

Expand All @@ -1000,7 +1037,9 @@ static void parseCommandLine(int argc, char *argv[])
if (arg == "--help" || arg == "-h") {
printUsage();
std::exit(0);
} else if (arg == "-p" || arg == "--port")
} else if (arg == "-l" || arg == "--library")
g_libraryType = argv[++i];
else if (arg == "-p" || arg == "--port")
g_port = std::stoi(argv[++i]);
}
}
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