An Overlay-based
Virtual Network Substrate
SpoVNet

source: sample/pingpong/PingPong.cpp @ 6941

Last change on this file since 6941 was 6941, checked in by Christoph Mayer, 14 years ago

-visualisierungsänderungen

File size: 6.9 KB
Line 
1#include "PingPong.h"
2#include "ariba/utility/configuration/Configuration.h"
3#include "ariba/utility/visual/DddVis.h"
4
5using ariba::utility::Configuration;
6using namespace ariba;
7
8namespace ariba {
9namespace application {
10namespace pingpong {
11
12// logging
13use_logging_cpp( PingPong );
14
15// the service that the pingpong wants to use
16ServiceID PingPong::PINGPONG_SERVICEID = ServiceID( 111 );
17
18// construction
19PingPong::PingPong() : pingId( 0 ) {
20        Timer::setInterval( 1000 );
21}
22
23// destruction
24PingPong::~PingPong() {
25}
26
27// implementation of the startup interface
28void PingPong::startup() {
29
30        logging_info( "starting up PingPong service ... " );
31
32        // create ariba module
33        logging_debug( "creating ariba underlay module ... " );
34        ariba = new AribaModule();
35
36        Name spovnetName("pingpong");
37        Name nodeName = Name::UNSPECIFIED;
38        this->name = string("<ping>");
39
40        // get settings from configuration object
41        if( Configuration::haveConfig() ){
42                Configuration& config = Configuration::instance();
43
44                // get node name
45                if (config.exists("node.name"))
46                        nodeName = config.read<string> ("node.name");
47
48                // configure ariba module
49                if (config.exists("ariba.endpoints"))
50                        ariba->setProperty("endpoints", config.read<string>("ariba.endpoints"));
51                if (config.exists("ariba.bootstrap.hints"))
52                        ariba->setProperty("bootstrap.hints", config.read<string>("ariba.bootstrap.hints"));
53                if (config.exists("pingpong.name"))
54                        name = config.read<string>("pingpong.name");
55
56                // get visualization
57                if( config.exists("ariba.visual3d.ip") && config.exists("ariba.visual3d.port")){
58                        string ip = config.read<string>("ariba.visual3d.ip");
59                        unsigned int port = config.read<unsigned int>("ariba.visual3d.port");
60                        ariba::utility::DddVis::instance().configure(ip, port);
61                }
62
63        } // if( Configuration::haveConfig() )
64
65        // start ariba module
66        ariba->start();
67
68        // create node and join
69        node = new Node( *ariba, nodeName );
70
71        // bind communication and node listener
72        node->bind( this );                              /*NodeListener*/
73        node->bind( this, PingPong::PINGPONG_SERVICEID); /*CommunicationListener*/
74
75        // start node module
76        node->start();
77
78        // when initiating, you can define the overlay type, default is Chord [CHORD_OVERLAY]
79        SpoVNetProperties params;
80        //params.setBaseOverlayType( SpoVNetProperties::ONE_HOP_OVERLAY ); // alternative: OneHop
81
82        // initiate the spovnet
83        logging_info("initiating spovnet");
84        node->initiate(spovnetName, params);
85
86        // join the spovnet
87        logging_info("joining spovnet");
88        node->join(spovnetName);
89
90        // ping pong started up...
91        logging_info( "pingpong starting up with"
92                        << " [spovnetid " << node->getSpoVNetId().toString() << "]"
93                        << " and [nodeid " << node->getNodeId().toString() << "]" );
94}
95
96// implementation of the startup interface
97void PingPong::shutdown() {
98
99        logging_info( "pingpong service starting shutdown sequence ..." );
100
101        // stop timer
102        Timer::stop();
103
104        // leave spovnet
105        node->leave();
106
107        // unbind communication and node listener
108        node->unbind( this );                               /*NodeListener*/
109        node->unbind( this, PingPong::PINGPONG_SERVICEID ); /*CommunicationListener*/
110
111        // stop the ariba module
112        ariba->stop();
113
114        // delete node and ariba module
115        delete node;
116        delete ariba;
117
118        // now we are completely shut down
119        logging_info( "pingpong service shut down" );
120}
121
122// timer event
123void PingPong::eventFunction() {
124
125        // we ping all nodes that are known in the overlay structure
126        // this can be all nodes (OneHop) overlay or just some neighbors
127        // in case of a Chord or Kademlia structure
128
129        // in this sample we use auto-links: we just send out our message
130        // to the node and the link is established automatically. for more
131        // control we would use the node->establishLink function to create
132        // a link and start using the link in the CommunicationListener::onLinkUp
133        // function that is implemented further down in PingPong::onLinkUp
134
135        logging_info( "pinging overlay neighbors with ping id " << ++pingId );
136        PingPongMessage pingmsg( pingId, name );
137
138        //-----------------------------------------------------------------------
139        // Option 1: get all neighboring nodes and send the message to each
140        //-----------------------------------------------------------------------
141        counter++;
142        if (counter<0 || counter>4) {
143                counter = 0;
144                string s;
145                for (int i=0; i<names.size();i++) {
146                        if (i!=0) s+= ", ";
147                        s = s+names[i];
148                }
149                logging_info("----> I am " << name << " and I know " << s);
150                names.clear();
151        }
152
153        vector<NodeID> nodes = node->getNeighborNodes();
154        BOOST_FOREACH( NodeID nid, nodes ){
155                logging_info( "sending ping message to " << nid.toString() );
156                node->sendMessage( pingmsg, nid, PingPong::PINGPONG_SERVICEID );
157        }
158
159        //-----------------------------------------------------------------------
160        // Option 2: send a "broadcast message" that actually does the same thing
161        //           internally, gets all neighboring nodes and sends the message
162        //-----------------------------------------------------------------------
163        // node->sendBroadcastMessage( pingmsg, PingPong::PINGPONG_SERVICEID );
164}
165
166void PingPong::onJoinCompleted( const SpoVNetID& vid ) {
167        logging_info( "pingpong node join completed, spovnetid=" << vid.toString() );
168
169        // start the timer to ping every second
170        Timer::start();
171}
172
173void PingPong::onJoinFailed( const SpoVNetID& vid ) {
174        logging_error("pingpong node join failed, spovnetid=" << vid.toString() );
175}
176
177void PingPong::onLeaveCompleted( const SpoVNetID& vid ){
178        logging_info("pingpong node leave completed, spovnetid=" << vid.toString() );
179}
180
181void PingPong::onLeaveFailed( const SpoVNetID& vid ){
182        logging_error("pingpong node leave failed, spovnetid=" << vid.toString() );
183}
184
185void PingPong::onMessage(const DataMessage& msg, const NodeID& remote, const LinkID& lnk) {
186        PingPongMessage* pingmsg = msg.getMessage()->convert<PingPongMessage> ();
187        bool found=false;
188        for (int i=0;i<names.size(); i++) if (names[i]==pingmsg->getName()) found=true;
189        if (!found) names.push_back(pingmsg->getName());
190        logging_info( "received ping message on link " << lnk.toString()
191                        << " from node " << remote.toString()
192                        << ": " << pingmsg->info() );
193}
194
195void PingPong::onLinkUp(const LinkID& lnk, const NodeID& remote){
196        logging_info( "received link-up event for link " << lnk.toString()
197                        << " and node " << remote.toString() );
198}
199
200void PingPong::onLinkDown(const LinkID& lnk, const NodeID& remote){
201        logging_info( "received link-down event for link " << lnk.toString()
202                        << " and node " << remote.toString() );
203}
204
205void PingPong::onLinkChanged(const LinkID& lnk, const NodeID& remote){
206        logging_info( "link-changed event for link " << lnk.toString()
207                        << " and node " << remote.toString() );
208}
209
210bool PingPong::onLinkRequest(const NodeID& remote, const DataMessage& msg) {
211        logging_info( "node " << remote.toString() << " wants to build up a link with us ... allowing" );
212        return true;
213}
214
215void PingPong::onLinkFail(const LinkID& lnk, const NodeID& remote){
216        logging_info( "received link-failed event for link " << lnk.toString()
217                        << " and node " << remote.toString() );
218}
219
220}}} // namespace ariba, application, pingpong
Note: See TracBrowser for help on using the repository browser.