This example creates two different nodes, Ping and Pong. The first node is executed with a fixed timing (see Timed Processing) and sends a “Ping!” event over its output port. The second component listens to its input port and answers with a “Pong!”.
Note: You can inspect the application with the RSB Logger, since the output scope is configured to also send over a remote transport. This should show an event with 1Hz on the /ping scope. The input port is configured to be only local, because otherwise the node would receive every event twice (over the locla and the remote transport).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | #include <iostream>
#include "cca/Node.h"
#include "cca/processing/all.h"
#include "cca/timing/PeriodicBeat.h"
#include "rsc/logging/LoggerFactory.h"
using namespace std;
using namespace boost;
using namespace cca;
using namespace rsc;
using namespace rsc::logging;
class Ping: public CCANode {
public:
Ping(const std::string &cname) :
CCANode(cname) {
createOutputPort<std::string>();
}
~Ping() {
}
void onProcess() {
std::string ping = ">Ping!<";
std::cout << ping << std::endl;
publish(0, boost::shared_ptr<std::string>(new std::string(ping)));
}
};
class Pong: public CCANode {
public:
Pong(const std::string &cname) :
CCANode(cname) {
createInputPort<std::string>();
}
~Pong() {
}
void onProcess() {
std::cout << " >Pong!<" << std::endl;
}
};
int main() {
LoggerFactory::getInstance().reconfigure(Logger::LEVEL_WARN);
CCANodePtr ping = CCANodePtr(new Ping("Ping Node"));
ping->setProcessingStrategy(TimedProcessing::create());
ping->configureOutputPort(0, PortConfiguration::LOCALREMOTE("/ping"));
std::cout << std::endl << *(ping->componentInfo()) << std::endl;
CCANodePtr pong = CCANodePtr(new Pong("Pong Node"));
pong->setProcessingStrategy(PortTriggeredProcessing::create(0));
pong->configureInputPort(0, PortConfiguration::LOCAL("/ping"));
std::cout << std::endl << *(pong->componentInfo()) << std::endl;
BeatPtr beat = PeriodicBeat::create(1000);
beat->registerReceiver(ping);
beat->run();
return EXIT_SUCCESS;
}
|