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( 1000 );
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 Name spovnetName("pingpong");
00036 Name nodeName = Name::UNSPECIFIED;
00037 this->name = string("<ping>");
00038
00039
00040 if( Configuration::haveConfig() ){
00041 Configuration& config = Configuration::instance();
00042
00043
00044 if (config.exists("node.name"))
00045 nodeName = config.read<string> ("node.name");
00046
00047
00048 if (config.exists("ariba.endpoints"))
00049 ariba->setProperty("endpoints", config.read<string>("ariba.endpoints"));
00050 if (config.exists("ariba.bootstrap.hints"))
00051 ariba->setProperty("bootstrap.hints", config.read<string>("ariba.bootstrap.hints"));
00052 if (config.exists("pingpong.name"))
00053 name = config.read<string>("pingpong.name");
00054
00055 }
00056
00057
00058 ariba->start();
00059
00060
00061 node = new Node( *ariba, nodeName );
00062
00063
00064 node->bind( this );
00065 node->bind( this, PingPong::PINGPONG_SERVICEID);
00066
00067
00068 node->start();
00069
00070
00071 SpoVNetProperties params;
00072
00073
00074
00075 logging_info("initiating spovnet");
00076 node->initiate(spovnetName, params);
00077
00078
00079 logging_info("joining spovnet");
00080 node->join(spovnetName);
00081
00082
00083 logging_info( "pingpong starting up with"
00084 << " [spovnetid " << node->getSpoVNetId().toString() << "]"
00085 << " and [nodeid " << node->getNodeId().toString() << "]" );
00086 }
00087
00088
00089 void PingPong::shutdown() {
00090
00091 logging_info( "pingpong service starting shutdown sequence ..." );
00092
00093
00094 Timer::stop();
00095
00096
00097 node->leave();
00098
00099
00100 node->unbind( this );
00101 node->unbind( this, PingPong::PINGPONG_SERVICEID );
00102
00103
00104 ariba->stop();
00105
00106
00107 delete node;
00108 delete ariba;
00109
00110
00111 logging_info( "pingpong service shut down" );
00112 }
00113
00114
00115 void PingPong::eventFunction() {
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127 logging_info( "pinging overlay neighbors with ping id " << ++pingId );
00128 PingPongMessage pingmsg( pingId, name );
00129
00130
00131
00132
00133 counter++;
00134 if (counter<0 || counter>4) {
00135 counter = 0;
00136 string s;
00137 for (int i=0; i<names.size();i++) {
00138 if (i!=0) s+= ", ";
00139 s = s+names[i];
00140 }
00141 logging_info("----> I am " << name << " and I know " << s);
00142 names.clear();
00143 }
00144
00145 vector<NodeID> nodes = node->getNeighborNodes();
00146 BOOST_FOREACH( NodeID nid, nodes ){
00147 logging_info( "sending ping message to " << nid.toString() );
00148 node->sendMessage( pingmsg, nid, PingPong::PINGPONG_SERVICEID );
00149 }
00150
00151
00152
00153
00154
00155
00156 }
00157
00158 void PingPong::onJoinCompleted( const SpoVNetID& vid ) {
00159 logging_info( "pingpong node join completed, spovnetid=" << vid.toString() );
00160
00161
00162 Timer::start();
00163 }
00164
00165 void PingPong::onJoinFailed( const SpoVNetID& vid ) {
00166 logging_error("pingpong node join failed, spovnetid=" << vid.toString() );
00167 }
00168
00169 void PingPong::onLeaveCompleted( const SpoVNetID& vid ){
00170 logging_info("pingpong node leave completed, spovnetid=" << vid.toString() );
00171 }
00172
00173 void PingPong::onLeaveFailed( const SpoVNetID& vid ){
00174 logging_error("pingpong node leave failed, spovnetid=" << vid.toString() );
00175 }
00176
00177 void PingPong::onMessage(const DataMessage& msg, const NodeID& remote, const LinkID& lnk) {
00178 PingPongMessage* pingmsg = msg.getMessage()->convert<PingPongMessage> ();
00179 bool found=false;
00180 for (int i=0;i<names.size(); i++) if (names[i]==pingmsg->getName()) found=true;
00181 if (!found) names.push_back(pingmsg->getName());
00182 logging_info( "received ping message on link " << lnk.toString()
00183 << " from node " << remote.toString()
00184 << ": " << pingmsg->info() );
00185 }
00186
00187 void PingPong::onLinkUp(const LinkID& lnk, const NodeID& remote){
00188 logging_info( "received link-up event for link " << lnk.toString()
00189 << " and node " << remote.toString() );
00190 }
00191
00192 void PingPong::onLinkDown(const LinkID& lnk, const NodeID& remote){
00193 logging_info( "received link-down event for link " << lnk.toString()
00194 << " and node " << remote.toString() );
00195 }
00196
00197 void PingPong::onLinkChanged(const LinkID& lnk, const NodeID& remote){
00198 logging_info( "link-changed event for link " << lnk.toString()
00199 << " and node " << remote.toString() );
00200 }
00201
00202 bool PingPong::onLinkRequest(const NodeID& remote, const DataMessage& msg) {
00203 logging_info( "node " << remote.toString() << " wants to build up a link with us ... allowing" );
00204 return true;
00205 }
00206
00207 void PingPong::onLinkFail(const LinkID& lnk, const NodeID& remote){
00208 logging_info( "received link-failed event for link " << lnk.toString()
00209 << " and node " << remote.toString() );
00210 }
00211
00212 }}}