![]() |
An Example of a Multi-Threaded 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. Note that all group-member objects are created with the same threading mode (MAESTRO_MODE_MULTI_THREADED). 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.protocol = "Top:Heal:Switch:Leave:Inter:Intra:Elect:Merge:Sync:Suspect:Top_appl:Pt2pt:Frag:Stable:Mnak:Bottom";
ops.transports = "UDP";
ops.programName = argv[0];
// Start Maestro/Ensemble in multi-threaded mode.
ops.threadMode = MAESTRO_MODE_MULTI_THREADED;
// 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.
}
}
}