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