source: sample/testdht/DHTTest.cpp@ 7464

Last change on this file since 7464 was 6796, checked in by mies, 15 years ago

DHTTest working with 2 nodes

File size: 5.6 KB
Line 
1#include "DHTTest.h"
2#include "ariba/utility/configuration/Configuration.h"
3
4using ariba::utility::Configuration;
5using namespace ariba;
6
7namespace ariba {
8namespace application {
9namespace dhttest {
10
11// logging
12use_logging_cpp( DHTTest );
13
14// the service that the dhttest wants to use
15ServiceID DHTTest::DHTTEST_SERVICEID = ServiceID( 111 );
16
17// construction
18DHTTest::DHTTest() {
19 Timer::setInterval( 10000 );
20}
21
22// destruction
23DHTTest::~DHTTest() {
24}
25
26// implementation of the startup interface
27void DHTTest::startup() {
28
29 logging_info( "starting up DHTTest service ... " );
30
31 // create ariba module
32 logging_debug( "creating ariba underlay module ... " );
33 ariba = new AribaModule();
34
35 Name spovnetName("dhttest");
36 Name nodeName = Name::UNSPECIFIED;
37 this->name = string("<dhttest>");
38
39 // get settings from configuration object
40 if( Configuration::haveConfig() ){
41 Configuration& config = Configuration::instance();
42
43 // get node name
44 if (config.exists("node.name"))
45 nodeName = config.read<string> ("node.name");
46
47 // configure ariba module
48 if (config.exists("ariba.endpoints"))
49 ariba->setProperty("endpoints", config.read<string>("ariba.endpoints"));
50 if (config.exists("ariba.bootstrap.hints"))
51 ariba->setProperty("bootstrap.hints", config.read<string>("ariba.bootstrap.hints"));
52 if (config.exists("dhttest.name"))
53 name = config.read<string>("dhttest.name");
54
55 // configure test parameteres
56 if (config.exists("dhttest.key"))
57 key = config.read<string>("dhttest.key");
58 if (config.exists("dhttest.data"))
59 data = config.read<string>("dhttest.data");
60
61 } // if( Configuration::haveConfig() )
62
63 // start ariba module
64 ariba->start();
65
66 // create node and join
67 node = new Node( *ariba, nodeName );
68
69 // bind communication and node listener
70 node->bind( this ); /*NodeListener*/
71 node->bind( this, DHTTest::DHTTEST_SERVICEID); /*CommunicationListener*/
72
73 // start node module
74 node->start();
75
76 // when initiating, you can define the overlay type, default is Chord [CHORD_OVERLAY]
77 SpoVNetProperties params;
78 //params.setBaseOverlayType( SpoVNetProperties::ONE_HOP_OVERLAY ); // alternative: OneHop
79
80 // initiate the spovnet
81 logging_info("initiating spovnet");
82 node->initiate(spovnetName, params);
83
84 // join the spovnet
85 logging_info("joining spovnet");
86 node->join(spovnetName);
87
88 // dht test started up...
89 logging_info( "dhttest starting up with"
90 << " [spovnetid " << node->getSpoVNetId().toString() << "]"
91 << " and [nodeid " << node->getNodeId().toString() << "]" );
92}
93
94// implementation of the startup interface
95void DHTTest::shutdown() {
96
97 logging_info( "dhttest service starting shutdown sequence ..." );
98
99 // stop timer
100 Timer::stop();
101
102 // leave spovnet
103 node->leave();
104
105 // unbind communication and node listener
106 node->unbind( this ); /*NodeListener*/
107 //node->unbind( this, DHTTest::DHTTEST_SERVICEID ); /*CommunicationListener*/
108
109 // stop the ariba module
110 ariba->stop();
111
112 // delete node and ariba module
113 delete node;
114 delete ariba;
115
116 // now we are completely shut down
117 logging_info( "dhttest service shut down" );
118}
119
120// timer event
121void DHTTest::eventFunction() {
122
123 switch(counter) {
124 case 1:
125 logging_info( "Putting '" << key << "'->'" << data << "' in the dht.");
126
127 node->put(stod(key), stod(data), 3600);
128 break;
129 default:
130 logging_info( "Trying to get '" << key << "' from the DHT. This is try number " << counter-1 <<".");
131
132 node->get(stod(key), DHTTEST_SERVICEID);
133 break;
134 }
135
136 counter++;
137
138}
139
140void DHTTest::onJoinCompleted( const SpoVNetID& vid ) {
141 logging_info( "dhttest node join completed, spovnetid=" << vid.toString() );
142
143 // key is set -> this node will be testing
144 if(!key.empty()) {
145 counter = 0;
146
147 // start the timer to ping every second
148 Timer::start();
149 }
150
151}
152
153void DHTTest::onJoinFailed( const SpoVNetID& vid ) {
154 logging_error("dhttest node join failed, spovnetid=" << vid.toString() );
155}
156
157void DHTTest::onLeaveCompleted( const SpoVNetID& vid ){
158 logging_info("dhttest node leave completed, spovnetid=" << vid.toString() );
159}
160
161void DHTTest::onLeaveFailed( const SpoVNetID& vid ){
162 logging_error("dhttest node leave failed, spovnetid=" << vid.toString() );
163}
164
165
166bool DHTTest::onLinkRequest(const NodeID& remote, const DataMessage& msg) {
167 logging_info( "node " << remote.toString() << " wants to build up a link with us ... allowing" );
168 return true;
169}
170
171void DHTTest::onLinkUp(const LinkID& lnk, const NodeID& remote){
172 logging_info( "received link-up event for link " << lnk.toString()
173 << " and node " << remote.toString() );
174}
175
176void DHTTest::onLinkDown(const LinkID& lnk, const NodeID& remote){
177 logging_info( "received link-down event for link " << lnk.toString()
178 << " and node " << remote.toString() );
179}
180
181void DHTTest::onLinkChanged(const LinkID& lnk, const NodeID& remote){
182 logging_info( "link-changed event for link " << lnk.toString()
183 << " and node " << remote.toString() );
184}
185
186void DHTTest::onLinkFail(const LinkID& lnk, const NodeID& remote){
187 logging_info( "received link-failed event for link " << lnk.toString()
188 << " and node " << remote.toString() );
189}
190
191void DHTTest::onKeyValue( const Data& key, const vector<Data>& value ) {
192 if (value.size() == 0) {
193 logging_info( "Received DHT answer for '" << dtos(key)
194 << "': no values stored! ");
195 return;
196 }
197 logging_info( "Received DHT answer for '" << dtos(key) << "' "
198 << " with value='" << dtos(value.front()) << "'.") ;
199 // TODO: implement
200}
201
202Data DHTTest::stod(string s) {
203 return Data((uint8_t*)s.data(), s.length()*8).clone();
204}
205
206string DHTTest::dtos(Data d) {
207 return string((char*)d.getBuffer(), d.getLength()/8);
208}
209
210}}} // namespace ariba, application, dhttest
Note: See TracBrowser for help on using the repository browser.