| 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_ID = ServiceID( 111 );
 | 
|---|
| 16 | 
 | 
|---|
| 17 | // construction
 | 
|---|
| 18 | PingPong::PingPong() : pingId( 0 ) {
 | 
|---|
| 19 |         Timer::setInterval( 5000 );
 | 
|---|
| 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 |         // get node name
 | 
|---|
| 45 |         Name nodeName = Name::UNSPECIFIED;
 | 
|---|
| 46 |         if (config.exists("node.name")) nodeName = config.read<string> ("node.name");
 | 
|---|
| 47 | 
 | 
|---|
| 48 |         // configure ariba module
 | 
|---|
| 49 |         if (config.exists("ariba.ip.addr")) ariba->setProperty("ip.addr",
 | 
|---|
| 50 |                         config.read<string>("ariba.ip.addr"));
 | 
|---|
| 51 |         if (config.exists("ariba.tcp.port")) ariba->setProperty("tcp.port",
 | 
|---|
| 52 |                         config.read<string>("ariba.tcp.port"));
 | 
|---|
| 53 |         if (config.exists("ariba.udp.port")) ariba->setProperty("udp.port",
 | 
|---|
| 54 |                         config.read<string>("ariba.udp.port"));
 | 
|---|
| 55 |         if (config.exists("ariba.bootstrap.hints")) ariba->setProperty("bootstrap.hints",
 | 
|---|
| 56 |                         config.read<string>("ariba.bootstrap.hints"));
 | 
|---|
| 57 | 
 | 
|---|
| 58 |         // start ariba module
 | 
|---|
| 59 |         ariba->start();
 | 
|---|
| 60 | 
 | 
|---|
| 61 |         // create node and join
 | 
|---|
| 62 |         node = new Node( *ariba, nodeName );
 | 
|---|
| 63 | 
 | 
|---|
| 64 |         // start node module
 | 
|---|
| 65 |         node->start();
 | 
|---|
| 66 | 
 | 
|---|
| 67 |         // initiate or join the spovnet
 | 
|---|
| 68 |         if (!isInitiator) node->join(spovnetName);
 | 
|---|
| 69 |         else node->initiate(spovnetName);
 | 
|---|
| 70 | 
 | 
|---|
| 71 |         // bind communication and node listener
 | 
|---|
| 72 |         node->bind( this );                       /*NodeListener*/
 | 
|---|
| 73 |         node->bind( this, PingPong::PINGPONG_ID); /*CommunicationListener*/
 | 
|---|
| 74 | 
 | 
|---|
| 75 |         // start the ping timer. if we are not
 | 
|---|
| 76 |         // the initiator this will happen in onJoinCompleted
 | 
|---|
| 77 |         if( isInitiator ) Timer::start();
 | 
|---|
| 78 | 
 | 
|---|
| 79 |         // ping pong started up...
 | 
|---|
| 80 |         logging_info( "pingpong starting up with"
 | 
|---|
| 81 |                         << " [spovnetid " << node->getSpoVNetId().toString() << "]"
 | 
|---|
| 82 |                         << " and [nodeid " << node->getNodeId().toString() << "]" );
 | 
|---|
| 83 | }
 | 
|---|
| 84 | 
 | 
|---|
| 85 | // implementation of the startup interface
 | 
|---|
| 86 | void PingPong::shutdown() {
 | 
|---|
| 87 | 
 | 
|---|
| 88 |         logging_info( "pingpong service starting shutdown sequence ..." );
 | 
|---|
| 89 | 
 | 
|---|
| 90 |         // stop timer
 | 
|---|
| 91 |         Timer::stop();
 | 
|---|
| 92 | 
 | 
|---|
| 93 |         // unbind communication and node listener
 | 
|---|
| 94 |         node->unbind( this );                        /*NodeListener*/
 | 
|---|
| 95 |         node->unbind( this, PingPong::PINGPONG_ID ); /*CommunicationListener*/
 | 
|---|
| 96 | 
 | 
|---|
| 97 |         // leave spovnet
 | 
|---|
| 98 |         node->leave();
 | 
|---|
| 99 | 
 | 
|---|
| 100 |         // stop the ariba module
 | 
|---|
| 101 |         ariba->stop();
 | 
|---|
| 102 | 
 | 
|---|
| 103 |         // delete node and ariba module
 | 
|---|
| 104 |         delete node;
 | 
|---|
| 105 |         delete ariba;
 | 
|---|
| 106 | 
 | 
|---|
| 107 |         // now we are completely shut down
 | 
|---|
| 108 |         logging_info( "pingpong service shut down" );
 | 
|---|
| 109 | }
 | 
|---|
| 110 | 
 | 
|---|
| 111 | // node listener interface
 | 
|---|
| 112 | void PingPong::onJoinCompleted( const SpoVNetID& vid ) {
 | 
|---|
| 113 |         logging_error( "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX pingpong node join completed, spovnetid=" << vid.toString() );
 | 
|---|
| 114 | 
 | 
|---|
| 115 |         // start the timer to ping every second
 | 
|---|
| 116 |         Timer::start();
 | 
|---|
| 117 | }
 | 
|---|
| 118 | 
 | 
|---|
| 119 | void PingPong::onJoinFailed( const SpoVNetID& vid ) {
 | 
|---|
| 120 |         logging_error("pingpong node join failed, spovnetid=" << vid.toString() );
 | 
|---|
| 121 | }
 | 
|---|
| 122 | 
 | 
|---|
| 123 | void PingPong::onLeaveCompleted( const SpoVNetID& vid ){
 | 
|---|
| 124 |         logging_info("pingpong node leave completed, spovnetid=" << vid.toString() );
 | 
|---|
| 125 | }
 | 
