source: sample/pingpong/PingPong.cpp@ 12063

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

..

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