This example demonstrate how to render a scene using the Bridge.
#ifndef EXAMPLE_BRIDGE_H
#define EXAMPLE_BRIDGE_H
#include <sstream>
#include <string>
#include "example_element_and_job.h"
std::string bytes_to_string(
mi::Size size)
{
char buffer[256];
if( size > 1048576)
snprintf( buffer,
sizeof( buffer)-1,
"%.1f MB",
static_cast<mi::Float64>( size)/1048576.0);
else if( size > 1024)
snprintf( buffer,
sizeof( buffer)-1,
"%.1f kB",
static_cast<mi::Float64>( size)/1024.0);
else
snprintf( buffer, sizeof( buffer)-1, "%llu bytes", size);
return buffer;
}
{
public:
My_serializable(
mi::Uint64 data = 0) : m_data( data) { }
typedef mi::base::Uuid_t<0xc10b4a48,0xcf64,0x4355,0x9d,0x12,0xc7,0x71,0x9e,0xee,0x4c,0x93> IID;
private:
};
class My_referencing_element
:
public mi::neuraylib::Element<0xfbbab662,0x91e6,0x4712,0x92,0xa0,0x0c,0xa6,0x74,0xb9,0xb0,0x0b>
{
public:
: m_tag( tag), m_serializable( new My_serializable( data)) { }
{
serializer->
write( &m_tag);
serializer->
serialize( m_serializable.get());
}
{
deserializer->
read( &m_tag);
m_serializable = static_cast<My_serializable*>(
}
{
if( m_tag)
}
const char* get_class_name() const { return "My_referencing_element"; }
const My_serializable* get_serializable() const
{
m_serializable->retain();
return m_serializable.get();
}
private:
};
class My_bridge_job_clientside : public
mi::bridge::Client_job<0xc6822a21,0x45c7,0x448d,0x95,0x52,0x73,0xe6,0xfa,0x62,0xa3,0x60>
{
public:
My_bridge_job_clientside(
: m_input_tag( input_tag), m_serializable( new My_serializable( data)), m_result( 0) { }
{
serializer->
write( &m_input_tag);
serializer->
serialize( m_serializable.get());
}
{
deserializer->
read( &m_result);
m_condition.signal();
}
{
if( m_input_tag)
}
void error_callback(
mi::Sint32 error_code,
const char* error_message)
{
fprintf( stderr, "Error while executing job, error code: %d, error message: %s",
error_code, error_message);
m_condition.signal();
}
void wait() { m_condition.wait(); }
mi::Uint64 get_result()
const {
return m_result; }
private:
};
{
fprintf( stderr, "Bridge detecting changes: %llu modified element(s) detected.\n",
fprintf( stderr, "Bridge calculating hashes: %llu pending calculation(s), %s uploaded.\n",
fprintf( stderr, "Bridge querying cache status: %llu pending request(s), %s uploaded.\n",
fprintf( stderr, "Bridge upload: %s/%s (%.0lf%%) total - data serialization for "
"%llu element(s) pending.\n",
total_progress,
} else {
std::stringstream s;
if( name)
s << "\"" << name << "\"";
else
s << "unnamed element";
fprintf( stderr, "Bridge upload: %s/%s (%.0lf%%) total - %s/%s (%.0lf%%) for %s.\n",
total_progress,
bytes_to_string(
element_progress,
s.str().c_str());
}
fprintf( stderr, "Bridge waiting for server to finish processing the upload.\n");
fprintf( stderr, "Bridge upload completed.\n");
}
}
class My_bridge_job_serverside : public
mi::bridge::Server_job<0xc6822a21,0x45c7,0x448d,0x95,0x52,0x73,0xe6,0xfa,0x62,0xa3,0x60>
{
public:
{
deserializer->
read( &m_input_tag);
m_serializable = static_cast<My_serializable*>(
}
void execute(
{
dice_transaction->access<My_referencing_element>( m_input_tag));
dice_transaction->access<My_element>( referencing_element->get_tag()));
referencing_element->get_serializable());
mi::Uint64 serializable_data = serializable->get_data();
mi::Uint64 job_serializable_data = m_serializable->get_data();
mi::Uint64 result = element_data + serializable_data + job_serializable_data;
serializer->
write( &result);
}
private:
};
#endif
Conditions allow threads to signal an event and to wait for such a signal, respectively.
Definition: condition.h:33
Handle class template for interfaces, automatizing the lifetime control via reference counting.
Definition: handle.h:113
Mixin class template for deriving interface implementations.
Definition: interface_implement.h:41
Class template for a compile-time representation of universally unique identifiers (UUIDs or GUIDs).
Definition: uuid.h:122
This mixin class provides a default implementation for some of the methods needed by mi::bridge::ICli...
Definition: ibridge_client.h:336
virtual void get_references(neuraylib::ITag_set *result) const
Empty body. The default job references no elements.
Definition: ibridge_client.h:412
Provides detailed information about the progress of a Bridge job.
Definition: ibridge_client.h:136
virtual Client_job_state get_state() const =0
Returns the state of the job.
virtual const char * get_currently_uploaded_element_name() const =0
Returns the name of the currently uploaded element or NULL if no element is currently being uploaded.
virtual Size get_currently_uploaded_element_uploaded_bytes() const =0
Returns the number of bytes uploaded for the currently uploaded element or 0 if no element is current...
virtual Size get_pending_hash_calculation_count() const =0
Returns the number of elements for which hashes needs to be calculated.
virtual Size get_cache_miss_bytes() const =0
Returns the total amount of bytes to upload for all cache misses.
virtual Size get_pending_cache_status_count() const =0
Returns the number of cache status requests that the server has not yet replied to.
virtual Size get_updated_element_count() const =0
Returns the number of elements that need to be updated before executing this job.
virtual Size get_currently_uploaded_element_size() const =0
Returns the size in bytes of the currently uploaded element or 0 if no element is currently being upl...
virtual Size get_uploaded_cache_miss_bytes() const =0
Returns the number of bytes that has been uploaded for the cache misses so far.
virtual Size get_pending_data_serialization_count() const =0
Returns the number of elements that have been queued up for serialization.
Used to specify a set of elements by name.
Definition: ibridge_client.h:207
Provides additional information about a bridge job.
Definition: ibridge_server.h:219
Database transactions started on the client will be automatically mirrored to the server and exposed ...
Definition: ibridge_server.h:328
virtual neuraylib::ITransaction * get_database_transaction() const =0
Returns the local transaction corresponding to this Bridge transaction, or NULL if this transaction h...
This mixin class provides a default implementation for some of the methods needed by mi::bridge::ISer...
Definition: ibridge_server.h:137
This mixin class can be used to implement the mi::neuraylib::IElement interface.
Definition: dice.h:1542
virtual void get_references(ITag_set *result) const
Empty body, i.e., leaves result unaltered.
Definition: dice.h:1608
Source for deserializing objects from byte streams.
Definition: ideserializer.h:35
virtual bool read(bool *value, Size count=1)=0
Reads values of type bool from the deserializer.
virtual ISerializable * deserialize()=0
Reads a serializable object from the deserializer.
A transaction provides a consistent view on the database.
Definition: dice.h:272
This interface represents the abstract base class for all database elements.
Definition: dice.h:837
All serializable objects have to be derived from this interface.
Definition: iserializer.h:137
Target for serializing objects to byte streams.
Definition: iserializer.h:171
virtual bool write(const bool *value, Size count=1)=0
Writes values of type bool to the serializer.
virtual bool serialize(const ISerializable *serializable)=0
Writes a serializable object to the serializer.
Used to store a set of tags.
Definition: dice.h:210
virtual void add_tag(Tag_struct tag)=0
Adds tag to the tag set.
A tag represents a unique identifier for database elements in the database.
Definition: iserializer.h:54
unsigned long long Uint64
64-bit unsigned integer.
Definition: types.h:62
Uint64 Size
Unsigned integral type that is large enough to hold the size of all types.
Definition: types.h:112
double Float64
64-bit float.
Definition: types.h:52
signed int Sint32
32-bit signed integer.
Definition: types.h:46
Client_job_state
Job states of Bridge jobs.
Definition: ibridge_client.h:95
@ CLIENT_JOB_PENDING
Waiting for execution of the job to finish.
Definition: ibridge_client.h:123
@ CLIENT_JOB_DONE
The result has been received and deserialized by the client-side job and the job is done.
Definition: ibridge_client.h:126
@ CLIENT_JOB_DETECTING_CHANGES
Detecting which database elements needs to be updated before executing the job.
Definition: ibridge_client.h:97
@ CLIENT_JOB_PREPARING
Calculates hashes for the elements that will be updated and to some extent serialize data that will b...
Definition: ibridge_client.h:101
@ CLIENT_JOB_QUERYING_CACHE_STATUS
Determining how much data needs to be uploaded before executing this job.
Definition: ibridge_client.h:108
@ CLIENT_JOB_UPLOADING
Uploading data.
Definition: ibridge_client.h:115
const Tag NULL_TAG
This value of the tag represents an invalid tag which can not be accessed.
Definition: iserializer.h:128
A 128 bit representation of a universally unique identifier (UUID or GUID).
Definition: uuid.h:26
#include <iostream>
#include "example_shared.h"
#include "example_element_and_job.h"
#include "example_bridge.h"
class Application_session_handler
{
public:
Application_session_handler( const std::string& security_token)
: m_security_token( security_token) { }
{
return security_token && m_security_token == security_token;
}
private:
std::string m_security_token;
};
{
check_success( dice_configuration.is_valid_interface());
check_success( dice_configuration->register_serializable_class<My_element>());
check_success( dice_configuration->register_serializable_class<My_referencing_element>());
check_success( dice_configuration->register_serializable_class<My_serializable>());
}
void run(
const char* disk_cache_path,
const char* bridge_server_address,
const char* application_path,
const char* security_token,
const char* ssl_cert_file,
const char* ssl_private_key_file,
const char* ssl_password)
{
if( ssl_cert_file && ssl_private_key_file && ssl_password)
http_server->start_ssl(
bridge_server_address, ssl_cert_file, ssl_private_key_file, ssl_password);
else
http_server->start( bridge_server_address);
bridge_server->create_application( application_path, http_server.get()));
if( application.is_valid_interface()) {
application->set_disk_cache( disk_cache_path);
application->register_job<My_bridge_job_serverside>();
new Application_session_handler( security_token));
check_success( application->set_session_handler(
application_session_handler.get()) == 0);
application->open();
sleep_seconds( 60);
application->close();
}
http_server->shutdown();
}
int main( int argc, char* argv[])
{
if( argc != 5 && argc != 8) {
std::cerr << "Usage: example_bridge_server <disk_cache_path> <bridge_server_address> \\\n"
<< " <application_path> <security_token> [<ssl_cert_file> \\\n"
<< " <ssl_private_key_file> <ssl_password>]" << std::endl;
keep_console_open();
return EXIT_FAILURE;
}
const char* disk_cache_path = argv[1];
const char* bridge_server_address = argv[2];
const char* application_path = argv[3];
const char* security_token = argv[4];
const char* ssl_cert_file = argc >= 8 ? argv[5] : 0;
const char* ssl_private_key_file = argc >= 8 ? argv[6] : 0;
const char* ssl_password = argc >= 8 ? argv[7] : 0;
check_success( neuray.is_valid_interface());
configuration( neuray.get());
check_start_success( result);
run( neuray.get(), disk_cache_path, bridge_server_address, application_path, security_token,
ssl_cert_file, ssl_private_key_file, ssl_password);
check_success( neuray->
shutdown() == 0);
neuray = 0;
check_success( unload());
keep_console_open();
return EXIT_SUCCESS;
}
API component that serves as entry point for the server-side Bridge API.
Definition: ibridge_server.h:849
Represents the server side of a Bridge session.
Definition: ibridge_server.h:523
virtual const char * get_security_token() const =0
Returns the security token specified by the client.
The factory can be used to instantiate the built-in HTTP classes.
Definition: http.h:922
This interface allows configuration of DiCE.
Definition: dice.h:82
This is an object representing the DiCE library.
Definition: ineuray.h:44
virtual Sint32 shutdown(bool blocking=true)=0
Shuts down the library.
virtual base::IInterface * get_api_component(const base::Uuid &uuid) const =0
Returns an API component from the DiCE API.
virtual Sint32 start(bool blocking=true)=0
Starts the operation of the DiCE library.
#include <iostream>
#include "example_shared.h"
#include "example_element_and_job.h"
#include "example_bridge.h"
class Client_session_state_callback
{
public:
Client_session_state_callback() : m_connected( false) { }
{
m_condition.signal();
}
void wait() { m_condition.wait(); }
bool get_connected() const { return m_connected; }
private:
bool m_connected;
};
{
check_success( dice_configuration.is_valid_interface());
check_success( dice_configuration->register_serializable_class<My_element>());
check_success( dice_configuration->register_serializable_class<My_referencing_element>());
check_success( dice_configuration->register_serializable_class<My_serializable>());
}
{
check_success( database.is_valid_interface());
check_success( dice_transaction.is_valid_interface());
check_success( element_tag);
element = 0;
new My_referencing_element( element_tag, 43));
check_success( referencing_element_tag);
referencing_element = 0;
bridge_client->get_session( bridge_server_url, security_token));
new Client_session_state_callback());
client_session->add_session_state_callback( client_session_state_callback.get());
client_session_state_callback->wait();
if( client_session_state_callback->get_connected()) {
dice_transaction->get_interface<mi::neuraylib::ITransaction>());
new My_bridge_job_clientside( referencing_element_tag, 44));
mi::Sint32 result = client_session->execute( bridge_job.get(), transaction.get());
check_success( result == 0);
bridge_job->wait();
check_success( bridge_job->get_result() == 42 + 43 + 44);
}
client_session_state_callback = 0;
dice_transaction->commit();
}
int main( int argc, char* argv[])
{
if( argc != 3) {
std::cerr << "Usage: example_bridge_client <bridge_server_url> <security_token>"
<< std::endl;
keep_console_open();
return EXIT_FAILURE;
}
const char* bridge_server_url = argv[1];
const char* security_token = argv[2];
check_success( neuray.is_valid_interface());
configuration( neuray.get());
check_start_success( result);
run( neuray.get(), bridge_server_url, security_token);
check_success( neuray->
shutdown() == 0);
neuray = 0;
check_success( unload());
keep_console_open();
return EXIT_SUCCESS;
}
API component that serves as entry point for the client-side Bridge API.
Definition: ibridge_client.h:723
This interface is used to interact with the distributed database.
Definition: idatabase.h:293
Client_session_state
The different states a client session can be in.
Definition: ibridge_client.h:456
@ CLIENT_SESSION_CONNECTED
The session has successfully established a connection to the Bridge server and is ready for use.
Definition: ibridge_client.h:463