Example: Ping PongΒΆ

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!”.

  1. At first a Ping node is defined (lines 15-29). The node has one output port to send a string (line 19). In its main processing method it prints a “Ping!” to the console and sends the string over its output port (line 27).
  2. Then a Pong node is defined (lines 31-43). The node has one input port to receive a string (line 35). In its main processing method it prints a “Pong!” as answer.
  3. In the main function of this example we create a ping node and configure it to be timed (line 48, see Timed Processing) and we configure the output port to send to the scope /ping (line 49).
  4. A pong node is created and configured to be triggered when an input arrives at its input port (line 53, see Port-Triggered Processing) and we configure the input port to listen to the scope /ping.
  5. To run the program we need to send a timing signal to the ping node. We create a beat and assign the ping node to it (lines 57-58). The pong node will be triggered by incoming input.

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;
}

Table of Contents

Related Documentation

This Page