source: sample/pingpong/PingPong.cpp@ 9322

Last change on this file since 9322 was 8597, checked in by Christoph Mayer, 14 years ago

-memory leaks

File size: 7.0 KB
Line 
1#include "PingPong.h"
2#include "ariba/utility/configuration/Configuration.h"
3#include "ariba/utility/visual/DddVis.h"
4
5using ariba::utility::Configuration;
6using namespace ariba;
7
8namespace ariba {
9namespace application {
10namespace pingpong {
11
12// logging
13use_logging_cpp( PingPong );
14
15// the service that the pingpong wants to use
16ServiceID PingPong::PINGPONG_SERVICEID = ServiceID( 111 );
17
18// construction
19PingPong::PingPong() : pingId( 0 ) {
20 Timer::setInterval( 1000 );
21}
22
23// destruction
24PingPong::~PingPong() {
25}
26
27// implementation of the startup interface
28void PingPong::startup() {
29
30 logging_info( "starting up PingPong service ... " );
31
32 // create ariba module
33 logging_debug( "creating ariba underlay module ... " );
34 ariba = new AribaModule();
35
36 Name spovnetName("pingpong");
37 Name nodeName = Name::UNSPECIFIED;
38 this->name = string("<ping>");
39
40 // get settings from configuration object
41 if( Configuration::haveConfig() ){
42 Configuration& config = Configuration::instance();
43
44 // get node name
45 if (config.exists("node.name"))
46 nodeName = config.read<string> ("node.name");
47
48 // configure ariba module
49 if (config.exists("ariba.endpoints"))
50 ariba->setProperty("endpoints", config.read<string>("ariba.endpoints"));
51 if (config.exists("ariba.bootstrap.hints"))
52 ariba->setProperty("bootstrap.hints", config.read<string>("ariba.bootstrap.hints"));
53 if (config.exists("pingpong.name"))
54 name = config.read<string>("pingpong.name");
55
56 // get visualization
57 if( config.exists("ariba.visual3d.ip") && config.exists("ariba.visual3d.port")){
58 string ip = config.read<string>("ariba.visual3d.ip");
59 unsigned int port = config.read<unsigned int>("ariba.visual3d.port");
60 unsigned int color = config.exists("node.color") ?
61 config.read<unsigned int>("node.color") : 0;
62 ariba::utility::DddVis::instance().configure(ip, port, color);
63 }
64
65 } // if( Configuration::haveConfig() )
66
67 // start ariba module
68 ariba->start();
69
70 // create node and join
71 node = new Node( *ariba, nodeName );
72
73 // bind communication and node listener
74 node->bind( this ); /*NodeListener*/
75 node->bind( this, PingPong::PINGPONG_SERVICEID); /*CommunicationListener*/
76
77 // start node module
78 node->start();
79
80 // when initiating, you can define the overlay type, default is Chord [CHORD_OVERLAY]
81 SpoVNetProperties params;
82 //params.setBaseOverlayType( SpoVNetProperties::ONE_HOP_OVERLAY ); // alternative: OneHop
83
84 // initiate the spovnet
85 logging_info("initiating spovnet");
86 node->initiate(spovnetName, params);
87
88 // join the spovnet
89 logging_info("joining spovnet");
90 node->join(spovnetName);
91
92 // ping pong started up...
93 logging_info( "pingpong starting up with"
94 << " [spovnetid " << node->getSpoVNetId().toString() << "]"
95 << " and [nodeid " << node->getNodeId().toString() << "]" );
96}
97
98// implementation of the startup interface
99void PingPong::shutdown() {
100
101 logging_info( "pingpong service starting shutdown sequence ..." );
102
103 // stop timer
104 Timer::stop();
105
106 // leave spovnet
107 node->leave();
108
109 // unbind communication and node listener
110 node->unbind( this ); /*NodeListener*/
111 node->unbind( this, PingPong::PINGPONG_SERVICEID ); /*CommunicationListener*/
112
113 // stop the ariba module
114 ariba->stop();
115
116 // delete node and ariba module
117 delete node;
118 delete ariba;
119
120 // now we are completely shut down
121 logging_info( "pingpong service shut down" );
122}
123
124// timer event
125void PingPong::eventFunction() {
126
127 // we ping all nodes that are known in the overlay structure
128 // this can be all nodes (OneHop) overlay or just some neighbors
129 // in case of a Chord or Kademlia structure
130
131 // in this sample we use auto-links: we just send out our message
132 // to the node and the link is established automatically. for more
133 // control we would use the node->establishLink function to create
134 // a link and start using the link in the CommunicationListener::onLinkUp
135 // function that is implemented further down in PingPong::onLinkUp
136
137 logging_info( "pinging overlay neighbors with ping id " << ++pingId );
138 PingPongMessage pingmsg( pingId, name );
139
140 //-----------------------------------------------------------------------
141 // Option 1: get all neighboring nodes and send the message to each
142 //-----------------------------------------------------------------------
143 counter++;
144 if (counter<0 || counter>4) {
145 counter = 0;
146 string s;
147 for (int i=0; i<names.size();i++) {
148 if (i!=0) s+= ", ";
149 s = s+names[i];
150 }
151 logging_info("----> I am " << name << " and I know " << s);
152 names.clear();
153 }
154
155 vector<NodeID> nodes = node->getNeighborNodes();
156 BOOST_FOREACH( NodeID nid, nodes ){
157 logging_info( "sending ping message to " << nid.toString() );
158 node->sendMessage( pingmsg, nid, PingPong::PINGPONG_SERVICEID );
159 }
160
161 //-----------------------------------------------------------------------
162 // Option 2: send a "broadcast message" that actually does the same thing
163 // internally, gets all neighboring nodes and sends the message
164 //-----------------------------------------------------------------------
165 // node->sendBroadcastMessage( pingmsg, PingPong::PINGPONG_SERVICEID );
166}
167
168void PingPong::onJoinCompleted( const SpoVNetID& vid ) {
169 logging_info( "pingpong node join completed, spovnetid=" << vid.toString() );
170
171 // start the timer to ping every second
172 Timer::start();
173}
174
175void PingPong::onJoinFailed( const SpoVNetID& vid ) {
176 logging_error("pingpong node join failed, spovnetid=" << vid.toString() );
177}
178
179void PingPong::onLeaveCompleted( const SpoVNetID& vid ){
180 logging_info("pingpong node leave completed, spovnetid=" << vid.toString() );
181}
182
183void PingPong::onLeaveFailed( const SpoVNetID& vid ){
184 logging_error("pingpong node leave failed, spovnetid=" << vid.toString() );
185}
186
187void PingPong::onMessage(const DataMessage& msg, const NodeID& remote, const LinkID& lnk) {
188 PingPongMessage* pingmsg = msg.getMessage()->convert<PingPongMessage> ();
189 bool found=false;
190 for (int i=0;i<names.size(); i++) if (names[i]==pingmsg->getName()) found=true;
191 if (!found) names.push_back(pingmsg->getName());
192 logging_info( "received ping message on link " << lnk.toString()
193 << " from node " << remote.toString()
194 << ": " << pingmsg->info() );
195 delete pingmsg;
196}
197
198void PingPong::onLinkUp(const LinkID& lnk, const NodeID& remote){
199 logging_info( "received link-up event for link " << lnk.toString()
200 << " and node " << remote.toString() );
201}
202
203void PingPong::onLinkDown(const LinkID& lnk, const NodeID& remote){
204 logging_info( "received link-down event for link " << lnk.toString()
205 << " and node " << remote.toString() );
206}
207
208void PingPong::onLinkChanged(const LinkID& lnk, const NodeID& remote){
209 logging_info( "link-changed event for link " << lnk.toString()
210 << " and node " << remote.toString() );
211}
212
213bool PingPong::onLinkRequest(const NodeID& remote) {
214 logging_info( "node " << remote.toString() << " wants to build up a link with us ... allowing" );
215 return true;
216}
217
218void PingPong::onLinkFail(const LinkID& lnk, const NodeID& remote){
219 logging_info( "received link-failed event for link " << lnk.toString()
220 << " and node " << remote.toString() );
221}
222
223}}} // namespace ariba, application, pingpong
Note: See TracBrowser for help on using the repository browser.