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