Siena Fast Forwarding Documentation (v. 1.13.0)
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 {
// this class implements the message interface
// ...
public:
// this method reads message from an input stream
istream & read_from(istream & i);
// this method returns a "text" for this message
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) {
//
// we output the message text to the output stream
// associated with the matching interface
//
*(os[i]) << msg->get_text() << endl;
if (--c <= 0) {
return true; // enough processing for this message:
} else { // true forces the matching process to end.
return false;
}
}
int main(int argc, char * argv[]) {
text_message m;
ostream * output_channels[10];
//
// here we initialize FT with some rules
// FT.ifconfig(...);
// FT.ifconfig(...);
// ...
//
// we also initialize our output channels
// output_channel[0] = ...;
// output_channel[1] = ...;
// ...
simple_handler h(output_channels);
while(m.read_from(cin)) { // then we start reading messages from stdin
h.setup(&m, 5); // and we process each message through FT
FT.match(m, h); // using our simple handler.
}
return 0;
}