Changes between Version 30 and Version 31 of Documentation/Tutorial/PingPong


Ignore:
Timestamp:
Apr 28, 2009, 4:25:12 PM (16 years ago)
Author:
huebsch
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Documentation/Tutorial/PingPong

    v30 v31  
    186186}}}
    187187
    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.
     188So 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{{{
     19101 void PingPong::eventFunction() {
     19202
     19303      // we ping all nodes that are known in the overlay structure
     19404      // this can be all nodes (OneHop) overlay or just some neighbors
     19505      // in case of a Chord or Kademlia structure
     19606
     19707      // in this sample we use auto-links: we just send out our message
     19808      // to the node and the link is established automatically. for more
     19909      // control we would use the node->establishLink function to create
     20010      // a link and start using the link in the CommunicationListener::onLinkUp
     20111      // function that is implemented further down in PingPong::onLinkUp
     20212
     20313      logging_info( "pinging overlay neighbors with ping id " << ++pingId );
     20414
     20515      PingPongMessage pingmsg( pingId );
     20616
     20717      //-----------------------------------------------------------------------
     20818      // Option 1: get all neighboring nodes and send the message to each
     20919      //-----------------------------------------------------------------------
     21020      vector<NodeID> nodes = node->getNeighborNodes();
     21121      BOOST_FOREACH( NodeID nid, nodes ){
     21222              node->sendMessage( pingmsg, nid, PingPong::PINGPONG_SERVICEID );
     21323      }
     21424
     21525      //-----------------------------------------------------------------------
     21626      // Option 2: send a "broadcast message" that actually does the same thing
     21727      //           internally, gets all neighboring nodes and sends the message
     21828      //-----------------------------------------------------------------------
     21929      // node->sendBroadcastMessage( pingmsg, PingPong::PINGPONG_SERVICEID );
     22030 }
     221}}}
     222
     223Everytime 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.
    222224
    223225{{{
     
    264266
    265267Getting back to ''!PingPong.cpp'': After the initiator has send a message to a joiner, it will arrive and has to be handled. This is accomplished in ''receiveMessage'' (the name says it). Every received message has to be decapsulated by a service, casting the data back to the service's message format (line 132).
     268
     269''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).
     270
     271{{{
     272100 void PingPong::onNodeLeave( const NodeID& id, const SpoVNetID& spovnetid ){
     273101     RemoteNodes::iterator i = remoteNodes.find( id );
     274102     if( i != remoteNodes.end() ) remoteNodes.erase( i );
     275103 }
     276}}}
     277
     278Node leaves in our case only lead to deletion of links we had stored before, for we don't need them anymore (line 102).