00001 #include "DHTTest.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 dhttest {
00010
00011
00012 use_logging_cpp( DHTTest );
00013
00014
00015 ServiceID DHTTest::DHTTEST_SERVICEID = ServiceID( 111 );
00016
00017
00018 DHTTest::DHTTest() {
00019 Timer::setInterval( 10000 );
00020 }
00021
00022
00023 DHTTest::~DHTTest() {
00024 }
00025
00026
00027 void DHTTest::startup() {
00028
00029 logging_info( "starting up DHTTest service ... " );
00030
00031
00032 logging_debug( "creating ariba underlay module ... " );
00033 ariba = new AribaModule();
00034
00035 Name spovnetName("dhttest");
00036 Name nodeName = Name::UNSPECIFIED;
00037 this->name = string("<dhttest>");
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("dhttest.name"))
00053 name = config.read<string>("dhttest.name");
00054
00055
00056 if (config.exists("dhttest.key"))
00057 key = config.read<string>("dhttest.key");
00058 if (config.exists("dhttest.data"))
00059 data = config.read<string>("dhttest.data");
00060
00061 }
00062
00063
00064 ariba->start();
00065
00066
00067 node = new Node( *ariba, nodeName );
00068
00069
00070 node->bind( this );
00071 node->bind( this, DHTTest::DHTTEST_SERVICEID);
00072
00073
00074 node->start();
00075
00076
00077 SpoVNetProperties params;
00078
00079
00080
00081 logging_info("initiating spovnet");
00082 node->initiate(spovnetName, params);
00083
00084
00085 logging_info("joining spovnet");
00086 node->join(spovnetName);
00087
00088
00089 logging_info( "dhttest starting up with"
00090 << " [spovnetid " << node->getSpoVNetId().toString() << "]"
00091 << " and [nodeid " << node->getNodeId().toString() << "]" );
00092 }
00093
00094
00095 void DHTTest::shutdown() {
00096
00097 logging_info( "dhttest service starting shutdown sequence ..." );
00098
00099
00100 Timer::stop();
00101
00102
00103 node->leave();
00104
00105
00106 node->unbind( this );
00107
00108
00109
00110 ariba->stop();
00111
00112
00113 delete node;
00114 delete ariba;
00115
00116
00117 logging_info( "dhttest service shut down" );
00118 }
00119
00120
00121 void DHTTest::eventFunction() {
00122
00123 switch(counter) {
00124 case 1:
00125 logging_info( "Putting '" << key << "'->'" << data << "' in the dht.");
00126
00127 node->put(stod(key), stod(data), 3600);
00128 break;
00129 default:
00130 logging_info( "Trying to get '" << key << "' from the DHT. This is try number " << counter-1 <<".");
00131
00132 node->get(stod(key), DHTTEST_SERVICEID);
00133 break;
00134 }
00135
00136 counter++;
00137
00138 }
00139
00140 void DHTTest::onJoinCompleted( const SpoVNetID& vid ) {
00141 logging_info( "dhttest node join completed, spovnetid=" << vid.toString() );
00142
00143
00144 if(!key.empty()) {
00145 counter = 0;
00146
00147
00148 Timer::start();
00149 }
00150
00151 }
00152
00153 void DHTTest::onJoinFailed( const SpoVNetID& vid ) {
00154 logging_error("dhttest node join failed, spovnetid=" << vid.toString() );
00155 }
00156
00157 void DHTTest::onLeaveCompleted( const SpoVNetID& vid ){
00158 logging_info("dhttest node leave completed, spovnetid=" << vid.toString() );
00159 }
00160
00161 void DHTTest::onLeaveFailed( const SpoVNetID& vid ){
00162 logging_error("dhttest node leave failed, spovnetid=" << vid.toString() );
00163 }
00164
00165
00166 bool DHTTest::onLinkRequest(const NodeID& remote) {
00167 logging_info( "node " << remote.toString() << " wants to build up a link with us ... allowing" );
00168 return true;
00169 }
00170
00171 void DHTTest::onLinkUp(const LinkID& lnk, const NodeID& remote){
00172 logging_info( "received link-up event for link " << lnk.toString()
00173 << " and node " << remote.toString() );
00174 }
00175
00176 void DHTTest::onLinkDown(const LinkID& lnk, const NodeID& remote){
00177 logging_info( "received link-down event for link " << lnk.toString()
00178 << " and node " << remote.toString() );
00179 }
00180
00181 void DHTTest::onLinkChanged(const LinkID& lnk, const NodeID& remote){
00182 logging_info( "link-changed event for link " << lnk.toString()
00183 << " and node " << remote.toString() );
00184 }
00185
00186 void DHTTest::onLinkFail(const LinkID& lnk, const NodeID& remote){
00187 logging_info( "received link-failed event for link " << lnk.toString()
00188 << " and node " << remote.toString() );
00189 }
00190
00191 void DHTTest::onKeyValue( const Data& key, const vector<Data>& value ) {
00192 if (value.size() == 0) {
00193 logging_info( "Received DHT answer for '" << dtos(key)
00194 << "': no values stored! ");
00195 return;
00196 }
00197 logging_info( "Received DHT answer for '" << dtos(key) << "' "
00198 << " with value='" << dtos(value.front()) << "'.") ;
00199
00200 }
00201
00202 Data DHTTest::stod(string s) {
00203 return Data((uint8_t*)s.data(), s.length()*8).clone();
00204 }
00205
00206 string DHTTest::dtos(Data d) {
00207 return string((char*)d.getBuffer(), d.getLength()/8);
00208 }
00209
00210 }}}