source: sample/pingpong/PingPong.cpp@ 2851

Last change on this file since 2851 was 2483, checked in by Christoph Mayer, 16 years ago

-autolinks impl. (funktioniert noch nicht komplett, macht aber im moment nichts schlechter)
-einige fixes im ablauf etc.

File size: 3.8 KB
Line 
1#include "PingPong.h"
2#include "ariba/utility/configuration/Configuration.h"
3
4using ariba::utility::Configuration;
5using namespace ariba;
6
7namespace ariba {
8namespace application {
9namespace pingpong {
10
11// logging
12use_logging_cpp( PingPong );
13
14// the service id of the ping pong service
15ServiceID PingPong::PINGPONG_ID = ServiceID( 111 );
16
17// construction
18PingPong::PingPong() : pingId( 0 ) {
19 Timer::setInterval( 5000 );
20}
21
22// destruction
23PingPong::~PingPong() {
24}
25
26// implementation of the startup interface
27void PingPong::startup() {
28
29 logging_info( "starting up PingPong service ... " );
30
31 // create ariba module
32 logging_debug( "creating ariba underlay module ... " );
33 ariba = new AribaModule();
34
35 // get the configuration object
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 // initiate or join the spovnet
69 if (!isInitiator) node->join(spovnetName);
70 else node->initiate(spovnetName);
71
72 // bind communication and node listener
73 node->bind( this );
74 node->bind( this, PingPong::PINGPONG_ID);
75
76 // start the ping timer. if we are not
77 // the initiator this will happen in onJoinCompleted
78 if( isInitiator ) Timer::start();
79
80 // ping pong started up...
81 logging_info( "pingpong started up ");
82}
83
84// implementation of the startup interface
85void PingPong::shutdown() {
86
87 logging_info( "pingpong service starting shutdown sequence ..." );
88
89 // stop timer
90 Timer::stop();
91
92 // unbind communication and node listener
93 node->unbind( this );
94 node->unbind( this, PingPong::PINGPONG_ID );
95
96 // leave spovnet
97 node->leave();
98
99 // stop the ariba module
100 ariba->stop();
101
102 // delete node and ariba module
103 delete node;
104 delete ariba;
105
106 // now we are completely shut down
107 logging_info( "pingpong service shut down" );
108}
109
110// node listener interface
111void PingPong::onJoinCompleted( const SpoVNetID& vid ) {
112 logging_info( "pingpong node join completed, spovnetid=" << vid.toString() );
113
114 // start the timer to ping every second
115 Timer::start();
116}
117
118void PingPong::onJoinFailed( const SpoVNetID& vid ) {
119 logging_error(" pingpong node join failed, spovnetid=" << vid.toString() );
120}
121
122// communication listener
123bool PingPong::onLinkRequest(const NodeID& remote, const DataMessage& msg) {
124 return false;
125}
126
127void PingPong::onMessage(const DataMessage& msg, const NodeID& remote, const LinkID& lnk) {
128
129 PingPongMessage* pingmsg = msg.getMessage()->decapsulate<PingPongMessage> ();
130
131 logging_info( "received ping message on link " << lnk.toString()
132 << " from node " << remote.toString()
133 << ": " << pingmsg->info() );
134}
135
136// timer event
137void PingPong::eventFunction() {
138
139 // we ping all nodes that are known in the overlay structure
140 // this can be all nodes (OneHop) overlay or just some neighbors
141 // in case of a Chord or Kademlia structure
142
143 logging_info( "pinging overlay neighbors with ping id " << ++pingId );
144
145 PingPongMessage pingmsg( pingId );
146 node->sendBroadcastMessage( pingmsg, PingPong::PINGPONG_ID );
147}
148
149}}} // namespace ariba, application, pingpong
Note: See TracBrowser for help on using the repository browser.