188 | | ''Ariba'' provides several callback functions that may used by services to catch all kinds of events that could be of interest. In this exampe we limit ourselves to the event cases of node joins and node leaves. When a node successfull joines to the SpoVNet instance, ''onNodeJoin'' is triggered nn the initiator's service side. He may then react to this event, exemplary shown in line 80-93, implementing ''onNodeJoin''. In this case, the initiator starts establishing a link to the joined node (line 85), essentially for all kinds of communications via ''Ariba''. We then store the link for further usage (line 88) and prepare a timer which intention is to trigger periodic events. In our case we initialize the timer to be triggered every 2 seconds (line 90), before starting it (line 91). |
189 | | |
190 | | {{{ |
191 | | 100 void PingPong::onNodeLeave( const NodeID& id, const SpoVNetID& spovnetid ){ |
192 | | 101 RemoteNodes::iterator i = remoteNodes.find( id ); |
193 | | 102 if( i != remoteNodes.end() ) remoteNodes.erase( i ); |
194 | | 103 } |
195 | | }}} |
196 | | |
197 | | Node leaves in our case only lead to deletion of links we had stored before, for we don't need them anymore (line 102). |
198 | | |
199 | | So far, the node is up and running, created or joined a SpoVNet instance. The initiaor started a timer as soon as another node had joined. Now we see what happens when the timer is triggered. |
200 | | |
201 | | |
202 | | {{{ |
203 | | 110 void PingPong::eventFunction(){ |
204 | | 111 |
205 | | 112 logging_info( "pinging our remote nodes" ); |
206 | | 113 |
207 | | 114 RemoteNodes::iterator i = remoteNodes.begin(); |
208 | | 115 RemoteNodes::iterator iend = remoteNodes.end(); |
209 | | 116 |
210 | | 117 pingid++; |
211 | | 118 |
212 | | 119 for( ; i != iend; i++ ){ |
213 | | 120 logging_info( " -> pinging " << i->first ); |
214 | | 121 |
215 | | 122 PingPongMessage pingmsg( pingid ); |
216 | | 123 overlay->sendMessage( &pingmsg, i->second ); |
217 | | 124 } |
218 | | 125 } |
219 | | }}} |
220 | | |
221 | | Everytime the timer 'fires', ''eventFunction'' is called on a node (lines 110-125). In this example, the initiator sends a message to every node that has joined up to this point in time. To accomplish this, it iterates through all established links (line 119), builts a message for every link and finally sends the message using the Base Overlay in ''Ariba'' (line 123). Sending messages is done by passing the message object to ''Ariba'', together with the target link to use. . We will now take a short look at how such a message is composed in the ping pong example. |
| 188 | So far, the node is up and running, created or joined a SpoVNet instance. As very node starts a timer as soon as it has joined, we now inspect what happens when the timer is triggered. |
| 189 | |
| 190 | {{{ |
| 191 | 01 void PingPong::eventFunction() { |
| 192 | 02 |
| 193 | 03 // we ping all nodes that are known in the overlay structure |
| 194 | 04 // this can be all nodes (OneHop) overlay or just some neighbors |
| 195 | 05 // in case of a Chord or Kademlia structure |
| 196 | 06 |
| 197 | 07 // in this sample we use auto-links: we just send out our message |
| 198 | 08 // to the node and the link is established automatically. for more |
| 199 | 09 // control we would use the node->establishLink function to create |
| 200 | 10 // a link and start using the link in the CommunicationListener::onLinkUp |
| 201 | 11 // function that is implemented further down in PingPong::onLinkUp |
| 202 | 12 |
| 203 | 13 logging_info( "pinging overlay neighbors with ping id " << ++pingId ); |
| 204 | 14 |
| 205 | 15 PingPongMessage pingmsg( pingId ); |
| 206 | 16 |
| 207 | 17 //----------------------------------------------------------------------- |
| 208 | 18 // Option 1: get all neighboring nodes and send the message to each |
| 209 | 19 //----------------------------------------------------------------------- |
| 210 | 20 vector<NodeID> nodes = node->getNeighborNodes(); |
| 211 | 21 BOOST_FOREACH( NodeID nid, nodes ){ |
| 212 | 22 node->sendMessage( pingmsg, nid, PingPong::PINGPONG_SERVICEID ); |
| 213 | 23 } |
| 214 | 24 |
| 215 | 25 //----------------------------------------------------------------------- |
| 216 | 26 // Option 2: send a "broadcast message" that actually does the same thing |
| 217 | 27 // internally, gets all neighboring nodes and sends the message |
| 218 | 28 //----------------------------------------------------------------------- |
| 219 | 29 // node->sendBroadcastMessage( pingmsg, PingPong::PINGPONG_SERVICEID ); |
| 220 | 30 } |
| 221 | }}} |
| 222 | |
| 223 | Everytime the timer 'fires', ''eventFunction'' is called on a node. In this example, every node sends a message to every node that is part of the network at this point in time. To accomplish this, it iterates through all established links (line 20-23), builts a message for every link and finally sends the message using the Base Overlay in ''Ariba'' (line 123). Sending messages is done by passing the message object to ''Ariba'', together with the target link to use. . We will now take a short look at how such a message is composed in the ping pong example. |