source: sample/pingpong/PingPong.cpp@ 6954

Last change on this file since 6954 was 6954, checked in by Christoph Mayer, 15 years ago

-small visualization enhancements

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