Registering to the dispatcher

Introduction

The REDnet dispatcher is a powerful tool to collect information about living peers on a network. Moreover, it centralizes all the network accesses to peers in a single place when viewed from client applications.

Peers which want to be listed by the dispatcher need to register themselves explicitly. This is done at start-up time by passing the dispatcher network address to the RNET::IPeer::Start method.

Note:

This tutorial needs the dispatcher application to be launched. It handles this automatically for you by creating a new process and passing it the right arguments at the very beginning of the main function. When the tutorial ends, the dispatcher is automatically killed.

Communicating with the dispatcher

The dispatcher must be run first. Then, the peers must know the exact network address (including the port) used by the dispatcher to directly connect to it. The initialization of the peer looks like:

// We choose to create a kind of default peer. There is no embedded web server
// but this time, we supply the network address of the dispatcher 
// (RNET::Address::LocalHost in our case because both dispatcher and peer are 
// running on the same machine).
if( ipeer->Start( RED::Object::GetIDFromString( "RegisteringToTheDispatcher" ),
                  1,                                // peer version
                  "",                               // webserver files path
                  0,                                // unlimited number of connections
                  18000,                            // communication port
                  RNET::Address::LocalHost( 18000 ) // set the local machine as the dispatcher address
                ) != RED_OK )
{
  printf( "[%s]: Error: REDnet failed to start the peer.\n", adapter->GetAddress().ToString().Buffer() );
  RNET::System::Shutdown();
  return RED_FAIL;
}

The main difference with previous tutorials is that we explicitly set the dispatcher address. To do so, we use the RNET::Address::LocalHost helper which returns the loop back interface address (a.k.a 'localhost'). By doing so, the peer starts and tries to connect immediately to the dispatcher. If the connection can't be established, the call will return with a ::RNET_FAIL error code. Using localhost enables the tutorial to run on any machine, even those without any hardware network interface. This is also forced for the dispatcher by using the -local command line parameter.

We can now notify the dispatcher that we are alive by setting the peer status using the RNET::IPeer::SetStatus method taken a status parameter. The value of the status variable is set by the user (read the next section for details). Each time the method is called, a message containing the peer status is posted to the dispatcher.

Main loop

The main loop of the tutorial is pretty simple. We let the user select which operation to perform:

This is handled by the code below:

RNET::IPeer* ipeer = peer->As< RNET::IPeer >();

char key = getc( stdin );

if( ( key == 'b' ) || ( key == 'B' ) )
{
  // Busy case.
  if( ipeer->SetStatus( RNET::RPS_BUSY, 100 ) != RED_OK )
    quit = true;
}
else if( ( key == 'r' ) || ( key == 'R' ) )
{
  // Ready case.
  if( ipeer->SetStatus( RNET::RPS_READY, 0 ) != RED_OK )
    quit = true;
}
else if( ( key == 'q' ) || ( key == 'Q' ) )
{
  // Quit case.
  quit = true;
}