55 | | 01 #include "PingPong.h" |
56 | | 02 #include "ariba/utility/configuration/Configuration.h" |
57 | | 03 |
58 | | 04 using ariba::utility::Configuration; |
59 | | 05 using namespace ariba; |
60 | | 06 |
61 | | 07 namespace ariba { |
62 | | 08 namespace application { |
63 | | 09 namespace pingpong { |
64 | | 10 |
65 | | 11 // logging |
66 | | 12 use_logging_cpp( PingPong ); |
67 | | 13 |
68 | | 14 // the service that the pingpong wants to use |
69 | | 15 ServiceID PingPong::PINGPONG_SERVICEID = ServiceID( 111 ); |
70 | | 16 |
71 | | 17 // construction |
72 | | 18 PingPong::PingPong() : pingId( 0 ) { |
73 | | 19 Timer::setInterval( 5000 ); |
74 | | 20 } |
75 | | 21 |
76 | | 22 // destruction |
77 | | 23 PingPong::~PingPong() { |
78 | | 24 } |
79 | | 25 |
80 | | 26 // implementation of the startup interface |
81 | | 27 void PingPong::startup() { |
82 | | 28 |
83 | | 29 logging_info( "starting up PingPong service ... " ); |
84 | | 30 |
85 | | 31 // create ariba module |
86 | | 32 logging_debug( "creating ariba underlay module ... " ); |
87 | | 33 ariba = new AribaModule(); |
88 | | 34 |
89 | | 35 // get the configuration object |
90 | | 36 Configuration& config = Configuration::instance(); |
91 | | 37 |
92 | | 38 // generate spovnet name |
93 | | 39 Name spovnetName("pingpong"); |
94 | | 40 |
95 | | 41 |
96 | | 42 |
97 | | 43 |
98 | | 44 // get node name |
99 | | 45 Name nodeName = Name::UNSPECIFIED; |
100 | | 46 if (config.exists("node.name")) nodeName = config.read<string> ("node.name"); |
101 | | 47 |
102 | | 48 // configure ariba module |
103 | | 49 if (config.exists("ariba.ip.addr")) ariba->setProperty("ip.addr", |
104 | | 50 config.read<string>("ariba.ip.addr")); |
105 | | 51 if (config.exists("ariba.tcp.port")) ariba->setProperty("tcp.port", |
106 | | 52 config.read<string>("ariba.tcp.port")); |
107 | | 53 if (config.exists("ariba.udp.port")) ariba->setProperty("udp.port", |
108 | | 54 config.read<string>("ariba.udp.port")); |
109 | | 55 if (config.exists("ariba.bootstrap.hints")) ariba->setProperty("bootstrap.hints", |
110 | | 56 config.read<string>("ariba.bootstrap.hints")); |
111 | | 57 |
112 | | 58 // start ariba module |
113 | | 59 ariba->start(); |
114 | | 60 |
115 | | 61 // create node and join |
116 | | 62 node = new Node( *ariba, nodeName ); |
117 | | 63 |
118 | | 64 // bind communication and node listener |
119 | | 65 node->bind( this ); /*NodeListener*/ |
120 | | 66 node->bind( this, PingPong::PINGPONG_SERVICEID); /*CommunicationListener*/ |
121 | | 67 |
122 | | 68 // start node module |
123 | | 69 node->start(); |
124 | | 70 |
125 | | 71 // initiate or join the spovnet |
126 | | 72 if (!isInitiator) node->join(spovnetName); |
127 | | 73 else node->initiate(spovnetName); |
128 | | 74 |
129 | | 75 // ping pong started up... |
130 | | 76 logging_info( "pingpong starting up with" |
131 | | 77 << " [spovnetid " << node->getSpoVNetId().toString() << "]" |
132 | | 78 << " and [nodeid " << node->getNodeId().toString() << "]" ); |
133 | | 79 } |
| 55 | 1 #include "PingPong.h" |
| 56 | 2 #include "ariba/utility/configuration/Configuration.h" |
| 57 | 3 #include "ariba/utility/visual/DddVis.h" |
| 58 | 4 |
| 59 | 5 using ariba::utility::Configuration; |
| 60 | 6 using namespace ariba; |
| 61 | 7 |
| 62 | 8 namespace ariba { |
| 63 | 9 namespace application { |
| 64 | 10 namespace pingpong { |
| 65 | 11 |
| 66 | 12 // logging |
| 67 | 13 use_logging_cpp( PingPong ); |
| 68 | 14 |
| 69 | 15 // the service that the pingpong wants to use |
| 70 | 16 ServiceID PingPong::PINGPONG_SERVICEID = ServiceID( 111 ); |
| 71 | 17 |
| 72 | 18 // construction |
| 73 | 19 PingPong::PingPong() : pingId( 0 ) { |
| 74 | 20 Timer::setInterval( 1000 ); |
| 75 | 21 } |
| 76 | 22 |
| 77 | 23 // destruction |
| 78 | 24 PingPong::~PingPong() { |
| 79 | 25 } |
| 80 | 26 |
| 81 | 27 // implementation of the startup interface |
| 82 | 28 void PingPong::startup() { |
| 83 | 29 |
| 84 | 30 logging_info( "starting up PingPong service ... " ); |
| 85 | 31 |
| 86 | 32 // create ariba module |
| 87 | 33 logging_debug( "creating ariba underlay module ... " ); |
| 88 | 34 ariba = new AribaModule(); |
| 89 | 35 |
| 90 | 36 Name spovnetName("pingpong"); |
| 91 | 37 Name nodeName = Name::UNSPECIFIED; |
| 92 | 38 this->name = string("<ping>"); |
| 93 | 39 |
| 94 | 40 // get settings from configuration object |
| 95 | 41 if( Configuration::haveConfig() ){ |
| 96 | 42 Configuration& config = Configuration::instance(); |
| 97 | 43 |
| 98 | 44 // get node name |
| 99 | 45 if (config.exists("node.name")) |
| 100 | 46 nodeName = config.read<string> ("node.name"); |
| 101 | 47 |
| 102 | 48 // configure ariba module |
| 103 | 49 if (config.exists("ariba.endpoints")) |
| 104 | 50 ariba->setProperty("endpoints", config.read<string>("ariba.endpoints")); |
| 105 | 51 if (config.exists("ariba.bootstrap.hints")) |
| 106 | 52 ariba->setProperty("bootstrap.hints", config.read<string>("ariba.bootstrap.hints")); |
| 107 | 53 if (config.exists("pingpong.name")) |
| 108 | 54 name = config.read<string>("pingpong.name"); |
| 109 | 55 |
| 110 | 56 // get visualization |
| 111 | 57 if( config.exists("ariba.visual3d.ip") && config.exists("ariba.visual3d.port")){ |
| 112 | 58 string ip = config.read<string>("ariba.visual3d.ip"); |
| 113 | 59 unsigned int port = config.read<unsigned int>("ariba.visual3d.port"); |
| 114 | 60 unsigned int color = config.exists("node.color") ? |
| 115 | 61 config.read<unsigned int>("node.color") : 0; |
| 116 | 62 ariba::utility::DddVis::instance().configure(ip, port, color); |
| 117 | 63 } |
| 118 | 64 |
| 119 | 65 } // if( Configuration::haveConfig() ) |
| 120 | 66 |
| 121 | 67 // start ariba module |
| 122 | 68 ariba->start(); |
| 123 | 69 |
| 124 | 70 // create node and join |
| 125 | 71 node = new Node( *ariba, nodeName ); |
| 126 | 72 |
| 127 | 73 // bind communication and node listener |
| 128 | 74 node->bind( this ); /*NodeListener*/ |
| 129 | 75 node->bind( this, PingPong::PINGPONG_SERVICEID); /*CommunicationListener*/ |
| 130 | 76 |
| 131 | 77 // start node module |
| 132 | 78 node->start(); |
| 133 | 79 |
| 134 | 80 // when initiating, you can define the overlay type, default is Chord [CHORD_OVERLAY] |
| 135 | 81 SpoVNetProperties params; |
| 136 | 82 //params.setBaseOverlayType( SpoVNetProperties::ONE_HOP_OVERLAY ); // alternative: OneHop |
| 137 | 83 |
| 138 | 84 // initiate the spovnet |
| 139 | 85 logging_info("initiating spovnet"); |
| 140 | 86 node->initiate(spovnetName, params); |
| 141 | 87 |
| 142 | 88 // join the spovnet |
| 143 | 89 logging_info("joining spovnet"); |
| 144 | 90 node->join(spovnetName); |
| 145 | 91 |
| 146 | 92 // ping pong started up... |
| 147 | 93 logging_info( "pingpong starting up with" |
| 148 | 94 << " [spovnetid " << node->getSpoVNetId().toString() << "]" |
| 149 | 95 << " and [nodeid " << node->getNodeId().toString() << "]" ); |
| 150 | 96 } |
175 | | 01 void PingPong::eventFunction() { |
176 | | 02 |
177 | | 03 // we ping all nodes that are known in the overlay structure |
178 | | 04 // this can be all nodes (OneHop) overlay or just some neighbors |
179 | | 05 // in case of a Chord or Kademlia structure |
180 | | 06 |
181 | | 07 // in this sample we use auto-links: we just send out our message |
182 | | 08 // to the node and the link is established automatically. for more |
183 | | 09 // control we would use the node->establishLink function to create |
184 | | 10 // a link and start using the link in the CommunicationListener::onLinkUp |
185 | | 11 // function that is implemented further down in PingPong::onLinkUp |
186 | | 12 |
187 | | 13 logging_info( "pinging overlay neighbors with ping id " << ++pingId ); |
188 | | 14 |
189 | | 15 PingPongMessage pingmsg( pingId ); |
190 | | 16 |
191 | | 17 //----------------------------------------------------------------------- |
192 | | 18 // Option 1: get all neighboring nodes and send the message to each |
193 | | 19 //----------------------------------------------------------------------- |
194 | | 20 vector<NodeID> nodes = node->getNeighborNodes(); |
195 | | 21 BOOST_FOREACH( NodeID nid, nodes ){ |
196 | | 22 node->sendMessage( pingmsg, nid, PingPong::PINGPONG_SERVICEID ); |
197 | | 23 } |
198 | | 24 |
199 | | 25 //----------------------------------------------------------------------- |
200 | | 26 // Option 2: send a "broadcast message" that actually does the same thing |
201 | | 27 // internally, gets all neighboring nodes and sends the message |
202 | | 28 //----------------------------------------------------------------------- |
203 | | 29 // node->sendBroadcastMessage( pingmsg, PingPong::PINGPONG_SERVICEID ); |
204 | | 30 } |
| 192 | 125 void PingPong::eventFunction() { |
| 193 | 126 |
| 194 | 127 // we ping all nodes that are known in the overlay structure |
| 195 | 128 // this can be all nodes (OneHop) overlay or just some neighbors |
| 196 | 129 // in case of a Chord or Kademlia structure |
| 197 | 130 |
| 198 | 131 // in this sample we use auto-links: we just send out our message |
| 199 | 132 // to the node and the link is established automatically. for more |
| 200 | 133 // control we would use the node->establishLink function to create |
| 201 | 134 // a link and start using the link in the CommunicationListener::onLinkUp |
| 202 | 135 // function that is implemented further down in PingPong::onLinkUp |
| 203 | 136 |
| 204 | 137 logging_info( "pinging overlay neighbors with ping id " << ++pingId ); |
| 205 | 138 PingPongMessage pingmsg( pingId, name ); |
| 206 | 139 |
| 207 | 140 //----------------------------------------------------------------------- |
| 208 | 141 // Option 1: get all neighboring nodes and send the message to each |
| 209 | 142 //----------------------------------------------------------------------- |
| 210 | 143 counter++; |
| 211 | 144 if (counter<0 || counter>4) { |
| 212 | 145 counter = 0; |
| 213 | 146 string s; |
| 214 | 147 for (int i=0; i<names.size();i++) { |
| 215 | 148 if (i!=0) s+= ", "; |
| 216 | 149 s = s+names[i]; |
| 217 | 150 } |
| 218 | 151 logging_info("----> I am " << name << " and I know " << s); |
| 219 | 152 names.clear(); |
| 220 | 153 } |
| 221 | 154 |
| 222 | 155 vector<NodeID> nodes = node->getNeighborNodes(); |
| 223 | 156 BOOST_FOREACH( NodeID nid, nodes ){ |
| 224 | 157 logging_info( "sending ping message to " << nid.toString() ); |
| 225 | 158 node->sendMessage( pingmsg, nid, PingPong::PINGPONG_SERVICEID ); |
| 226 | 159 } |
| 227 | 160 |
| 228 | 161 //----------------------------------------------------------------------- |
| 229 | 162 // Option 2: send a "broadcast message" that actually does the same thing |
| 230 | 163 // internally, gets all neighboring nodes and sends the message |
| 231 | 164 //----------------------------------------------------------------------- |
| 232 | 165 // node->sendBroadcastMessage( pingmsg, PingPong::PINGPONG_SERVICEID ); |
| 233 | 166 } |
210 | | 01 #include "PingPongMessage.h" |
211 | | 02 |
212 | | 03 namespace ariba { |
213 | | 04 namespace application { |
214 | | 05 namespace pingpong { |
215 | | 06 |
216 | | 07 vsznDefault(PingPongMessage); |
217 | | 08 |
218 | | 09 PingPongMessage::PingPongMessage() : id(0) { |
219 | | 10 } |
220 | | 11 |
221 | | 12 PingPongMessage::PingPongMessage(uint8_t _id) : id(_id) { |
222 | | 13 } |
223 | | 14 |
224 | | 15 PingPongMessage::~PingPongMessage(){ |
225 | | 16 } |
226 | | 17 |
227 | | 18 string PingPongMessage::info(){ |
228 | | 19 return "ping pong message id " + ariba::utility::Helper::ultos(id); |
229 | | 20 } |
230 | | 21 |
231 | | 22 uint8_t PingPongMessage::getid(){ |
232 | | 23 return id; |
233 | | 24 } |
234 | | 25 |
235 | | 26 }}} // namespace ariba, appplication, pingpong |
| 239 | 01 #include "PingPongMessage.h" |
| 240 | 02 |
| 241 | 03 namespace ariba { |
| 242 | 04 namespace application { |
| 243 | 05 namespace pingpong { |
| 244 | 06 |
| 245 | 07 vsznDefault(PingPongMessage); |
| 246 | 08 |
| 247 | 09 PingPongMessage::PingPongMessage() : id(0) { |
| 248 | 10 } |
| 249 | 11 |
| 250 | 12 PingPongMessage::PingPongMessage(uint8_t _id) : id(_id) { |
| 251 | 13 } |
| 252 | 14 |
| 253 | 15 PingPongMessage::~PingPongMessage(){ |
| 254 | 16 } |
| 255 | 17 |
| 256 | 18 string PingPongMessage::info(){ |
| 257 | 19 return "ping pong message id " + ariba::utility::Helper::ultos(id); |
| 258 | 20 } |
| 259 | 21 |
| 260 | 22 uint8_t PingPongMessage::getid(){ |
| 261 | 23 return id; |
| 262 | 24 } |
| 263 | 25 |
| 264 | 26 }}} // namespace ariba, appplication, pingpong |