source: sample/pingpong/PingPong.cpp@ 12061

Last change on this file since 12061 was 12061, checked in by hock@…, 11 years ago

PingPong: some clean up

File size: 7.2 KB
Line 
1#include "PingPong.h"
2#include "ariba/utility/configuration/Configuration.h"
3#include "ariba/utility/visual/DddVis.h"
4
5#include <boost/property_tree/ptree.hpp>
6#include <boost/property_tree/json_parser.hpp>
7
8
9using ariba::utility::Configuration;
10using namespace ariba;
11
12namespace ariba {
13namespace application {
14namespace pingpong {
15
16// logging
17use_logging_cpp( PingPong );
18
19// the service that the pingpong wants to use
20ServiceID PingPong::PINGPONG_SERVICEID = ServiceID( 111 );
21
22// construction
23PingPong::PingPong( string config) :
24 node(),
25 pingId( 0 ),
26 config_file(config)
27{
28 Timer::setInterval( 5000 );
29}
30
31// destruction
32PingPong::~PingPong() {
33}
34
35// implementation of the startup interface
36void PingPong::startup()
37{
38 using boost::property_tree::ptree;
39 using boost::property_tree::json_parser::read_json;
40
41 // set up logging
42 logging_rootlevel_debug();
43 logging_classlevel_debug(PingPong);
44
45 logging_info( "[PINGPONG]\t starting up PingPong service ... " );
46
47 // read config
48 ptree config;
49 try
50 {
51 read_json(config_file, config);
52 }
53 catch ( exception& e )
54 {
55 logging_warn("ERROR: Failed to read config file »" << config_file << "«: ");
56 logging_warn(e.what());
57 logging_warn("---> Using fallback config.");
58
59 config.put("ariba.spovnet_name", "pingpong");
60 }
61
62 // use node name also in the application
63 name = config.get("ariba.node_name", "NO_NAME");
64
65 // bind communication and node listener
66 node.bind( this ); /*NodeListener*/
67 node.bind( this, PingPong::PINGPONG_SERVICEID); /*CommunicationListener*/
68
69 // connecting
70 logging_debug( "connecting ... " );
71
72 node.connect(config.get_child("ariba"));
73
74 // ping pong started up...
75 logging_info( "[PINGPONG]\t pingpong starting up with"
76 << " [spovnetid " << node.getSpoVNetId().toString() << "]"
77 << " and [nodeid " << node.getNodeId().toString() << "]" );
78}
79
80// implementation of the startup interface
81void PingPong::shutdown() {
82
83 logging_info( "[PINGPONG]\t pingpong service starting shutdown sequence ..." );
84
85 // stop timer
86 Timer::stop();
87
88 // leave spovnet
89 node.leave();
90
91 // unbind communication and node listener
92 node.unbind( this ); /*NodeListener*/
93 node.unbind( this, PingPong::PINGPONG_SERVICEID ); /*CommunicationListener*/
94
95 // now we are completely shut down
96 logging_info( "pingpong service shut down" );
97}
98
99// timer event
100void PingPong::eventFunction() {
101
102 if ( node.getNeighborNodes().size() == 0)
103 {
104 logging_info( "[PINGPONG]\t +++ no neighbors +++" );
105 return;
106 }
107
108 // we ping all nodes that are known in the overlay structure
109 // this can be all nodes (OneHop) overlay or just some neighbors
110 // in case of a Chord or Kademlia structure
111 // NOTE: Currently ariba only supports the Chord overlay.
112
113 // in this sample we use auto-links: we just send out our message
114 // to the node and the link is established automatically. for more
115 // control we would use the node->establishLink function to create
116 // a link and start using the link in the CommunicationListener::onLinkUp
117 // function that is implemented further down in PingPong::onLinkUp
118
119 // NOTE: This example still uses the old deprecated ariba::message messages
120 // with the ariba built-in serialization.
121 // For future applications please use the new reboost::message_t messages.
122 // Data is stored in high efficient zero-copy buffers of type
123 // reboost::shared_buffer_t. These buffers hold plain data, so you have to
124 // serialize the data "on your own".
125 // We reccomend third-party serialization libraries like:
126 // - Protocol Buffers (http://en.wikipedia.org/wiki/Protocol_Buffers)
127 // - MessagePack (http://en.wikipedia.org/wiki/MessagePack)
128
129 pingId++;
130 logging_info( endl << "|||||||||| >>>>>>>>>>" << endl
131 << "[PINGPONG]\t PINGING overlay neighbors with ping id " << pingId );
132 PingPongMessage pingmsg( pingId, name );
133
134 //-----------------------------------------------------------------------
135 // Option 1: get all neighboring nodes and send the message to each
136 //-----------------------------------------------------------------------
137 counter++;
138 if (counter<0 || counter>4) {
139 counter = 0;
140 string s;
141 for (int i=0; i<names.size();i++) {
142 if (i!=0) s+= ", ";
143 s = s+names[i];
144 }
145 logging_info("[PINGPONG]\t ----> I am " << name << " and I know " << s);
146 names.clear();
147 }
148
149 vector<NodeID> nodes = node.getNeighborNodes();
150 BOOST_FOREACH( NodeID nid, nodes ){
151 logging_info( "[PINGPONG]\t sending ping message to " << nid.toString() );
152 node.sendMessage( pingmsg, nid, PingPong::PINGPONG_SERVICEID );
153 }
154
155 //-----------------------------------------------------------------------
156 // Option 2: send a "broadcast message" that actually does the same thing
157 // internally, gets all neighboring nodes and sends the message
158 //-----------------------------------------------------------------------
159 // node->sendBroadcastMessage( pingmsg, PingPong::PINGPONG_SERVICEID );
160}
161
162void PingPong::onJoinCompleted( const SpoVNetID& vid ) {
163 logging_info( "pingpong node join completed, spovnetid=" << vid.toString() );
164
165 // start the timer to ping every second
166 Timer::start();
167}
168
169void PingPong::onJoinFailed( const SpoVNetID& vid ) {
170 logging_error("pingpong node join failed, spovnetid=" << vid.toString() );
171}
172
173void PingPong::onLeaveCompleted( const SpoVNetID& vid ){
174 logging_info("pingpong node leave completed, spovnetid=" << vid.toString() );
175}
176
177void PingPong::onLeaveFailed( const SpoVNetID& vid ){
178 logging_error("pingpong node leave failed, spovnetid=" << vid.toString() );
179}
180
181void PingPong::onMessage(const DataMessage& msg, const NodeID& remote, const LinkID& lnk) {
182 PingPongMessage* pingmsg = msg.getMessage()->convert<PingPongMessage> ();
183 bool found=false;
184 for (int i=0;i<names.size(); i++) if (names[i]==pingmsg->getName()) found=true;
185 if (!found) names.push_back(pingmsg->getName());
186 logging_info( endl << "<<<<<<<<<< ||||||||||" << endl
187 << "[PINGPONG]\t RECEIVED ping message on link "
188 << lnk.toString()
189 << " " << (node.isLinkDirect(lnk) ? "[DIRECT-LINK]" : "[INDIRECT-LINK]")
190 << " HopCount: " << node.getHopCount(lnk)
191 << " from node " << remote.toString()
192 << " (" << pingmsg->getName() << ")"
193 << ": " << pingmsg->info() );
194 delete pingmsg;
195}
196
197void PingPong::onLinkUp(const LinkID& lnk, const NodeID& remote){
198 logging_info( "received link-up event for link " << lnk.toString()
199 << " and node " << remote.toString() );
200}
201
202void PingPong::onLinkDown(const LinkID& lnk, const NodeID& remote){
203 logging_info( "received link-down event for link " << lnk.toString()
204 << " and node " << remote.toString() );
205}
206
207void PingPong::onLinkChanged(const LinkID& lnk, const NodeID& remote){
208 logging_info( "link-changed event for link " << lnk.toString()
209 << " and node " << remote.toString() );
210}
211
212bool PingPong::onLinkRequest(const NodeID& remote) {
213 logging_info( "node " << remote.toString() << " wants to build up a link with us ... allowing" );
214 return true;
215}
216
217void PingPong::onLinkFail(const LinkID& lnk, const NodeID& remote){
218 logging_info( "received link-failed event for link " << lnk.toString()
219 << " and node " << remote.toString() );
220}
221
222}}} // namespace ariba, application, pingpong
Note: See TracBrowser for help on using the repository browser.