![]() |
An Example of a Maestro Application |
The program in the example below defines a subclass (called Mbr) of the Maestro_GroupMember class. Note that multicast and view callbacks (which are no-op in the base class) are overloaded to implement application-specific functionality.
In the main function, several Mbr objects are created and join the group. After joining the group, the members periodically send multicast messages.
#include "Maestro.h"
#define NMEMBERS 10
class Mbr: public Maestro_GroupMember {
public:
// Initialize the group-member object and join the group.
Mbr(Maestro_GrpMemb_Options &ops) : Maestro_GroupMember(ops) {
join(); // Join the group
}
protected:
// This callback is invoked when a multicast message is received.
void grpMemb_ReceiveCast_Callback(Maestro_EndpID &origin,
Maestro_Message &msg) {
Maestro_String contents;
msg >> contents;
cout << "CAST from " << origin << ": " << contents << endl;
}
// This callback is invoked when a new view is accepted.
void grpMemb_AcceptedView_Callback(Maestro_GrpMemb_ViewData &viewData,
Maestro_Message &msg) {
cout << "Accepted View: " << viewData.members << endl ;
}
};
main(int argc, char *argv[]) {
Mbr *m[NMEMBERS];
int i;
Maestro_GrpMemb_Options ops;
ops.heartbeatRate = 1000; // Schedule a heartbeat every 1000 msec.
ops.groupName = "lapa";
ops.properties = "Gmp:Sync:Heal:Switch:Frag:Suspect:Flow:Total";
ops.transports = "UDP";
ops.argv = argv[0];
// Create NMEMBERS group-member objects.
for (i = 0; i < NMEMBERS; i++) {
m[i] = new Mbr(i, ops);
}
// Loop forever, periodically sending multicast messages.
while (1) {
sleep(1);
for (i = 0; i < NMEMBERS; i++) {
Maestro_Message msg;
Maestro_String str("hello");
msg << str;
m[i]->cast(msg); // Multicast the message.
}
}
}