close Warning: Can't use blame annotator:
No changeset 2363 in the repository

source: sample/pingpong/PingPong.cpp@ 4590

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

-dynamische wahl des overlays durch den initiator, nodes kriegen bei join mitgeteilt welche overlay struktur zu verwenden ist

File size: 6.2 KB
RevLine 
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 that the pingpong wants to use
15ServiceID PingPong::PINGPONG_SERVICEID = 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 = config.read<string> ("node.name");
47
48 // configure ariba module
49 if (config.exists("ariba.ip.addr")) ariba->setProperty("ip.addr",
50 config.read<string>("ariba.ip.addr"));
51 if (config.exists("ariba.tcp.port")) ariba->setProperty("tcp.port",
52 config.read<string>("ariba.tcp.port"));
53 if (config.exists("ariba.udp.port")) ariba->setProperty("udp.port",
54 config.read<string>("ariba.udp.port"));
55 if (config.exists("ariba.bootstrap.hints")) ariba->setProperty("bootstrap.hints",
56 config.read<string>("ariba.bootstrap.hints"));
57
58 // start ariba module
59 ariba->start();
60
61 // create node and join
62 node = new Node( *ariba, nodeName );
63
64 // bind communication and node listener
65 node->bind( this ); /*NodeListener*/
66 node->bind( this, PingPong::PINGPONG_SERVICEID); /*CommunicationListener*/
67
68 // start node module
69 node->start();
70
71 // when initiating, you can define the overlay type, default is Chord [CHORD_OVERLAY]
72 SpoVNetProperties params;
73 //params.setBaseOverlayType( SpoVNetProperties::ONE_HOP_OVERLAY ); // alternative: OneHop
74
75 // initiate or join the spovnet
76 if (!isInitiator) node->join(spovnetName);
77 else node->initiate(spovnetName, params);
78
79 // ping pong started up...
80 logging_info( "pingpong starting up with"
81 << " [spovnetid " << node->getSpoVNetId().toString() << "]"
82 << " and [nodeid " << node->getNodeId().toString() << "]" );
83}
84
85// implementation of the startup interface
86void PingPong::shutdown() {
87
88 logging_info( "pingpong service starting shutdown sequence ..." );
89
90 // stop timer
91 Timer::stop();
92
93 // leave spovnet
94 node->leave();
95
96 // unbind communication and node listener
97 node->unbind( this ); /*NodeListener*/
98 node->unbind( this, PingPong::PINGPONG_SERVICEID ); /*CommunicationListener*/
99
100 // stop the ariba module
101 ariba->stop();
102
103 // delete node and ariba module
104 delete node;
105 delete ariba;
106
107 // now we are completely shut down
108 logging_info( "pingpong service shut down" );
109}
110
111// timer event
112void PingPong::eventFunction() {
113
114 // we ping all nodes that are known in the overlay structure
115 // this can be all nodes (OneHop) overlay or just some neighbors
116 // in case of a Chord or Kademlia structure
117
118 // in this sample we use auto-links: we just send out our message
119 // to the node and the link is established automatically. for more
120 // control we would use the node->establishLink function to create
121 // a link and start using the link in the CommunicationListener::onLinkUp
122 // function that is implemented further down in PingPong::onLinkUp
123
124 logging_info( "pinging overlay neighbors with ping id " << ++pingId );
125
126 PingPongMessage pingmsg( pingId );
127
128 //-----------------------------------------------------------------------
129 // Option 1: get all neighboring nodes and send the message to each
130 //-----------------------------------------------------------------------
131 vector<NodeID> nodes = node->getNeighborNodes();
132 BOOST_FOREACH( NodeID nid, nodes ){
133 node->sendMessage( pingmsg, nid, PingPong::PINGPONG_SERVICEID );
134 }
135
136 //-----------------------------------------------------------------------
137 // Option 2: send a "broadcast message" that actually does the same thing
138 // internally, gets all neighboring nodes and sends the message
139 //-----------------------------------------------------------------------
140 // node->sendBroadcastMessage( pingmsg, PingPong::PINGPONG_SERVICEID );
141}
142
143void PingPong::onJoinCompleted( const SpoVNetID& vid ) {
144 logging_info( "pingpong node join completed, spovnetid=" << vid.toString() );
145
146 // start the timer to ping every second
147 Timer::start();
148}
149
150void PingPong::onJoinFailed( const SpoVNetID& vid ) {
151 logging_error("pingpong node join failed, spovnetid=" << vid.toString() );
152}
153
154void PingPong::onLeaveCompleted( const SpoVNetID& vid ){
155 logging_info("pingpong node leave completed, spovnetid=" << vid.toString() );
156}
157
158void PingPong::onLeaveFailed( const SpoVNetID& vid ){
159 logging_error("pingpong node leave failed, spovnetid=" << vid.toString() );
160}
161
162void PingPong::onMessage(const DataMessage& msg, const NodeID& remote, const LinkID& lnk) {
163 PingPongMessage* pingmsg = msg.getMessage()->convert<PingPongMessage> ();
164
165 logging_info( "received ping message on link " << lnk.toString()
166 << " from node " << remote.toString()
167 << ": " << pingmsg->info() );
168}
169
170void PingPong::onLinkUp(const LinkID& lnk, const NodeID& remote){
171 logging_info( "received link-up event for link " << lnk.toString()
172 << " and node " << remote.toString() );
173}
174
175void PingPong::onLinkDown(const LinkID& lnk, const NodeID& remote){
176 logging_info( "received link-down event for link " << lnk.toString()
177 << " and node " << remote.toString() );
178}
179
180void PingPong::onLinkChanged(const LinkID& lnk, const NodeID& remote){
181 logging_info( "link-changed event for link " << lnk.toString()
182 << " and node " << remote.toString() );
183}
184
185bool PingPong::onLinkRequest(const NodeID& remote, const DataMessage& msg) {
186 logging_info( "node " << remote.toString() << " wants to build up a link with us ... allowing" );
187 return true;
188}
189
190void PingPong::onLinkFail(const LinkID& lnk, const NodeID& remote){
191 logging_info( "received link-failed event for link " << lnk.toString()
192 << " and node " << remote.toString() );
193}
194
195}}} // namespace ariba, application, pingpong
Note: See TracBrowser for help on using the repository browser.