00001 #include "PingPong.h"
00002 #include "ariba/utility/configuration/Configuration.h"
00003
00004 using ariba::utility::Configuration;
00005 using namespace ariba;
00006
00007 namespace ariba {
00008 namespace application {
00009 namespace pingpong {
00010
00011
00012 use_logging_cpp( PingPong );
00013
00014
00015 ServiceID PingPong::PINGPONG_SERVICEID = ServiceID( 111 );
00016
00017
00018 PingPong::PingPong() : pingId( 0 ) {
00019 Timer::setInterval( 5000 );
00020 }
00021
00022
00023 PingPong::~PingPong() {
00024 }
00025
00026
00027 void PingPong::startup() {
00028
00029 logging_info( "starting up PingPong service ... " );
00030
00031
00032 logging_debug( "creating ariba underlay module ... " );
00033 ariba = new AribaModule();
00034
00035
00036 Configuration& config = Configuration::instance();
00037
00038
00039 Name spovnetName("pingpong");
00040
00041
00042 this->isInitiator = Configuration::instance().read<bool>("node.initiator");
00043
00044
00045 Name nodeName = Name::UNSPECIFIED;
00046 if (config.exists("node.name")) nodeName = config.read<string> ("node.name");
00047
00048
00049 if (config.exists("ariba.ip.addr")) ariba->setProperty("ip.addr",
00050 config.read<string>("ariba.ip.addr"));
00051 if (config.exists("ariba.tcp.port")) ariba->setProperty("tcp.port",
00052 config.read<string>("ariba.tcp.port"));
00053 if (config.exists("ariba.udp.port")) ariba->setProperty("udp.port",
00054 config.read<string>("ariba.udp.port"));
00055 if (config.exists("ariba.bootstrap.hints")) ariba->setProperty("bootstrap.hints",
00056 config.read<string>("ariba.bootstrap.hints"));
00057
00058
00059 ariba->start();
00060
00061
00062 node = new Node( *ariba, nodeName );
00063
00064
00065 node->bind( this );
00066 node->bind( this, PingPong::PINGPONG_SERVICEID);
00067
00068
00069 node->start();
00070
00071
00072 SpoVNetProperties params;
00073
00074
00075
00076 if (!isInitiator) node->join(spovnetName);
00077 else node->initiate(spovnetName, params);
00078
00079
00080 logging_info( "pingpong starting up with"
00081 << " [spovnetid " << node->getSpoVNetId().toString() << "]"
00082 << " and [nodeid " << node->getNodeId().toString() << "]" );
00083 }
00084
00085
00086 void PingPong::shutdown() {
00087
00088 logging_info( "pingpong service starting shutdown sequence ..." );
00089
00090
00091 Timer::stop();
00092
00093
00094 node->leave();
00095
00096
00097 node->unbind( this );
00098 node->unbind( this, PingPong::PINGPONG_SERVICEID );
00099
00100
00101 ariba->stop();
00102
00103
00104 delete node;
00105 delete ariba;
00106
00107
00108 logging_info( "pingpong service shut down" );
00109 }
00110
00111
00112 void PingPong::eventFunction() {
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124 logging_info( "pinging overlay neighbors with ping id " << ++pingId );
00125
00126 PingPongMessage pingmsg( pingId );
00127
00128
00129
00130
00131 vector<NodeID> nodes = node->getNeighborNodes();
00132 BOOST_FOREACH( NodeID nid, nodes ){
00133 node->sendMessage( pingmsg, nid, PingPong::PINGPONG_SERVICEID );
00134 }
00135
00136
00137
00138
00139
00140
00141 }
00142
00143 void PingPong::onJoinCompleted( const SpoVNetID& vid ) {
00144 logging_info( "pingpong node join completed, spovnetid=" << vid.toString() );
00145
00146
00147 Timer::start();
00148 }
00149
00150 void PingPong::onJoinFailed( const SpoVNetID& vid ) {
00151 logging_error("pingpong node join failed, spovnetid=" << vid.toString() );
00152 }
00153
00154 void PingPong::onLeaveCompleted( const SpoVNetID& vid ){
00155 logging_info("pingpong node leave completed, spovnetid=" << vid.toString() );
00156 }
00157
00158 void PingPong::onLeaveFailed( const SpoVNetID& vid ){
00159 logging_error("pingpong node leave failed, spovnetid=" << vid.toString() );
00160 }
00161
00162 void PingPong::onMessage(const DataMessage& msg, const NodeID& remote, const LinkID& lnk) {
00163 PingPongMessage* pingmsg = msg.getMessage()->convert<PingPongMessage> ();
00164
00165 logging_info( "received ping message on link " << lnk.toString()
00166 << " from node " << remote.toString()
00167 << ": " << pingmsg->info() );
00168 }
00169
00170 void PingPong::onLinkUp(const LinkID& lnk, const NodeID& remote){
00171 logging_info( "received link-up event for link " << lnk.toString()
00172 << " and node " << remote.toString() );
00173 }
00174
00175 void PingPong::onLinkDown(const LinkID& lnk, const NodeID& remote){
00176 logging_info( "received link-down event for link " << lnk.toString()
00177 << " and node " << remote.toString() );
00178 }
00179
00180 void PingPong::onLinkChanged(const LinkID& lnk, const NodeID& remote){
00181 logging_info( "link-changed event for link " << lnk.toString()
00182 << " and node " << remote.toString() );
00183 }
00184
00185 bool PingPong::onLinkRequest(const NodeID& remote, const DataMessage& msg) {
00186 logging_info( "node " << remote.toString() << " wants to build up a link with us ... allowing" );
00187 return true;
00188 }
00189
00190 void PingPong::onLinkFail(const LinkID& lnk, const NodeID& remote){
00191 logging_info( "received link-failed event for link " << lnk.toString()
00192 << " and node " << remote.toString() );
00193 }
00194
00195 }}}