This example starts the DiCE API in different networking modes.
#include <iostream>
#include "example_shared.h"
{
public:
void connection_callback(
mi::Uint32 host_id,
bool flag)
{
if( flag) {
fprintf( stderr,
"The connection to the cluster was established. Own host id is %u.\n", host_id);
m_own_host_id = host_id;
}
else
fprintf( stderr, "The connection to the cluster was lost.\n");
}
void membership_callback(
mi::Uint32 host_id,
bool flag)
{
if( flag)
fprintf( stderr, "Host %u joined the cluster.\n", host_id);
else
fprintf( stderr, "Host %u left the cluster.\n", host_id);
}
{
fprintf( stderr, "Host %u communicated its properties:\n", host_id);
if( value.is_valid_interface())
fprintf( stderr, " application_name: %s\n", value->get_c_str());
}
{
fprintf( stderr, "The synchronizer is now host %u", host_id);
if( m_own_host_id == host_id)
fprintf( stderr, " (this host)");
fprintf( stderr, ".\n");
}
void database_status_callback( const char* status)
{
fprintf( stderr, "The database reports its status as \"%s\".\n", status);
}
private:
};
void configuration(
int argc,
char* argv[])
{
network_configuration->register_host_callback( host_callback);
const char* mode = argv[1];
if( strcmp( mode, ".") == 0) {
;
} else if( strcmp( mode, "OFF") == 0) {
check_success( network_configuration->set_mode(
} else if( strcmp( mode, "TCP") == 0) {
check_success( network_configuration->set_mode(
} else if( strcmp( mode, "UDP") == 0) {
check_success( network_configuration->set_mode(
} else if( strcmp( mode, "TCP_WITH_DISCOVERY") == 0) {
check_success( network_configuration->set_mode(
} else {
check_success( false);
}
if( strcmp( argv[2], ".") != 0)
check_success( network_configuration->set_multicast_address( argv[2]) == 0);
if( strcmp( argv[3], ".") != 0)
check_success( network_configuration->set_cluster_interface( argv[3]) == 0);
if( strcmp( argv[4], ".") != 0)
check_success( network_configuration->set_discovery_address( argv[4]) == 0);
for( int i = 5; i < argc; ++i)
check_success( network_configuration->add_configured_host( argv[i]) == 0);
general_configuration->set_host_property( "application_name", "example_networking");
}
{
switch( mode) {
fprintf( stderr, "mode: OFF\n");
break;
fprintf( stderr, "mode: TCP\n");
break;
fprintf( stderr, "mode: UDP\n");
break;
fprintf( stderr, "mode: TCP_WITH_DISCOVERY\n");
break;
default:
fprintf( stderr, "mode: error\n");
break;
}
fprintf( stderr, "multicast address: %s\n", string->get_c_str());
string = network_configuration->get_cluster_interface();
fprintf( stderr, "cluster interface: %s\n", string->get_c_str());
string = network_configuration->get_discovery_address();
fprintf( stderr, "discovery address: %s\n", string->get_c_str());
fprintf( stderr, "configured hosts: ");
for(
mi::Uint32 i = 0; i < network_configuration->get_number_of_configured_hosts(); ++i) {
string = network_configuration->get_configured_host( i);
fprintf( stderr, "%s ", string->get_c_str());
}
fprintf( stderr, "\n");
}
int main( int argc, char* argv[])
{
if( argc < 5) {
std::cerr << "Usage: example_networking <mode> <multicast_address> <cluster_interface>\\\n"
<< " <discovery_address> [<host1> <host2> ... <hostN>]" << std::endl;
keep_console_open();
return EXIT_FAILURE;
}
check_success( neuray.is_valid_interface());
configuration( neuray.get(), host_callback.get(), argc, argv);
print_configuration( neuray.get());
check_start_success( result);
sleep_seconds( 30);
check_success( neuray->
shutdown() == 0);
network_configuration->unregister_host_callback( host_callback.get());
network_configuration = 0;
neuray = 0;
check_success( unload());
keep_console_open();
return EXIT_SUCCESS;
}
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
This interface is used to query and change the general configuration.
Definition: igeneral_configuration.h:25
Abstract interface to report cluster status changes.
Definition: ihost_callback.h:35
This interface contains information about a local or remote host.
Definition: ihost_properties.h:27
virtual const IString * get_property(const char *key) const =0
Generic access to properties.
This interface is used to query and change the networking configuration.
Definition: inetwork_configuration.h:33
Mode
Constants for the networking mode.
Definition: inetwork_configuration.h:42
@ MODE_UDP
Networking is switched to UDP mode with multicast.
Definition: inetwork_configuration.h:53
@ MODE_OFF
Networking is switched off.
Definition: inetwork_configuration.h:44
@ MODE_TCP_WITH_DISCOVERY
Networking is using TCP/IP connections between hosts.
Definition: inetwork_configuration.h:57
@ MODE_TCP
Networking is using TCP/IP connections between hosts.
Definition: inetwork_configuration.h:48
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.
unsigned int Uint32
32-bit unsigned integer.
Definition: types.h:49
signed int Sint32
32-bit signed integer.
Definition: types.h:46