| 1 | #include "PingPong.h" | 
|---|
| 2 | #include "ariba/utility/configuration/Configuration.h" | 
|---|
| 3 |  | 
|---|
| 4 | using ariba::utility::Configuration; | 
|---|
| 5 | using namespace ariba; | 
|---|
| 6 |  | 
|---|
| 7 | namespace ariba { | 
|---|
| 8 | namespace application { | 
|---|
| 9 | namespace pingpong { | 
|---|
| 10 |  | 
|---|
| 11 | // logging | 
|---|
| 12 | use_logging_cpp( PingPong ); | 
|---|
| 13 |  | 
|---|
| 14 | // the service that the pingpong wants to use | 
|---|
| 15 | ServiceID PingPong::PINGPONG_SERVICEID = ServiceID( 111 ); | 
|---|
| 16 |  | 
|---|
| 17 | // construction | 
|---|
| 18 | PingPong::PingPong() : pingId( 0 ) { | 
|---|
| 19 | Timer::setInterval( 1000 ); | 
|---|
| 20 | } | 
|---|
| 21 |  | 
|---|
| 22 | // destruction | 
|---|
| 23 | PingPong::~PingPong() { | 
|---|
| 24 | } | 
|---|
| 25 |  | 
|---|
| 26 | // implementation of the startup interface | 
|---|
| 27 | void PingPong::startup() { | 
|---|
| 28 |  | 
|---|
| 29 | logging_info( "starting up PingPong service ... " ); | 
|---|
| 30 |  | 
|---|
| 31 | // create ariba module | 
|---|
| 32 | logging_debug( "creating ariba underlay module ... " ); | 
|---|
| 33 | ariba = new AribaModule(); | 
|---|
| 34 |  | 
|---|
| 35 | // get the configuration object | 
|---|
| 36 | Configuration& config = Configuration::instance(); | 
|---|
| 37 |  | 
|---|
| 38 | // generate spovnet name | 
|---|
| 39 | Name spovnetName("pingpong"); | 
|---|
| 40 |  | 
|---|
| 41 | // get initiator flag | 
|---|
| 42 | this->isInitiator = Configuration::instance().read<bool>("node.initiator"); | 
|---|
| 43 |  | 
|---|
| 44 | this->name = string("<ping>"); | 
|---|
| 45 |  | 
|---|
| 46 | // get node name | 
|---|
| 47 | Name nodeName = Name::UNSPECIFIED; | 
|---|
| 48 | if (config.exists("node.name")) nodeName = config.read<string> ("node.name"); | 
|---|
| 49 |  | 
|---|
| 50 | // configure ariba module | 
|---|
| 51 | if (config.exists("ariba.ip.addr")) ariba->setProperty("ip.addr", | 
|---|
| 52 | config.read<string>("ariba.ip.addr")); | 
|---|
| 53 | if (config.exists("ariba.tcp.port")) ariba->setProperty("tcp.port", | 
|---|
| 54 | config.read<string>("ariba.tcp.port")); | 
|---|
| 55 | if (config.exists("ariba.udp.port")) ariba->setProperty("udp.port", | 
|---|
| 56 | config.read<string>("ariba.udp.port")); | 
|---|
| 57 | if (config.exists("ariba.bootstrap.hints")) ariba->setProperty("bootstrap.hints", | 
|---|
| 58 | config.read<string>("ariba.bootstrap.hints")); | 
|---|
| 59 | if (config.exists("pingpong.name")) name = | 
|---|
| 60 | config.read<string>("pingpong.name"); | 
|---|
| 61 |  | 
|---|
| 62 | // start ariba module | 
|---|
| 63 | ariba->start(); | 
|---|
| 64 |  | 
|---|
| 65 | // create node and join | 
|---|
| 66 | node = new Node( *ariba, nodeName ); | 
|---|
| 67 |  | 
|---|
| 68 | // bind communication and node listener | 
|---|
| 69 | node->bind( this );                              /*NodeListener*/ | 
|---|
| 70 | node->bind( this, PingPong::PINGPONG_SERVICEID); /*CommunicationListener*/ | 
|---|
| 71 |  | 
|---|
| 72 | // start node module | 
|---|
| 73 | node->start(); | 
|---|
| 74 |  | 
|---|
| 75 | // when initiating, you can define the overlay type, default is Chord [CHORD_OVERLAY] | 
|---|
| 76 | SpoVNetProperties params; | 
|---|
| 77 | //params.setBaseOverlayType( SpoVNetProperties::ONE_HOP_OVERLAY ); // alternative: OneHop | 
|---|
| 78 |  | 
|---|
| 79 | // initiate or join the spovnet | 
|---|
| 80 | if (!isInitiator) node->join(spovnetName); | 
|---|
| 81 | else node->initiate(spovnetName, params); | 
|---|
| 82 |  | 
|---|
| 83 | // ping pong started up... | 
|---|
| 84 | logging_info( "pingpong starting up with" | 
|---|
| 85 | << " [spovnetid " << node->getSpoVNetId().toString() << "]" | 
|---|
| 86 | << " and [nodeid " << node->getNodeId().toString() << "]" ); | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 | // implementation of the startup interface | 
|---|
| 90 | void PingPong::shutdown() { | 
|---|
| 91 |  | 
|---|
| 92 | logging_info( "pingpong service starting shutdown sequence ..." ); | 
|---|
| 93 |  | 
|---|
| 94 | // stop timer | 
|---|
| 95 | Timer::stop(); | 
|---|
| 96 |  | 
|---|
| 97 | // leave spovnet | 
|---|
| 98 | node->leave(); | 
|---|
| 99 |  | 
|---|
| 100 | // unbind communication and node listener | 
|---|
| 101 | node->unbind( this );                               /*NodeListener*/ | 
|---|
| 102 | node->unbind( this, PingPong::PINGPONG_SERVICEID ); /*CommunicationListener*/ | 
|---|
| 103 |  | 
|---|
| 104 | // stop the ariba module | 
|---|
| 105 | ariba->stop(); | 
|---|
| 106 |  | 
|---|
| 107 | // delete node and ariba module | 
|---|
| 108 | delete node; | 
|---|
| 109 | delete ariba; | 
|---|
| 110 |  | 
|---|
| 111 | // now we are completely shut down | 
|---|
| 112 | logging_info( "pingpong service shut down" ); | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|
| 115 | // timer event | 
|---|
| 116 | void PingPong::eventFunction() { | 
|---|
| 117 |  | 
|---|
| 118 | // we ping all nodes that are known in the overlay structure | 
|---|
| 119 | // this can be all nodes (OneHop) overlay or just some neighbors | 
|---|
| 120 | // in case of a Chord or Kademlia structure | 
|---|
| 121 |  | 
|---|
| 122 | // in this sample we use auto-links: we just send out our message | 
|---|
| 123 | // to the node and the link is established automatically. for more | 
|---|
| 124 | // control we would use the node->establishLink function to create | 
|---|
| 125 | // a link and start using the link in the CommunicationListener::onLinkUp | 
|---|
| 126 | // function that is implemented further down in PingPong::onLinkUp | 
|---|
| 127 |  | 
|---|
| 128 | //      logging_info( "pinging overlay neighbors with ping id " << ++pingId ); | 
|---|
| 129 |  | 
|---|
| 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("----> I am " << name << " and I know " << s); | 
|---|
| 144 | names.clear(); | 
|---|
| 145 | } | 
|---|
| 146 | vector<NodeID> nodes = node->getNeighborNodes(); | 
|---|
| 147 | BOOST_FOREACH( NodeID nid, nodes ){ | 
|---|
| 148 | node->sendMessage( pingmsg, nid, PingPong::PINGPONG_SERVICEID ); | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | //----------------------------------------------------------------------- | 
|---|
| 152 | // Option 2: send a "broadcast message" that actually does the same thing | 
|---|
| 153 | //           internally, gets all neighboring nodes and sends the message | 
|---|
| 154 | //----------------------------------------------------------------------- | 
|---|
| 155 | // node->sendBroadcastMessage( pingmsg, PingPong::PINGPONG_SERVICEID ); | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | void PingPong::onJoinCompleted( const SpoVNetID& vid ) { | 
|---|
| 159 | logging_info( "pingpong node join completed, spovnetid=" << vid.toString() ); | 
|---|
| 160 |  | 
|---|
| 161 | // start the timer to ping every second | 
|---|
| 162 | Timer::start(); | 
|---|
| 163 | } | 
|---|
| 164 |  | 
|---|
| 165 | void PingPong::onJoinFailed( const SpoVNetID& vid ) { | 
|---|
| 166 | logging_error("pingpong node join failed, spovnetid=" << vid.toString() ); | 
|---|
| 167 | } | 
|---|
| 168 |  | 
|---|
| 169 | void PingPong::onLeaveCompleted( const SpoVNetID& vid ){ | 
|---|
| 170 | logging_info("pingpong node leave completed, spovnetid=" << vid.toString() ); | 
|---|
| 171 | } | 
|---|
| 172 |  | 
|---|
| 173 | void PingPong::onLeaveFailed( const SpoVNetID& vid ){ | 
|---|
| 174 | logging_error("pingpong node leave failed, spovnetid=" << vid.toString() ); | 
|---|
| 175 | } | 
|---|
| 176 |  | 
|---|
| 177 | void PingPong::onMessage(const DataMessage& msg, const NodeID& remote, const LinkID& lnk) { | 
|---|
| 178 | PingPongMessage* pingmsg = msg.getMessage()->convert<PingPongMessage> (); | 
|---|
| 179 | bool found=false; | 
|---|
| 180 | for (int i=0;i<names.size(); i++) if (names[i]==pingmsg->getName()) found=true; | 
|---|
| 181 | if (!found) names.push_back(pingmsg->getName()); | 
|---|
| 182 | //      logging_info( "received ping message on link " << lnk.toString() | 
|---|
| 183 | //                      << " from node " << remote.toString() | 
|---|
| 184 | //                      << ": " << pingmsg->info() ); | 
|---|
| 185 | } | 
|---|
| 186 |  | 
|---|
| 187 | void PingPong::onLinkUp(const LinkID& lnk, const NodeID& remote){ | 
|---|
| 188 | logging_info( "received link-up event for link " << lnk.toString() | 
|---|
| 189 | << " and node " << remote.toString() ); | 
|---|
| 190 | } | 
|---|
| 191 |  | 
|---|
| 192 | void PingPong::onLinkDown(const LinkID& lnk, const NodeID& remote){ | 
|---|
| 193 | logging_info( "received link-down event for link " << lnk.toString() | 
|---|
| 194 | << " and node " << remote.toString() ); | 
|---|
| 195 | } | 
|---|
| 196 |  | 
|---|
| 197 | void PingPong::onLinkChanged(const LinkID& lnk, const NodeID& remote){ | 
|---|
| 198 | logging_info( "link-changed event for link " << lnk.toString() | 
|---|
| 199 | << " and node " << remote.toString() ); | 
|---|
| 200 | } | 
|---|
| 201 |  | 
|---|
| 202 | bool PingPong::onLinkRequest(const NodeID& remote, const DataMessage& msg) { | 
|---|
| 203 | logging_info( "node " << remote.toString() << " wants to build up a link with us ... allowing" ); | 
|---|
| 204 | return true; | 
|---|
| 205 | } | 
|---|
| 206 |  | 
|---|
| 207 | void PingPong::onLinkFail(const LinkID& lnk, const NodeID& remote){ | 
|---|
| 208 | logging_info( "received link-failed event for link " << lnk.toString() | 
|---|
| 209 | << " and node " << remote.toString() ); | 
|---|
| 210 | } | 
|---|
| 211 |  | 
|---|
| 212 | }}} // namespace ariba, application, pingpong | 
|---|