| 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 id of the ping pong service
|
---|
| 15 | ServiceID PingPong::PINGPONG_ID = ServiceID( 111 );
|
---|
| 16 |
|
---|
| 17 | // construction
|
---|
| 18 | PingPong::PingPong() : pingId( 0 ) {
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 | // destruction
|
---|
| 22 | PingPong::~PingPong() {
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | // implementation of the startup interface
|
---|
| 26 | void PingPong::startup() {
|
---|
| 27 |
|
---|
| 28 | logging_info( "starting up PingPong service ... " );
|
---|
| 29 |
|
---|
| 30 | // create ariba module
|
---|
| 31 | logging_debug( "creating ariba underlay module ... " );
|
---|
| 32 | ariba = new AribaModule();
|
---|
| 33 |
|
---|
| 34 | // get the configuration object
|
---|
| 35 | Configuration& config = Configuration::instance();
|
---|
| 36 |
|
---|
| 37 | // generate spovnet name
|
---|
| 38 | Name spovnetName("pingpong");
|
---|
| 39 |
|
---|
| 40 | // get initiator flag
|
---|
| 41 | this->isInitiator = Configuration::instance().read<bool> ("node.initiator");
|
---|
| 42 |
|
---|
| 43 | // get node name
|
---|
| 44 | Name nodeName = Name::UNSPECIFIED;
|
---|
| 45 | if (config.exists("node.name")) nodeName
|
---|
| 46 | = 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);
|
---|
| 73 | node->bind(this, PingPong::PINGPONG_ID);
|
---|
| 74 |
|
---|
| 75 | // ping pong started up...
|
---|
| 76 | logging_info( "pingpong started up ");
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | // implementation of the startup interface
|
---|
| 80 | void PingPong::shutdown() {
|
---|
| 81 |
|
---|
| 82 | logging_info( "pingpong service starting shutdown sequence ..." );
|
---|
| 83 |
|
---|
| 84 | // stop timer
|
---|
| 85 | Timer::stop();
|
---|
| 86 |
|
---|
| 87 | // unbind listeners
|
---|
| 88 | node->unbind( this );
|
---|
| 89 | node->unbind( this, PingPong::PINGPONG_ID );
|
---|
| 90 |
|
---|
| 91 | // leave spovnet
|
---|
| 92 | node->leave();
|
---|
| 93 |
|
---|
| 94 | // stop the ariba module
|
---|
| 95 | ariba->stop();
|
---|
| 96 |
|
---|
| 97 | // delete node and ariba module
|
---|
| 98 | delete node;
|
---|
| 99 | delete ariba;
|
---|
| 100 |
|
---|
| 101 | // now we are completely shut down
|
---|
| 102 | logging_info( "pingpong service shut down" );
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | // node listener interface
|
---|
| 106 | void PingPong::onJoinCompleted( const SpoVNetID& vid ) {
|
---|
| 107 | logging_info( "pingpong node join completed, spovnetid=" << vid.toString() );
|
---|
| 108 |
|
---|
| 109 | // start the timer to ping every second
|
---|
| 110 | Timer::setInterval( 1000 );
|
---|
| 111 | Timer::start();
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | void PingPong::onJoinFailed( const SpoVNetID& vid ) {
|
---|
| 115 | logging_error(" pingpong node join failed, spovnetid=" << vid.toString() );
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | // communication listener
|
---|
| 119 | bool PingPong::onLinkRequest(const NodeID& remote, Message* msg) {
|
---|
| 120 | return false;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | void PingPong::onMessage(Message* msg, const NodeID& remote,
|
---|
| 124 | const LinkID& lnk = LinkID::UNSPECIFIED) {
|
---|
| 125 |
|
---|
| 126 | PingPongMessage* pingmsg = msg->decapsulate<PingPongMessage> ();
|
---|
| 127 |
|
---|
| 128 | logging_info( "received ping message on link " << lnk.toString()
|
---|
| 129 | << " from node " << remote.toString()
|
---|
| 130 | << ": " << pingmsg->toString() );
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | // timer event
|
---|
| 134 | void PingPong::eventFunction() {
|
---|
| 135 |
|
---|
| 136 | // we ping all nodes that are known in the overlay structure
|
---|
| 137 | // this can be all nodes (OneHop) overlay or just some neighbors
|
---|
| 138 | // in case of a Chord or Kademlia structure
|
---|
| 139 |
|
---|
| 140 | logging_info( "pinging overlay neighbors with ping id " << ++pingId );
|
---|
| 141 |
|
---|
| 142 | PingPongMessage pingmsg( pingId );
|
---|
| 143 | node->sendBroadcastMessage( pingmsg, PingPong::PINGPONG_ID );
|
---|
| 144 |
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | }}} // namespace ariba, application, pingpong
|
---|