source: sample/pingpong/PingPong.cpp@ 10072

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