1 |
|
---|
2 | #include "PingPong.h"
|
---|
3 | #include "ariba/utility/configuration/Configuration.h"
|
---|
4 |
|
---|
5 | using ariba::utility::Configuration;
|
---|
6 | using namespace ariba;
|
---|
7 |
|
---|
8 | namespace ariba {
|
---|
9 | namespace application {
|
---|
10 | namespace pingpong {
|
---|
11 |
|
---|
12 | // logging
|
---|
13 | use_logging_cpp( PingPong);
|
---|
14 |
|
---|
15 | // the service id of the ping pong service
|
---|
16 | ServiceID PingPong::PINGPONG_ID = ServiceID(111);
|
---|
17 |
|
---|
18 | // construction
|
---|
19 | PingPong::PingPong() : pingId( 0 ) {
|
---|
20 | }
|
---|
21 |
|
---|
22 | // destruction
|
---|
23 | PingPong::~PingPong() {
|
---|
24 | }
|
---|
25 |
|
---|
26 | // implementation of the startup interface
|
---|
27 | void PingPong::startup() {
|
---|
28 |
|
---|
29 | // create ariba module
|
---|
30 | logging_info("Creating ariba underlay module ... ");
|
---|
31 | ariba = new AribaModule();
|
---|
32 |
|
---|
33 | logging_info("Starting up PingPong service ... ");
|
---|
34 |
|
---|
35 | // --- get config ---
|
---|
36 | Configuration& config = Configuration::instance();
|
---|
37 |
|
---|
38 | // generate spovnet name
|
---|
39 | Name spovnetName("pingpong");
|
---|
40 |
|
---|
41 | // get initiator flag
|
---|
42 | this->isInitiator = Configuration::instance().read<bool> ("node.initiator");
|
---|
43 |
|
---|
44 | // get node name
|
---|
45 | Name nodeName = Name::UNSPECIFIED;
|
---|
46 | if (config.exists("node.name")) nodeName
|
---|
47 | = config.read<string> ("node.name");
|
---|
48 |
|
---|
49 | // configure ariba module
|
---|
50 | if (config.exists("ariba.ip.addr")) ariba->setProperty("ip.addr",
|
---|
51 | config.read<string> ("ariba.ip.addr"));
|
---|
52 | if (config.exists("ariba.tcp.port")) ariba->setProperty("tcp.port",
|
---|
53 | config.read<string> ("ariba.tcp.port"));
|
---|
54 | if (config.exists("ariba.udp.port")) ariba->setProperty("udp.port",
|
---|
55 | config.read<string> ("ariba.udp.port"));
|
---|
56 | if (config.exists("ariba.bootstrap.hints")) ariba->setProperty("bootstrap.hints",
|
---|
57 | config.read<string> ("ariba.bootstrap.hints"));
|
---|
58 |
|
---|
59 | // start ariba module
|
---|
60 | ariba->start();
|
---|
61 |
|
---|
62 | // create node and join
|
---|
63 | node = new Node( *ariba, nodeName );
|
---|
64 |
|
---|
65 | // start node module
|
---|
66 | node->start();
|
---|
67 |
|
---|
68 | if (!isInitiator) {
|
---|
69 | node->join(spovnetName);
|
---|
70 | } else {
|
---|
71 | node->initiate(spovnetName);
|
---|
72 | }
|
---|
73 |
|
---|
74 | // bind communication and node listener
|
---|
75 | node->bind(this);
|
---|
76 | node->bind(this, PingPong::PINGPONG_ID);
|
---|
77 |
|
---|
78 | // ping pong started up...
|
---|
79 | logging_info("PingPong started up");
|
---|
80 | }
|
---|
81 |
|
---|
82 | // implementation of the startup interface
|
---|
83 | void PingPong::shutdown() {
|
---|
84 | logging_info("PingPong service starting shutdown sequence ...");
|
---|
85 |
|
---|
86 | // stop timer
|
---|
87 | Timer::stop();
|
---|
88 |
|
---|
89 | // leave spovnet
|
---|
90 | node->leave();
|
---|
91 |
|
---|
92 | // unbind listeners
|
---|
93 | node->unbind( this );
|
---|
94 | node->unbind( this, PingPong::PINGPONG_ID );
|
---|
95 |
|
---|
96 | logging_info("PingPong service shut down!");
|
---|
97 | }
|
---|
98 |
|
---|
99 | // node listener interface
|
---|
100 | void PingPong::onJoinCompleted( const SpoVNetID& vid ) {
|
---|
101 | logging_info("PingPong node join completed, spovnetid=" << vid.toString() );
|
---|
102 | }
|
---|
103 |
|
---|
104 | void PingPong::onJoinFailed( const SpoVNetID& vid ) {
|
---|
105 | logging_info("PingPong node join failed, spovnetid=" << vid.toString() );
|
---|
106 | }
|
---|
107 |
|
---|
108 | // communication listener
|
---|
109 | bool PingPong::onLinkRequest(const NodeID& remote, Message* msg) {
|
---|
110 | return false;
|
---|
111 | }
|
---|
112 |
|
---|
113 | void PingPong::onMessage(Message* msg, const NodeID& remote, const LinkID& lnk =
|
---|
114 | LinkID::UNSPECIFIED) {
|
---|
115 | PingPongMessage* incoming = msg->decapsulate<PingPongMessage> ();
|
---|
116 |
|
---|
117 | logging_info("received ping message on link " << lnk.toString()
|
---|
118 | << " from node with id " << (int) incoming->getid());
|
---|
119 | }
|
---|
120 |
|
---|
121 | // timer event
|
---|
122 | void PingPong::eventFunction() {
|
---|
123 | }
|
---|
124 |
|
---|
125 | }}} // namespace ariba, application, pingpong
|
---|