| 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 | 
 | 
|---|
| 9 | using ariba::utility::Configuration;
 | 
|---|
| 10 | using namespace ariba;
 | 
|---|
| 11 | 
 | 
|---|
| 12 | namespace ariba {
 | 
|---|
| 13 | namespace application {
 | 
|---|
| 14 | namespace pingpong {
 | 
|---|
| 15 | 
 | 
|---|
| 16 | // logging
 | 
|---|
| 17 | use_logging_cpp( PingPong );
 | 
|---|
| 18 | 
 | 
|---|
| 19 | // the service that the pingpong wants to use
 | 
|---|
| 20 | ServiceID PingPong::PINGPONG_SERVICEID = ServiceID( 111 );
 | 
|---|
| 21 | 
 | 
|---|
| 22 | // construction
 | 
|---|
| 23 | PingPong::PingPong( string config)  :
 | 
|---|
| 24 |         node(),
 | 
|---|
| 25 |         pingId( 0 ),
 | 
|---|
| 26 |         config_file(config)
 | 
|---|
| 27 | {
 | 
|---|
| 28 |         Timer::setInterval( 5000 );
 | 
|---|
| 29 | }
 | 
|---|
| 30 | 
 | 
|---|
| 31 | // destruction
 | 
|---|
| 32 | PingPong::~PingPong() {
 | 
|---|
| 33 | }
 | 
|---|
| 34 | 
 | 
|---|
| 35 | // implementation of the startup interface
 | 
|---|
| 36 | void 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
 | 
|---|
| 81 | void 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
 | 
|---|
| 100 | void PingPong::eventFunction()
 | 
|---|
| 101 | {
 | 
|---|
| 102 |         // We ping all nodes that are known in the overlay structure
 | 
|---|
| 103 |         // this can be all nodes (OneHop) overlay or just some neighbors
 | 
|---|
| 104 |         // in case of a Chord or Kademlia structure
 | 
|---|
| 105 |         // NOTE: Currently ariba only supports the Chord overlay.
 | 
|---|
| 106 | 
 | 
|---|
| 107 |         // In this sample we use auto-links: we just send out our message
 | 
|---|
| 108 |         // to the node and the link is established automatically. for more
 | 
|---|
| 109 |         // control we would use the node->establishLink function to create
 | 
|---|
| 110 |         // a link and start using the link in the CommunicationListener::onLinkUp
 | 
|---|
| 111 |         // function that is implemented further down in PingPong::onLinkUp
 | 
|---|
| 112 |         
 | 
|---|
| 113 |         // NOTE: This example still uses the old deprecated ariba::message messages 
 | 
|---|
| 114 |         //   with the ariba built-in serialization. 
 | 
|---|
| 115 |         // For future applications please use the new reboost::message_t messages. 
 | 
|---|
| 116 |         //   Data is stored in high efficient zero-copy buffers of type 
 | 
|---|
| 117 |         //   reboost::shared_buffer_t. These buffers hold plain data, so you have to 
 | 
|---|
| 118 |         //   serialize the data "on your own".
 | 
|---|
| 119 |         // We recommend third-party serialization libraries like:
 | 
|---|
| 120 |         //   - Protocol Buffers (http://en.wikipedia.org/wiki/Protocol_Buffers)
 | 
|---|
| 121 |         //   - MessagePack      (http://en.wikipedia.org/wiki/MessagePack)
 | 
|---|
| 122 | 
 | 
|---|
| 123 |         
 | 
|---|
| 124 |         // don't do anything if we have no neighbors
 | 
|---|
| 125 |         if ( node.getNeighborNodes().size() == 0)
 | 
|---|
| 126 |     {
 | 
|---|
| 127 |         logging_info( "[PINGPONG]\t +++ no neighbors +++" );
 | 
|---|
| 128 |         return;
 | 
|---|
| 129 |     }
 | 
|---|
| 130 |     
 | 
|---|
| 131 |     
 | 
|---|
| 132 |         pingId++;
 | 
|---|
| 133 |     logging_info( endl << "|||||||||| >>>>>>>>>>" << endl
 | 
|---|
| 134 |                 << "[PINGPONG]\t PINGING overlay neighbors with ping id " << pingId );
 | 
|---|
| 135 |         PingPongMessage pingmsg( pingId, name );
 | 
|---|
| 136 |         
 | 
|---|
| 137 |         //-----------------------------------------------------------------------
 | 
|---|
| 138 |         // Option 1: get all neighboring nodes and send the message to each
 | 
|---|
| 139 |         //-----------------------------------------------------------------------
 | 
|---|
| 140 |         counter++;
 | 
|---|
| 141 |         if (counter<0 || counter>4) {
 | 
|---|
| 142 |                 counter = 0;
 | 
|---|
| 143 |                 string s;
 | 
|---|
| 144 |                 for (int i=0; i<names.size();i++) {
 | 
|---|
| 145 |                         if (i!=0) s+= ", ";
 | 
|---|
| 146 |                         s = s+names[i];
 | 
|---|
| 147 |                 }
 | 
|---|
| 148 |                 logging_info("[PINGPONG]\t ----> I am " << name << " and I know " << s);
 | 
|---|
| 149 |                 names.clear();
 | 
|---|
| 150 |         }
 | 
|---|
| 151 | 
 | 
|---|
| 152 |         vector<NodeID> nodes = node.getNeighborNodes();
 | 
|---|
| 153 |         BOOST_FOREACH( NodeID nid, nodes ){
 | 
|---|
| 154 |                 logging_info( "[PINGPONG]\t sending ping message to " << nid.toString() );
 | 
|---|
| 155 |                 node.sendMessage( pingmsg, nid, PingPong::PINGPONG_SERVICEID );
 | 
|---|
| 156 |         }
 | 
|---|
| 157 | 
 | 
|---|
| 158 |         //-----------------------------------------------------------------------
 | 
|---|
| 159 |         // Option 2: send a "broadcast message" that actually does the same thing
 | 
|---|
| 160 |         //           internally, gets all neighboring nodes and sends the message
 | 
|---|
| 161 |         //-----------------------------------------------------------------------
 | 
|---|
| 162 |         // node->sendBroadcastMessage( pingmsg, PingPong::PINGPONG_SERVICEID );
 | 
|---|
| 163 | }
 | 
|---|
| 164 | 
 | 
|---|
| 165 | void PingPong::onJoinCompleted( const SpoVNetID& vid ) {
 | 
|---|
| 166 |         logging_info( "pingpong node join completed, spovnetid=" << vid.toString() );
 | 
|---|
| 167 | 
 | 
|---|
| 168 |         // start the timer to ping every second
 | 
|---|
| 169 |         Timer::start();
 | 
|---|
| 170 | }
 | 
|---|
| 171 | 
 | 
|---|
| 172 | void PingPong::onJoinFailed( const SpoVNetID& vid ) {
 | 
|---|
| 173 |         logging_error("pingpong node join failed, spovnetid=" << vid.toString() );
 | 
|---|
| 174 | }
 | 
|---|
| 175 | 
 | 
|---|
| 176 | void PingPong::onLeaveCompleted( const SpoVNetID& vid ){
 | 
|---|
| 177 |         logging_info("pingpong node leave completed, spovnetid=" << vid.toString() );
 | 
|---|
| 178 | }
 | 
|---|
| 179 | 
 | 
|---|
| 180 | void PingPong::onLeaveFailed( const SpoVNetID& vid ){
 | 
|---|
| 181 |         logging_error("pingpong node leave failed, spovnetid=" << vid.toString() );
 | 
|---|
| 182 | }
 | 
|---|
| 183 | 
 | 
|---|
| 184 | void PingPong::onMessage(const DataMessage& msg, const NodeID& remote, const LinkID& lnk) {
 | 
|---|
| 185 |         PingPongMessage* pingmsg = msg.getMessage()->convert<PingPongMessage> ();
 | 
|---|
| 186 |         bool found=false;
 | 
|---|
| 187 |         for (int i=0;i<names.size(); i++) if (names[i]==pingmsg->getName()) found=true;
 | 
|---|
| 188 |         if (!found) names.push_back(pingmsg->getName());
 | 
|---|
| 189 |         logging_info( endl << "<<<<<<<<<< ||||||||||" << endl
 | 
|---|
| 190 |                 << "[PINGPONG]\t RECEIVED ping message on link "
 | 
|---|
| 191 |                 << lnk.toString()
 | 
|---|
| 192 |                 << " " << (node.isLinkDirect(lnk) ? "[DIRECT-LINK]" : "[INDIRECT-LINK]")
 | 
|---|
| 193 |                 << " HopCount: " << node.getHopCount(lnk)
 | 
|---|
| 194 |                         << " from node " << remote.toString()
 | 
|---|
| 195 |                         << " (" << pingmsg->getName() << ")"
 | 
|---|
| 196 |                         << ": " << pingmsg->info() );
 | 
|---|
| 197 |         delete pingmsg;
 | 
|---|
| 198 | }
 | 
|---|
| 199 | 
 | 
|---|
| 200 | void PingPong::onLinkUp(const LinkID& lnk, const NodeID& remote){
 | 
|---|
| 201 |         logging_info( "received link-up event for link " << lnk.toString()
 | 
|---|
| 202 |                         << " and node " << remote.toString() );
 | 
|---|
| 203 | }
 | 
|---|
| 204 | 
 | 
|---|
| 205 | void PingPong::onLinkDown(const LinkID& lnk, const NodeID& remote){
 | 
|---|
| 206 |         logging_info( "received link-down event for link " << lnk.toString()
 | 
|---|
| 207 |                         << " and node " << remote.toString() );
 | 
|---|
| 208 | }
 | 
|---|
| 209 | 
 | 
|---|
| 210 | void PingPong::onLinkChanged(const LinkID& lnk, const NodeID& remote){
 | 
|---|
| 211 |         logging_info( "link-changed event for link " << lnk.toString()
 | 
|---|
| 212 |                         << " and node " << remote.toString() );
 | 
|---|
| 213 | }
 | 
|---|
| 214 | 
 | 
|---|
| 215 | bool PingPong::onLinkRequest(const NodeID& remote) {
 | 
|---|
| 216 |         logging_info( "node " << remote.toString() << " wants to build up a link with us ... allowing" );
 | 
|---|
| 217 |         return true;
 | 
|---|
| 218 | }
 | 
|---|
| 219 | 
 | 
|---|
| 220 | void PingPong::onLinkFail(const LinkID& lnk, const NodeID& remote){
 | 
|---|
| 221 |         logging_info( "received link-failed event for link " << lnk.toString()
 | 
|---|
| 222 |                         << " and node " << remote.toString() );
 | 
|---|
| 223 | }
 | 
|---|
| 224 | 
 | 
|---|
| 225 | }}} // namespace ariba, application, pingpong
 | 
|---|