|---|
| 126 | 
 | 
|---|
| 127 | void PingPong::onLeaveFailed( const SpoVNetID& vid ){
 | 
|---|
| 128 |         logging_error("pingpong node leave failed, spovnetid=" << vid.toString() );
 | 
|---|
| 129 | }
 | 
|---|
| 130 | 
 | 
|---|
| 131 | // timer event
 | 
|---|
| 132 | void PingPong::eventFunction() {
 | 
|---|
| 133 | 
 | 
|---|
| 134 |         // we ping all nodes that are known in the overlay structure
 | 
|---|
| 135 |         // this can be all nodes (OneHop) overlay or just some neighbors
 | 
|---|
| 136 |         // in case of a Chord or Kademlia structure
 | 
|---|
| 137 | 
 | 
|---|
| 138 |         logging_info( "pinging overlay neighbors with ping id " << ++pingId );
 | 
|---|
| 139 | 
 | 
|---|
| 140 |         PingPongMessage pingmsg( pingId );
 | 
|---|
| 141 | 
 | 
|---|
| 142 |         //-----------------------------------------------------------------------
 | 
|---|
| 143 |         // Option 1: get all neighboring nodes and send the message to each
 | 
|---|
| 144 |         //-----------------------------------------------------------------------
 | 
|---|
| 145 |         vector<NodeID> nodes = node->getNeighborNodes();
 | 
|---|
| 146 |         BOOST_FOREACH( NodeID nid, nodes ){
 | 
|---|
| 147 |                 node->sendMessage( pingmsg, nid, PingPong::PINGPONG_ID );
 | 
|---|
| 148 |         }
 | 
|---|
| 149 | 
 | 
|---|
| 150 |         //-----------------------------------------------------------------------
 | 
|---|
| 151 |         // Option 2: send a "broadcast message" that actually does the same thing
 | 
|---|
| 152 |         //           internally, gets all neighboring nodes and sends the message
 | 
|---|
| 153 |         //-----------------------------------------------------------------------
 | 
|---|
| 154 |         // node->sendBroadcastMessage( pingmsg, PingPong::PINGPONG_ID );
 | 
|---|
| 155 | }
 | 
|---|
| 156 | 
 | 
|---|
| 157 | // communication listener
 | 
|---|
| 158 | bool PingPong::onLinkRequest(const NodeID& remote, const DataMessage& msg) {
 | 
|---|
| 159 |         return false;
 | 
|---|
| 160 | }
 | 
|---|
| 161 | 
 | 
|---|
| 162 | void PingPong::onMessage(const DataMessage& msg, const NodeID& remote, const LinkID& lnk) {
 | 
|---|
| 163 | 
 | 
|---|
| 164 |         PingPongMessage* pingmsg = msg.getMessage()->convert<PingPongMessage> ();
 | 
|---|
| 165 | 
 | 
|---|
| 166 |         logging_info( "received ping message on link " << lnk.toString()
 | 
|---|
| 167 |                         << " from node " << remote.toString()
 | 
|---|
| 168 |                         << ": " << pingmsg->info() );
 | 
|---|
| 169 | }
 | 
|---|
| 170 | 
 | 
|---|
| 171 | void PingPong::onLinkUp(const LinkID& lnk, const NodeID& remote){
 | 
|---|
| 172 |         logging_info( "received link-up event for link " << lnk.toString()
 | 
|---|
| 173 |                         << " and node " << remote.toString() );
 | 
|---|
| 174 | }
 | 
|---|
| 175 | 
 | 
|---|
| 176 | void PingPong::onLinkDown(const LinkID& lnk, const NodeID& remote){
 | 
|---|
| 177 |         logging_info( "received link-down event for link " << lnk.toString()
 | 
|---|
| 178 |                         << " and node " << remote.toString() );
 | 
|---|
| 179 | }
 | 
|---|
| 180 | 
 | 
|---|
| 181 | void PingPong::onLinkChanged(const LinkID& lnk, const NodeID& remote){
 | 
|---|
| 182 |         logging_info( "received link-changed event for link " << lnk.toString()
 | 
|---|
| 183 |                         << " and node " << remote.toString() );
 | 
|---|
| 184 | }
 | 
|---|
| 185 | 
 | 
|---|
| 186 | void PingPong::onLinkFail(const LinkID& lnk, const NodeID& remote){
 | 
|---|
| 187 |         logging_info( "received link-failed event for link " << lnk.toString()
 | 
|---|
| 188 |                         << " and node " << remote.toString() );
 | 
|---|
| 189 | }
 | 
|---|
| 190 | 
 | 
|---|
| 191 | void PingPong::onLinkQoSChanged(const LinkID& lnk, const NodeID& remote, const LinkProperties& prop){
 | 
|---|
| 192 |         logging_info( "received link-qos-changed event for link " << lnk.toString()
 | 
|---|
| 193 |                                 << " and node " << remote.toString()
 | 
|---|
| 194 |                                 << " with link properties " << prop.toString() );
 | 
|---|
| 195 | }
 | 
|---|
| 196 | 
 | 
|---|
| 197 | void PingPong::onMessageSent(seqnum_t seq_num, bool failed, const DataMessage& msg ){
 | 
|---|
| 198 |         logging_info( "received message sent event for seqnum " << seq_num
 | 
|---|
| 199 |                         << " with result " << failed );
 | 
|---|
| 200 | }
 | 
|---|
| 201 | 
 | 
|---|
| 202 | }}} // namespace ariba, application, pingpong
 | 
|---|