forwarding_messages.cc
This example shows how to set up a
match handler for use with the
forwarding table. In this example, the forwarding table processes messages of type
text_message delegating the output function to an object of type
simple_handler.
simple_handler puts out each message to a set of output streams associated with the matching interfaces. The handler also implements a cut-off mechanism that limits the number of output streams that a message is copied to. In this particular example, this limit is set to 5. Notice that the handler is initialized with a reference to message that is of the actual message type. This allows the handler to use specific access methods for its output function. In this example, the handler uses the
get_text() method to obtain the output text from the message object.
#include <iostream>
#include <string>
#include <siena/types.h>
using namespace std;
using namespace siena;
class text_message: public message {
public:
istream & read_from(istream & i);
const string & get_text() const;
};
class simple_handler: public MatchHandler {
private:
ostream * channels[];
const text_message * msg;
int counter;
public:
simple_handler(ostream * oc[]): channels(oc) {}
setup_message(const text_message * m, int c) {
msg = m;
counter = c;
}
virtual bool output(if_t i);
};
bool simple_handler::output(if_t i) {
*(os[i]) << msg->get_text() << endl;
if (--c <= 0) {
return true;
} else {
return false;
}
}
int main(int argc, char * argv[]) {
text_message m;
ostream * output_channels[10];
FwdTable FT;
simple_handler h(output_channels);
while(m.read_from(cin)) {
h.setup(&m, 5);
FT.match(m, h);
}
return 0;
}