This example demonstrates the distributed cache.
#ifndef EXAMPLE_DISTRIBUTED_CACHE_H
#define EXAMPLE_DISTRIBUTED_CACHE_H
#include <iostream>
#include <map>
#include "example_shared.h"
class IMy_serializable : public
mi::base::Interface_declare<0x693c4b52,0x593d,0x407b,0xab,0x2a,0x5a,0x82,0x1c,0x8c,0xd1,0xc5,
mi::neuraylib::ISerializable>
{
public:
};
class My_serializable_1
:
public mi::neuraylib::Base<0xa07af7f8,0x9002,0x42fa,0x84,0x15,0x11,0x2a,0xae,0x10,0xca,0x3f,
IMy_serializable>
{
public:
void destroy() { delete this; }
void set_data(
mi::Uint64 data) { m_data = data; }
private:
};
class My_serializable_2
:
public mi::neuraylib::Base<0xe7ce6357,0xd8bb,0x4dfb,0xab,0xce,0x37,0x76,0x1c,0x80,0xba,0x70,
IMy_serializable>
{
public:
void destroy() { delete this; }
void set_data(
mi::Uint64 data) { m_data = data; }
private:
};
{
public:
Object_receiver(
mi::Uint64 receiver_id) : m_next_id( 0), m_receiver_id( receiver_id) { }
{
IMy_serializable* object;
if( class_id == My_serializable_1::IID())
object = new My_serializable_1();
else if( class_id == My_serializable_2::IID())
object = new My_serializable_2();
else {
check_success( false);
return 0;
}
object->deserialize( deserializer);
m_objects[id] = object;
"Receiver with ID %llu created object with ID %llu at address %p with data %llu.",
m_receiver_id, id, object, object->get_data());
return id;
}
void destroy_object(
mi::Uint64 id,
bool owner_died)
{
IMy_serializable* object = m_objects[id];
m_objects.erase( id);
"Receiver with ID %llu destroyed object with ID %llu at address %p with data %llu "
"(owner died: %s)",
m_receiver_id, id, object, object->get_data(), owner_died ? "yes" : "no");
object->release();
}
private:
std::map<mi::Uint64, IMy_serializable*> m_objects;
};
#endif
Mixin class template for deriving new interface declarations.
Definition: interface_declare.h:43
Mixin class template for deriving interface implementations.
Definition: interface_implement.h:41
This mixin class can be used to implement the mi::base::IInterface interface.
Definition: dice.h:1462
virtual base::Uuid get_class_id() const
Returns the class ID corresponding to the template parameters of this mixin class.
Definition: dice.h:1525
base::Uuid_t< id1, ... > IID
Declares the interface ID.
Definition: dice.h:1468
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.
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.
void printf(Message_severity level, const char *module_category, const char *message,...) __attribute__((format(printf
Emits a message to the application's log.
@ MESSAGE_SEVERITY_INFO
This is a normal operational message.
Definition: enums.h:39
unsigned long long Uint64
64-bit unsigned integer.
Definition: types.h:62
A 128 bit representation of a universally unique identifier (UUID or GUID).
Definition: uuid.h:26
#include <iostream>
#include <map>
#include "example_shared.h"
#include "example_distributed_cache.h"
{
check_success( network_configuration->set_mode(
check_success( network_configuration->set_multicast_address( multicast_address) == 0);
check_success( dice_configuration.is_valid_interface());
check_success( dice_configuration->register_serializable_class<My_serializable_1>());
check_success( dice_configuration->register_serializable_class<My_serializable_2>());
}
{
IMy_serializable** objects = new IMy_serializable*[N];
IMy_serializable* object = ( (i+1)%2 )
? static_cast<IMy_serializable*>( new My_serializable_1)
: static_cast<IMy_serializable*>( new My_serializable_2);
object->set_data( i*i);
"Storing object with index %llu at address %p with data %llu.",
i, object, object->get_data());
distributed_cache->store_object( object);
objects[i] = object;
sleep_seconds( 1);
}
IMy_serializable* object = objects[i];
objects[i] = 0;
"Removing object with index %llu at address %p with data %llu.",
i, object, object->get_data());
distributed_cache->remove_object( object);
object->release();
sleep_seconds( 1);
}
delete[] objects;
}
{
Object_receiver object_receiver_1( 1);
Object_receiver object_receiver_2( 2);
distributed_cache->register_receiver( My_serializable_1::IID(), &object_receiver_1);
distributed_cache->register_receiver( My_serializable_2::IID(), &object_receiver_2);
distributed_cache->unregister_receiver( My_serializable_1::IID());
distributed_cache->unregister_receiver( My_serializable_2::IID());
}
int main( int argc, char* argv[])
{
if( argc != 4 || ((strcmp( argv[1], "client") != 0) && (strcmp( argv[1], "worker") != 0))) {
std::cerr << "Usage: example_distributed_cache <mode> <multicast_address> <N>" << std::endl;
keep_console_open();
return EXIT_FAILURE;
}
bool is_worker = strcmp( argv[1], "worker") == 0;
const char* multicast_address = argv[2];
check_success( neuray.is_valid_interface());
configuration( neuray.get(), multicast_address);
g_logger = logging_configuration->get_forwarding_logger();
logging_configuration = 0;
check_start_success( result);
is_worker ? run_worker( neuray.get(), N) : run_client( neuray.get(), N);
check_success( neuray->
shutdown() == 0);
g_logger = 0;
neuray = 0;
check_success( unload());
keep_console_open();
return EXIT_SUCCESS;
}
This interface allows configuration of DiCE.
Definition: dice.h:82
The distributed cache service can be used to create temporary objects in a cluster.
Definition: idistcache.h:72
This interface is used for configuring the logging for the DiCE library.
Definition: ilogging_configuration.h:68
This interface is used to query and change the networking configuration.
Definition: inetwork_configuration.h:33
@ MODE_UDP
Networking is switched to UDP mode with multicast.
Definition: inetwork_configuration.h:53
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.
virtual void message(Message_severity level, const char *module_category, const char *message)
Emits a message to the application's log.
Definition: ilogger.h:215
float Float32
32-bit float.
Definition: types.h:51
signed int Sint32
32-bit signed integer.
Definition: types.h:46